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.
This commit is contained in:
John "Elwin" Edwards 2015-01-04 16:55:57 -05:00
parent 7e294b2136
commit 2064d2a9e6
2 changed files with 81 additions and 13 deletions

View file

@ -40,6 +40,10 @@ var statInterval = null;
var statDelta = 8000; var statDelta = 8000;
/* A WebSocket to listen for status events. */ /* A WebSocket to listen for status events. */
var statsock = null; var statsock = null;
/* List of currently active games. */
var currentList = [];
/* Last time the list was updated. */
var currentTS = null;
function writeData(hexstr) { function writeData(hexstr) {
var codenum; var codenum;
@ -315,7 +319,6 @@ function formlogin(ev) {
return; return;
} }
/* FIXME game list API has changed */
function tableCurrent(gamelist) { function tableCurrent(gamelist) {
var gamediv = document.getElementById("gametable"); var gamediv = document.getElementById("gametable");
while (gamediv.children.length > 2) while (gamediv.children.length > 2)
@ -383,11 +386,54 @@ function wsCurrent() {
return; return;
} }
if (msg.t == "t") { if (msg.t == "t") {
tableCurrent(msg.g); currentList = msg.g;
currentTS = new Date();
tableCurrent(currentList);
} }
else if ((msg.t == "b" || msg.t == "e") && else if (msg.t == "p") {
msg.p == sessionStorage.getItem("lname")) { var now = new Date();
getchoices(); 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") {
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) { statsock.onclose = function (ev) {

View file

@ -698,7 +698,6 @@ function getStatus(callback) {
var statusinfo = {"s": allowlogin, "g": []}; var statusinfo = {"s": allowlogin, "g": []};
for (var tag in sessions) { for (var tag in sessions) {
var gamedesc = {}; var gamedesc = {};
gamedesc["tag"] = tag;
gamedesc["p"] = sessions[tag].pname; gamedesc["p"] = sessions[tag].pname;
gamedesc["g"] = sessions[tag].game.uname; gamedesc["g"] = sessions[tag].game.uname;
gamedesc["i"] = now - sessions[tag].lasttime; gamedesc["i"] = now - sessions[tag].lasttime;
@ -706,7 +705,12 @@ function getStatus(callback) {
} }
statusinfo["dgl"] = []; statusinfo["dgl"] = [];
for (var tag in dglgames) { 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); callback(statusinfo);
} }
@ -973,12 +977,30 @@ function wsHandler(wsRequest) {
wsRequest.reject(404, "No such resource."); 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() { function pushStatus() {
getStatus(function(info) { var now = new Date();
info["t"] = "t"; var statusinfo = {"t": "p", "s": allowlogin, "g": [], "dgl": []};
gamemux.emit('list', info); 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 () { function shutdown () {
@ -1067,6 +1089,6 @@ ctlServer.listen(ctlsocket, function () {
wsServer.on("request", wsHandler); wsServer.on("request", wsHandler);
tslog('WebSockets are online'); tslog('WebSockets are online');
progressWatcher = startProgressWatcher(); progressWatcher = startProgressWatcher();
setInterval(pushStatus, 4000); setInterval(pushStatus, 40000);
}); });