Experimental: improve animation, reduce lags in stack moves sending. Add Allmate
[xogo.git] / app.js
diff --git a/app.js b/app.js
index bb16537..6f17c64 100644 (file)
--- a/app.js
+++ b/app.js
@@ -191,7 +191,9 @@ function getGameLink() {
   const vname = $.getElementById("selectVariant").value;
   const color = $.getElementById("selectColor").value;
   for (const select of $.querySelectorAll("#gameOptions select")) {
-    const value = parseInt(select.value, 10) || select.value;
+    let value = parseInt(select.value, 10);
+    if (isNaN(value)) //not an integer
+      value = select.value;
     options[ select.id.split("_")[1] ] = value;
   }
   for (const input of $.querySelectorAll("#gameOptions input")) {
@@ -229,7 +231,7 @@ function fillGameInfos(gameInfos, oppIndex) {
           if (j == options.length)
             break;
           const opt = options[j];
-          if (!opt[1])
+          if (!opt[1]) //includes 0 and false (lighter display)
             continue;
           htmlContent +=
             '<span class="option">' +
@@ -454,9 +456,28 @@ function notifyMe(code) {
 
 let curMoves = [],
     lastFen;
-const afterPlay = (move_s) => {
-  const callbackAfterSend = () => {
-    curMoves = [];
+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,
+             success: () => curMoves = [],
+             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(move_s);
     if (result != "*") {
       setTimeout(() => {
@@ -464,23 +485,6 @@ const afterPlay = (move_s) => {
         send("gameover", {gid: gid});
       }, 2000);
     }
-  };
-  // Pack into one moves array, then send
-  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 (vr.turn != playerColor) {
-    toggleTurnIndicator(false);
-    send("newmove",
-         {gid: gid, moves: curMoves, fen: vr.getFen()},
-         {
-           retry: true,
-           success: callbackAfterSend,
-           error: () => alert("Move not sent: reload page")
-         });
   }
 };
 
@@ -530,8 +534,9 @@ function initializeGame(obj) {
       afterPlay: afterPlay,
       options: options
     });
-    if (!obj.fen) {
-      // Game creation: both players set FEN, in case of one is offline
+    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);
     }
@@ -543,8 +548,9 @@ function initializeGame(obj) {
         break;
       }
     }
-    fillGameInfos(obj, playerColor == "w" ? 1 : 0);
-    if (obj.randvar)
+    const playerIndex = (playerColor == "w" ? 0 : 1);
+    fillGameInfos(obj, 1 - playerIndex);
+    if (obj.players[playerIndex].randvar && gameCreation)
       toggleVisible("gameInfos");
     else
       toggleVisible("boardContainer");