diff rlgterm.js @ 58:7a50b4412fea

Move credentials into the drivers. The session id, or whether the session is currently active on the server, are better handled in the drivers than in termemu.js.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 19 Jun 2012 13:40:30 -0700
parents e7c864e80a79
children 00b985b8ba6a
line wrap: on
line diff
--- a/rlgterm.js	Mon Jun 18 16:53:44 2012 -0700
+++ b/rlgterm.js	Tue Jun 19 13:40:30 2012 -0700
@@ -71,6 +71,8 @@
 /* Login name and key */
 var lname = null;
 var lcred = null;
+/* The session id assigned by the server. */
+var sessid = null;
 
 function writeData(hexstr) {
   var codenum;
@@ -211,10 +213,10 @@
 }
 
 function getData() {
-  if (termemu.sessid == null)
+  if (sessid == null)
     return;
   var datareq = new XMLHttpRequest();
-  var msg = JSON.stringify({"id": termemu.sessid, "t": "n"});
+  var msg = JSON.stringify({"id": sessid, "t": "n"});
   datareq.onreadystatechange = function () {
     if (datareq.readyState == 4 && datareq.status == 200) {
       var wasdata = processMsg(datareq.responseText);
@@ -243,7 +245,7 @@
 
 function sendback(str) {
   /* For responding to terminal queries. */
-  var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": str};
+  var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": str};
   var datareq = new XMLHttpRequest();
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
@@ -252,7 +254,7 @@
 }
 
 function sendkey(ev) {
-  if (termemu.sessid == null)
+  if (sessid == null)
     return;
   var keynum = ev.keyCode;
   var code;
@@ -290,10 +292,10 @@
     return;
   }
   // Isn't this check redundant?
-  if (termemu.sessid != null)
+  if (sessid != null)
     ev.preventDefault();
   var datareq = new XMLHttpRequest();
-  var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": code};
+  var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": code};
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
   datareq.send(JSON.stringify(msgDict));
@@ -305,7 +307,7 @@
 }
 
 function vkey(c) {
-  if (termemu.sessid == null)
+  if (sessid == null)
     return;
   var keystr;
   if (c.match(/^[a-z]$/)) {
@@ -343,7 +345,7 @@
   else
     return;
   var datareq = new XMLHttpRequest();
-  var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": keystr};
+  var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": keystr};
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
   datareq.send(JSON.stringify(msgDict));
@@ -380,7 +382,7 @@
 
 function formlogin(ev) {
   ev.preventDefault();
-  if (termemu.sessid != null)
+  if (sessid != null)
     return;
   var loginmsg = {};
   loginmsg["name"] = document.getElementById("input_name").value;
@@ -410,7 +412,7 @@
 }
 
 function getcurrent() {
-  if (termemu.sessid)
+  if (sessid)
     return;
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
@@ -454,7 +456,7 @@
 }
 
 function getchoices() {
-  if (termemu.sessid != null || !lcred)
+  if (sessid != null || !lcred)
     return;
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
@@ -521,7 +523,7 @@
 }
 
 function startgame(game) {
-  if (termemu.sessid != null || !lcred)
+  if (sessid != null || !lcred)
     return;
   var smsg = {};
   smsg["key"] = lcred;
@@ -535,10 +537,10 @@
     var reply = JSON.parse(req.responseText);
     if (reply.t == 's') {
       /* Success */
-      termemu.sessid = reply.id;
+      sessid = reply.id;
       termemu.resize(reply.h, reply.w);
       setTitle("Playing as " + lname);
-      debug(1, "Playing with id " + termemu.sessid);
+      debug(1, "Playing with id " + sessid);
       setmode("play");
       getData();
     }
@@ -556,7 +558,7 @@
 
 function formreg(ev) {
   ev.preventDefault();
-  if (termemu.sessid != null)
+  if (sessid != null)
     return;
   var regmsg = {};
   regmsg["name"] = document.getElementById("regin_name").value;
@@ -589,10 +591,10 @@
 }
 
 function gameover() {
-  if (termemu.sessid == null)
+  if (sessid == null)
     return;
   /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */
-  termemu.sessid = null;
+  sessid = null;
   ajaxstate.clear();
   setTitle("Game over.");
   nsend = 0;
@@ -609,7 +611,7 @@
 }
 
 function stop() {
-  if (!termemu.sessid)
+  if (!sessid)
     return;
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
@@ -619,7 +621,7 @@
     }
   };
   req.open('POST', '/feed', true);
-  req.send(JSON.stringify({"id": termemu.sessid, "t": "q"}));
+  req.send(JSON.stringify({"id": sessid, "t": "q"}));
   return;
 }