A few fixes + prepare translations + rules for last 4 variants
[vchess.git] / public / javascripts / components / game.js
index 085dafa..94340e6 100644 (file)
@@ -1105,7 +1105,7 @@ Vue.component('my-game', {
                this.conn.onclose = socketCloseListener;
                // Listen to keyboard left/right to navigate in game
                document.onkeydown = event => {
-                       if (["idle","chat"].includes(this.mode) &&
+                       if (["human","computer"].includes(this.mode) &&
                                !!this.vr && this.vr.moves.length > 0 && [37,39].includes(event.keyCode))
                        {
                                event.preventDefault();
@@ -1120,13 +1120,23 @@ Vue.component('my-game', {
                const self = this;
                this.compWorker.onmessage = function(e) {
                        let compMove = e.data;
-                       compMove.computer = true; //TODO: imperfect attempt to avoid ghost move
+                       if (!compMove)
+                               return; //may happen if MarseilleRules and subTurn==2 (TODO: a bit ugly...)
+                       if (!Array.isArray(compMove))
+                               compMove = [compMove]; //to deal with MarseilleRules
+                       // TODO: imperfect attempt to avoid ghost move:
+                       compMove.forEach(m => { m.computer = true; });
                        // (first move) HACK: small delay to avoid selecting elements
                        // before they appear on page:
                        const delay = Math.max(500-(Date.now()-self.timeStart), 0);
                        setTimeout(() => {
                                if (self.mode == "computer") //warning: mode could have changed!
-                                       self.play(compMove, "animate")
+                                       self.play(compMove[0], "animate");
+                               if (compMove.length == 2)
+                                       setTimeout( () => {
+                                               if (self.mode == "computer")
+                                                       self.play(compMove[1], "animate");
+                                       }, 750);
                        }, delay);
                }
        },
@@ -1588,24 +1598,24 @@ Vue.component('my-game', {
                                        // Send the move to web worker (TODO: including his own moves?!)
                                        this.compWorker.postMessage(["newmove",move]);
                                }
+                               const eog = this.vr.checkGameOver();
+                               if (eog != "*")
+                               {
+                                       if (["human","computer"].includes(this.mode))
+                                               this.endGame(eog);
+                                       else
+                                       {
+                                               // Just show score on screen (allow undo)
+                                               this.score = eog;
+                                               this.showScoreMsg();
+                                       }
+                               }
                        }
                        else
                        {
                                VariantRules.PlayOnBoard(this.vr.board, move);
                                this.$forceUpdate(); //TODO: ?!
                        }
-                       const eog = this.vr.checkGameOver();
-                       if (eog != "*")
-                       {
-                               if (["human","computer"].includes(this.mode))
-                                       this.endGame(eog);
-                               else
-                               {
-                                       // Just show score on screen (allow undo)
-                                       this.score = eog;
-                                       this.showScoreMsg();
-                               }
-                       }
                        if (["human","computer","friend"].includes(this.mode))
                                this.updateStorage(); //after our moves and opponent moves
                        if (this.mode == "computer" && this.vr.turn != this.mycolor && this.score == "*")