New variant idea
[xogo.git] / app.js
diff --git a/app.js b/app.js
index a011513..c5d5c7f 100644 (file)
--- a/app.js
+++ b/app.js
@@ -4,11 +4,11 @@ let $ = document; //shortcut
 // Initialisations
 
 // https://stackoverflow.com/a/27747377/12660887
-function dec2hex (dec) { return dec.toString(16).padStart(2, "0") }
 function generateId (len) {
-  var arr = new Uint8Array((len || 40) / 2)
-  window.crypto.getRandomValues(arr)
-  return Array.from(arr, dec2hex).join('')
+  const dec2hex = (dec) => dec.toString(16).padStart(2, "0");
+  let arr = new Uint8Array(len / 2); //len/2 because 2 chars per hex value
+  window.crypto.getRandomValues(arr); //fill with random integers
+  return Array.from(arr, dec2hex).join('');
 }
 
 // Populate variants dropdown list
@@ -34,32 +34,36 @@ $.getElementById("myName").value = localStorage.getItem("name");
 let inputName = document.getElementById("myName");
 let formField = document.getElementById("ng-name");
 const setActive = (active) => {
-  if (active) formField.classList.add("form-field--is-active");
+  if (active)
+    formField.classList.add("form-field--is-active");
   else {
     formField.classList.remove("form-field--is-active");
-    inputName.value === "" ?
-      formField.classList.remove("form-field--is-filled") :
-       formField.classList.add("form-field--is-filled");
+    inputName.value == ''
+      ? formField.classList.remove("form-field--is-filled")
+      : formField.classList.add("form-field--is-filled");
   }
 };
+setActive(true);
 inputName.onblur = () => setActive(false);
 inputName.onfocus = () => setActive(true);
