From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 5 Apr 2020 12:37:04 +0000 (+0200)
Subject: Fix game download & upload for multi-moves variants
X-Git-Url: https://git.auder.net/doc/html/css/scripts/%7B%7B%20targetUrl%20%7D%7D?a=commitdiff_plain;h=fef153df51fe60a5af4c5b2a05e0b1177187bf62;p=vchess.git

Fix game download & upload for multi-moves variants
---

diff --git a/client/src/components/BaseGame.vue b/client/src/components/BaseGame.vue
index 1e1a2149..7156670c 100644
--- a/client/src/components/BaseGame.vue
+++ b/client/src/components/BaseGame.vue
@@ -277,10 +277,10 @@ export default {
       pgn += '[Black "' + this.game.players[1].name + '"]\n';
       pgn += '[Fen "' + this.game.fenStart + '"]\n';
       pgn += '[Result "' + this.game.score + '"]\n';
-      if (!!this.game.id) {
-        pgn += '[Cadence "' + this.game.cadence + '"]\n';
+      if (!!this.game.id)
         pgn += '[Url "' + params.serverUrl + '/game/' + this.game.id + '"]\n';
-      }
+      if (!!this.game.cadence)
+        pgn += '[Cadence "' + this.game.cadence + '"]\n';
       pgn += '\n';
       for (let i = 0; i < this.moves.length; i += 2) {
         if (i > 0) pgn += " ";
diff --git a/client/src/components/MoveList.vue b/client/src/components/MoveList.vue
index a9fa9834..6c999755 100644
--- a/client/src/components/MoveList.vue
+++ b/client/src/components/MoveList.vue
@@ -45,7 +45,7 @@ div
     span.score-msg {{ st.tr[message] }}
   .moves-list(v-if="!['none','highlight'].includes(show)")
     .tr(v-for="moveIdx in evenNumbers")
-      .td {{ firstNum + moveIdx / 2 + 1 }}
+      .td {{ firstNum + moveIdx / 2 }}
       .td(v-if="moveIdx < moves.length-1 || show == 'all'"
         :class="{'highlight-lm': cursor == moveIdx}"
         @click="() => gotoMove(moveIdx)"
@@ -56,7 +56,7 @@ div
         :class="{'highlight-lm': highlightBlackmove(moveIdx+1)}"
         @click="() => gotoMove(moveIdx+1)"
       )
-        | {{ notation(moves[moveIdx+1]) }}
+        | {{ notation(moves[moveIdx + 1]) }}
 </template>
 
 <script>
diff --git a/client/src/components/UploadGame.vue b/client/src/components/UploadGame.vue
index b4e4d853..ea85ea75 100644
--- a/client/src/components/UploadGame.vue
+++ b/client/src/components/UploadGame.vue
@@ -61,21 +61,18 @@ export default {
             // "unknown result" to avoid running the clocks...
             game.result = (value != "*" ? value : "?");
             break;
-          case "Url":
-            // Prefix "I_" to say "this is an import"
-            game.id = "i" + value.match(/\/game\/([a-zA-Z0-9]+)$/)[1];
-            break;
           case "Cadence":
             game.cadence = value;
             break;
         }
         idx++;
       }
-      if (!game.id) {
-        game.id = "i" + getRandString();
+      // Always generate random ID for imported games, because they could be
+      // downloaded at different states (prefix 'i' for 'Import').
+      game.id = 'i' + getRandString()
+      if (!game.cadence)
         // Provide a random cadence, just to be sure nothing breaks:
         game.cadence = "1d";
-      }
       game.chats = []; //not stored in PGN :)
       // Skip "human moves" section:
       while (lines[++idx].length > 0) {}
@@ -85,25 +82,39 @@ export default {
       .then((vModule) => {
         window.V = vModule[game.vname + "Rules"];
         while (++idx < lines.length && lines[idx].length > 0) {
-          const lineParts = lines[idx].split(" ");
-          const startEnd = lineParts[1].split('.');
-          let move = {};
-          if (startEnd[0] != "-") move.start = V.SquareToCoords(startEnd[0]);
-          if (startEnd[1] != "-") move.end = V.SquareToCoords(startEnd[1]);
-          const appearVanish = lineParts[2].split('/').map(lpart => {
-            if (lpart == "-") return [];
-            return lpart.split('.').map(psq => {
-              const xy = V.SquareToCoords(psq.substr(2));
-              return {
-                x: xy.x,
-                y: xy.y,
-                c: psq[0],
-                p: psq[1]
-              };
+          const spaceIdx = lines[idx].indexOf(' ');
+          const skipMoveNum = lines[idx].substr(spaceIdx + 1);
+          const lineParts = skipMoveNum.split(",");
+          let move = [];
+          lineParts.forEach(lpart => {
+            const smParts = lpart.split(' ');
+            const startEnd = smParts[0].split('.');
+            let sm = {};
+            sm.start =
+              startEnd[0] != "-"
+                ? V.SquareToCoords(startEnd[0])
+                : { x: -1, y: -1 };
+            sm.end =
+              startEnd[1] != "-"
+                ? V.SquareToCoords(startEnd[1])
+                : { x: -1, y: -1 };
+            const appearVanish = smParts[1].split('/').map(av => {
+              if (av == "-") return [];
+              return av.split('.').map(psq => {
+                const xy = V.SquareToCoords(psq.substr(2));
+                return {
+                  x: xy.x,
+                  y: xy.y,
+                  c: psq[0],
+                  p: psq[1]
+                };
+              });
             });
+            sm.appear = appearVanish[0];
+            sm.vanish = appearVanish[1];
+            move.push(sm);
           });
-          move.appear = appearVanish[0];
-          move.vanish = appearVanish[1];
+          if (move.length == 1) move = move[0];
           game.moves.push(move);
         }
         this.$emit("game-uploaded", game);
diff --git a/client/src/views/MyGames.vue b/client/src/views/MyGames.vue
index 52e6dc3b..ea9b49d3 100644
--- a/client/src/views/MyGames.vue
+++ b/client/src/views/MyGames.vue
@@ -198,6 +198,8 @@ export default {
             alert(this.st.tr["An error occurred. Try again!"]);
             return;
           }
+          // NOTE: since a random new ID is generated for imported games,
+          // this error will not occur.
           else alert(this.st.tr["The game was already imported"]);
         }
         this.$router.push("/game/" + game.id);