-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (127 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var fs = require('fs');
var emulator = require('./emulator');
var join = require('path').join;
var md5 = require('crypto').createHash('md5');
var debug = require('debug')('weplay-slack');
var express = require('express');
var bodyParser = require('body-parser');
var superagent = require('superagent');
if (!process.env.WEPLAY_ROM) {
console.log('You must specify the ENV variable `WEPLAY_ROM` '
+ 'pointint to location of rom file to broadcast.');
process.exit(1);
}
process.title = 'weplay-emulator';
// rom
var file = process.env.WEPLAY_ROM;
if ('/' != file[0]) file = join(process.cwd(), file);
debug('rom %s', file);
var sav = file.replace(/\.[a-z]+$/, '.sav');
var rom = fs.readFileSync(file);
var hash = md5.update(file).digest('hex');
debug('rom hash %s', hash);
var screens = process.env.WEPLAY_SCREENS_DIR;
if ('/' != screens[0]) screens = join(process.cwd(), screens);
// save interval
var saveInterval = process.env.WEPLAY_SAVE_INTERVAL || 60000;
debug('save interval %d', saveInterval);
// load emulator
var emu;
var screen;
function load(){
debug('loading emulator');
emu = emulator();
emu.on('error', function(){
console.log(new Date + ' - restarting emulator');
emu.destroy();
setTimeout(load, 1000);
});
emu.on('frame', function(frame){
screen = frame;
});
fs.readFile(sav, { encoding: 'utf-8' }, function(err, state){
if (!err && state) {
debug('init from state');
emu.initWithState(JSON.parse(state));
} else {
debug('init from rom');
emu.initWithRom(rom);
}
emu.run();
save();
});
function save(){
debug('will save in %d', saveInterval);
setTimeout(function(){
var snap = JSON.stringify(emu.snapshot());
if (snap) {
debug('saving state');
fs.writeFile(sav, snap, function() {
debug('state saved');
});
save();
}
}, saveInterval);
}
}
//emu.move(move.toString());
load();
var app = express();
app.use(bodyParser());
app.get('/:time.png', function(req, res) {
var time = parseInt(req.params.time, 10);
res.set('Content-Type', 'image/png');
res.sendfile(join(screens, time + '.png'));
});
var timeout;
var keys = {
right: 0,
r: 0,
left: 1,
l: 1,
up: 2,
u: 2,
down: 3,
d: 3,
a: 4,
b: 5,
select: 6,
e: 6,
start: 7,
s: 7
};
app.post('/input', function(req, res) {
var commands = (req.body.text || '').toLowerCase().trim().replace(/\s+/g, ' ').split(' ');
var valid = true;
for (var i = 0; i < commands.length; i++) {
if (!keys.hasOwnProperty(commands[i])) {
valid = false;
}
}
if (valid) {
for (var i = 0; i < commands.length; i++) {
(function(command) {
setTimeout(function() {
emu.move(keys[command]);
if (typeof timeout == 'undefined') {
timeout = setTimeout(display, 5000);
}
}, i * 1000);
})(commands[i]);
}
}
res.send(200);
});
var agent = superagent.agent();
function display() {
timeout = undefined;
var time = new Date().valueOf();
fs.writeFile(join(screens, time + '.png'), screen, function(err) {
if (err) return;
agent
.post(process.env.WEPLAY_OUT_URL)
.send(process.env.WEPLAY_HOST + '/' + time + '.png') // date is added to prevent caching by slack
.end(function (err, res) {});
});
}
app.listen(80);