diff rlgwebd.js @ 94:597e9477b8ae

RLG-Web: Allow games to be taken over. Make it possible to reconnect to a game if the user has left the page without saving.
author John "Elwin" Edwards <elwin@sdf.org>
date Wed, 11 Jul 2012 10:30:33 -0700
parents e07f98799120
children 3dbfdaf62623
line wrap: on
line diff
--- a/rlgwebd.js	Wed Jul 11 07:37:56 2012 -0700
+++ b/rlgwebd.js	Wed Jul 11 10:30:33 2012 -0700
@@ -309,6 +309,23 @@
     }
     tslog("Flushing queue for player %s", player.id);
   };
+  this.reset = function () {
+    /* To be called when the game is taken over. */
+    if (this.Qtimeout) {
+      clearTimeout(this.Qtimeout);
+      this.Qtimeout = null;
+    }
+    for (var i = 0; i < this.recvQ.length; i++) {
+      if (this.recvQ[i] !== undefined) {
+        this.session.write(this.recvQ[i]);
+      }
+    }
+    this.recvQ = [];
+    this.nrecv = 0;
+    this.nsend = 0;
+    this.sendQ = [{"t": "d", "n": this.nsend++, 
+        "d": this.session.framebuf.toString("hex", 0, this.session.frameoff)}];
+  };
   this.quit = function() {
     if (this.alive)
       this.session.close();
@@ -611,7 +628,9 @@
   }
   // A callback to pass to the game-in-progress checker.
   var launch = function(err, fname) {
-    if (fname) {
+    var nodematch = new RegExp("^" + username + ":node:");
+    if (fname && (fname.match(nodematch) === null)) {
+      /* It's being played in dgamelaunch. */
       sendError(res, 4, null);
       tslog("%s is already playing %s", username, gname);
       return;
@@ -630,7 +649,23 @@
         tslog("Unable to allocate TTY for %s", gname);
       }
     };
-    new Player(gname, lkey, dims, respondlaunch);
+    if (fname) {
+      for (var cid in clients) {
+        cli = clients[cid];
+        if ((cli instanceof Player) && 
+             cli.session.pname == username &&
+             cli.session.game.uname == gname) {
+          cli.reset();
+          respondlaunch(cli, true);
+          tslog("Game %d has been taken over.", cli.session.sessid);
+          return;
+        }
+      }
+      sendError(res, 7);
+    }
+    else {
+      new Player(gname, lkey, dims, respondlaunch);
+    }
   };
   checkprogress(username, games[gname], launch, []);
 }