1 function undisplay(element) {
2 element.style.display="none";
5 function add_message(severity, message) {
6 message = `<div class="message ${severity}" onclick="undisplay(this)">
7 <span class="hide-button" onclick="undisplay(this.parentElement)">×</span>
10 const message_area = document.getElementById('message-area');
11 message_area.insertAdjacentHTML('beforeend', message);
14 function lmno_login_loadend(request, username) {
15 if (request.status === 404) {
16 add_message("danger", "User authentication failed. Please try again.");
20 /* Now that user is logged in, advance to the desired page (if any). */
21 const url = new URL(window.location);
22 const next_param = url.searchParams.get('next');
24 window.location.href = next_param;
28 /* Otherwise, just report the successful login. */
29 add_message("success", `User ${username} logged in. Have fun.`);
32 function lmno_login(form) {
33 const username = form.username.value;
34 const password = form.password.value;
36 console.log("In lmno_login with username: " + username);
37 var request = new XMLHttpRequest();
38 request.addEventListener("loadend", () => lmno_login_loadend(request, username));
40 request.open("POST", "/login");
41 request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
42 request.send(`{"username": "${username}", "password": "${password}"}`);
45 function lmno_join_loadend(request, game_id) {
46 if (request.status === 404) {
47 add_message("danger", game_id + " is not a valid game ID. Try again.");
51 /* Now that its validated, send the browser to the URL for the game_id. */
52 window.location.href = "/" + game_id;
55 function lmno_join(form) {
56 const game_id = form.id.value;
58 var request = new XMLHttpRequest();
59 request.addEventListener("loadend", () => lmno_join_loadend(request, game_id));
61 request.open("GET", "/" + game_id);
62 request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
68 function lmno_new_loadend() {
69 if (this.status == 200) {
70 /* Response contains the game ID which we simply point the browser to. */
71 const game_id = JSON.parse(this.response);
72 window.location.href = ('/' + game_id);
76 add_message("danger", `An error occured creating a new game (${this.status}).`);
79 function lmno_new(engine) {
80 const request = new XMLHttpRequest();
81 request.addEventListener("loadend", lmno_new_loadend);
83 request.open("POST", "/new/" + engine);
84 request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");