-inputName.focus();
 
 /////////
 // Utils
 
 function setName() {
+  // 'onChange' event on name input text field [HTML]
   localStorage.setItem("name", $.getElementById("myName").value);
 }
 
 // Turn a "tab" on, and "close" all others
 function toggleVisible(element) {
-  for (elt of document.querySelectorAll('main > div')) {
-    if (elt.id != element) elt.style.display = "none";
-    else elt.style.display = "block";
+  for (elt of document.querySelectorAll("main > div")) {
+    if (elt.id != element)
+      elt.style.display = "none";
+    else
+      elt.style.display = "block";
   }
-  if (element.id == "boardContainer") {
+  if (element == "boardContainer") {
     // Avoid smartphone scrolling effects (TODO?)
     document.querySelector("html").style.overflow = "hidden";
     document.body.style.overflow = "hidden";
@@ -67,63 +71,72 @@ function toggleVisible(element) {
   else {
     document.querySelector("html").style.overflow = "visible";
     document.body.style.overflow = "visible";
-    if (element.id == "newGame") {
-      // Workaround "superposed texts" effect
-      inputName.focus();
-      inputName.blur();
-    }
+    // Workaround "superposed texts" effect:
+    if (element == "newGame")
+      setActive(false);
   }
 }
 
 let seek_vname;
 function seekGame() {
   seek_vname = $.getElementById("selectVariant").value;
-  send("seekgame", {vname: seek_vname, name: localStorage.getItem("name")});
-  toggleVisible("pendingSeek");
+  if (send("seekgame",
+           {vname: seek_vname, name: localStorage.getItem("name")})
+  ) {
+    toggleVisible("pendingSeek");
+  }
 }
 function cancelSeek() {
-  send("cancelseek", {vname: seek_vname});
-  toggleVisible("newGame");
+  if (send("cancelseek", {vname: seek_vname}))
+    toggleVisible("newGame");
 }
 
-function sendRematch() {
-  send("rematch", { gid: gid });
-  toggleVisible("pendingRematch");
+function sendRematch(random) {
+  if (send("rematch", {gid: gid, random: !!random}))
+    toggleVisible("pendingRematch");
 }
 function cancelRematch() {
-  send("norematch", { gid: gid });
-  toggleVisible("newGame");
+  if (send("norematch", {gid: gid}))
+    toggleVisible("newGame");
 }
 
 // Play with a friend (or not ^^)
 function showNewGameForm() {
   const vname = $.getElementById("selectVariant").value;
-  if (vname == "_random") alert("Select a variant first");
+  if (vname == "_random")
+    alert("Select a variant first");
   else {
     $.getElementById("gameLink").innerHTML = "";
     $.getElementById("selectColor").selectedIndex = 0;
     toggleVisible("newGameForm");
     import(`/variants/${vname}/class.js`).then(module => {
       window.V = module.default;
+      for (const [k, v] of Object.entries(V.Aliases))
+        window[k] = v;
       prepareOptions();
     });
   }
 }
-function backToNormalSeek() { toggleVisible("newGame"); }
+function backToNormalSeek() {
+  toggleVisible("newGame");
+}
 
-function toggleStyle(e, word) {
+function toggleStyle(event, obj) {
+  const word = obj.innerHTML;
   options[word] = !options[word];
-  e.target.classList.toggle("highlight-word");
+  event.target.classList.toggle("highlight-word");
 }
 
 let options;
 function prepareOptions() {
   options = {};
-  let optHtml = V.Options.select.map(select => { return `
+  let optHtml = "";
+  if (V.Options.select) {
+    optHtml += V.Options.select.map(select => { return `
       <div class="option-select">
         <label for="var_${select.variable}">${select.label}</label>
         <div class="select">
-          <select id="var_${select.variable}" data-numeric="1">` +
+          <select id="var_${select.variable}">` +
           select.options.map(option => { return `
             <option
               value="${option.value}"
@@ -136,29 +149,35 @@ function prepareOptions() {
           <span class="focus"></span>
         </div>
       </div>`;
-  }).join("");
-  optHtml += V.Options.check.map(check => {
-    return `
-      <div class="option-check">
-        <label class="checkbox">
-          <input id="var_${check.variable}"
-                 type="checkbox"${check.defaut ? " checked" : ""}/>
+    }).join("");
+  }
+  if (V.Options.input) {
+    optHtml += V.Options.input.map(input => { return `
+      <div class="option-input">
+        <label class="input">
+          <input id="var_${input.variable}"
+                 type="${input.type}"
+                 ${input.type == "checkbox" && input.defaut
+                   ? "checked"
+                   : 'value="' + input.defaut + '"'}
+          />
           <span class="spacer"></span>
-          <span>${check.label}</span>
+          <span>${input.label}</span>
         </label>
       </div>`;
-  }).join("");
-  if (V.Options.styles.length >= 1) {
+    }).join("");
+  }
+  if (V.Options.styles) {
     optHtml += '<div class="words">';
     let i = 0;
     const stylesLength = V.Options.styles.length;
     while (i < stylesLength) {
       optHtml += '<div class="row">';
       for (let j=i; j<i+4; j++) {
-        if (j == stylesLength) break;
+        if (j == stylesLength)
+          break;
         const style = V.Options.styles[j];
-        optHtml +=
-          `<span onClick="toggleStyle(event, '${style}')">${style}</span>`;
+        optHtml += `<span onClick="toggleStyle(event, this)">${style}</span>`;
       }
       optHtml += "</div>";
       i += 4;
@@ -172,21 +191,26 @@ function getGameLink() {
   const vname = $.getElementById("selectVariant").value;
   const color = $.getElementById("selectColor").value;
   for (const select of $.querySelectorAll("#gameOptions select")) {
-    let value = select.value;
-    if (select.attributes["data-numeric"]) value = parseInt(value, 10);
-    if (value) options[ select.id.split("_")[1] ] = value;
+    let value = parseInt(select.value, 10);
+    if (isNaN(value)) //not an integer
+      value = select.value;
+    options[ select.id.split("_")[1] ] = value;
   }
-  for (const check of $.querySelectorAll("#gameOptions input")) {
-    if (check.checked) options[ check.id.split("_")[1] ] = check.checked;
+  for (const input of $.querySelectorAll("#gameOptions input")) {
+    const variable = input.id.split("_")[1];
+    if (input.type == "number")
+      options[variable] = parseInt(input.value, 10); //TODO: real numbers?
+    else if (input.type == "checkbox")
+      options[variable] = input.checked;
   }
   send("creategame", {
     vname: vname,
-    player: { sid: sid, name: localStorage.getItem("name"), color: color },
+    player: {sid: sid, name: localStorage.getItem("name"), color: color},
     options: options
   });
 }
 
-const fillGameInfos = (gameInfos, oppIndex) => {
+function fillGameInfos(gameInfos, oppIndex) {
   fetch(`/variants/${gameInfos.vname}/rules.html`)
   .then(res => res.text())
   .then(txt => {
@@ -204,12 +228,15 @@ const fillGameInfos = (gameInfos, oppIndex) => {
       while (i < options.length) {
         htmlContent += '<div class="row">';
         for (let j=i; j<i+4; j++) {
-          if (j == options.length) break;
+          if (j == options.length)
+            break;
           const opt = options[j];
+          if (!opt[1]) //includes 0 and false (lighter display)
+            continue;
           htmlContent +=
             '<span class="option">' +
             (opt[1] === true ? opt[0] : `${opt[0]}:${opt[1]}`) + " " +
-            '</span>';
+            "</span>";
         }
         htmlContent += "</div>";
         i += 4;
@@ -223,27 +250,66 @@ const fillGameInfos = (gameInfos, oppIndex) => {
       </div>`;
     $.getElementById("gameInfos").innerHTML = htmlContent;
   });
-};
+}
 
 ////////////////
 // Communication
 
-let socket, gid, attempt = 0;
+let socket, gid, recoAttempt = 0;
 const autoReconnectDelay = () => {
-  return [100, 200, 500, 1000, 3000, 10000, 30000][Math.min(attempt, 6)];
+  return [100, 200, 500, 1000, 3000, 10000, 30000][Math.min(recoAttempt, 6)];
 };
 
-function copyClipboard(msg) { navigator.clipboard.writeText(msg); }
+function send(code, data, opts) {
+  opts = opts || {};
+  const trySend = () => {
+    if (socket.readyState == 1) {
+      socket.send(JSON.stringify(Object.assign({code: code}, data)));
+      if (opts.success)
+        opts.success();
+      return true;
+    }
+    return false;
+  };
+  const firstTry = trySend();
+  if (!firstTry) {
+    if (opts.retry) {
+      // Retry for a few seconds (sending move)
+      let sendAttempt = 1;
+      const retryLoop = setInterval(
+        () => {
+          if (trySend() || ++sendAttempt >= 3)
+            clearInterval(retryLoop);
+          if (sendAttempt >= 3 && opts.error)
+            opts.error();
+        },
+        1000
+      );
+    }
+    else if (opts.error)
+      opts.error();
+  }
+  return firstTry;
+}
+
+function copyClipboard(msg) {
+  navigator.clipboard.writeText(msg);
+}
 function getWhatsApp(msg) {
   return `https://api.whatsapp.com/send?text=${encodeURIComponent(msg)}`;
 }
 
 const tryResumeGame = () => {
-  attempt = 0;
+  recoAttempt = 0;
   // If a game is found, resume it:
   if (localStorage.getItem("gid")) {
     gid = localStorage.getItem("gid");
-    send("getgame", { gid: gid });
+    send("getgame",
+         {gid: gid},
+         {
+           retry: true,
+           error: () => alert("Cannot load game: no connection")
+         });
   }
   else {
     // If URL indicates "play with a friend", start game:
@@ -251,9 +317,14 @@ const tryResumeGame = () => {
     if (hashIdx >= 0) {
       const urlParts = $.URL.split('#');
       gid = urlParts[1];
-      send("joingame", { gid: gid, name: localStorage.getItem("name") });
       localStorage.setItem("gid", gid);
-      history.replaceState(null, '', urlParts[0]);
+      history.replaceState(null, '', urlParts[0]); //hide game ID
+      send("joingame",
+           {gid: gid, name: localStorage.getItem("name")},
+           {
+             retry: true,
+             error: () => alert("Cannot load game: no connection")
+           });
     }
   }
 };
@@ -263,7 +334,8 @@ const messageCenter = (msg) => {
   switch (obj.code) {
     // Start new game:
     case "gamestart": {
-      if (document.hidden) notifyMe("game");
+      if (document.hidden)
+        notifyMe("game");
       gid = obj.gid;
       initializeGame(obj);
       break;
@@ -275,7 +347,7 @@ const messageCenter = (msg) => {
         <p>
           <a href="${getWhatsApp(link)}">WhatsApp</a>
           /
-          <span onClick='copyClipboard("${link}")'>ToClipboard</span>
+          <span onClick="copyClipboard('${link}')">ToClipboard</span>
         </p>
         <p>${link}</p>
       `;
@@ -295,13 +367,18 @@ const messageCenter = (msg) => {
       break;
     // Receive opponent's move:
     case "newmove":
-      if (document.hidden) notifyMe("move");
+      // Basic check: was it really opponent's turn?
+      if (vr.turn == playerColor)
+        break;
+      if (document.hidden)
+        notifyMe("move");
       vr.playReceivedMove(obj.moves, () => {
-        if (vr.getCurrentScore(obj.moves[obj.moves.length-1]) != "*") {
+        if (vr.getCurrentScore(obj.moves) != "*") {
           localStorage.removeItem("gid");
           setTimeout( () => toggleVisible("gameStopped"), 2000 );
         }
-        else toggleTurnIndicator(true);
+        else
+          toggleTurnIndicator(true);
       });
       break;
     // Opponent stopped game (draw, abort, resign...)
@@ -313,11 +390,15 @@ const messageCenter = (msg) => {
     case "closerematch":
       toggleVisible("newGame");
       break;
+    case "filechange":
+      // TODO?: could be more subtle
+      setTimeout(() => location.reload(), 100);
+      break;
   }
 };
 
 const handleError = (err) => {
-  if (err.code === 'ECONNREFUSED') {
+  if (err.code === "ECONNREFUSED") {
     removeAllListeners();
     alert("Server refused connection. Please reload page later");
   }
@@ -331,35 +412,34 @@ const handleClose = () => {
   }, autoReconnectDelay());
 };
 
-const removeAllListeners = () => {
+function removeAllListeners() {
   socket.removeEventListener("open", tryResumeGame);
   socket.removeEventListener("message", messageCenter);
   socket.removeEventListener("error", handleError);
   socket.removeEventListener("close", handleClose);
-};
+}
 
-const connectToWSS = () => {
+function connectToWSS() {
   socket =
     new WebSocket(`${Params.socket_server}${Params.socket_path}?sid=${sid}`);
   socket.addEventListener("open", tryResumeGame);
   socket.addEventListener("message", messageCenter);
   socket.addEventListener("error", handleError);
   socket.addEventListener("close", handleClose);
-  attempt++;
-};
+  recoAttempt++;
+}
 connectToWSS();
 
-const send = (code, data) => {
-  socket.send(JSON.stringify(Object.assign({code: code}, data)));
-};
-
 ///////////
 // Playing
 
 function toggleTurnIndicator(myTurn) {
-  let indicator = $.getElementById("chessboard");
-  if (myTurn) indicator.style.outline = "thick solid green";
-  else indicator.style.outline = "thick solid lightgrey";
+  let indicator =
+    $.getElementById("boardContainer").querySelector(".chessboard");
+  if (myTurn)
+    indicator.style.outline = "thick solid green";
+  else
+    indicator.style.outline = "thick solid lightgrey";
 }
 
 function notifyMe(code) {
@@ -368,54 +448,67 @@ function notifyMe(code) {
     new Notification("New " + code, { vibrate: [200, 100, 200] });
     new Audio("/assets/new_" + code + ".mp3").play();
   }
-  if (Notification.permission === 'granted') doNotify();
-  else if (Notification.permission !== 'denied') {
+  if (Notification.permission === "granted")
+    doNotify();
+  else if (Notification.permission !== "denied") {
     Notification.requestPermission().then(permission => {
-      if (permission === 'granted') doNotify();
+      if (permission === "granted")
+        doNotify();
     });
   }
 }
 
-let curMoves = [];
-const afterPlay = (move) => { //pack into one moves array, then send
-  curMoves.push({
-    appear: move.appear,
-    vanish: move.vanish,
-    start: move.start,
-    end: move.end
-  });
-  if (vr.turn != playerColor) {
-    toggleTurnIndicator(false);
-    send("newmove", { gid: gid, moves: curMoves, fen: vr.getFen() });
+let curMoves = [],
+    lastFen;
+const afterPlay = (move_s, newTurn, ops) => {
+  if (ops.send) {
+    // Pack into one moves array, then send (if turn changed)
+    if (Array.isArray(move_s))
+      // Array of simple moves (e.g. Chakart)
+      Array.prototype.push.apply(curMoves, move_s);
+    else
+      // Usual case
+      curMoves.push(move_s);
+    if (newTurn != playerColor) {
+      send("newmove",
+           {gid: gid, moves: curMoves, fen: vr.getFen()},
+           {
+             retry: true,
+             error: () => alert("Move not sent: reload page")
+           }
+      );
+    }
+  }
+  if (ops.res && newTurn != playerColor) {
+    toggleTurnIndicator(false); //now all moves are sent and animated
+    const result = vr.getCurrentScore(curMoves);
     curMoves = [];
-    const result = vr.getCurrentScore(move);
     if (result != "*") {
-      setTimeout( () => {
+      setTimeout(() => {
         toggleVisible("gameStopped");
-        send("gameover", { gid: gid });
+        send("gameover", {gid: gid});
       }, 2000);
     }
   }
 };
 
-// Avoid loading twice the same stylesheet:
-const conditionalLoadCSS = (vname) => {
-  const allIds = [].slice.call($.styleSheets).map(s => s.id);
-  const newId = vname + "_css";
-  if (!allIds.includes(newId)) {
-    $.getElementsByTagName("head")[0].insertAdjacentHTML(
-      "beforeend",
-      `<link id="${newId}" rel="stylesheet"
-             href="/variants/${vname}/style.css"/>`);
-  }
-};
-
-let vr, playerColor;
+let vr = null, playerColor, lastVname = undefined;
 function initializeGame(obj) {
   const options = obj.options || {};
   import(`/variants/${obj.vname}/class.js`).then(module => {
     window.V = module.default;
-    conditionalLoadCSS(obj.vname);
+    for (const [k, v] of Object.entries(V.Aliases))
+      window[k] = v;
+    if (lastVname != obj.vname) {
+      // Load CSS + unload potential previous one.
+      if (lastVname)
+        document.getElementById(lastVname + "_css").remove();
+      $.getElementsByTagName("head")[0].insertAdjacentHTML(
+        "beforeend",
+        `<link id="${obj.vname + '_css'}" rel="stylesheet"
+               href="/variants/${obj.vname}/style.css"/>`);
+      lastVname = obj.vname;
+    }
     playerColor = (sid == obj.players[0].sid ? "w" : "b");
     // Init + remove potential extra DOM elements from a previous game:
     document.getElementById("boardContainer").innerHTML = `
@@ -437,18 +530,22 @@ function initializeGame(obj) {
           </g>
         </svg>
       </div>
-      <div class="resizeable" id="chessboard"></div>`;
+      <div class="chessboard"></div>`;
+    if (vr)
+      // Avoid interferences:
+      vr.removeListeners();
     vr = new V({
       seed: obj.seed, //may be null if FEN already exists (running game)
       fen: obj.fen,
-      element: "chessboard",
+      element: "boardContainer",
       color: playerColor,
       afterPlay: afterPlay,
       options: options
     });
-    if (!obj.fen) {
-      // Game creation
-      if (playerColor == "w") send("setfen", {gid: obj.gid, fen: vr.getFen()});
+    const gameCreation = !obj.fen;
+    if (gameCreation) {
+      // Both players set FEN, in case of one is offline
+      send("setfen", {gid: obj.gid, fen: vr.getFen()});
       localStorage.setItem("gid", obj.gid);
     }
     const select = $.getElementById("selectVariant");
@@ -459,16 +556,18 @@ function initializeGame(obj) {
         break;
       }
     }
-    fillGameInfos(obj, playerColor == "w" ? 1 : 0);
-    if (obj.randvar) toggleVisible("gameInfos");
-    else toggleVisible("boardContainer");
+    const playerIndex = (playerColor == "w" ? 0 : 1);
+    fillGameInfos(obj, 1 - playerIndex);
+    if (obj.players[playerIndex].randvar && gameCreation)
+      toggleVisible("gameInfos");
+    else
+      toggleVisible("boardContainer");
     toggleTurnIndicator(vr.turn == playerColor);
   });
 }
 
 function confirmStopGame() {
-  if (confirm("Stop game?")) {
-    send("gameover", { gid: gid, relay: true });
+  if (confirm("Stop game?") && send("gameover", {gid: gid, relay: true})) {
     localStorage.removeItem("gid");
     toggleVisible("gameStopped");
   }
@@ -477,16 +576,15 @@ function confirmStopGame() {
 function toggleGameInfos() {
   if ($.getElementById("gameInfos").style.display == "none")
     toggleVisible("gameInfos");
-  else {
+  else
     toggleVisible("boardContainer");
-    // Quickfix for the "vanished piece" bug (move played while on game infos)
-    vr.setupPieces(); //TODO: understand better
-  }
 }
 
 $.body.addEventListener("keydown", (e) => {
-  if (!localStorage.getItem("gid")) return;
-  if (e.keyCode == 27) confirmStopGame();
+  if (!localStorage.getItem("gid"))
+    return;
+  if (e.keyCode == 27)
+    confirmStopGame();
   else if (e.keyCode == 32) {
     e.preventDefault();
     toggleGameInfos();