Bugs fixing, finalization of rules in french+english
[vchess.git] / public / javascripts / components / game.js
index d8f06b3..ac03c8f 100644 (file)
@@ -62,9 +62,8 @@ Vue.component('my-game', {
                        },
                        [h('i', { 'class': { "material-icons": true } }, "accessibility")])
                );
-               if (variant != "Dark" &&
-                       (["idle","computer","friend"].includes(this.mode)
-                               || ["friend","human"].includes(this.mode) && this.score != "*"))
+               if (["idle","computer","friend"].includes(this.mode)
+                       || (this.mode == "human" && this.score != "*"))
                {
                        actionArray.push(
                                h('button',
@@ -81,9 +80,8 @@ Vue.component('my-game', {
                                [h('i', { 'class': { "material-icons": true } }, "computer")])
                        );
                }
-               if (variant != "Dark" &&
-                       (["idle","friend"].includes(this.mode)
-                               || ["computer","human"].includes(this.mode) && this.score != "*"))
+               if (variant != "Dark" && (["idle","friend"].includes(this.mode)
+                       || (["computer","human"].includes(this.mode) && this.score != "*")))
                {
                        actionArray.push(
                                h('button',
@@ -1105,7 +1103,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();
@@ -1130,12 +1128,13 @@ Vue.component('my-game', {
                        // before they appear on page:
                        const delay = Math.max(500-(Date.now()-self.timeStart), 0);
                        setTimeout(() => {
+                               const animate = (variant!="Dark" ? "animate" : null);
                                if (self.mode == "computer") //warning: mode could have changed!
-                                       self.play(compMove[0], "animate");
+                                       self.play(compMove[0], animate);
                                if (compMove.length == 2)
                                        setTimeout( () => {
                                                if (self.mode == "computer")
-                                                       self.play(compMove[1], "animate");
+                                                       self.play(compMove[1], animate);
                                        }, 750);
                        }, delay);
                }
@@ -1288,6 +1287,12 @@ Vue.component('my-game', {
                },
                clickComputerGame: function(e) {
                        this.getRidOfTooltip(e.currentTarget);
+                       if (this.mode == "computer" && this.score == "*"
+                               && this.vr.turn != this.mycolor)
+                       {
+                               // Wait for computer reply first (avoid potential "ghost move" bug)
+                               return;
+                       }
                        this.newGame("computer");
                },
                clickFriendGame: function(e) {
@@ -1307,7 +1312,7 @@ Vue.component('my-game', {
                        this.endGame(this.mycolor=="w"?"0-1":"1-0");
                },
                newGame: function(mode, fenInit, color, oppId) {
-                       const fen = "rnbbqkrn/1ppppp1p/p5p1/8/8/3P4/PPP1PPPP/BNQBRKRN w1 1111 -"; //fenInit || VariantRules.GenRandInitFen();
+                       const fen = fenInit || VariantRules.GenRandInitFen();
                        console.log(fen); //DEBUG
                        if (mode=="human" && !oppId)
                        {
@@ -1345,8 +1350,6 @@ Vue.component('my-game', {
                                                        return;
                                                }
                                        }
-                                       else if (score == "*")
-                                               return this.continueGame("computer");
                                }
                        }
                        else if (mode == "friend")
@@ -1388,7 +1391,7 @@ Vue.component('my-game', {
                        else if (mode == "computer")
                        {
                                this.compWorker.postMessage(["init",this.vr.getFen()]);
-                               this.mycolor = "w";//(Math.random() < 0.5 ? 'w' : 'b');
+                               this.mycolor = (Math.random() < 0.5 ? 'w' : 'b');
                                if (this.mycolor != this.vr.turn)
                                        this.playComputerMove();
                        }
@@ -1417,7 +1420,7 @@ Vue.component('my-game', {
                        else if (mode == "computer")
                        {
                                this.compWorker.postMessage(["init",fen]);
-                               if (this.mycolor != this.vr.turn)
+                               if (score == "*" && this.mycolor != this.vr.turn)
                                        this.playComputerMove();
                        }
                        //else: nothing special to do in friend mode
@@ -1598,24 +1601,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 == "*")