diff rlgwebd.js @ 163:0f6da35b27a0

RLGWebD: overhaul the list of current games. The /status WebSocket now only sends a complete list when opened. At 40-second intervals, it sends a list of games that have been updated in the last minute. The client now uses this to keep its own list.
author John "Elwin" Edwards
date Sun, 04 Jan 2015 16:55:57 -0500
parents 5a7e7ec136c8
children 3a97e4ee50f0
line wrap: on
line diff
--- a/rlgwebd.js	Sat Jan 03 20:07:42 2015 -0500
+++ b/rlgwebd.js	Sun Jan 04 16:55:57 2015 -0500
@@ -698,7 +698,6 @@
   var statusinfo = {"s": allowlogin, "g": []};
   for (var tag in sessions) {
     var gamedesc = {};
-    gamedesc["tag"] = tag;
     gamedesc["p"] = sessions[tag].pname;
     gamedesc["g"] = sessions[tag].game.uname;
     gamedesc["i"] = now - sessions[tag].lasttime;
@@ -706,7 +705,12 @@
   }
   statusinfo["dgl"] = [];
   for (var tag in dglgames) {
-    statusinfo["dgl"].push(tag);
+    var dglinfo = {};
+    var slash = tag.search('/');
+    dglinfo["g"] = tag.slice(0, slash);
+    dglinfo["p"] = tag.slice(slash + 1);
+    dglinfo["i"] = -1;
+    statusinfo["dgl"].push(dglinfo);
   }
   callback(statusinfo);
 }
@@ -973,12 +977,30 @@
     wsRequest.reject(404, "No such resource.");
 }
 
-/* TODO use a list instead */
+/* Only games with low idle time are included.  Use getStatus() for the
+ * complete list. */
 function pushStatus() {
-  getStatus(function(info) {
-    info["t"] = "t";
-    gamemux.emit('list', info);
-  });
+  var now = new Date();
+  var statusinfo = {"t": "p", "s": allowlogin, "g": [], "dgl": []};
+  for (var tag in sessions) {
+    var delta = now - sessions[tag].lasttime;
+    if (delta < 60000) {
+      var gamedesc = {};
+      gamedesc["p"] = sessions[tag].pname;
+      gamedesc["g"] = sessions[tag].game.uname;
+      gamedesc["i"] = delta;
+      statusinfo["g"].push(gamedesc);
+    }
+  }
+  for (var tag in dglgames) {
+    var dglinfo = {};
+    var slash = tag.search('/');
+    dglinfo["g"] = tag.slice(0, slash);
+    dglinfo["p"] = tag.slice(slash + 1);
+    dglinfo["i"] = -1;
+    statusinfo["dgl"].push(dglinfo);
+  }
+  gamemux.emit('list', statusinfo);
 }
 
 function shutdown () {
@@ -1067,6 +1089,6 @@
   wsServer.on("request", wsHandler);
   tslog('WebSockets are online'); 
   progressWatcher = startProgressWatcher();
-  setInterval(pushStatus, 4000);
+  setInterval(pushStatus, 40000);
 });