Mercurial > hg > rlgwebd
comparison rlgterm.js @ 111:f56fdfeed01a
Replace taking over games with forced saves.
Instead of reusing the id, just SIGHUP the game process. This works
whether it is using polling, WebSockets, or dgamelaunch.
| author | John "Elwin" Edwards <elwin@sdf.org> |
|---|---|
| date | Sun, 15 Jul 2012 22:33:44 -0700 |
| parents | 67b393f10c2b |
| children | 4f2b89e6fde2 |
comparison
equal
deleted
inserted
replaced
| 110:18a81cc0084b | 111:f56fdfeed01a |
|---|---|
| 620 var acttext; | 620 var acttext; |
| 621 if (reply.stat[gname] == "s") | 621 if (reply.stat[gname] == "s") |
| 622 acttext = "Resume your game"; | 622 acttext = "Resume your game"; |
| 623 else if (reply.stat[gname] == "0") | 623 else if (reply.stat[gname] == "0") |
| 624 acttext = "Start a game"; | 624 acttext = "Start a game"; |
| 625 else if (reply.stat[gname] == "p") | 625 else if (reply.stat[gname] == "p" || reply.stat[gname] == "d") |
| 626 acttext = "Reconnect"; | 626 acttext = "Force save"; |
| 627 else if (reply.stat[gname] == "d") | |
| 628 acttext = "Game in progress (dgl)"; | |
| 629 else | 627 else |
| 630 continue; | 628 continue; |
| 631 var button = document.createElement("span"); | 629 var button = document.createElement("span"); |
| 632 button.appendChild(document.createTextNode(acttext)); | 630 button.appendChild(document.createTextNode(acttext)); |
| 633 if ("s0p".indexOf(reply.stat[gname]) >= 0) { | 631 if ("s0".indexOf(reply.stat[gname]) >= 0) { |
| 634 button.onclick = makeStarter(gname); | 632 button.onclick = makeStarter(gname); |
| 633 button.className = "ibutton"; | |
| 634 } | |
| 635 else { | |
| 636 button.onclick = makeStopper(gname); | |
| 635 button.className = "ibutton"; | 637 button.className = "ibutton"; |
| 636 } | 638 } |
| 637 var actdiv = document.createElement("div"); | 639 var actdiv = document.createElement("div"); |
| 638 actdiv.appendChild(button); | 640 actdiv.appendChild(button); |
| 639 var gamediv = document.createElement("div"); | 641 var gamediv = document.createElement("div"); |
| 717 req.open('POST', '/play', true); | 719 req.open('POST', '/play', true); |
| 718 req.send(JSON.stringify(smsg)); | 720 req.send(JSON.stringify(smsg)); |
| 719 return; | 721 return; |
| 720 } | 722 } |
| 721 | 723 |
| 724 function makeStopper(gname) { | |
| 725 if (!(gname in games)) | |
| 726 return null; | |
| 727 var game = games[gname]; | |
| 728 function stopper(ev) { | |
| 729 stopgame(game); | |
| 730 } | |
| 731 return stopper; | |
| 732 } | |
| 733 | |
| 734 function stopgame(game) { | |
| 735 if (!session.lcred) | |
| 736 return; | |
| 737 var stopmsg = {"key": session.lcred, "g": game.uname}; | |
| 738 var req = new XMLHttpRequest(); | |
| 739 req.onerror = errHandler; | |
| 740 req.onreadystatechange = function () { | |
| 741 if (req.readyState != 4 || req.status != 200) | |
| 742 return; | |
| 743 var reply = JSON.parse(req.responseText); | |
| 744 if (reply.t == 'E') { | |
| 745 if (reply.c == 7) | |
| 746 message("That game has already stopped."); | |
| 747 else if (reply.c == 1) { | |
| 748 logout(); | |
| 749 message("The server forgot about you, please log in again.", "warn"); | |
| 750 } | |
| 751 else { | |
| 752 message("That game could not be stopped because: " + reply.s + | |
| 753 "This might be a bug.", "warn"); | |
| 754 } | |
| 755 } | |
| 756 } | |
| 757 req.open('POST', '/quit', true); | |
| 758 req.send(JSON.stringify(stopmsg)); | |
| 759 return; | |
| 760 } | |
| 761 | |
| 722 function wsStart(game) { | 762 function wsStart(game) { |
| 723 var sockurl = "ws://" + window.location.host + "/play/" + game.uname; | 763 var sockurl = "ws://" + window.location.host + "/play/" + game.uname; |
| 724 sockurl += "?key=" + session.lcred + "&w=80&h=24"; | 764 sockurl += "?key=" + session.lcred + "&w=80&h=24"; |
| 725 ws = new WebSocket(sockurl); | 765 ws = new WebSocket(sockurl); |
| 726 ws.onopen = function (event) { | 766 ws.onopen = function (event) { |
