rlgwebd.js: add a player status interface.
Give information on a player's current status at "/pstatus/<name>". Also fix some shift()/unshift() confusion in checkprogress().
This commit is contained in:
parent
7e88c7cdc3
commit
43f30438ed
1 changed files with 73 additions and 2 deletions
75
rlgwebd.js
75
rlgwebd.js
|
|
@ -26,21 +26,25 @@ var games = {
|
|||
"rogue3": {
|
||||
"name": "Rogue V3",
|
||||
"uname": "rogue3",
|
||||
"suffix": ".r3sav",
|
||||
"path": "/bin/rogue3"
|
||||
},
|
||||
"rogue4": {
|
||||
"name": "Rogue V4",
|
||||
"uname": "rogue4",
|
||||
"suffix": ".r4sav",
|
||||
"path": "/bin/rogue4"
|
||||
},
|
||||
"rogue5": {
|
||||
"name": "Rogue V5",
|
||||
"uname": "rogue5",
|
||||
"suffix": ".r5sav",
|
||||
"path": "/bin/rogue5"
|
||||
},
|
||||
"srogue": {
|
||||
"name": "Super-Rogue",
|
||||
"uname": "srogue",
|
||||
"suffix": ".srsav",
|
||||
"path": "/bin/srogue"
|
||||
}
|
||||
};
|
||||
|
|
@ -236,16 +240,57 @@ function checkprogress(user, game, callback, args) {
|
|||
var fre = RegExp("^" + user + ":");
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
if (files[i].match(fre)) {
|
||||
args.shift(null, files[i]);
|
||||
args.unshift(null, files[i]);
|
||||
callback.apply(null, args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
args.shift(null, false);
|
||||
args.unshift(null, false);
|
||||
callback.apply(null, args);
|
||||
});
|
||||
}
|
||||
|
||||
function checksaved(user, game, callback, args) {
|
||||
var savedirc = game.uname + "save";
|
||||
var basename = String(dropToUID) + "-" + user + game.suffix;
|
||||
var savefile = path.join("/var/games/roguelike", savedirc, basename);
|
||||
path.exists(savefile, function (exist) {
|
||||
args.unshift(exist);
|
||||
callback.apply(null, args);
|
||||
});
|
||||
}
|
||||
|
||||
function playerstatus(user, callback) {
|
||||
var sdata = {};
|
||||
tslog("Starting (user %s)", user);
|
||||
function finishp() {
|
||||
for (var gname in games) {
|
||||
if (!(gname in sdata))
|
||||
return;
|
||||
}
|
||||
tslog("Finished: %s", JSON.stringify(sdata));
|
||||
callback(sdata);
|
||||
}
|
||||
function regsaved(exists, game) {
|
||||
if (exists)
|
||||
sdata[game.uname] = "s";
|
||||
else
|
||||
sdata[game.uname] = "0";
|
||||
finishp();
|
||||
}
|
||||
function regactive(err, filename, game) {
|
||||
if (!err && filename) {
|
||||
sdata[game.uname] = "p";
|
||||
finishp();
|
||||
}
|
||||
else
|
||||
checksaved(user, game, regsaved, [game]);
|
||||
}
|
||||
for (var gname in games) {
|
||||
checkprogress(user, games[gname], regactive, [games[gname]]);
|
||||
}
|
||||
}
|
||||
|
||||
/* A few utility functions */
|
||||
function timestamp() {
|
||||
dd = new Date();
|
||||
|
|
@ -609,6 +654,29 @@ function statusmsg(req, res) {
|
|||
res.end();
|
||||
}
|
||||
|
||||
function pstatusmsg(req, res) {
|
||||
if (req.method == 'HEAD') {
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
var target = url.parse(req.url).pathname;
|
||||
var pmatch = target.match(/^\/pstatus\/(.*)/);
|
||||
if (pmatch && pmatch[1])
|
||||
var pname = pmatch[1];
|
||||
else {
|
||||
sendError(res, 2, "No name given.");
|
||||
return;
|
||||
}
|
||||
var reply = {"name": pname};
|
||||
playerstatus(pname, function (pdata) {
|
||||
reply["stat"] = pdata;
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.write(JSON.stringify(reply));
|
||||
res.end();
|
||||
});
|
||||
}
|
||||
|
||||
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
|
||||
"Login failed", "Already playing", "Game launch failed",
|
||||
"Server shutting down", "Game not in progress" ];
|
||||
|
|
@ -698,6 +766,9 @@ function webHandler(req, res) {
|
|||
else if (target == '/status') {
|
||||
statusmsg(req, res);
|
||||
}
|
||||
else if (target.match(/^\/pstatus\//)) {
|
||||
pstatusmsg(req, res);
|
||||
}
|
||||
else /* Go look for it in the filesystem */
|
||||
serveStatic(req, res, target);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue