From 2828b5f33c770e4291e1d8d08630b5196a68f845 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Wed, 11 Jul 2012 10:30:33 -0700 Subject: [PATCH] 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. --- rlgterm.js | 17 ++++++++++++++--- rlgwebd.js | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/rlgterm.js b/rlgterm.js index fe9c9f6..0acf41a 100644 --- a/rlgterm.js +++ b/rlgterm.js @@ -562,14 +562,14 @@ function getchoices() { else if (reply.stat[gname] == "0") acttext = "Start a game"; else if (reply.stat[gname] == "p") - acttext = "Game in progress"; + acttext = "Reconnect"; else if (reply.stat[gname] == "d") acttext = "Game in progress (dgl)"; else continue; var button = document.createElement("span"); button.appendChild(document.createTextNode(acttext)); - if ("s0".indexOf(reply.stat[gname]) >= 0) { + if ("s0p".indexOf(reply.stat[gname]) >= 0) { button.onclick = makeStarter(gname); button.className = "ibutton"; } @@ -628,9 +628,20 @@ function startgame(game) { } else if (reply.t == 'E') { debug(1, "Could not start game: " + reply.s); - message("Your game could not be started: " + reply.s); if (reply.c == 1) { logout(); + message("The server forgot about you, please log in again."); + } + else if (reply.c == 4) { + message("You can't play that game because it is currently being " + + "played over SSH."); + } + else if (reply.c == 7) { + message("The game is being saved, try again in a few seconds."); + } + else { + message("The server says it can't start your game because \"" + + reply.s + "\". This is probably a bug."); } } }; diff --git a/rlgwebd.js b/rlgwebd.js index cec8ec8..4d1f2b3 100755 --- a/rlgwebd.js +++ b/rlgwebd.js @@ -309,6 +309,23 @@ function Player(gamename, lkey, dims, callback) { } 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 @@ function startgame(req, res, formdata) { } // 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 @@ function startgame(req, res, formdata) { 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, []); }