diff rlgterm.js @ 44:b848cb50cd69

rlgterm.js: present game options to the player. Use the new player-status interface to display the state of any ongoing games when the player logs in.
author John "Elwin" Edwards <elwin@sdf.org>
date Sat, 09 Jun 2012 14:13:07 -0700
parents ea3b7775009d
children 1bbd0e76ba92
line wrap: on
line diff
--- a/rlgterm.js	Sat Jun 09 13:04:46 2012 -0700
+++ b/rlgterm.js	Sat Jun 09 14:13:07 2012 -0700
@@ -48,6 +48,26 @@
   }
 };
 
+/* Data on the available games. */
+var games = {
+  "rogue3": {
+    "name": "Rogue V3",
+    "uname": "rogue3"
+  },
+  "rogue4": {
+    "name": "Rogue V4",
+    "uname": "rogue4"
+  },
+  "rogue5": {
+    "name": "Rogue V5",
+    "uname": "rogue5"
+  },
+  "srogue": {
+    "name": "Super-Rogue",
+    "uname": "srogue"
+  }
+};
+
 /* Login name and key */
 var lname = null;
 var lcred = null;
@@ -385,6 +405,50 @@
   return;
 }
 
+function getchoices() {
+  if (termemu.sessid != null || !lcred)
+    return;
+  var req = new XMLHttpRequest();
+  req.onreadystatechange = function () {
+    if (req.readyState != 4 || req.status != 200) 
+      return;
+    var reply;
+    try {
+      reply = JSON.parse(req.responseText);
+    } catch (e) {
+      if (e instanceof SyntaxError)
+        return;
+    }
+    if (!("name" in reply) || reply["name"] != lname || !("stat" in reply))
+      return;
+    var statdiv = document.createElement("div");
+    for (var gname in reply.stat) {
+      if (!(gname in games))
+        continue;
+      var stext;
+      if (reply.stat[gname] == "s")
+        stext = games[gname].name + ": Resume your game";
+      else if (reply.stat[gname] == "0")
+        stext = games[gname].name + ": Start a game";
+      else if (reply.stat[gname] == "p")
+        stext = games[gname].name + ": Game in progress";
+      else
+        continue;
+      var tnode = document.createTextNode(stext);
+      var gamediv = document.createElement("div");
+      gamediv.appendChild(tnode);
+      statdiv.appendChild(gamediv);
+    }
+    if (!statdiv.hasChildNodes()) {
+      statdiv.appendChild(document.createTextNode("No known games?"));
+    }
+    document.getElementById("startgame").appendChild(statdiv);
+  };
+  req.open('GET', '/pstatus/' + lname, true);
+  req.send();
+  return;
+}
+
 function startgame(ev) {
   ev.preventDefault();
   if (termemu.sessid != null || !lcred)
@@ -494,6 +558,7 @@
     document.getElementById("startgame").style.display = "block";
     document.getElementById("login").style.display = "none";
     document.getElementById("register").style.display = "none";
+    getchoices();
   }
   else if (mode == "login") {
     document.getElementById("keyboard").style.display = "none";