comparison rlgterm.js @ 89:6145e1ea7ace

RLG-Web: add a new message framework. Add a message() function meant for UI messages, not debugging info.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 10 Jul 2012 08:08:54 -0700
parents d644e7d46852
children 6c6a6e4f1e5b
comparison
equal deleted inserted replaced
88:d644e7d46852 89:6145e1ea7ace
259 return; 259 return;
260 } 260 }
261 } 261 }
262 262
263 function errHandler() { 263 function errHandler() {
264 debug(1, "Server unavailable?"); 264 //debug(1, "Server unavailable?");
265 message("Unable to connect to the server.");
265 } 266 }
266 267
267 function sendback(str) { 268 function sendback(str) {
268 /* For responding to terminal queries. */ 269 /* For responding to terminal queries. */
269 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str}; 270 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str};
443 if (reply.t == 'l') { 444 if (reply.t == 'l') {
444 /* Success */ 445 /* Success */
445 session.lcred = reply.k; 446 session.lcred = reply.k;
446 session.lname = reply.u; 447 session.lname = reply.u;
447 setTitle("Logged in as " + reply.u); 448 setTitle("Logged in as " + reply.u);
448 debug(1, "Logged in as " + reply.u + " with id " + reply.k); 449 debug(0, "Logged in as " + reply.u + " with id " + reply.k);
450 message("You are now logged in as " + reply.u + ".");
449 setmode("choose"); 451 setmode("choose");
450 } 452 }
451 else if (reply.t == 'E') { 453 else if (reply.t == 'E') {
452 debug(1, "Could not log in: " + reply.s); 454 debug(0, "Could not log in: " + reply.s);
455 message("Logging in failed: " + reply.s);
453 document.getElementById("input_name").value = ""; 456 document.getElementById("input_name").value = "";
454 document.getElementById("input_pw").value = ""; 457 document.getElementById("input_pw").value = "";
455 } 458 }
456 }; 459 };
457 req.open('POST', '/login', true); 460 req.open('POST', '/login', true);
620 session.id = reply.id; 623 session.id = reply.id;
621 session.playing = true; 624 session.playing = true;
622 termemu.resize(reply.h, reply.w); 625 termemu.resize(reply.h, reply.w);
623 setTitle("Playing as " + session.lname); 626 setTitle("Playing as " + session.lname);
624 debug(1, "Playing with id " + session.id); 627 debug(1, "Playing with id " + session.id);
628 message("Your game has started.");
625 setmode("play"); 629 setmode("play");
626 getData(); 630 getData();
627 } 631 }
628 else if (reply.t == 'E') { 632 else if (reply.t == 'E') {
629 debug(1, "Could not start game: " + reply.s); 633 debug(1, "Could not start game: " + reply.s);
634 message("Your game could not be started: " + reply.s);
630 if (reply.c == 1) { 635 if (reply.c == 1) {
631 logout(); 636 logout();
632 } 637 }
633 } 638 }
634 }; 639 };
654 termemu.resize(reply.h, reply.w); 659 termemu.resize(reply.h, reply.w);
655 termemu.reset(); 660 termemu.reset();
656 termemu.toAltBuf(); 661 termemu.toAltBuf();
657 setTitle("Watching"); 662 setTitle("Watching");
658 debug(1, "Watching with id " + session.id); 663 debug(1, "Watching with id " + session.id);
664 message("You are now watching.");
659 setmode("play"); 665 setmode("play");
660 getData(); 666 getData();
661 } 667 }
662 else if (reply.t == 'E') { 668 else if (reply.t == 'E') {
663 debug(1, "Could not watch game " + gamenumber + ": " + reply.s); 669 debug(1, "Could not watch game " + gamenumber + ": " + reply.s);
670 message("The game could not be watched: " + reply.s);
664 getcurrent(); 671 getcurrent();
665 } 672 }
666 }; 673 };
667 req.open('POST', '/watch', true); 674 req.open('POST', '/watch', true);
668 req.send(JSON.stringify(wmsg)); 675 req.send(JSON.stringify(wmsg));
691 return; 698 return;
692 var reply = JSON.parse(req.responseText); 699 var reply = JSON.parse(req.responseText);
693 if (reply.t == 'r') { 700 if (reply.t == 'r') {
694 /* Success */ 701 /* Success */
695 debug(1, "Registered account: " + reply.u); 702 debug(1, "Registered account: " + reply.u);
703 message("Welcome " + reply.u + ", you are now registered.");
696 session.lcred = reply.k; 704 session.lcred = reply.k;
697 session.lname = reply.u; 705 session.lname = reply.u;
698 setTitle("Logged in as " + session.lname); 706 setTitle("Logged in as " + session.lname);
699 debug(1, "Logged in as " + session.lname + " with id " + session.lcred); 707 debug(1, "Logged in as " + session.lname + " with id " + session.lcred);
708 message("You are now logged in as " + reply.u + ".");
700 setmode("choose"); 709 setmode("choose");
701 } 710 }
702 else if (reply.t == 'E') { 711 else if (reply.t == 'E') {
703 debug(1, "Could not register: " + reply.s); 712 debug(1, "Could not register: " + reply.s);
713 message("Registration failed.");
704 document.getElementById("regin_name").value = ""; 714 document.getElementById("regin_name").value = "";
705 document.getElementById("regin_pw").value = ""; 715 document.getElementById("regin_pw").value = "";
706 document.getElementById("regin_email").value = ""; 716 document.getElementById("regin_email").value = "";
707 } 717 }
708 }; 718 };
793 return; 803 return;
794 if (element.style.display != "block") 804 if (element.style.display != "block")
795 element.style.display = "block"; 805 element.style.display = "block";
796 else 806 else
797 element.style.display = "none"; 807 element.style.display = "none";
808 }
809
810 function message(msg, type) {
811 var msgdiv = document.createElement("div");
812 var msgtext = document.createTextNode(msg);
813 msgdiv.appendChild(msgtext);
814 var msgcontainer = document.getElementById("messages");
815 msgcontainer.insertBefore(msgdiv, msgcontainer.firstChild);
798 } 816 }
799 817
800 function debug(level, msg) { 818 function debug(level, msg) {
801 if (level < debugSuppress) 819 if (level < debugSuppress)
802 return; 820 return;