comparison 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
comparison
equal deleted inserted replaced
65:e63c26dc0497 66:57bf0dcd080e
799 res.write(JSON.stringify(msgs)); 799 res.write(JSON.stringify(msgs));
800 res.end(); 800 res.end();
801 } 801 }
802 802
803 function statusmsg(req, res) { 803 function statusmsg(req, res) {
804 var now = new Date();
804 var reply = {"s": allowlogin, "g": []}; 805 var reply = {"s": allowlogin, "g": []};
806 function respond() {
807 res.writeHead(200, { "Content-Type": "application/json" });
808 if (req.method != 'HEAD')
809 res.write(JSON.stringify(reply));
810 res.end();
811 }
812 function idleset(i, idletime) {
813 if (i >= 0 && i < reply.g.length) {
814 reply.g[i].i = idletime;
815 }
816 for (var j = 0; j < reply.g.length; j++) {
817 if (!("i" in reply.g[j]))
818 return;
819 }
820 respond();
821 }
805 for (var sessid in sessions) { 822 for (var sessid in sessions) {
806 var gamedesc = {}; 823 var gamedesc = {};
807 gamedesc["n"] = sessid; 824 gamedesc["n"] = sessid;
808 gamedesc["p"] = sessions[sessid].pname; 825 gamedesc["p"] = sessions[sessid].pname;
809 gamedesc["g"] = sessions[sessid].game.uname; 826 gamedesc["g"] = sessions[sessid].game.uname;
810 reply["g"].push(gamedesc); 827 reply["g"].push(gamedesc);
811 } 828 }
812 res.writeHead(200, { "Content-Type": "application/json" }); 829 if (reply.g.length == 0) {
813 if (req.method != 'HEAD') 830 respond();
814 res.write(JSON.stringify(reply)); 831 return;
815 res.end(); 832 }
833 function makecallback(i) {
834 return function (err, stats) {
835 if (err)
836 idleset(i, null);
837 else
838 idleset(i, now - stats.mtime);
839 }
840 }
841 for (var i = 0; i < reply.g.length; i++) {
842 if (reply.g[i].n in sessions) {
843 fs.fstat(sessions[reply.g[i].n].record.fd, makecallback(i));
844 }
845 else {
846 idleset(i, null);
847 }
848 }
816 } 849 }
817 850
818 function pstatusmsg(req, res) { 851 function pstatusmsg(req, res) {
819 if (req.method == 'HEAD') { 852 if (req.method == 'HEAD') {
820 res.writeHead(200, { "Content-Type": "application/json" }); 853 res.writeHead(200, { "Content-Type": "application/json" });