]> git.cworth.org Git - lmno.games/blob - lmno.js
Add JavaScript handling for the recently-added login form
[lmno.games] / lmno.js
1 function undisplay(element) {
2   element.style.display="none";
3 }
4
5 function add_message(severity, message) {
6   message = `<div class="message ${severity}" onclick="undisplay(this)">
7 <span class="hide-button" onclick="undisplay(this.parentElement)">&times;</span>
8 ${message}
9 </div>`;
10   const message_area = document.getElementById('message-area');
11   message_area.insertAdjacentHTML('beforeend', message);
12 }
13
14 function lmno_login_loadend(request, username) {
15   if (request.status === 404) {
16     add_message("danger", "User authentication failed. Please try again.");
17     return;
18   }
19
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');
23   if (next_param) {
24     window.location.href = next_param;
25     return;
26   }
27
28   /* Otherwise, just report the successful login. */
29   add_message("success", `User ${username} logged in. Have fun.`);
30 }
31
32 function lmno_login(form) {
33   const username = form.username.value;
34   const password = form.password.value;
35
36   console.log("In lmno_login with username: " + username);
37   var request = new XMLHttpRequest();
38   request.addEventListener("loadend", () => lmno_login_loadend(request, username));
39
40   request.open("POST", "/login");
41   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
42   request.send(`{"username": "${username}", "password": "${password}"}`);
43 }
44
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.");
48     return;
49   }
50
51   /* Now that its validated, send the browser to the URL for the game_id. */
52   window.location.href = "/" + game_id;
53 }
54
55 function lmno_join(form) {
56   const game_id = form.id.value;
57
58   var request = new XMLHttpRequest();
59   request.addEventListener("loadend", () => lmno_join_loadend(request, game_id));
60
61   request.open("GET", "/" + game_id);
62   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
63   request.send();
64
65   form.reset();
66 }
67
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);
73     return;
74   }
75
76   add_message("danger", `An error occured creating a new game (${this.status}).`);
77 }
78
79 function lmno_new(engine) {
80   const request = new XMLHttpRequest();
81   request.addEventListener("loadend", lmno_new_loadend);
82
83   request.open("POST", "/new/" + engine);
84   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
85   request.send();
86
87   return false;
88 }