diff rlgterm.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 a2a25b7631f1
children bf518a00190b
line wrap: on
line diff
--- a/rlgterm.js	Sat Jan 03 20:07:42 2015 -0500
+++ b/rlgterm.js	Sun Jan 04 16:55:57 2015 -0500
@@ -40,6 +40,10 @@
 var statDelta = 8000;
 /* A WebSocket to listen for status events. */
 var statsock = null;
+/* List of currently active games. */
+var currentList = [];
+/* Last time the list was updated. */
+var currentTS = null;
 
 function writeData(hexstr) {
   var codenum;
@@ -315,7 +319,6 @@
   return;
 }
 
-/* FIXME game list API has changed */
 function tableCurrent(gamelist) {
   var gamediv = document.getElementById("gametable");
   while (gamediv.children.length > 2)
@@ -383,11 +386,54 @@
         return;
     }
     if (msg.t == "t") {
-      tableCurrent(msg.g);
+      currentList = msg.g;
+      currentTS = new Date();
+      tableCurrent(currentList);
+    }
+    else if (msg.t == "p") {
+      var now = new Date();
+      var idletimes = {};
+      for (var i = 0; i < msg.g.length; i++) {
+        var tag = msg.g[i].g + "/" + msg.g[i].p;
+        idletimes[tag] = msg.g[i].i;
+      }
+      for (var i = 0; i < currentList.length; i++) {
+        var tag = currentList[i].g + "/" + currentList[i].p;
+        if (tag in idletimes) {
+          currentList[i].i = idletimes[tag];
+        }
+        else {
+          currentList[i].i += now - currentTS;
+        }
+      }
+      currentTS = now;
+      tableCurrent(currentList);
     }
-    else if ((msg.t == "b" || msg.t == "e") && 
-             msg.p == sessionStorage.getItem("lname")) {
-      getchoices();
+    else if (msg.t == "b") {
+      var justbegun = {};
+      justbegun.g = msg.g;
+      justbegun.p = msg.p;
+      justbegun.i = 0;
+      currentList.push(justbegun);
+      tableCurrent(currentList);
+      if (msg.p == sessionStorage.getItem("lname")) {
+        getchoices();
+      }
+    }
+    else if (msg.t == "e") {
+      var i = 0;
+      while (i < currentList.length) {
+        if (currentList[i].g == msg.g && currentList[i].p == msg.p)
+          break;
+        i++;
+      }
+      if (i < currentList.length) {
+        currentList.splice(i, 1);
+      }
+      tableCurrent(currentList);
+      if (msg.p == sessionStorage.getItem("lname")) {
+        getchoices();
+      }
     }
   };
   statsock.onclose = function (ev) {