comparison 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
comparison
equal deleted inserted replaced
93:104409bf5f03 94:597e9477b8ae
307 var nextn = client.nrecv + client.recvQ.length + 1; 307 var nextn = client.nrecv + client.recvQ.length + 1;
308 client.Qtimeout = setTimeout(client.flushQ, 30000, client, nextn); 308 client.Qtimeout = setTimeout(client.flushQ, 30000, client, nextn);
309 } 309 }
310 tslog("Flushing queue for player %s", player.id); 310 tslog("Flushing queue for player %s", player.id);
311 }; 311 };
312 this.reset = function () {
313 /* To be called when the game is taken over. */
314 if (this.Qtimeout) {
315 clearTimeout(this.Qtimeout);
316 this.Qtimeout = null;
317 }
318 for (var i = 0; i < this.recvQ.length; i++) {
319 if (this.recvQ[i] !== undefined) {
320 this.session.write(this.recvQ[i]);
321 }
322 }
323 this.recvQ = [];
324 this.nrecv = 0;
325 this.nsend = 0;
326 this.sendQ = [{"t": "d", "n": this.nsend++,
327 "d": this.session.framebuf.toString("hex", 0, this.session.frameoff)}];
328 };
312 this.quit = function() { 329 this.quit = function() {
313 if (this.alive) 330 if (this.alive)
314 this.session.close(); 331 this.session.close();
315 }; 332 };
316 function openH(success, id) { 333 function openH(success, id) {
609 tslog("Request for nonexistant game \"%s\"", gname); 626 tslog("Request for nonexistant game \"%s\"", gname);
610 return; 627 return;
611 } 628 }
612 // A callback to pass to the game-in-progress checker. 629 // A callback to pass to the game-in-progress checker.
613 var launch = function(err, fname) { 630 var launch = function(err, fname) {
614 if (fname) { 631 var nodematch = new RegExp("^" + username + ":node:");
632 if (fname && (fname.match(nodematch) === null)) {
633 /* It's being played in dgamelaunch. */
615 sendError(res, 4, null); 634 sendError(res, 4, null);
616 tslog("%s is already playing %s", username, gname); 635 tslog("%s is already playing %s", username, gname);
617 return; 636 return;
618 } 637 }
619 // Game starting has been approved. 638 // Game starting has been approved.
628 else { 647 else {
629 sendError(res, 5, "Failed to open TTY"); 648 sendError(res, 5, "Failed to open TTY");
630 tslog("Unable to allocate TTY for %s", gname); 649 tslog("Unable to allocate TTY for %s", gname);
631 } 650 }
632 }; 651 };
633 new Player(gname, lkey, dims, respondlaunch); 652 if (fname) {
653 for (var cid in clients) {
654 cli = clients[cid];
655 if ((cli instanceof Player) &&
656 cli.session.pname == username &&
657 cli.session.game.uname == gname) {
658 cli.reset();
659 respondlaunch(cli, true);
660 tslog("Game %d has been taken over.", cli.session.sessid);
661 return;
662 }
663 }
664 sendError(res, 7);
665 }
666 else {
667 new Player(gname, lkey, dims, respondlaunch);
668 }
634 }; 669 };
635 checkprogress(username, games[gname], launch, []); 670 checkprogress(username, games[gname], launch, []);
636 } 671 }
637 672
638 function watch(req, res, formdata) { 673 function watch(req, res, formdata) {