-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
84 lines (70 loc) · 1.93 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
var fs = require('fs');
var emulator = require('./emulator');
var join = require('path').join;
var md5 = require('crypto').createHash('md5');
var msgpack = require('msgpack');
var debug = require('debug')('weplay:worker');
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';
// redis
var redis = require('./redis')();
var sub = require('./redis')();
var io = require('socket.io-emitter')(redis);
// rom
var file = process.env.WEPLAY_ROM;
if ('/' != file[0]) file = join(process.cwd(), file);
debug('rom %s', file);
var rom = fs.readFileSync(file);
var hash = md5.update(file).digest('hex');
debug('rom hash %s', hash);
// save interval
var saveInterval = process.env.WEPLAY_SAVE_INTERVAL || 60000;
debug('save interval %d', saveInterval);
// load emulator
var emu;
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){
io.emit('frame', frame);
redis.set('weplay:frame', frame);
});
redis.get('weplay:state:' + hash, function(err, state){
if (err) throw err;
if (state) {
debug('init from state');
emu.initWithState(msgpack.unpack(state));
} else {
debug('init from rom');
emu.initWithRom(rom);
}
emu.run();
save();
});
function save(){
debug('will save in %d', saveInterval);
setTimeout(function(){
var snap = emu.snapshot();
if (snap) {
debug('saving state');
redis.set('weplay:state:' + hash, msgpack.pack(snap));
save();
}
}, saveInterval);
}
}
sub.subscribe('weplay:move');
sub.on('message', function(channel, move){
if ('weplay:move' != channel) return;
emu.move(move.toString());
});
load();