diff rlgwebd.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 18a81cc0084b
children 4f2b89e6fde2
line wrap: on
line diff
--- a/rlgwebd.js	Sun Jul 15 21:03:36 2012 -0700
+++ b/rlgwebd.js	Sun Jul 15 22:33:44 2012 -0700
@@ -896,6 +896,42 @@
   return;
 }
 
+/* Stops a running game if the request has the proper key. */
+function stopgame(res, formdata) {
+  if (!("key" in formdata) || !(formdata["key"] in logins)) {
+    sendError(res, 1);
+    return;
+  }
+  var pname = logins[formdata["key"]].name;
+  if (!("g" in formdata) || !(formdata["g"] in games)) {
+    sendError(res, 2, "No such game.");
+    return;
+  }
+  var gname = formdata["g"];
+  function checkback(err, fname) {
+    if (!fname) {
+      sendError(res, 7);
+      return;
+    }
+    var fullfile = path.join("/dgldir/inprogress-" + gname, fname);
+    fs.readFile(fullfile, "utf8", function(err, fdata) {
+      if (err) {
+        sendError(res, 7);
+        return;
+      }
+      var pid = parseInt(fdata.split('\n')[0], 10);
+      process.kill(pid, 'SIGHUP');
+      /* The response doesn't mean that the game is gone.  The only way
+       * to make sure a dgamelaunch-supervised game is over would be to
+       * poll fname until it disappears. */
+      res.writeHead(200, {'Content-Type': 'application/json'});
+      res.write(JSON.stringify({"t": "q"}));
+      res.end();
+    });
+  }
+  checkprogress(pname, games[gname], checkback, []);
+}
+
 function findClient(formdata, playersOnly) {
   if (typeof(formdata) != "object")
     return null;
@@ -1125,6 +1161,9 @@
       else if (target == "/watch") {
         watch(req, res, formdata);
       }
+      else if (target == "/quit") {
+        stopgame(res, formdata);
+      }
       else {
         res.writeHead(405, resheaders);
         res.end();