comparison rlgterm.js @ 58:7a50b4412fea

Move credentials into the drivers. The session id, or whether the session is currently active on the server, are better handled in the drivers than in termemu.js.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 19 Jun 2012 13:40:30 -0700
parents e7c864e80a79
children 00b985b8ba6a
comparison
equal deleted inserted replaced
57:e7c864e80a79 58:7a50b4412fea
69 }; 69 };
70 70
71 /* Login name and key */ 71 /* Login name and key */
72 var lname = null; 72 var lname = null;
73 var lcred = null; 73 var lcred = null;
74 /* The session id assigned by the server. */
75 var sessid = null;
74 76
75 function writeData(hexstr) { 77 function writeData(hexstr) {
76 var codenum; 78 var codenum;
77 var codes = []; 79 var codes = [];
78 var nc; 80 var nc;
209 } 211 }
210 return havedata; 212 return havedata;
211 } 213 }
212 214
213 function getData() { 215 function getData() {
214 if (termemu.sessid == null) 216 if (sessid == null)
215 return; 217 return;
216 var datareq = new XMLHttpRequest(); 218 var datareq = new XMLHttpRequest();
217 var msg = JSON.stringify({"id": termemu.sessid, "t": "n"}); 219 var msg = JSON.stringify({"id": sessid, "t": "n"});
218 datareq.onreadystatechange = function () { 220 datareq.onreadystatechange = function () {
219 if (datareq.readyState == 4 && datareq.status == 200) { 221 if (datareq.readyState == 4 && datareq.status == 200) {
220 var wasdata = processMsg(datareq.responseText); 222 var wasdata = processMsg(datareq.responseText);
221 if (wasdata != null) { 223 if (wasdata != null) {
222 if (wasdata) 224 if (wasdata)
241 } 243 }
242 } 244 }
243 245
244 function sendback(str) { 246 function sendback(str) {
245 /* For responding to terminal queries. */ 247 /* For responding to terminal queries. */
246 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": str}; 248 var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": str};
247 var datareq = new XMLHttpRequest(); 249 var datareq = new XMLHttpRequest();
248 datareq.onreadystatechange = postResponseHandler; 250 datareq.onreadystatechange = postResponseHandler;
249 datareq.open('POST', '/feed', true); 251 datareq.open('POST', '/feed', true);
250 datareq.send(JSON.stringify(msgDict)); 252 datareq.send(JSON.stringify(msgDict));
251 return; 253 return;
252 } 254 }
253 255
254 function sendkey(ev) { 256 function sendkey(ev) {
255 if (termemu.sessid == null) 257 if (sessid == null)
256 return; 258 return;
257 var keynum = ev.keyCode; 259 var keynum = ev.keyCode;
258 var code; 260 var code;
259 if (keynum >= ev.DOM_VK_A && keynum <= ev.DOM_VK_Z) { 261 if (keynum >= ev.DOM_VK_A && keynum <= ev.DOM_VK_Z) {
260 /* Letters. This assumes the codes are 65-90. */ 262 /* Letters. This assumes the codes are 65-90. */
288 else { 290 else {
289 debug(1, "Ignoring keycode " + keynum); 291 debug(1, "Ignoring keycode " + keynum);
290 return; 292 return;
291 } 293 }
292 // Isn't this check redundant? 294 // Isn't this check redundant?
293 if (termemu.sessid != null) 295 if (sessid != null)
294 ev.preventDefault(); 296 ev.preventDefault();
295 var datareq = new XMLHttpRequest(); 297 var datareq = new XMLHttpRequest();
296 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": code}; 298 var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": code};
297 datareq.onreadystatechange = postResponseHandler; 299 datareq.onreadystatechange = postResponseHandler;
298 datareq.open('POST', '/feed', true); 300 datareq.open('POST', '/feed', true);
299 datareq.send(JSON.stringify(msgDict)); 301 datareq.send(JSON.stringify(msgDict));
300 return; 302 return;
301 } 303 }
303 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c", 305 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
304 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e" 306 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
305 } 307 }
306 308
307 function vkey(c) { 309 function vkey(c) {
308 if (termemu.sessid == null) 310 if (sessid == null)
309 return; 311 return;
310 var keystr; 312 var keystr;
311 if (c.match(/^[a-z]$/)) { 313 if (c.match(/^[a-z]$/)) {
312 if (termemu.ctrlp()) { 314 if (termemu.ctrlp()) {
313 var n = c.charCodeAt(0) - 96; 315 var n = c.charCodeAt(0) - 96;
341 keystr = c.charCodeAt(0).toString(16); 343 keystr = c.charCodeAt(0).toString(16);
342 } 344 }
343 else 345 else
344 return; 346 return;
345 var datareq = new XMLHttpRequest(); 347 var datareq = new XMLHttpRequest();
346 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": keystr}; 348 var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": keystr};
347 datareq.onreadystatechange = postResponseHandler; 349 datareq.onreadystatechange = postResponseHandler;
348 datareq.open('POST', '/feed', true); 350 datareq.open('POST', '/feed', true);
349 datareq.send(JSON.stringify(msgDict)); 351 datareq.send(JSON.stringify(msgDict));
350 return; 352 return;
351 } 353 }
378 return; 380 return;
379 } 381 }
380 382
381 function formlogin(ev) { 383 function formlogin(ev) {
382 ev.preventDefault(); 384 ev.preventDefault();
383 if (termemu.sessid != null) 385 if (sessid != null)
384 return; 386 return;
385 var loginmsg = {}; 387 var loginmsg = {};
386 loginmsg["name"] = document.getElementById("input_name").value; 388 loginmsg["name"] = document.getElementById("input_name").value;
387 loginmsg["pw"] = document.getElementById("input_pw").value; 389 loginmsg["pw"] = document.getElementById("input_pw").value;
388 var req = new XMLHttpRequest(); 390 var req = new XMLHttpRequest();
408 req.send(JSON.stringify(loginmsg)); 410 req.send(JSON.stringify(loginmsg));
409 return; 411 return;
410 } 412 }
411 413
412 function getcurrent() { 414 function getcurrent() {
413 if (termemu.sessid) 415 if (sessid)
414 return; 416 return;
415 var req = new XMLHttpRequest(); 417 var req = new XMLHttpRequest();
416 req.onreadystatechange = function () { 418 req.onreadystatechange = function () {
417 if (req.readyState != 4 || req.status != 200) 419 if (req.readyState != 4 || req.status != 200)
418 return; 420 return;
452 req.send(); 454 req.send();
453 return; 455 return;
454 } 456 }
455 457
456 function getchoices() { 458 function getchoices() {
457 if (termemu.sessid != null || !lcred) 459 if (sessid != null || !lcred)
458 return; 460 return;
459 var req = new XMLHttpRequest(); 461 var req = new XMLHttpRequest();
460 req.onreadystatechange = function () { 462 req.onreadystatechange = function () {
461 if (req.readyState != 4 || req.status != 200) 463 if (req.readyState != 4 || req.status != 200)
462 return; 464 return;
519 } 521 }
520 return starter; 522 return starter;
521 } 523 }
522 524
523 function startgame(game) { 525 function startgame(game) {
524 if (termemu.sessid != null || !lcred) 526 if (sessid != null || !lcred)
525 return; 527 return;
526 var smsg = {}; 528 var smsg = {};
527 smsg["key"] = lcred; 529 smsg["key"] = lcred;
528 smsg["game"] = game.uname; 530 smsg["game"] = game.uname;
529 smsg["h"] = 24; 531 smsg["h"] = 24;
533 if (req.readyState != 4 || req.status != 200) 535 if (req.readyState != 4 || req.status != 200)
534 return; 536 return;
535 var reply = JSON.parse(req.responseText); 537 var reply = JSON.parse(req.responseText);
536 if (reply.t == 's') { 538 if (reply.t == 's') {
537 /* Success */ 539 /* Success */
538 termemu.sessid = reply.id; 540 sessid = reply.id;
539 termemu.resize(reply.h, reply.w); 541 termemu.resize(reply.h, reply.w);
540 setTitle("Playing as " + lname); 542 setTitle("Playing as " + lname);
541 debug(1, "Playing with id " + termemu.sessid); 543 debug(1, "Playing with id " + sessid);
542 setmode("play"); 544 setmode("play");
543 getData(); 545 getData();
544 } 546 }
545 else if (reply.t == 'E') { 547 else if (reply.t == 'E') {
546 debug(1, "Could not start game: " + reply.s); 548 debug(1, "Could not start game: " + reply.s);
554 return; 556 return;
555 } 557 }
556 558
557 function formreg(ev) { 559 function formreg(ev) {
558 ev.preventDefault(); 560 ev.preventDefault();
559 if (termemu.sessid != null) 561 if (sessid != null)
560 return; 562 return;
561 var regmsg = {}; 563 var regmsg = {};
562 regmsg["name"] = document.getElementById("regin_name").value; 564 regmsg["name"] = document.getElementById("regin_name").value;
563 regmsg["pw"] = document.getElementById("regin_pw").value; 565 regmsg["pw"] = document.getElementById("regin_pw").value;
564 regmsg["email"] = document.getElementById("regin_email").value; 566 regmsg["email"] = document.getElementById("regin_email").value;
587 req.send(JSON.stringify(regmsg)); 589 req.send(JSON.stringify(regmsg));
588 return; 590 return;
589 } 591 }
590 592
591 function gameover() { 593 function gameover() {
592 if (termemu.sessid == null) 594 if (sessid == null)
593 return; 595 return;
594 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */ 596 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */
595 termemu.sessid = null; 597 sessid = null;
596 ajaxstate.clear(); 598 ajaxstate.clear();
597 setTitle("Game over."); 599 setTitle("Game over.");
598 nsend = 0; 600 nsend = 0;
599 nrecv = 0; 601 nrecv = 0;
600 msgQ = []; 602 msgQ = [];
607 lname = null; 609 lname = null;
608 setmode("login"); 610 setmode("login");
609 } 611 }
610 612
611 function stop() { 613 function stop() {
612 if (!termemu.sessid) 614 if (!sessid)
613 return; 615 return;
614 var req = new XMLHttpRequest(); 616 var req = new XMLHttpRequest();
615 req.onreadystatechange = function () { 617 req.onreadystatechange = function () {
616 if (req.readyState == 4 && req.status == 200) { 618 if (req.readyState == 4 && req.status == 200) {
617 processMsg(req.responseText); 619 processMsg(req.responseText);
618 return; 620 return;
619 } 621 }
620 }; 622 };
621 req.open('POST', '/feed', true); 623 req.open('POST', '/feed', true);
622 req.send(JSON.stringify({"id": termemu.sessid, "t": "q"})); 624 req.send(JSON.stringify({"id": sessid, "t": "q"}));
623 return; 625 return;
624 } 626 }
625 627
626 function setmode(mode, ev) { 628 function setmode(mode, ev) {
627 if (ev) 629 if (ev)