Mercurial > hg > rlgwebd
diff 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 |
line wrap: on
line diff
--- a/rlgterm.js Wed Jun 06 10:53:29 2012 -0700 +++ b/rlgterm.js Thu Jun 07 15:43:06 2012 -0700 @@ -48,6 +48,10 @@ } }; +/* Login name and key */ +var lname = null; +var lcred = null; + function writeData(hexstr) { var codenum; var codes = []; @@ -138,8 +142,8 @@ if (!msgDict.t) return null; else if (msgDict.t == "E") { - if (msgDict.c == 1 || msgDict.c == 6) { - logout(); + if (msgDict.c == 1 || msgDict.c == 6 || msgDict == 7) { + gameover(); } debug(1, "Server error: " + msgDict.s); } @@ -174,7 +178,7 @@ setTitle(msgDict.d); } else if (msgDict.t == "q") { - logout(); + gameover(); } else { debug(1, "Unrecognized server message " + msg); @@ -357,9 +361,39 @@ var loginmsg = {}; loginmsg["name"] = document.getElementById("input_name").value; loginmsg["pw"] = document.getElementById("input_pw").value; - loginmsg["game"] = document.getElementById("input_game").value; - loginmsg["h"] = 24; - loginmsg["w"] = 80; + var req = new XMLHttpRequest(); + req.onreadystatechange = function () { + if (req.readyState != 4 || req.status != 200) + return; + var reply = JSON.parse(req.responseText); + if (reply.t == 'l') { + /* Success */ + lcred = reply.k; + lname = reply.u; + setTitle("Logged in as " + reply.u); + debug(1, "Logged in as " + reply.u + " with id " + reply.k); + setmode("choose"); + } + else if (reply.t == 'E') { + debug(1, "Could not log in: " + reply.s); + document.getElementById("input_name").value = ""; + document.getElementById("input_pw").value = ""; + } + }; + req.open('POST', '/login', true); + req.send(JSON.stringify(loginmsg)); + return; +} + +function startgame(ev) { + ev.preventDefault(); + if (termemu.sessid != null || !lcred) + return; + var smsg = {}; + smsg["key"] = lcred; + smsg["game"] = document.getElementById("input_game").value; + smsg["h"] = 24; + smsg["w"] = 80; var req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req.readyState != 4 || req.status != 200) @@ -369,20 +403,17 @@ /* Success */ termemu.sessid = reply.id; termemu.resize(reply.h, reply.w); - setTitle("Playing as " + loginmsg["name"]); - debug(1, "Logged in with id " + termemu.sessid); - //document.getElementById("loginform").style.display = "none"; + setTitle("Playing as " + lname); + debug(1, "Playing with id " + termemu.sessid); setmode("play"); getData(); } else if (reply.t == 'E') { debug(1, "Could not start game: " + reply.s); - document.getElementById("input_name").value = ""; - document.getElementById("input_pw").value = ""; } }; - req.open('POST', '/login', true); - req.send(JSON.stringify(loginmsg)); + req.open('POST', '/play', true); + req.send(JSON.stringify(smsg)); return; } @@ -402,7 +433,11 @@ if (reply.t == 'r') { /* Success */ debug(1, "Registered account: " + reply.d); - setmode("login"); + lcred = reply.k; + lname = reply.u; + setTitle("Logged in as " + lname); + debug(1, "Logged in as " + lname + "with id " + lcred); + setmode("choose"); } else if (reply.t == 'E') { debug(1, "Could not register: " + reply.s); @@ -416,7 +451,7 @@ return; } -function logout() { +function gameover() { if (termemu.sessid == null) return; /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */ @@ -426,11 +461,13 @@ nsend = 0; nrecv = 0; msgQ = []; - setmode("login"); + setmode("choose"); return; } function stop() { + if (!termemu.sessid) + return; var req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { @@ -448,16 +485,25 @@ ev.preventDefault(); if (mode == "play") { document.getElementById("keyboard").style.display = "block"; + document.getElementById("startgame").style.display = "none"; + document.getElementById("login").style.display = "none"; + document.getElementById("register").style.display = "none"; + } + if (mode == "choose") { + document.getElementById("keyboard").style.display = "none"; + document.getElementById("startgame").style.display = "block"; document.getElementById("login").style.display = "none"; document.getElementById("register").style.display = "none"; } else if (mode == "login") { document.getElementById("keyboard").style.display = "none"; + document.getElementById("startgame").style.display = "none"; document.getElementById("login").style.display = "block"; document.getElementById("register").style.display = "none"; } else if (mode == "register") { document.getElementById("keyboard").style.display = "none"; + document.getElementById("startgame").style.display = "none"; document.getElementById("login").style.display = "none"; document.getElementById("register").style.display = "block"; }