changeset 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 e63c26dc0497
children b6a3b26fe0dc
files index-rlg.html rlgterm.js rlgwebd.js
diffstat 3 files changed, 59 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/index-rlg.html	Wed Jun 20 09:12:26 2012 -0700
+++ b/index-rlg.html	Wed Jun 20 11:37:05 2012 -0700
@@ -88,9 +88,9 @@
 <div class="modal" id="current">
 <div id="gametable" class="formtable">
  <div class="tcapt">Games in Progress</div>
- <div><div>Player</div><div>Game</div><div>Actions</div></div>
+ <div><div>Player</div><div>Game</div><div>Idle time</div><div>Actions</div></div>
 </div>
-<div id="nogames">There are no games to watch.</div>
+<div id="nogames">No one is playing right now.</div>
 <div class="rfloat"><span class="ibutton" onclick="getcurrent()">Refresh</span></div>
 </div>
 <div class="modal" id="startgame">
--- a/rlgterm.js	Wed Jun 20 09:12:26 2012 -0700
+++ b/rlgterm.js	Wed Jun 20 11:37:05 2012 -0700
@@ -453,6 +453,7 @@
       var cell1 = document.createElement("div");
       var cell2 = document.createElement("div");
       var cell3 = document.createElement("div");
+      var cell4 = document.createElement("div");
       cell1.appendChild(document.createTextNode(reply.g[i].p));
       var uname = reply.g[i].g;
       if (uname in games)
@@ -461,14 +462,16 @@
         debug(1, "Unrecognized game: " + uname);
         continue;
       }
+      cell3.appendChild(document.createTextNode(idlestr(reply.g[i].i)));
       var button = document.createElement("span");
       button.appendChild(document.createTextNode("Watch"));
       button.onclick = makeWatcher(reply.g[i].n);
       button.className = "ibutton";
-      cell3.appendChild(button);
+      cell4.appendChild(button);
       row.appendChild(cell1);
       row.appendChild(cell2);
       row.appendChild(cell3);
+      row.appendChild(cell4);
       gamediv.appendChild(row);
     }
   };
@@ -779,6 +782,22 @@
   return;
 }
 
+function idlestr(ms) {
+  if (typeof(ms) != "number")
+    return "?";
+  var seconds = Math.round(ms / 1000);
+  var ss = String(seconds % 60);
+  if (ss.length < 2)
+    ss = "0" + ss;
+  var mm = String(Math.floor((seconds % 3600) / 60));
+  if (mm.length < 2)
+    mm = "0" + mm;
+  var hh = String(Math.floor(seconds / 3600));
+  if (hh.length < 2)
+    hh = "0" + hh;
+  return hh + ":" + mm + ":" + ss;
+}
+
 function bell(on) {
   var imgnode = document.getElementById("bell");
   if (on) {
--- 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) {