annotate rlgwebd.js @ 156:127f9e256d02

Keep a list of dgamelaunch games and put it in the /status message.
author John "Elwin" Edwards
date Tue, 01 Apr 2014 16:43:35 -0700
parents 245a2959f504
children e7f809f06c5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 var http = require('http');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
4 var net = require('net');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var fs = require('fs');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
8 var events = require('events');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 var child_process = require('child_process');
141
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
10 // Dependencies
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
11 var posix = require("posix");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
12 var pty = require("pty.js");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
13 var WebSocketServer = require("websocket").server;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
15 /* Configuration variables */
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
16 // The first file is NOT in the chroot.
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
17 var ctlsocket = "/var/local/rlgwebd/ctl";
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
18 var httpPort = 8080;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 var chrootDir = "/var/dgl/";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 var dropToUID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 var dropToGID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 var serveStaticRoot = "/var/www/"; // inside the chroot
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
23 var playtimeout = 3600000; // Idle time before games are autosaved, in ms
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
24
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
25 /* Data on the games available. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
26 var games = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
27 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
28 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
29 "uname": "rogue3",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
30 "suffix": ".r3sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
31 "path": "/usr/bin/rogue3",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
32 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
35 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
36 "uname": "rogue4",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
37 "suffix": ".r4sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
38 "path": "/usr/bin/rogue4",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
39 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 "uname": "rogue5",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
44 "suffix": ".r5sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
45 "path": "/usr/bin/rogue5",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
46 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 "uname": "srogue",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
51 "suffix": ".srsav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
52 "path": "/usr/bin/srogue",
121
077adfeea038 Correct the clear sequence for srogue.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
53 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
54 },
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
55 "arogue5": {
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
56 "name": "Advanced Rogue 5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
57 "uname": "arogue5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
58 "suffix": ".ar5sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
59 "path": "/usr/bin/arogue5",
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
60 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
62 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
63
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
64 /* Global state */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
65 var logins = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
66 var sessions = {};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
67 var clients = {};
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
68 var dglgames = {};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
69 var allowlogin = true;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
70 var nextsession = 0;
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
71 var gamemux = new events.EventEmitter();
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
72
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
73 /* Constructor. A TermSession handles a pty and the game running on it.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
74 * game: (String) Name of the game to launch.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
75 * lkey: (String, key) The user's id, a key into logins.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
76 * dims: (Array [Number, Number]) Height and width of the pty.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
77 * handlers: (Object) Key-value pairs, event names and functions to
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
78 * install to handle them.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
79 * Events:
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
80 * "open": Emitted on startup. Parameters: success (Boolean)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
81 * "data": Data generated by child. Parameters: buf (Buffer)
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
82 * "exit": Child terminated. Parameters: none
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83 */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
84 function TermSession(game, lkey, dims, handlers) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
85 var ss = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
86 /* Subclass EventEmitter to do the hard work. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
87 events.EventEmitter.call(this);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
88 for (var evname in handlers)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
89 this.on(evname, handlers[evname]);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
90 /* Don't launch anything that's not a real game. */
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
91 if (game in games) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
92 this.game = games[game];
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
93 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
94 else {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
95 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
96 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
98 if (lkey in logins) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
99 this.key = lkey;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
100 this.pname = logins[lkey].name;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
101 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
102 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
103 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
104 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 /* Grab a spot in the sessions table. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
107 this.sessid = nextsession++;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 sessions[this.sessid] = this;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
109 /* Set up the sizes. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
110 this.w = Math.floor(Number(dims[1]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
111 if (!(this.w > 0 && this.w < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
112 this.w = 80;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
113 this.h = Math.floor(Number(dims[0]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
114 if (!(this.h > 0 && this.h < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
115 this.h = 24;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
116 /* Environment. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
117 var childenv = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
118 for (var key in process.env) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
119 childenv[key] = process.env[key];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
120 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
121 var args = ["-n", this.pname];
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
122 var spawnopts = {"env": childenv, "cwd": "/", "rows": this.h, "cols": this.w,
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
123 "name": "xterm-256color"};
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
124 this.term = pty.spawn(this.game.path, args, spawnopts);
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
125 tslog("%s playing %s (index %d, pid %d)", this.pname, this.game.uname,
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
126 this.sessid, this.term.pid);
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
127 this.emit('open', true, this.sessid);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
128 gamemux.emit('begin', this.sessid, this.pname, this.game.uname);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
129 /* Set up the lockfile and ttyrec */
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
130 var ts = timestamp();
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
131 var progressdir = path.join("/dgldir/inprogress", this.game.uname);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
132 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec");
110
18a81cc0084b RLG-Web: fix lockfile mixup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
133 var lmsg = this.term.pid.toString() + '\n' + this.h + '\n' + this.w + '\n';
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
134 fs.writeFile(this.lock, lmsg, "utf8");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
135 var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.game.uname,
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
136 ts + ".ttyrec");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
137 this.record = fs.createWriteStream(ttyrec, { mode: 0664 });
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
138 /* Holds the output since the last screen clear, so watchers can begin
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
139 * with a complete screen. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
140 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
141 this.frameoff = 0;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
142 logins[lkey].sessions.push(this.sessid);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 /* END setup */
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
144 function ttyrec_chunk(datastr) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 var ts = new Date();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
146 var buf = new Buffer(datastr);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 var chunk = new Buffer(buf.length + 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 /* TTYREC headers */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 chunk.writeUInt32LE(Math.floor(ts.getTime() / 1000), 0);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151 chunk.writeUInt32LE(buf.length, 8);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 buf.copy(chunk, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 ss.record.write(chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
154 ss.framepush(buf);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
155 ss.emit('data', buf);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
156 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
157 this.term.on("data", ttyrec_chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
158 this.framepush = function(chunk) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
159 /* If this chunk resets the screen, discard what preceded it. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
160 if (bufncmp(chunk, this.game.clear, this.game.clear.length)) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
161 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
162 this.frameoff = 0;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
163 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
164 /* Make sure there's space. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
165 while (this.framebuf.length < chunk.length + this.frameoff) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
166 var nbuf = new Buffer(this.framebuf.length * 2);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
167 this.framebuf.copy(nbuf, 0, 0, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
168 this.framebuf = nbuf;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
169 if (this.framebuf.length > 65536) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
170 tslog("Warning: Game %d frame buffer at %d bytes", this.sessid,
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
171 this.framebuf.length);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
172 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
173 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
174 chunk.copy(this.framebuf, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
175 this.frameoff += chunk.length;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
176 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
177 this.write = function(data) {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
178 this.term.write(data);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
179 };
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
180 // Teardown.
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
181 this.term.on("exit", function () {
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
182 var id = ss.sessid;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 fs.unlink(ss.lock);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
184 ss.record.end();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
185 ss.emit('exit');
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
186 gamemux.emit('end', id, ss.pname, ss.game.uname);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
187 delete sessions[id];
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
188 tslog("Game %s ended.", id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 });
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
190 this.close = function () {
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
191 if (this.sessid in sessions)
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
192 this.term.kill('SIGHUP');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
193 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
194 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
195 TermSession.prototype = new events.EventEmitter();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
196
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
197 function Watcher(session) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
198 var ss = this; // that
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
199 this.session = session;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
200 this.alive = true;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
201 /* State for messaging. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
202 this.nsend = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
203 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
204 /* Get a place in the table. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
205 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
206 while (this.id in clients) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
207 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
208 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
209 clients[this.id] = this;
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
210 /* Recreate the current screen state from the session's buffer. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
211 this.sendQ.push({"t": "d", "n": this.nsend++,
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
212 "d": session.framebuf.toString("hex", 0, session.frameoff)});
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
213 function dataH(buf) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
214 var reply = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
215 reply.t = "d";
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
216 reply.n = ss.nsend++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
217 reply.d = buf.toString("hex");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
218 ss.sendQ.push(reply);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
219 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
220 function exitH() {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
221 ss.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
222 ss.sendQ.push({"t": "q"});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
223 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
224 session.on('data', dataH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
225 session.on('exit', exitH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
226 this.read = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
227 /* Returns an array of all outstanding messages, empty if none. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
228 var temp = this.sendQ;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
229 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
230 /* Clean up if finished. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
231 if (!this.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
232 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
233 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
234 return temp;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
235 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
236 this.quit = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
237 this.session.removeListener('data', dataH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
238 this.session.removeListener('exit', exitH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
239 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
240 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
241 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
242
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
243 function Player(gamename, lkey, dims, callback) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
244 var ss = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
245 this.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
246 /* State for messaging. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
247 this.nsend = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
248 this.nrecv = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
249 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
250 this.recvQ = []
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
251 this.Qtimeout = null;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
252 /* Get a place in the table. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
253 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
254 while (this.id in clients) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
255 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
256 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
257 clients[this.id] = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
258
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
259 this.read = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
260 var temp = this.sendQ;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
261 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
262 /* Clean up if finished. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
263 if (!this.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
264 clearTimeout(this.Qtimeout);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
265 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
266 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
267 return temp;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
268 };
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
269 this.write = function (data, n) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
270 if (!this.alive || typeof (n) != "number") {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
271 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
272 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
273 var oindex = n - this.nrecv;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
274 if (oindex === 0) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
275 this.session.write(data);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
276 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
277 var next;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
278 while ((next = this.recvQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
279 this.session.write(next);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
280 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
281 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
282 if (this.recvQ.length == 0 && this.Qtimeout) {
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
283 clearTimeout(this.Qtimeout);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
284 this.Qtimeout = null;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
285 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
286 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
287 else if (oindex > 0 && oindex <= 1024) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
288 tslog("Client %s: Stashing message %d at %d", this.id, n, oindex - 1);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
289 this.recvQ[oindex - 1] = data;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
290 if (!this.Qtimeout) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
291 var nextn = this.nrecv + this.recvQ.length + 1;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
292 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
293 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
294 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
295 /* Otherwise, discard it */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
296 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
297 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
298 this.flushQ = function (client, n) {
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
299 /* Callback for when an unreceived message times out.
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
300 * n is the first empty space that will not be given up on. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
301 if (!client.alive || client.nrecv >= n)
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
302 return;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
303 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
304 var next;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
305 /* Clear the queue up to n */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
306 while (client.nrecv < n) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
307 next = client.recvQ.shift();
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
308 if (next !== undefined)
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
309 client.session.write(next);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
310 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
311 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
312 /* Clear out anything that's ready. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
313 while ((next = client.recvQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
314 client.session.write(next);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
315 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
316 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
317 /* Now set another timeout if necessary. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
318 if (client.recvQ.length != 0) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
319 var nextn = client.nrecv + client.recvQ.length + 1;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
320 client.Qtimeout = setTimeout(client.flushQ, 30000, client, nextn);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
321 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
322 tslog("Flushing queue for player %s", player.id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
323 };
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
324 this.reset = function () {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
325 /* To be called when the game is taken over. */
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
326 if (this.Qtimeout) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
327 clearTimeout(this.Qtimeout);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
328 this.Qtimeout = null;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
329 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
330 for (var i = 0; i < this.recvQ.length; i++) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
331 if (this.recvQ[i] !== undefined) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
332 this.session.write(this.recvQ[i]);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
333 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
334 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
335 this.recvQ = [];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
336 this.nrecv = 0;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
337 this.nsend = 0;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
338 this.sendQ = [{"t": "d", "n": this.nsend++,
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
339 "d": this.session.framebuf.toString("hex", 0, this.session.frameoff)}];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
340 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
341 this.quit = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
342 if (this.alive)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
343 this.session.close();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
344 };
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
345 function openH(success, id) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
346 if (success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
347 ss.alive = true;
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
348 ss.session = sessions[id];
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
349 ss.h = sessions[id].h;
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
350 ss.w = sessions[id].w;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
352 callback(ss, success);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
353 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
354 function dataH(chunk) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
355 var reply = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
356 reply.t = "d";
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
357 reply.n = ss.nsend++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
358 reply.d = chunk.toString("hex");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
359 ss.sendQ.push(reply);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
360 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
361 function exitH() {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
362 ss.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
363 ss.sendQ.push({"t": "q"});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
364 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
365 var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
366 this.session = new TermSession(gamename, lkey, dims, handlers);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
369 // Also known as WebSocketAndTermSessionClosureGlueFactory
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
370 function wsWatcher(conn, session) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
371 var ss = this; // is this even needed?
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
372 var dataH = function(buf) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
373 conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
374 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
375 var exitH = function() {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
376 if (conn.connected)
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
377 conn.close();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
378 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
379 session.on('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
380 session.on('exit', exitH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
381 conn.on('close', function(code, desc) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
382 session.removeListener('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
383 session.removeListener('exit', exitH);
102
f34a286c51bd RLG-Web server: code cleanup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
384 if (session.sessid in sessions)
f34a286c51bd RLG-Web server: code cleanup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
385 tslog("A WebSocket watcher has left game %d", session.sessid);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
386 });
101
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
387 conn.sendUTF(JSON.stringify({
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
388 "t": "w", "w": session.w, "h": session.h,
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
389 "p": session.pname, "g": session.game.uname
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
390 }));
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
391 conn.sendUTF(JSON.stringify({"t": "d",
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
392 "d": session.framebuf.toString("hex", 0, session.frameoff)}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
393 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
394
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
395 function wsPlay(wsReq, game, lkey, dims) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
396 var conn;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
397 var session;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
398 /* Listeners on the WebSocket */
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
399 function messageH(message) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
400 var parsedMsg = getMsgWS(message);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
401 if (parsedMsg.t == 'q') {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
402 session.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
403 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
404 else if (parsedMsg.t == 'd') {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
405 var hexstr = parsedMsg.d.replace(/[^0-9a-f]/gi, "");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
406 if (hexstr.length % 2 != 0) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
407 hexstr = hexstr.slice(0, -1);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
408 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
409 var keybuf = new Buffer(hexstr, "hex");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
410 session.write(keybuf);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
411 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
412 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
413 function closeH() {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
414 session.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
415 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
416 /* These listen on the TermSession. */
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
417 function openH(success, id) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
418 if (success) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
419 var reply = {"t": "s", "id": id, "w": sessions[id].w, "h":
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
420 sessions[id].h, "p": sessions[id].pname, "g": game};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
421 conn = wsReq.accept(null, wsReq.origin);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
422 conn.sendUTF(JSON.stringify(reply));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
423 conn.on('message', messageH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
424 conn.on('close', closeH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
425 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
426 else {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
427 wsReq.reject(500, errorcodes[5]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
428 tslog("Unable to allocate TTY for %s", game);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
429 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
430 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
431 function dataH(chunk) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
432 var msg = {};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
433 msg.t = "d";
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
434 msg.d = chunk.toString("hex");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
435 conn.sendUTF(JSON.stringify(msg));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
436 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
437 function exitH() {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
438 if (conn.connected)
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
439 conn.sendUTF(JSON.stringify({"t": "q"}));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
440 conn.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
441 session.removeListener('open', openH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
442 session.removeListener('data', dataH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
443 session.removeListener('exit', exitH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
444 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
445 var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
446 session = new TermSession(game, lkey, dims, handlers);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
447 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
448
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
449 function wsStart(wsReq) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
450 var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
451 if (!playmatch[1] || !(playmatch[1] in games)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
452 wsReq.reject(404, errorcodes[2]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
453 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
454 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
455 var gname = playmatch[1];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
456 if (!allowlogin) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
457 wsReq.reject(404, errorcodes[6]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
458 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
459 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
460 if (!("key" in wsReq.resourceURL.query)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
461 wsReq.reject(404, "No key given.");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
462 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
463 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
464 var lkey = wsReq.resourceURL.query["key"];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
465 if (!(lkey in logins)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
466 wsReq.reject(404, errorcodes[1]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
467 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
468 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
469 var pname = logins[lkey].name;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
470 var dims = [wsReq.resourceURL.query.h, wsReq.resourceURL.query.w];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
471 function progcallback(err, fname) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
472 if (fname) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
473 wsReq.reject(404, errorcodes[4]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
474 tslog("%s is already playing %s", pname, gname);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
475 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
476 else
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
477 wsPlay(wsReq, gname, lkey, dims);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
478 };
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
479 checkprogress(pname, games[gname], progcallback, []);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
480 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
481
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
482 /* Some functions which check whether a player is currently playing or
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
483 * has a saved game. Maybe someday they will provide information on
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
484 * the game. */
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
485 function checkprogress(user, game, callback, args) {
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
486 var progressdir = path.join("/dgldir/inprogress", game.uname);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
487 fs.readdir(progressdir, function(err, files) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
488 if (err) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
489 args.unshift(err, null);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
490 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
491 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
492 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
493 var fre = RegExp("^" + user + ":");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
494 for (var i = 0; i < files.length; i++) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
495 if (files[i].match(fre)) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
496 args.unshift(null, files[i]);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
497 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
498 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
499 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
500 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
501 args.unshift(null, false);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
502 callback.apply(null, args);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
503 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
504 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
505
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
506 function checksaved(user, game, callback, args) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
507 var savedirc = game.uname + "save";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
508 var basename = String(dropToUID) + "-" + user + game.suffix;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
509 var savefile = path.join("/var/games/roguelike", savedirc, basename);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
510 fs.exists(savefile, function (exist) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
511 args.unshift(exist);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
512 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
513 });
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
514 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
515
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
516 function playerstatus(user, callback) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
517 var sdata = {};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
518 function finishp() {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
519 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
520 if (!(gname in sdata))
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
521 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
522 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
523 callback(sdata);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
524 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
525 function regsaved(exists, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
526 if (exists)
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
527 sdata[game.uname] = "s";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
528 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
529 sdata[game.uname] = "0";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
530 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
531 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
532 function regactive(err, filename, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
533 if (!err && filename) {
88
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
534 if (filename.match(/^[^:]*:node:/))
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
535 sdata[game.uname] = "p";
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
536 else
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
537 sdata[game.uname] = "d";
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
538 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
539 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
540 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
541 checksaved(user, game, regsaved, [game]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
542 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
543 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
544 checkprogress(user, games[gname], regactive, [games[gname]]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
545 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
546 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
547
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548 /* A few utility functions */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
549 function timestamp() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
550 dd = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
551 sd = dd.toISOString();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
552 sd = sd.slice(0, sd.indexOf("."));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
553 return sd.replace("T", ".");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
554 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
555
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
556 function randkey(words) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
557 if (!words || !(words > 0))
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
558 words = 1;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
559 function rand32() {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
560 rnum = Math.floor(Math.random() * 65536 * 65536);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
561 hexstr = rnum.toString(16);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
562 while (hexstr.length < 8)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
563 hexstr = "0" + hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
564 return hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
565 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
566 var key = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
567 for (var i = 0; i < words; i++)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
568 key += rand32();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
569 return key;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
570 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
571
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
572 /* Compares two buffers, returns true for equality up to index n */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
573 function bufncmp(buf1, buf2, n) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
574 if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2))
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
575 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
576 for (var i = 0; i < n; i++) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
577 if (i == buf1.length && i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
578 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
579 if (i == buf1.length || i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
580 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
581 if (buf1[i] != buf2[i])
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
582 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
583 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
584 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
585 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
586
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
587 function tslog() {
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
588 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
589 console.log.apply(console, arguments);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
590 }
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
591
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
592 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
593 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
594 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
595 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
596 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
597 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
598 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
599 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
600 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
601 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
602 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
603 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
604 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
605 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
606 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
607 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
608 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
609 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
610
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
611 function getMsg(posttext) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
612 var jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
613 if (!posttext)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
614 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
615 try {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
616 jsonobj = JSON.parse(posttext);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
617 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
618 catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
619 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
620 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
621 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
622 if (typeof(jsonobj) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
623 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
624 return jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
625 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
626
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
627 function getMsgWS(msgObj) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
628 if (msgObj.type != "utf8")
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
629 return {};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
630 return getMsg(msgObj.utf8Data);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
631 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
632
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
633 function reaper() {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
634 var now = new Date();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
635 function reapcheck(session) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
636 fs.fstat(session.record.fd, function (err, stats) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
637 if (!err && now - stats.mtime > playtimeout) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
638 tslog("Reaping session %s", session.sessid);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
639 /* Dissociate it with its login name. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
640 var sn = logins[session.key].sessions.indexOf(session.sessid);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
641 if (sn >= 0) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
642 logins[session.key].sessions.splice(sn, 1);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
643 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
644 /* Shut it down. */
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
645 session.close();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
646 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
647 });
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
648 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
649 for (var sessid in sessions) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
650 reapcheck(sessions[sessid]);
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
651 }
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
652 for (var lkey in logins) {
132
823e878e5840 Logins don't time out anymore.
John "Elwin" Edwards <elwin@sdf.org>
parents: 126
diff changeset
653 if (logins[lkey].sessions.length > 0) {
823e878e5840 Logins don't time out anymore.
John "Elwin" Edwards <elwin@sdf.org>
parents: 126
diff changeset
654 /* Check for games that have terminated normally, and remove them. */
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
655 var expired = [];
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
656 var targarray = logins[lkey].sessions;
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
657 /* Let's not find out what happens if you modify an array
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
658 * you're iterating through. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
659 for (var i = 0; i < targarray.length; i++) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
660 if (!(targarray[i] in sessions))
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
661 expired.push(targarray[i]);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
662 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
663 if (expired.length > 0) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
664 for (var j = 0; j < expired.length; j++) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
665 targarray.splice(targarray.indexOf(expired[j]), 1);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
666 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
667 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
668 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
669 }
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
670 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
671
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
672 function login(req, res, formdata) {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
673 if (!allowlogin) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
674 sendError(res, 6, null, false);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
675 return;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
676 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
677 if (!("name" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
678 sendError(res, 2, "Username not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
679 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
680 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
681 else if (!("pw" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
682 sendError(res, 2, "Password not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
683 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
684 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
685 var username = String(formdata["name"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
686 var password = String(formdata["pw"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
687 function checkit(code, signal) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
688 /* Checks the exit status, see sqlickrypt.c for details. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
689 if (code != 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
690 sendError(res, 3);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
691 if (code == 1)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
692 tslog("Password check failed for user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
693 else if (code == 2)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
694 tslog("Attempted login by nonexistent user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
695 else
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
696 tslog("Login failed: sqlickrypt error %d", code);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
697 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
698 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
699 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
700 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
701 lkey = randkey(2);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
702 logins[lkey] = {"name": username, "ts": new Date(), "sessions": []};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
703 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
704 var reply = {"t": "l", "k": lkey, "u": username};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
705 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
706 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
707 tslog("%s has logged in (key %s)", username, lkey);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
708 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
709 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
710 /* Launch the sqlickrypt utility to check the password. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
711 var pwchecker = child_process.spawn("/bin/sqlickrypt", ["check"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
712 pwchecker.on("exit", checkit);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
713 pwchecker.stdin.end(username + '\n' + password + '\n', "utf8");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
714 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
715 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
716
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
717 function startgame(req, res, formdata) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
718 if (!allowlogin) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
719 sendError(res, 6, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
720 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
721 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
722 if (!("key" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
723 sendError(res, 2, "No key given.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
724 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
725 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
726 else if (!("game" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
727 sendError(res, 2, "No game specified.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
728 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
729 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
730 var lkey = String(formdata["key"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
731 if (!(lkey in logins)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
732 sendError(res, 1, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
733 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
734 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
735 var username = logins[lkey].name;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
736 var gname = formdata["game"];
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
737 // If dims are not given or invalid, the constructor will handle it.
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
738 var dims = [formdata["h"], formdata["w"]];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
739 if (!(gname in games)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
740 sendError(res, 2, "No such game: " + gname);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
741 tslog("Request for nonexistant game \"%s\"", gname);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
742 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
743 }
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
744 // A callback to pass to the game-in-progress checker.
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
745 var launch = function(err, fname) {
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
746 var nodematch = new RegExp("^" + username + ":node:");
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
747 if (fname && (fname.match(nodematch) === null)) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
748 /* It's being played in dgamelaunch. */
109
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 107
diff changeset
749 sendError(res, 4, "dgamelaunch");
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
750 tslog("%s is already playing %s", username, gname);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
751 return;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
752 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
753 // Game starting has been approved.
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
754 var respondlaunch = function(nclient, success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
755 if (success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
756 res.writeHead(200, {'Content-Type': 'application/json'});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
757 var reply = {"t": "s", "id": nclient.id, "w": nclient.w, "h":
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
758 nclient.h, "p": username, "g": gname};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
759 res.write(JSON.stringify(reply));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
760 res.end();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
761 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
762 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
763 sendError(res, 5, "Failed to open TTY");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
764 tslog("Unable to allocate TTY for %s", gname);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
765 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
766 };
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
767 if (fname) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
768 for (var cid in clients) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
769 cli = clients[cid];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
770 if ((cli instanceof Player) &&
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
771 cli.session.pname == username &&
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
772 cli.session.game.uname == gname) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
773 cli.reset();
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
774 respondlaunch(cli, true);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
775 tslog("Game %d has been taken over.", cli.session.sessid);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
776 return;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
777 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
778 }
109
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 107
diff changeset
779 /* If there's no player, it's a WebSocket game, and shouldn't be
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 107
diff changeset
780 * seized. */
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 107
diff changeset
781 sendError(res, 4, "WebSocket");
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
782 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
783 else {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
784 new Player(gname, lkey, dims, respondlaunch);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
785 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
786 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
787 checkprogress(username, games[gname], launch, []);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
788 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
789
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
790 function watch(req, res, formdata) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
791 if (!("n" in formdata)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
792 sendError(res, 2, "Game number not given");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
793 return;
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
794 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
795 var gamenumber = Number(formdata["n"]);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
796 if (!(gamenumber in sessions)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
797 sendError(res, 7);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
798 return;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
799 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
800 var session = sessions[gamenumber];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
801 var watch = new Watcher(session);
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
802 var reply = {"t": "w", "id": watch.id, "w": session.w, "h": session.h,
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
803 "p": session.pname, "g": session.game.uname};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
804 res.writeHead(200, {'Content-Type': 'application/json'});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
805 res.write(JSON.stringify(reply));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
806 res.end();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
807 tslog("Game %d is being watched (key %s)", gamenumber, watch.id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
808 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
809
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
810 /* Sets things up for a new user, like dgamelaunch's commands[register] */
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
811 function regsetup(username) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
812 function regsetup_l2(err) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
813 for (var g in games) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
814 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
815 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
816 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
817 fs.mkdir(path.join("/dgldir/userdata", username), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
818 fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
819 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
820
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
821 function register(req, res, formdata) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
822 var uname, passwd, email;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
823 if (typeof (formdata.name) != "string" || formdata.name === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
824 sendError(res, 2, "No name given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
825 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
826 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
827 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
828 uname = formdata["name"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
829 if (typeof (formdata.pw) != "string" || formdata.pw === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
830 sendError(res, 2, "No password given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
831 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
832 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
833 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
834 passwd = formdata["pw"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
835 if (typeof (formdata.email) != "string" || formdata.email === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
836 /* E-mail is optional */
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
837 email = "nobody@nowhere.not";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
838 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
839 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
840 email = formdata["email"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
841 function checkreg(code, signal) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
842 if (code === 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
843 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
844 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
845 lkey = randkey(2);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
846 logins[lkey] = {"name": uname, "ts": new Date(), "sessions": []};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
847 var reply = {"t": "r", "k": lkey, "u": uname};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
848 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
849 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
850 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
851 tslog("Added new user: %s", uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
852 regsetup(uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
853 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
854 else if (code == 4) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
855 sendError(res, 2, "Invalid characters in name or email.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
856 tslog("Attempted registration: %s %s", uname, email);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
857 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
858 else if (code == 1) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
859 sendError(res, 2, "Username " + uname + " is already being used.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
860 tslog("Attempted duplicate registration: %s", uname);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
861 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
862 else {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
863 sendError(res, 0, null);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
864 tslog("sqlickrypt register failed with code %d", code);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
865 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
866 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
867 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
868 child_adder.on("exit", checkreg);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
869 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
870 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
871 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
872
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
873 /* Ends the game, obviously. Less obviously, stops watching the game if
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
874 * the client is a Watcher instead of a Player. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
875 function endgame(client, res) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
876 if (!client.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
877 sendError(res, 7, null, true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
878 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
879 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
880 client.quit();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
881 // Give things some time to happen.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
882 if (client instanceof Player)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
883 setTimeout(readFeed, 200, client, res);
62
7793ad53b90f rlgwebd.js: fix failure to respond.
John "Elwin" Edwards <elwin@sdf.org>
parents: 60
diff changeset
884 else
7793ad53b90f rlgwebd.js: fix failure to respond.
John "Elwin" Edwards <elwin@sdf.org>
parents: 60
diff changeset
885 readFeed(client, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
886 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
887 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
888
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
889 /* Stops a running game if the request has the proper key. */
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
890 function stopgame(res, formdata) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
891 if (!("key" in formdata) || !(formdata["key"] in logins)) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
892 sendError(res, 1);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
893 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
894 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
895 var pname = logins[formdata["key"]].name;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
896 if (!("g" in formdata) || !(formdata["g"] in games)) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
897 sendError(res, 2, "No such game.");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
898 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
899 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
900 var gname = formdata["g"];
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
901 function checkback(err, fname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
902 if (!fname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
903 sendError(res, 7);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
904 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
905 }
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
906 var fullfile = path.join("/dgldir/inprogress", gname, fname);
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
907 fs.readFile(fullfile, "utf8", function(err, fdata) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
908 if (err) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
909 sendError(res, 7);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
910 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
911 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
912 var pid = parseInt(fdata.split('\n')[0], 10);
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
913 try {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
914 process.kill(pid, 'SIGHUP');
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
915 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
916 catch (err) {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
917 /* If the PID is invalid, the lockfile is stale. */
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
918 if (err.code == "ESRCH") {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
919 var nodere = RegExp("^" + pname + ":node:");
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
920 if (fname.match(nodere)) {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
921 fs.unlink(fullfile);
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
922 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
923 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
924 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
925 /* The response doesn't mean that the game is gone. The only way
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
926 * to make sure a dgamelaunch-supervised game is over would be to
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
927 * poll fname until it disappears. */
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
928 res.writeHead(200, {'Content-Type': 'application/json'});
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
929 res.write(JSON.stringify({"t": "q"}));
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
930 res.end();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
931 });
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
932 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
933 checkprogress(pname, games[gname], checkback, []);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
934 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
935
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
936 function findClient(formdata, playersOnly) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
937 if (typeof(formdata) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
938 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
939 if ("id" in formdata) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
940 var id = formdata["id"];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
941 if (id in clients && (!playersOnly || clients[id] instanceof Player)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
942 return clients[id];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
943 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
944 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
945 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
946 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
947
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
948 function startProgressWatcher() {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
949 var watchdirs = [];
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
950 for (var gname in games) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
951 watchdirs.push(path.join("/dgldir/inprogress", gname));
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
952 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
953 var subproc = child_process.spawn("/bin/watcher", watchdirs);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
954 subproc.stdout.setEncoding('utf8');
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
955 subproc.stdout.on('data', function (chunk) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
956 var fname = chunk.slice(2, -1);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
957 var filere = /.*\/([^\/]*)\/([^\/:]*):(node:)?(.*)/;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
958 var matchresult = fname.match(filere);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
959 if (!matchresult || matchresult[3])
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
960 return;
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
961 var gname = matchresult[1];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
962 var pname = matchresult[2];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
963 var tag = gname + "/" + pname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
964 if (chunk[0] == "E") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
965 tslog("DGL: %s is playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
966 dglgames[tag] = fname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
967 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
968 else if (chunk[0] == "C") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
969 tslog("DGL: %s started playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
970 dglgames[tag] = fname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
971 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
972 else if (chunk[0] == "D") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
973 tslog("DGL: %s finished playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
974 delete dglgames[tag];
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
975 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
976 else {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
977 tslog("Watcher says: %s", chunk)
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
978 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
979 });
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
980 subproc.stdout.resume();
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
981 return subproc;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
982 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
983
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
984 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
985 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
986 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
987 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
988 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
989 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
990 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
991 var extension = path.extname(realname);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
992 fs.exists(realname, function (exists) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
993 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
994 if (!exists || !extension || extension == ".html")
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
995 resheaders["Content-Type"] = "text/html; charset=utf-8";
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
996 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
997 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
998 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
999 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1000 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1001 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1002 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1003 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1004 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1005 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1006 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1007 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1008 if (error) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1009 res.writeHead(500, {});
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1010 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1011 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1012 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1013 res.writeHead(200, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1014 if (req.method != 'HEAD')
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1015 res.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1016 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1017 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1018 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1019 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1020 else {
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1021 send404(res, nname, req.method == 'HEAD');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1022 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1023 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1024 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1025 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1026
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1027 function readFeed(client, res) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1028 if (!client) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1029 sendError(res, 7, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1030 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1031 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1032 var msgs = client.read();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1033 if (!allowlogin && !msgs.length) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1034 sendError(res, 6, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1035 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1036 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1037 res.writeHead(200, { "Content-Type": "application/json" });
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1038 res.write(JSON.stringify(msgs));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1039 res.end();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1040 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1041
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1042 function getStatus(callback) {
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1043 var now = new Date();
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1044 var statusinfo = {"s": allowlogin, "g": []};
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1045 function idleset(i, idletime) {
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1046 if (i >= 0 && i < statusinfo.g.length) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1047 statusinfo.g[i].i = idletime;
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1048 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1049 for (var j = 0; j < statusinfo.g.length; j++) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1050 if (!("i" in statusinfo.g[j]))
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1051 return;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1052 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1053 callback(statusinfo);
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1054 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1055 for (var sessid in sessions) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1056 var gamedesc = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1057 gamedesc["n"] = sessid;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1058 gamedesc["p"] = sessions[sessid].pname;
64
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 62
diff changeset
1059 gamedesc["g"] = sessions[sessid].game.uname;
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1060 statusinfo["g"].push(gamedesc);
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1061 }
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
1062 statusinfo["dgl"] = [];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
1063 for (var tag in dglgames) {
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
1064 statusinfo["dgl"].push(tag);
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
1065 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1066 if (statusinfo.g.length == 0) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1067 callback(statusinfo);
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1068 return;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1069 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1070 function makecallback(i) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1071 return function (err, stats) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1072 if (err)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1073 idleset(i, null);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1074 else
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1075 idleset(i, now - stats.mtime);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1076 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1077 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1078 for (var i = 0; i < statusinfo.g.length; i++) {
148
bac9c3b01692 Check that the ttyrec stream has an fd before passing it to fstat.
John "Elwin" Edwards
parents: 144
diff changeset
1079 /* fd sometimes isn't a number, presumably when the file isn't open yet. */
bac9c3b01692 Check that the ttyrec stream has an fd before passing it to fstat.
John "Elwin" Edwards
parents: 144
diff changeset
1080 var ssid = statusinfo.g[i].n;
bac9c3b01692 Check that the ttyrec stream has an fd before passing it to fstat.
John "Elwin" Edwards
parents: 144
diff changeset
1081 if (ssid in sessions && typeof(sessions[ssid].record.fd) == 'number') {
bac9c3b01692 Check that the ttyrec stream has an fd before passing it to fstat.
John "Elwin" Edwards
parents: 144
diff changeset
1082 fs.fstat(sessions[ssid].record.fd, makecallback(i));
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1083 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1084 else {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1085 idleset(i, null);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1086 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
1087 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1088 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1089
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1090 function statusmsg(req, res) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1091 function respond(info) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1092 res.writeHead(200, { "Content-Type": "application/json" });
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1093 if (req.method != 'HEAD')
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1094 res.write(JSON.stringify(info));
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1095 res.end();
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1096 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1097 getStatus(respond);
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1098 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
1099
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1100 function pstatusmsg(req, res) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1101 if (req.method == 'HEAD') {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1102 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1103 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1104 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1105 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1106 var target = url.parse(req.url).pathname;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1107 var pmatch = target.match(/^\/pstatus\/(.*)/);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1108 if (pmatch && pmatch[1])
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1109 var pname = pmatch[1];
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1110 else {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1111 sendError(res, 2, "No name given.");
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1112 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1113 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1114 var reply = {"name": pname};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1115 playerstatus(pname, function (pdata) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1116 reply["stat"] = pdata;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1117 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1118 res.write(JSON.stringify(reply));
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1119 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1120 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1121 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1122
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1123 function getuinfo(req, res) {
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1124 var urlobj = url.parse(req.url, true);
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1125 var query = urlobj.query;
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1126 if (!("key" in query) || !(query["key"] in logins)) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1127 sendError(res, 1);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1128 return;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1129 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1130 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1131 if (!match || !match[1]) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1132 send404(res, urlobj.pathname, req.method == 'HEAD');
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1133 return;
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1134 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1135 var which = match[1];
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1136 var name = logins[query["key"]].name;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1137 var reply = { "u": name };
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1138 function send() {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1139 res.writeHead(200, { "Content-Type": "application/json" });
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1140 res.write(JSON.stringify(reply));
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1141 res.end();
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1142 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1143 if (which == "pw") {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1144 /* Don't actually divulge passwords. */
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1145 reply["pw"] = "";
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1146 send();
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1147 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1148 else if (which == "email") {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1149 var address;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1150 function finish(code, signal) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1151 if (code != 0) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1152 tslog("sqlickrypt: %d with name %s", code, name);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1153 sendError(res, 2);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1154 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1155 else {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1156 reply["email"] = address;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1157 send();
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1158 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1159 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1160 var subproc = child_process.spawn("/bin/sqlickrypt", ["getmail"]);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1161 subproc.stdout.on("data", function (data) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1162 address = data.toString().replace(/\n/g, "");
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1163 });
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1164 subproc.on("exit", finish);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1165 subproc.stdin.end(name + '\n', "utf8");
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1166 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1167 else {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1168 send404(res, urlobj.pathname, req.method == 'HEAD');
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1169 return;
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1170 }
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1171 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1172
126
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1173 function setuinfo(req, res, postdata) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1174 var urlobj = url.parse(req.url, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1175 var query = urlobj.query;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1176 if (!("key" in query) || !(query["key"] in logins)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1177 sendError(res, 1);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1178 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1179 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1180 var name = logins[query["key"]].name;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1181 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1182 if (!match || !match[1]) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1183 send404(res, urlobj.pathname, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1184 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1185 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1186 var which = match[1];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1187 if (!("v" in postdata)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1188 sendError(res, 2, "No value provided");
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1189 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1190 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1191 if (which == "email" || which == "pw") {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1192 var args;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1193 if (which == "email")
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1194 args = ["setmail"];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1195 else
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1196 args = ["setpw"];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1197 var child = child_process.execFile("/bin/sqlickrypt", args,
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1198 function (err, stdout, stderr) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1199 if (err) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1200 tslog("Could not set %s: sqlickrypt error %d", which, err.code);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1201 sendError(res, 2);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1202 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1203 else {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1204 tslog("User %s has changed %s", name, which);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1205 res.writeHead(200, { "Content-Type": "application/json" });
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1206 res.end(JSON.stringify({"t": "t"}));
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1207 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1208 });
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1209 child.stdin.end(name + "\n" + postdata.v + "\n", "utf8");
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1210 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1211 else {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1212 send404(res, urlobj.pathname, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1213 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1214 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1215
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1216 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1217 "Login failed", "Already playing", "Game launch failed",
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1218 "Server shutting down", "Game not in progress" ];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1219
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1220 function sendError(res, ecode, msg, box) {
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
1221 res.writeHead(200, { "Content-Type": "application/json" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1222 var edict = {"t": "E"};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1223 if (!(ecode < errorcodes.length && ecode > 0))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1224 ecode = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1225 edict["c"] = ecode;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1226 edict["s"] = errorcodes[ecode];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1227 if (msg)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1228 edict["s"] += ": " + msg;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1229 if (box)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1230 res.write(JSON.stringify([edict]));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1231 else
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1232 res.write(JSON.stringify(edict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1233 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1234 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1235
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1236 function send404(res, path, nopage) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1237 res.writeHead(404, {"Content-Type": "text/html; charset=utf-8"});
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1238 if (!nopage) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1239 res.write("<html><head><title>" + path + "</title></head>\n<body><h1>"
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1240 + path + " Not Found</h1></body></html>\n");
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1241 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1242 res.end();
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1243 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1244
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1245 function webHandler(req, res) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1246 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1247 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1248 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1249 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1250 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1251
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1252 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1253 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1254 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1255 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1256 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1257
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1258 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1259 function respond() {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1260 formdata = getMsg(reqbody);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1261 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1262 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1263 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1264 if (target == '/feed') {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1265 var client = findClient(formdata, false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1266 if (!client) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1267 sendError(res, 7, null, true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1268 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1269 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1270 if (formdata.t == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1271 /* The client wants to terminate the process. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1272 endgame(client, res);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
1273 return; // endgame() calls readFeed() itself.
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1274 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1275 else if (formdata.t == "d" && typeof(formdata.d) == "string") {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1276 if (!(client instanceof Player)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1277 sendError(res, 7, "Watching", true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1278 return;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1279 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1280 /* process the keys */
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1281 var hexstr = formdata.d.replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1282 if (hexstr.length % 2 != 0) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1283 sendError(res, 2, "incomplete byte", true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1284 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1285 }
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1286 var keybuf = new Buffer(hexstr, "hex");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1287 client.write(keybuf, formdata.n);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1288 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1289 readFeed(client, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1290 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1291 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1292 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1293 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1294 else if (target == "/addacct") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1295 register(req, res, formdata);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1296 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1297 else if (target == "/play") {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1298 startgame(req, res, formdata);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1299 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1300 else if (target == "/watch") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1301 watch(req, res, formdata);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1302 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
1303 else if (target == "/quit") {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
1304 stopgame(res, formdata);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
1305 }
126
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1306 else if (target.match(/^\/uinfo\//)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1307 setuinfo(req, res, formdata);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
1308 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1309 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1310 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1311 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1312 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1313 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1314 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1315 if (target == '/feed') {
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1316 if (req.method == 'HEAD') {
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1317 res.writeHead(200, {"Content-Type": "application/json"});
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1318 res.end();
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1319 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1320 else
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1321 sendError(res, 7, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1322 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1323 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1324 else if (target == '/status') {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1325 statusmsg(req, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1326 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
1327 else if (target.match(/^\/uinfo\//)) {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1328 getuinfo(req, res);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
1329 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1330 else if (target.match(/^\/pstatus\//)) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1331 pstatusmsg(req, res);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1332 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1333 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1334 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1335 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1336 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1337 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1338 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1339 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1340 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1341 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1342 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1343 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1344
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1345 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1346
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1347 function wsHandler(wsRequest) {
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1348 var watchmatch = wsRequest.resource.match(/^\/watch\/([0-9]*)$/);
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1349 var playmatch = wsRequest.resource.match(/^\/play\//);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1350 if (watchmatch !== null) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1351 if (watchmatch[1] && Number(watchmatch[1]) in sessions) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1352 var tsession = sessions[Number(watchmatch[1])];
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1353 var conn = wsRequest.accept(null, wsRequest.origin);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1354 new wsWatcher(conn, tsession);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1355 tslog("Game %d is being watched via WebSockets", tsession.sessid);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1356 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1357 else
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1358 wsRequest.reject(404, errorcodes[7]);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1359 }
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1360 else if (playmatch !== null) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1361 wsStart(wsRequest);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1362 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
1363 else if (wsRequest.resourceURL.pathname == "/status") {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1364 var conn = wsRequest.accept(null, wsRequest.origin);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1365 var tell = function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1366 getStatus(function (info) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1367 info["t"] = "t";
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1368 conn.sendUTF(JSON.stringify(info));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1369 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1370 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1371 var beginH = function (n, name, game) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1372 conn.sendUTF(JSON.stringify({"t": "b", "n": n, "p": name, "g": game}));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1373 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1374 var listH = function (list) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1375 conn.sendUTF(JSON.stringify(list));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1376 };
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
1377 var endH = function (n, pname, gname) {
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
1378 conn.sendUTF(JSON.stringify({"t": "e", "n": n, "p": pname, "g": gname}));
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1379 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1380 gamemux.on('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1381 gamemux.on('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1382 gamemux.on('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1383 conn.on('message', tell);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1384 conn.on('close', function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1385 gamemux.removeListener('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1386 gamemux.removeListener('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1387 gamemux.removeListener('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1388 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1389 tell();
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1390 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1391 else
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1392 wsRequest.reject(404, "No such resource.");
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1393 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1394
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1395 function pushStatus() {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1396 getStatus(function(info) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1397 info["t"] = "t";
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1398 gamemux.emit('list', info);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1399 });
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1400 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1401
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1402 function shutdown () {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1403 httpServer.close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1404 httpServer.removeAllListeners('request');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1405 ctlServer.close();
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1406 tslog("Shutting down...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1407 process.exit();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1408 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1409
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1410 function conHandler(chunk) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1411 var msg = chunk.toString().split('\n')[0];
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1412 if (msg == "quit") {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1413 allowlogin = false;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1414 tslog("Disconnecting...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1415 for (var sessid in sessions) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1416 sessions[sessid].close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1417 }
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1418 progressWatcher.stdin.end("\n");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1419 setTimeout(shutdown, 2000);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1420 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1421 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1422
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1423 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1424 for (var sessid in sessions) {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
1425 sessions[sessid].term.kill('SIGHUP');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1426 }
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1427 tslog("Quitting...");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1428 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1429 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1430
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1431 /* Initialization STARTS HERE */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1432 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1433
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1434 if (process.getuid() != 0) {
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1435 tslog("Not running as root, cannot chroot.");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1436 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1437 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1438
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1439 var httpServer; // declare here so shutdown() can find it
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1440 var wsServer;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1441 var progressWatcher;
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1442
85
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1443 /* This could be nonblocking, but nothing else can start yet anyway. */
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1444 if (fs.existsSync(ctlsocket)) {
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1445 fs.unlinkSync(ctlsocket);
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1446 }
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1447
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1448 /* Open the control socket before chrooting where it can't be found */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1449 var ctlServer = net.createServer(function (sock) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1450 sock.on('data', conHandler);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1451 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1452 ctlServer.listen(ctlsocket, function () {
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1453 /* rlgwebd.js now assumes that it has been started by the rlgwebd shell
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1454 * script, or some other method that detaches it and sets up stdio. */
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1455 /* chroot and drop permissions. posix.chroot() does chdir() itself. */
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1456 try {
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1457 posix.chroot(chrootDir);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1458 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1459 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1460 tslog("chroot to %s failed: %s", chrootDir, err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1461 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1462 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1463 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1464 // drop gid first, that requires UID=0
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1465 process.setgid(dropToGID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1466 process.setuid(dropToUID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1467 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1468 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1469 tslog("Could not drop permissions: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1470 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1471 }
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1472 httpServer = http.createServer(webHandler);
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1473 httpServer.listen(httpPort);
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1474 tslog('rlgwebd running on port %d', httpPort);
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
1475 setInterval(reaper, playtimeout / 4);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1476 wsServer = new WebSocketServer({"httpServer": httpServer});
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1477 wsServer.on("request", wsHandler);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1478 tslog('WebSockets are online');
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1479 progressWatcher = startProgressWatcher();
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1480 setInterval(pushStatus, 4000);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1481 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1482