Display idle time of games in progress.

statusmsg() now includes games' idle times, and the client displays
them using getcurrent().
This commit is contained in:
John "Elwin" Edwards 2012-06-20 11:37:05 -07:00
parent f750d9eb34
commit 2d9c8bd011
3 changed files with 59 additions and 7 deletions

View file

@ -801,7 +801,24 @@ function readFeed(client, res) {
}
function statusmsg(req, res) {
var now = new Date();
var reply = {"s": allowlogin, "g": []};
function respond() {
res.writeHead(200, { "Content-Type": "application/json" });
if (req.method != 'HEAD')
res.write(JSON.stringify(reply));
res.end();
}
function idleset(i, idletime) {
if (i >= 0 && i < reply.g.length) {
reply.g[i].i = idletime;
}
for (var j = 0; j < reply.g.length; j++) {
if (!("i" in reply.g[j]))
return;
}
respond();
}
for (var sessid in sessions) {
var gamedesc = {};
gamedesc["n"] = sessid;
@ -809,10 +826,26 @@ function statusmsg(req, res) {
gamedesc["g"] = sessions[sessid].game.uname;
reply["g"].push(gamedesc);
}
res.writeHead(200, { "Content-Type": "application/json" });
if (req.method != 'HEAD')
res.write(JSON.stringify(reply));
res.end();
if (reply.g.length == 0) {
respond();
return;
}
function makecallback(i) {
return function (err, stats) {
if (err)
idleset(i, null);
else
idleset(i, now - stats.mtime);
}
}
for (var i = 0; i < reply.g.length; i++) {
if (reply.g[i].n in sessions) {
fs.fstat(sessions[reply.g[i].n].record.fd, makecallback(i));
}
else {
idleset(i, null);
}
}
}
function pstatusmsg(req, res) {