comparison rlgwebd.js @ 169:6f4b7e1b32e8

rlgwebd.js: clean up TermSession parameters. Use the player name instead of a login key that has already been verified and resolved to a player name.
author John "Elwin" Edwards
date Fri, 09 Jan 2015 09:43:21 -0500
parents 0ceaca924b4c
children 50e4c9feeac2
comparison
equal deleted inserted replaced
168:0ceaca924b4c 169:6f4b7e1b32e8
65 var dglgames = {}; 65 var dglgames = {};
66 var allowlogin = true; 66 var allowlogin = true;
67 var gamemux = new events.EventEmitter(); 67 var gamemux = new events.EventEmitter();
68 68
69 /* Constructor. A TermSession handles a pty and the game running on it. 69 /* Constructor. A TermSession handles a pty and the game running on it.
70 * game: (String) Name of the game to launch. 70 * gname: (String) Name of the game to launch.
71 * lkey: (String, key) The user's id, a key into logins. 71 * pname: (String) The player's name.
72 * dims: (Array [Number, Number]) Height and width of the pty. 72 * dims: (Array [Number, Number]) Height and width of the pty.
73 * handlers: (Object) Key-value pairs, event names and functions to 73 * handlers: (Object) Key-value pairs, event names and functions to
74 * install to handle them. 74 * install to handle them.
75 * Events: 75 * Events:
76 * "data": Data generated by child. Parameters: buf (Buffer) 76 * "data": Data generated by child. Parameters: buf (Buffer)
77 * "exit": Child terminated. Parameters: none 77 * "exit": Child terminated. Parameters: none
78 */ 78 */
79 function TermSession(game, lkey, dims, handlers) { 79 function TermSession(gname, pname, dims, handlers) {
80 var ss = this; 80 var ss = this;
81 /* Subclass EventEmitter to do the hard work. */ 81 /* Subclass EventEmitter to do the hard work. */
82 events.EventEmitter.call(this); 82 events.EventEmitter.call(this);
83 for (var evname in handlers) 83 for (var evname in handlers)
84 this.on(evname, handlers[evname]); 84 this.on(evname, handlers[evname]);
85 /* Don't launch anything that's not a real game. */ 85 /* Don't launch anything that's not a real game. */
86 if (game in games) { 86 if (gname in games) {
87 this.game = games[game]; 87 this.game = games[gname];
88 } 88 }
89 else { 89 else {
90 this.failed = true; 90 this.failed = true;
91 return; 91 return;
92 } 92 }
93 if (lkey in logins) { 93 this.pname = pname;
94 this.key = lkey;
95 this.pname = logins[lkey].name;
96 }
97 else {
98 this.failed = true;
99 return;
100 }
101 /* Grab a spot in the sessions table. */ 94 /* Grab a spot in the sessions table. */
102 sessions[this.game.uname + "/" + this.pname] = this; 95 sessions[this.game.uname + "/" + this.pname] = this;
103 /* Set up the sizes. */ 96 /* Set up the sizes. */
104 this.w = Math.floor(Number(dims[1])); 97 this.w = Math.floor(Number(dims[1]));
105 if (!(this.w > 0 && this.w < 256)) 98 if (!(this.w > 0 && this.w < 256))
320 })); 313 }));
321 conn.sendUTF(JSON.stringify({"t": "d", 314 conn.sendUTF(JSON.stringify({"t": "d",
322 "d": session.framebuf.toString("hex", 0, session.frameoff)})); 315 "d": session.framebuf.toString("hex", 0, session.frameoff)}));
323 } 316 }
324 317
325 function wsPlay(wsReq, game, lkey, dims) { 318 function wsPlay(wsReq, game, pname, dims) {
326 tslog("wsPlay: running for %s/%s", game, logins[lkey].name); 319 tslog("wsPlay: running for %s/%s", game, pname);
327 tslog("Request is for %s", logins[wsReq.resourceURL.query["key"]].name); 320 tslog("Request is for %s", logins[wsReq.resourceURL.query["key"]].name);
328 var conn; 321 var conn;
329 var session; 322 var session;
330 /* Listeners on the WebSocket */ 323 /* Listeners on the WebSocket */
331 function messageH(message) { 324 function messageH(message) {
358 conn.close(); 351 conn.close();
359 session.removeListener('data', dataH); 352 session.removeListener('data', dataH);
360 session.removeListener('exit', exitH); 353 session.removeListener('exit', exitH);
361 } 354 }
362 var handlers = {'data': dataH, 'exit': exitH}; 355 var handlers = {'data': dataH, 'exit': exitH};
363 session = new TermSession(game, lkey, dims, handlers); 356 session = new TermSession(game, pname, dims, handlers);
364 if (!session.failed) { 357 if (!session.failed) {
365 var tag = session.game.uname + "/" + session.pname; 358 var tag = session.game.uname + "/" + session.pname;
366 var reply = {"t": "s", "tag": tag, "w": session.w, "h": session.h, 359 var reply = {"t": "s", "tag": tag, "w": session.w, "h": session.h,
367 "p": session.pname, "g": session.game.uname}; 360 "p": session.pname, "g": session.game.uname};
368 tslog("Accepting for %s", tag); 361 tslog("Accepting for %s", tag);
405 if (fname) { 398 if (fname) {
406 wsReq.reject(404, errorcodes[4]); 399 wsReq.reject(404, errorcodes[4]);
407 tslog("%s is already playing %s", pname, gname); 400 tslog("%s is already playing %s", pname, gname);
408 } 401 }
409 else 402 else
410 wsPlay(wsReq, gname, lkey, dims); 403 wsPlay(wsReq, gname, pname, dims);
411 }; 404 };
412 checkprogress(pname, games[gname], progcallback, []); 405 checkprogress(pname, games[gname], progcallback, []);
413 } 406 }
414 407
415 /* Some functions which check whether a player is currently playing or 408 /* Some functions which check whether a player is currently playing or