comparison 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
comparison
equal deleted inserted replaced
110:18a81cc0084b 111:f56fdfeed01a
894 else 894 else
895 readFeed(client, res); 895 readFeed(client, res);
896 return; 896 return;
897 } 897 }
898 898
899 /* Stops a running game if the request has the proper key. */
900 function stopgame(res, formdata) {
901 if (!("key" in formdata) || !(formdata["key"] in logins)) {
902 sendError(res, 1);
903 return;
904 }
905 var pname = logins[formdata["key"]].name;
906 if (!("g" in formdata) || !(formdata["g"] in games)) {
907 sendError(res, 2, "No such game.");
908 return;
909 }
910 var gname = formdata["g"];
911 function checkback(err, fname) {
912 if (!fname) {
913 sendError(res, 7);
914 return;
915 }
916 var fullfile = path.join("/dgldir/inprogress-" + gname, fname);
917 fs.readFile(fullfile, "utf8", function(err, fdata) {
918 if (err) {
919 sendError(res, 7);
920 return;
921 }
922 var pid = parseInt(fdata.split('\n')[0], 10);
923 process.kill(pid, 'SIGHUP');
924 /* The response doesn't mean that the game is gone. The only way
925 * to make sure a dgamelaunch-supervised game is over would be to
926 * poll fname until it disappears. */
927 res.writeHead(200, {'Content-Type': 'application/json'});
928 res.write(JSON.stringify({"t": "q"}));
929 res.end();
930 });
931 }
932 checkprogress(pname, games[gname], checkback, []);
933 }
934
899 function findClient(formdata, playersOnly) { 935 function findClient(formdata, playersOnly) {
900 if (typeof(formdata) != "object") 936 if (typeof(formdata) != "object")
901 return null; 937 return null;
902 if ("id" in formdata) { 938 if ("id" in formdata) {
903 var id = formdata["id"]; 939 var id = formdata["id"];
1123 startgame(req, res, formdata); 1159 startgame(req, res, formdata);
1124 } 1160 }
1125 else if (target == "/watch") { 1161 else if (target == "/watch") {
1126 watch(req, res, formdata); 1162 watch(req, res, formdata);
1127 } 1163 }
1164 else if (target == "/quit") {
1165 stopgame(res, formdata);
1166 }
1128 else { 1167 else {
1129 res.writeHead(405, resheaders); 1168 res.writeHead(405, resheaders);
1130 res.end(); 1169 res.end();
1131 } 1170 }
1132 } 1171 }