Mercurial > hg > rlgwebd
comparison rlgterm.js @ 39:e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
The user now logs in with a username and password, receiving a token
which is then used for any actions requiring authentication. Starting
a game is one such action. Games use a different set of id keys.
This allows users to supply their passwords once and then play any
number of successive games. Also, newly registered users do not need
to supply their passwords again.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Thu, 07 Jun 2012 15:43:06 -0700 |
parents | b06a14876645 |
children | ea3b7775009d |
comparison
equal
deleted
inserted
replaced
38:b06a14876645 | 39:e8ac0e3d2614 |
---|---|
45 this.set(200); | 45 this.set(200); |
46 this.state = 0; | 46 this.state = 0; |
47 } | 47 } |
48 } | 48 } |
49 }; | 49 }; |
50 | |
51 /* Login name and key */ | |
52 var lname = null; | |
53 var lcred = null; | |
50 | 54 |
51 function writeData(hexstr) { | 55 function writeData(hexstr) { |
52 var codenum; | 56 var codenum; |
53 var codes = []; | 57 var codes = []; |
54 var nc; | 58 var nc; |
136 return null; | 140 return null; |
137 } | 141 } |
138 if (!msgDict.t) | 142 if (!msgDict.t) |
139 return null; | 143 return null; |
140 else if (msgDict.t == "E") { | 144 else if (msgDict.t == "E") { |
141 if (msgDict.c == 1 || msgDict.c == 6) { | 145 if (msgDict.c == 1 || msgDict.c == 6 || msgDict == 7) { |
142 logout(); | 146 gameover(); |
143 } | 147 } |
144 debug(1, "Server error: " + msgDict.s); | 148 debug(1, "Server error: " + msgDict.s); |
145 } | 149 } |
146 else if (msgDict.t == "n") { | 150 else if (msgDict.t == "n") { |
147 havedata = false; | 151 havedata = false; |
172 } | 176 } |
173 else if (msgDict.t == "T") { | 177 else if (msgDict.t == "T") { |
174 setTitle(msgDict.d); | 178 setTitle(msgDict.d); |
175 } | 179 } |
176 else if (msgDict.t == "q") { | 180 else if (msgDict.t == "q") { |
177 logout(); | 181 gameover(); |
178 } | 182 } |
179 else { | 183 else { |
180 debug(1, "Unrecognized server message " + msg); | 184 debug(1, "Unrecognized server message " + msg); |
181 } | 185 } |
182 return havedata; | 186 return havedata; |
355 if (termemu.sessid != null) | 359 if (termemu.sessid != null) |
356 return; | 360 return; |
357 var loginmsg = {}; | 361 var loginmsg = {}; |
358 loginmsg["name"] = document.getElementById("input_name").value; | 362 loginmsg["name"] = document.getElementById("input_name").value; |
359 loginmsg["pw"] = document.getElementById("input_pw").value; | 363 loginmsg["pw"] = document.getElementById("input_pw").value; |
360 loginmsg["game"] = document.getElementById("input_game").value; | 364 var req = new XMLHttpRequest(); |
361 loginmsg["h"] = 24; | 365 req.onreadystatechange = function () { |
362 loginmsg["w"] = 80; | 366 if (req.readyState != 4 || req.status != 200) |
367 return; | |
368 var reply = JSON.parse(req.responseText); | |
369 if (reply.t == 'l') { | |
370 /* Success */ | |
371 lcred = reply.k; | |
372 lname = reply.u; | |
373 setTitle("Logged in as " + reply.u); | |
374 debug(1, "Logged in as " + reply.u + " with id " + reply.k); | |
375 setmode("choose"); | |
376 } | |
377 else if (reply.t == 'E') { | |
378 debug(1, "Could not log in: " + reply.s); | |
379 document.getElementById("input_name").value = ""; | |
380 document.getElementById("input_pw").value = ""; | |
381 } | |
382 }; | |
383 req.open('POST', '/login', true); | |
384 req.send(JSON.stringify(loginmsg)); | |
385 return; | |
386 } | |
387 | |
388 function startgame(ev) { | |
389 ev.preventDefault(); | |
390 if (termemu.sessid != null || !lcred) | |
391 return; | |
392 var smsg = {}; | |
393 smsg["key"] = lcred; | |
394 smsg["game"] = document.getElementById("input_game").value; | |
395 smsg["h"] = 24; | |
396 smsg["w"] = 80; | |
363 var req = new XMLHttpRequest(); | 397 var req = new XMLHttpRequest(); |
364 req.onreadystatechange = function () { | 398 req.onreadystatechange = function () { |
365 if (req.readyState != 4 || req.status != 200) | 399 if (req.readyState != 4 || req.status != 200) |
366 return; | 400 return; |
367 var reply = JSON.parse(req.responseText); | 401 var reply = JSON.parse(req.responseText); |
368 if (reply.t == 'l') { | 402 if (reply.t == 'l') { |
369 /* Success */ | 403 /* Success */ |
370 termemu.sessid = reply.id; | 404 termemu.sessid = reply.id; |
371 termemu.resize(reply.h, reply.w); | 405 termemu.resize(reply.h, reply.w); |
372 setTitle("Playing as " + loginmsg["name"]); | 406 setTitle("Playing as " + lname); |
373 debug(1, "Logged in with id " + termemu.sessid); | 407 debug(1, "Playing with id " + termemu.sessid); |
374 //document.getElementById("loginform").style.display = "none"; | |
375 setmode("play"); | 408 setmode("play"); |
376 getData(); | 409 getData(); |
377 } | 410 } |
378 else if (reply.t == 'E') { | 411 else if (reply.t == 'E') { |
379 debug(1, "Could not start game: " + reply.s); | 412 debug(1, "Could not start game: " + reply.s); |
380 document.getElementById("input_name").value = ""; | |
381 document.getElementById("input_pw").value = ""; | |
382 } | 413 } |
383 }; | 414 }; |
384 req.open('POST', '/login', true); | 415 req.open('POST', '/play', true); |
385 req.send(JSON.stringify(loginmsg)); | 416 req.send(JSON.stringify(smsg)); |
386 return; | 417 return; |
387 } | 418 } |
388 | 419 |
389 function formreg(ev) { | 420 function formreg(ev) { |
390 ev.preventDefault(); | 421 ev.preventDefault(); |
400 return; | 431 return; |
401 var reply = JSON.parse(req.responseText); | 432 var reply = JSON.parse(req.responseText); |
402 if (reply.t == 'r') { | 433 if (reply.t == 'r') { |
403 /* Success */ | 434 /* Success */ |
404 debug(1, "Registered account: " + reply.d); | 435 debug(1, "Registered account: " + reply.d); |
405 setmode("login"); | 436 lcred = reply.k; |
437 lname = reply.u; | |
438 setTitle("Logged in as " + lname); | |
439 debug(1, "Logged in as " + lname + "with id " + lcred); | |
440 setmode("choose"); | |
406 } | 441 } |
407 else if (reply.t == 'E') { | 442 else if (reply.t == 'E') { |
408 debug(1, "Could not register: " + reply.s); | 443 debug(1, "Could not register: " + reply.s); |
409 document.getElementById("regin_name").value = ""; | 444 document.getElementById("regin_name").value = ""; |
410 document.getElementById("regin_pw").value = ""; | 445 document.getElementById("regin_pw").value = ""; |
414 req.open('POST', '/addacct', true); | 449 req.open('POST', '/addacct', true); |
415 req.send(JSON.stringify(regmsg)); | 450 req.send(JSON.stringify(regmsg)); |
416 return; | 451 return; |
417 } | 452 } |
418 | 453 |
419 function logout() { | 454 function gameover() { |
420 if (termemu.sessid == null) | 455 if (termemu.sessid == null) |
421 return; | 456 return; |
422 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */ | 457 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */ |
423 termemu.sessid = null; | 458 termemu.sessid = null; |
424 ajaxstate.clear(); | 459 ajaxstate.clear(); |
425 setTitle("Game over."); | 460 setTitle("Game over."); |
426 nsend = 0; | 461 nsend = 0; |
427 nrecv = 0; | 462 nrecv = 0; |
428 msgQ = []; | 463 msgQ = []; |
429 setmode("login"); | 464 setmode("choose"); |
430 return; | 465 return; |
431 } | 466 } |
432 | 467 |
433 function stop() { | 468 function stop() { |
469 if (!termemu.sessid) | |
470 return; | |
434 var req = new XMLHttpRequest(); | 471 var req = new XMLHttpRequest(); |
435 req.onreadystatechange = function () { | 472 req.onreadystatechange = function () { |
436 if (req.readyState == 4 && req.status == 200) { | 473 if (req.readyState == 4 && req.status == 200) { |
437 processMsg(req.responseText); | 474 processMsg(req.responseText); |
438 return; | 475 return; |
446 function setmode(mode, ev) { | 483 function setmode(mode, ev) { |
447 if (ev) | 484 if (ev) |
448 ev.preventDefault(); | 485 ev.preventDefault(); |
449 if (mode == "play") { | 486 if (mode == "play") { |
450 document.getElementById("keyboard").style.display = "block"; | 487 document.getElementById("keyboard").style.display = "block"; |
488 document.getElementById("startgame").style.display = "none"; | |
451 document.getElementById("login").style.display = "none"; | 489 document.getElementById("login").style.display = "none"; |
452 document.getElementById("register").style.display = "none"; | 490 document.getElementById("register").style.display = "none"; |
453 } | 491 } |
492 if (mode == "choose") { | |
493 document.getElementById("keyboard").style.display = "none"; | |
494 document.getElementById("startgame").style.display = "block"; | |
495 document.getElementById("login").style.display = "none"; | |
496 document.getElementById("register").style.display = "none"; | |
497 } | |
454 else if (mode == "login") { | 498 else if (mode == "login") { |
455 document.getElementById("keyboard").style.display = "none"; | 499 document.getElementById("keyboard").style.display = "none"; |
500 document.getElementById("startgame").style.display = "none"; | |
456 document.getElementById("login").style.display = "block"; | 501 document.getElementById("login").style.display = "block"; |
457 document.getElementById("register").style.display = "none"; | 502 document.getElementById("register").style.display = "none"; |
458 } | 503 } |
459 else if (mode == "register") { | 504 else if (mode == "register") { |
460 document.getElementById("keyboard").style.display = "none"; | 505 document.getElementById("keyboard").style.display = "none"; |
506 document.getElementById("startgame").style.display = "none"; | |
461 document.getElementById("login").style.display = "none"; | 507 document.getElementById("login").style.display = "none"; |
462 document.getElementById("register").style.display = "block"; | 508 document.getElementById("register").style.display = "block"; |
463 } | 509 } |
464 } | 510 } |
465 | 511 |