Mercurial > hg > rlgwebd
diff rlgwebd.js @ 66:57bf0dcd080e
Display idle time of games in progress.
statusmsg() now includes games' idle times, and the client displays
them using getcurrent().
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Wed, 20 Jun 2012 11:37:05 -0700 |
parents | e3082fd06520 |
children | e4773ac5d4d5 |
line wrap: on
line diff
--- a/rlgwebd.js Wed Jun 20 09:12:26 2012 -0700 +++ b/rlgwebd.js Wed Jun 20 11:37:05 2012 -0700 @@ -801,7 +801,24 @@ } 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 @@ 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) {