]> git.cworth.org Git - lmno.games/blob - lmno.js
empires: Add a top-level static page for the Empires game
[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 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
21 function lmno_join(form) {
22   const game_id = form.id.value;
23
24   var request = new XMLHttpRequest();
25   request.addEventListener("loadend", () => join_loadend(request, game_id));
26
27   request.open("GET", "/" + game_id);
28   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
29   request.send();
30
31   form.reset();
32 }
33
34 function new_loadend() {
35   if (this.status == 200) {
36     /* Response contains the game ID which we simply point the browser to. */
37     const game_id = JSON.parse(this.response);
38     window.location.href = ('/' + game_id);
39     return;
40   }
41
42   add_message("danger", `An error occured creating a new game (${this.status}).`);
43 }
44
45 function lmno_new(engine) {
46   console.log("In lmno_new");
47   const request = new XMLHttpRequest();
48   request.addEventListener("loadend", new_loadend);
49
50   request.open("POST", "/new/" + engine);
51   request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
52   request.send();
53
54   return false;
55 }