Finish moving DglSession methods into the prototype.

This commit is contained in:
John "Elwin" Edwards 2015-01-20 10:17:05 -05:00
parent 677d739bed
commit 583514764f

View file

@ -270,12 +270,11 @@ TermSession.prototype.destroy = function () {
}; };
function DglSession(filename) { function DglSession(filename) {
var ss = this;
BaseGame.call(this); BaseGame.call(this);
var pathcoms = filename.split('/'); var pathcoms = filename.split('/');
this.gname = pathcoms[pathcoms.length - 2]; this.gname = pathcoms[pathcoms.length - 2];
if (!(this.gname in games)) { if (!(this.gname in games)) {
ss.emit('open', false); this.emit('open', false);
return; return;
} }
var basename = pathcoms[pathcoms.length - 1]; var basename = pathcoms[pathcoms.length - 1];
@ -287,82 +286,94 @@ function DglSession(filename) {
* getting into a race. */ * getting into a race. */
this.reading = false; this.reading = false;
this.rpos = 0; this.rpos = 0;
this.readchunk = function () { fs.readFile(filename, {encoding: "utf8"}, (function (err, data) {
if (err) {
this.emit('open', false);
return;
}
var lines = data.split('\n');
this.h = Number(lines[1]);
this.w = Number(lines[2]);
fs.open(this.ttyrec, "r", (function (err, fd) {
if (err) {
this.emit('open', false);
}
else {
this.fd = fd;
this.emit('open', true);
tslog("DGL %s: open", this.tag());
gamemux.emit('begin', this.gname, this.pname, 'dgl');
this.startchunk();
this.recwatcher = fs.watch(this.ttyrec, this.notifier.bind(this));
}
}).bind(this));
}).bind(this));
}
DglSession.prototype = new BaseGame();
/* 3 functions to get data from the ttyrec file. */
DglSession.prototype.startchunk = function () {
if (this.reading) if (this.reading)
return; return;
this.reading = true; this.reading = true;
var header = new Buffer(12); var header = new Buffer(12);
fs.read(ss.fd, header, 0, 12, ss.rpos, function (err, n, buf) { fs.read(this.fd, header, 0, 12, this.rpos, this.datachunk.bind(this));
};
DglSession.prototype.datachunk = function (err, n, buf) {
/* Stop recursion if end of file has been reached. */ /* Stop recursion if end of file has been reached. */
if (err || n < 12) { if (err || n < 12) {
if (!err && n > 0) { if (!err && n > 0) {
tslog("DGL %s: expected 12-byte header, got %d", ss.tag(), n); tslog("DGL %s: expected 12-byte header, got %d", this.tag(), n);
} }
ss.reading = false; this.reading = false;
return; return;
} }
ss.rpos += 12; this.rpos += 12;
/* Update timestamp, to within 1 second. */ /* Update timestamp, to within 1 second. */
ss.lasttime = new Date(1000 * buf.readUInt32LE(0)); this.lasttime = new Date(1000 * buf.readUInt32LE(0));
var datalen = buf.readUInt32LE(8); var datalen = buf.readUInt32LE(8);
if (datalen > 16384) { if (datalen > 16384) {
// Something is probably wrong... // Something is probably wrong...
tslog("DGL %s: looking for %d bytes", ss.tag(), datalen); tslog("DGL %s: looking for %d bytes", this.tag(), datalen);
} }
var databuf = new Buffer(datalen); var databuf = new Buffer(datalen);
fs.read(ss.fd, databuf, 0, datalen, ss.rpos, function (err, n, buf) { fs.read(this.fd, databuf, 0, datalen, this.rpos, this.handledata.bind(this));
if (err || n < datalen) {
/* Next time, read the header again. */
ss.rpos -= 12;
ss.reading = false;
tslog("DGL %s: expected %d bytes, got %d", ss.tag(), datalen, n);
return;
}
ss.rpos += n;
ss.reading = false;
/* Process the data */
ss.framepush(buf);
var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")});
for (var i = 0; i < ss.watchers.length; i++) {
if (ss.watchers[i].connected)
ss.watchers[i].sendUTF(wmsg);
}
ss.emit("data", buf);
//tslog("DGL %s: %d bytes", ss.tag(), buf.length);
/* Recurse. */
ss.readchunk();
});
});
}; };
fs.readFile(filename, {encoding: "utf8"}, function (err, data) {
if (err) { DglSession.prototype.handledata = function (err, n, buf) {
ss.emit('open', false); if (err || n < buf.length) {
/* Next time, read the header again. */
this.rpos -= 12;
this.reading = false;
tslog("DGL %s: expected %d bytes, got %d", this.tag(), buf.length, n);
return; return;
} }
var lines = data.split('\n'); this.rpos += n;
ss.h = Number(lines[1]); this.reading = false;
ss.w = Number(lines[2]); /* Process the data */
fs.open(ss.ttyrec, "r", function(err, fd) { this.framepush(buf);
if (err) { var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")});
ss.emit('open', false); for (var i = 0; i < this.watchers.length; i++) {
if (this.watchers[i].connected)
this.watchers[i].sendUTF(wmsg);
} }
else { this.emit("data", buf);
ss.fd = fd; /* Recurse. */
ss.emit('open', true); this.startchunk();
tslog("DGL %s: open", ss.tag()); };
gamemux.emit('begin', ss.gname, ss.pname, 'dgl');
ss.readchunk(); /* Handles events from the ttyrec file watcher. */
ss.recwatcher = fs.watch(ss.ttyrec, function (ev, finame) { DglSession.prototype.notifier = function (ev, finame) {
if (ev == "change") if (ev == "change")
ss.readchunk(); this.startchunk();
}); /* If another kind of event appears, something strange happened. */
} };
});
}); DglSession.prototype.close = function () {
this.close = function () { this.recwatcher.close();
this.recwatcher.close()
/* Ensure all data is handled before quitting. */ /* Ensure all data is handled before quitting. */
this.readchunk(); this.startchunk();
var connlist = this.watchers; var connlist = this.watchers;
this.watchers = []; this.watchers = [];
for (var i = 0; i < connlist.length; i++) { for (var i = 0; i < connlist.length; i++) {
@ -372,10 +383,8 @@ function DglSession(filename) {
fs.close(this.fd); fs.close(this.fd);
this.emit("close"); this.emit("close");
gamemux.emit('end', this.gname, this.pname); gamemux.emit('end', this.gname, this.pname);
tslog("DGL %s: closed", ss.tag()); tslog("DGL %s: closed", this.tag());
}; };
}
DglSession.prototype = new BaseGame();
function wsStartGame(wsReq) { function wsStartGame(wsReq) {
var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/); var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/);