Fix memory leak of moves hash on server side. Draft Align4
[xogo.git] / app.js
diff --git a/app.js b/app.js
index 17fe770..4c0b5e4 100644 (file)
--- a/app.js
+++ b/app.js
@@ -136,7 +136,7 @@ function prepareOptions() {
       <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}"
@@ -151,25 +151,16 @@ function prepareOptions() {
       </div>`;
     }).join("");
   }
-  if (V.Options.check) {
-    optHtml += V.Options.check.map(check => { return `
-      <div class="option-check">
-        <label class="checkbox">
-          <input id="var_${check.variable}"
-                 type="checkbox"${check.defaut ? " checked" : ""}/>
-          <span class="spacer"></span>
-          <span>${check.label}</span>
-        </label>
-      </div>`;
-    }).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}"
-                 content="${input.defaut}"/>
+                 ${input.type == "checkbox" && input.defaut
+                   ? "checked"
+                   : 'value="' + input.defaut + '"'}
+          />
           <span class="spacer"></span>
           <span>${input.label}</span>
         </label>
@@ -200,15 +191,17 @@ 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,
@@ -238,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">' +
@@ -463,10 +456,10 @@ function notifyMe(code) {
 
 let curMoves = [],
     lastFen;
-const afterPlay = (move) => {
+const afterPlay = (move_s) => {
   const callbackAfterSend = () => {
     curMoves = [];
-    const result = vr.getCurrentScore(move);
+    const result = vr.getCurrentScore(move_s);
     if (result != "*") {
       setTimeout(() => {
         toggleVisible("gameStopped");
@@ -475,7 +468,12 @@ const afterPlay = (move) => {
     }
   };
   // Pack into one moves array, then send
-  curMoves.push(move);
+  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",
@@ -525,7 +523,7 @@ function initializeGame(obj) {
           </g>
         </svg>
       </div>
-      <div class="resizeable chessboard"></div>`;
+      <div class="chessboard"></div>`;
     vr = new V({
       seed: obj.seed, //may be null if FEN already exists (running game)
       fen: obj.fen,
@@ -547,8 +545,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)
       toggleVisible("gameInfos");
     else
       toggleVisible("boardContainer");