]> git.cworth.org Git - lmno.games/blob - lmno.js
Allow a user to un-select a star
[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_join_loadend(request, game_id) {
15   if (request.status === 404) {
16     add_message("danger", game_id + " is not a valid game ID. Try again.");
17     return;
18   }
19
20   /* Now that its validated, send the browser to the URL for the game_id. */
21   window.location.href = "/" + game_id;
22 }
23
24 function lmno_join(form) {
25   const game_id = form.id.value;
26
27   var request = new XMLHttpRequest();
28   request.addEventListener("loadend", () => lmno_join_loadend(request, game_id));
29
30   request.open("GET", "/" + game_id);
31   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
32   request.send();
33
34   form.reset();
35 }
36
37 function lmno_new_loadend() {
38   if (this.status == 200) {
39     /* Response contains the game ID which we simply point the browser to. */
40     const game_id = JSON.parse(this.response);
41     window.location.href = ('/' + game_id);
42     return;
43   }
44
45   add_message("danger", `An error occured creating a new game (${this.status}).`);
46 }
47
48 function lmno_new(engine) {
49   const request = new XMLHttpRequest();
50   request.addEventListener("loadend", lmno_new_loadend);
51
52   request.open("POST", "/new/" + engine);
53   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
54   request.send();
55
56   return false;
57 }