Parameters adjustments + cosmetics
[vchess.git] / client / src / views / Game.vue
index b66c6ef..b10be75 100644 (file)
@@ -17,9 +17,7 @@
       button(@click="offerDraw") Draw
       button(@click="() => abortGame()") Abort
       button(@click="resign") Resign
-    div(v-if="game.mode=='corr'")
-      textarea(v-show="score=='*' && vr.turn==game.mycolor" v-model="corrMsg")
-      div(v-show="cursor>=0") {{ moves[cursor].message }}
+    textarea(v-if="game.score=='*'" v-model="corrMsg")
 </template>
 
 <!--
@@ -84,23 +82,14 @@ export default {
           : 0;
         return ppt(newState[i] - removeTime);
       });
-      const myTurn = (currentTurn == this.game.mycolor);
       let clockUpdate = setInterval(() => {
-        if (countdown <= 0 || this.vr.turn != currentTurn || this.game.score != "*")
+        if (countdown < 0 || this.vr.turn != currentTurn || this.game.score != "*")
         {
           clearInterval(clockUpdate);
-          if (countdown <= 0 && myTurn)
+          if (countdown < 0)
           {
             this.$refs["basegame"].endGame(
-              this.game.mycolor=="w" ? "0-1" : "1-0", "Time");
-            const oppsid = this.getOppSid();
-            if (!!oppsid)
-            {
-              this.st.conn.send(JSON.stringify({
-                code: "timeover",
-                target: oppsid,
-              }));
-            }
+              this.vr.turn=="w" ? "0-1" : "1-0", "Time");
           }
         }
         else
@@ -223,10 +212,6 @@ export default {
           this.$refs["basegame"].endGame(
             this.game.mycolor=="w" ? "1-0" : "0-1", "Resign");
           break;
-        case "timeover":
-          this.$refs["basegame"].endGame(
-            this.game.mycolor=="w" ? "1-0" : "0-1", "Time");
-          break;
         case "abort":
           this.$refs["basegame"].endGame("?", "Abort: " + data.msg);
           break;
@@ -349,6 +334,7 @@ export default {
           game.clocks = [tc.mainTime, tc.mainTime];
           game.initime = [0, 0];
           const L = game.moves.length;
+          game.moves.sort((m1,m2) => m1.idx - m2.idx); //in case of
           if (L >= 3)
           {
             let addTime = [0, 0];
@@ -362,6 +348,17 @@ export default {
           }
           if (L >= 1)
             game.initime[L%2] = game.moves[L-1].played;
+          // Now that we used idx and played, re-format moves as for live games
+          game.moves = game.moves.map( (m) => {
+            const s = m.squares;
+            return {
+              appear: s.appear,
+              vanish: s.vanish,
+              start: s.start,
+              end: s.end,
+              message: m.message,
+            };
+          });
         }
         const myIdx = game.players.findIndex(p => {
           return p.sid == this.st.user.sid || p.uid == this.st.user.id;
@@ -406,9 +403,7 @@ export default {
       else
       {
         // Local or corr game
-        GameStorage.get(this.gameRef.id, (game) => {
-          afterRetrieval(game);
-        });
+        GameStorage.get(this.gameRef.id, afterRetrieval);
       }
     },
     // Post-process a move (which was just played)
@@ -435,41 +430,71 @@ export default {
           // elapsed time is measured in milliseconds
           addTime = this.game.increment - elapsed/1000;
         }
-        this.st.conn.send(JSON.stringify({
-          code: "newmove",
-          target: this.game.oppid,
-          move: Object.assign({}, filtered_move, {addTime: addTime}),
-        }));
+        let sendMove = Object.assign({}, filtered_move, {addTime: addTime});
+        if (this.game.type == "corr")
+          sendMove.message = this.corrMsg;
+        const oppsid = this.getOppSid();
+        if (!!oppsid)
+        {
+          this.st.conn.send(JSON.stringify({
+            code: "newmove",
+            target: oppsid,
+            move: sendMove,
+          }));
+        }
+        if (this.game.type == "corr" && this.corrMsg != "")
+        {
+          // Add message to last move in BaseGame:
+          // TODO: not very good style...
+          this.$refs["basegame"].setCurrentMessage(this.corrMsg);
+        }
       }
       else
         addTime = move.addTime; //supposed transmitted
       const nextIdx = ["w","b"].indexOf(this.vr.turn);
-      GameStorage.update(this.gameRef.id,
+      // Since corr games are stored at only one location, update should be
+      // done only by one player for each move:
+      if (this.game.type == "live" || move.color == this.game.mycolor)
       {
-        fen: move.fen,
-        move:
+        if (this.game.type == "corr")
         {
-          squares: filtered_move,
-          message: this.corrMsg, //TODO
-          played: Date.now(), //TODO: on server?
-          idx: this.game.moves.length - 1,
-          color: move.color,
-        },
-        clocks: this.game.clocks.map((t,i) => i==colorIdx
-          ? this.game.clocks[i] + addTime
-          : this.game.clocks[i]),
-        initime: this.game.initime.map((t,i) => i==nextIdx
-          ? Date.now()
-          : this.game.initime[i]),
-      });
+          GameStorage.update(this.gameRef.id,
+          {
+            fen: move.fen,
+            move:
+            {
+              squares: filtered_move,
+              message: this.corrMsg,
+              played: Date.now(), //TODO: on server?
+              idx: this.game.moves.length,
+            },
+          });
+        }
+        else //live
+        {
+          GameStorage.update(this.gameRef.id,
+          {
+            fen: move.fen,
+            move: filtered_move,
+            clocks: this.game.clocks.map((t,i) => i==colorIdx
+              ? this.game.clocks[i] + addTime
+              : this.game.clocks[i]),
+            initime: this.game.initime.map((t,i) => i==nextIdx
+              ? Date.now()
+              : this.game.initime[i]),
+          });
+        }
+      }
       // Also update current game object:
       this.game.moves.push(move);
       this.game.fen = move.fen;
       //TODO: just this.game.clocks[colorIdx] += addTime;
       this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime);
       this.game.initime[nextIdx] = Date.now();
+      // Finally reset curMoveMessage if needed
+      if (this.game.type == "corr" && move.color == this.game.mycolor)
+        this.corrMsg = "";
     },
-    // TODO: this update function should also work for corr games
     gameOver: function(score) {
       this.game.mode = "analyze";
       this.game.score = score;