Revise server code + a few fixes in trnalsations and ComputerGame
[vchess.git] / client / src / components / ComputerGame.vue
index 402bc7f..a5e03b3 100644 (file)
@@ -1,5 +1,11 @@
 <template lang="pug">
-BaseGame(:game="game" :vr="vr" @newmove="processMove" @gameover="gameOver")
+BaseGame(
+  ref="basegame"
+  :game="game"
+  :vr="vr"
+  @newmove="processMove"
+  @gameover="gameOver"
+)
 </template>
 
 <script>
@@ -29,14 +35,7 @@ export default {
     "gameInfo.fen": function() {
       this.launchGame();
     },
-    "gameInfo.score": function(newScore) {
-      if (newScore != "*") {
-        this.game.score = newScore; //user action
-        if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp
-      }
-    }
   },
-  // Modal end of game, and then sub-components
   created: function() {
     // Computer moves web worker logic:
     this.compWorker = new Worker();
@@ -47,18 +46,18 @@ export default {
         this.$emit("game-stopped"); //no more moves: mate or stalemate
         return; //after game ends, no more moves, nothing to do
       }
-      if (!Array.isArray(compMove)) compMove = [compMove]; //to deal with MarseilleRules
+      if (!Array.isArray(compMove)) compMove = [compMove]; //potential multi-move
       // Small delay for the bot to appear "more human"
       const delay = Math.max(500 - (Date.now() - this.timeStart), 0);
       setTimeout(() => {
         if (this.currentUrl != document.location.href) return; //page change
-        // NOTE: Dark and 2-moves are incompatible
-        const animate = this.gameInfo.vname != "Dark";
+        // NOTE: do not animate move if special display (ShowMoves != "all")
+        const animate = V.ShowMoves == "all";
         const animDelay = animate ? 250 : 0;
         let moveIdx = 0;
         let self = this;
         (function executeMove() {
-          self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
+          self.$refs["basegame"].play(compMove[moveIdx++], "received");
           if (moveIdx >= compMove.length) {
             self.compThink = false;
             if (self.game.score != "*")
@@ -90,6 +89,8 @@ export default {
       if (mycolor != "w" || this.gameInfo.mode == "auto")
         this.playComputerMove();
     },
+    // NOTE: a "goto" action could lead to an error when comp is thinking,
+    // but it's OK because from the user viewpoint the game just stops.
     playComputerMove: function() {
       this.timeStart = Date.now();
       this.compThink = true;
@@ -110,7 +111,7 @@ export default {
     gameOver: function(score, scoreMsg) {
       this.game.score = score;
       this.game.scoreMsg = scoreMsg;
-      this.$emit("game-over", score); //bubble up to Rules.vue
+      if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp
     }
   }
 };