comparison 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
comparison
equal deleted inserted replaced
162:5a7e7ec136c8 163:0f6da35b27a0
38 var statInterval = null; 38 var statInterval = null;
39 /* How frequently to check. */ 39 /* How frequently to check. */
40 var statDelta = 8000; 40 var statDelta = 8000;
41 /* A WebSocket to listen for status events. */ 41 /* A WebSocket to listen for status events. */
42 var statsock = null; 42 var statsock = null;
43 /* List of currently active games. */
44 var currentList = [];
45 /* Last time the list was updated. */
46 var currentTS = null;
43 47
44 function writeData(hexstr) { 48 function writeData(hexstr) {
45 var codenum; 49 var codenum;
46 var codes = []; 50 var codes = [];
47 var nc; 51 var nc;
313 req.open('POST', '/login', true); 317 req.open('POST', '/login', true);
314 req.send(JSON.stringify(loginmsg)); 318 req.send(JSON.stringify(loginmsg));
315 return; 319 return;
316 } 320 }
317 321
318 /* FIXME game list API has changed */
319 function tableCurrent(gamelist) { 322 function tableCurrent(gamelist) {
320 var gamediv = document.getElementById("gametable"); 323 var gamediv = document.getElementById("gametable");
321 while (gamediv.children.length > 2) 324 while (gamediv.children.length > 2)
322 gamediv.removeChild(gamediv.children[2]); 325 gamediv.removeChild(gamediv.children[2]);
323 if (gamelist.length === 0) { 326 if (gamelist.length === 0) {
381 } catch (e) { 384 } catch (e) {
382 if (e instanceof SyntaxError) 385 if (e instanceof SyntaxError)
383 return; 386 return;
384 } 387 }
385 if (msg.t == "t") { 388 if (msg.t == "t") {
386 tableCurrent(msg.g); 389 currentList = msg.g;
387 } 390 currentTS = new Date();
388 else if ((msg.t == "b" || msg.t == "e") && 391 tableCurrent(currentList);
389 msg.p == sessionStorage.getItem("lname")) { 392 }
390 getchoices(); 393 else if (msg.t == "p") {
394 var now = new Date();
395 var idletimes = {};
396 for (var i = 0; i < msg.g.length; i++) {
397 var tag = msg.g[i].g + "/" + msg.g[i].p;
398 idletimes[tag] = msg.g[i].i;
399 }
400 for (var i = 0; i < currentList.length; i++) {
401 var tag = currentList[i].g + "/" + currentList[i].p;
402 if (tag in idletimes) {
403 currentList[i].i = idletimes[tag];
404 }
405 else {
406 currentList[i].i += now - currentTS;
407 }
408 }
409 currentTS = now;
410 tableCurrent(currentList);
411 }
412 else if (msg.t == "b") {
413 var justbegun = {};
414 justbegun.g = msg.g;
415 justbegun.p = msg.p;
416 justbegun.i = 0;
417 currentList.push(justbegun);
418 tableCurrent(currentList);
419 if (msg.p == sessionStorage.getItem("lname")) {
420 getchoices();
421 }
422 }
423 else if (msg.t == "e") {
424 var i = 0;
425 while (i < currentList.length) {
426 if (currentList[i].g == msg.g && currentList[i].p == msg.p)
427 break;
428 i++;
429 }
430 if (i < currentList.length) {
431 currentList.splice(i, 1);
432 }
433 tableCurrent(currentList);
434 if (msg.p == sessionStorage.getItem("lname")) {
435 getchoices();
436 }
391 } 437 }
392 }; 438 };
393 statsock.onclose = function (ev) { 439 statsock.onclose = function (ev) {
394 statsock = null; 440 statsock = null;
395 } 441 }