Avoid direct references to (Dark) variant name in BaseGame, Analyse and Board
[vchess.git] / client / src / components / ComputerGame.vue
1 <template lang="pug">
2 BaseGame(
3 :game="game"
4 :vr="vr"
5 @newmove="processMove"
6 @gameover="gameOver"
7 )
8 </template>
9
10 <script>
11 import BaseGame from "@/components/BaseGame.vue";
12 import { store } from "@/store";
13 import Worker from "worker-loader!@/playCompMove";
14 export default {
15 name: "my-computer-game",
16 components: {
17 BaseGame
18 },
19 // gameInfo: fen + mode + vname
20 // mode: "auto" (game comp vs comp) or "versus" (normal)
21 props: ["gameInfo"],
22 data: function() {
23 return {
24 st: store.state,
25 game: {},
26 vr: null,
27 // Web worker to play computer moves without freezing interface:
28 timeStart: undefined, //time when computer starts thinking
29 compThink: false, //avoid asking a new move while one is being searched
30 compWorker: null
31 };
32 },
33 watch: {
34 "gameInfo.fen": function() {
35 this.launchGame();
36 },
37 "gameInfo.score": function(newScore) {
38 if (newScore != "*") {
39 this.game.score = newScore; //user action
40 if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp
41 }
42 }
43 },
44 // Modal end of game, and then sub-components
45 created: function() {
46 // Computer moves web worker logic:
47 this.compWorker = new Worker();
48 this.compWorker.onmessage = e => {
49 let compMove = e.data;
50 if (!compMove) {
51 this.compThink = false;
52 this.$emit("game-stopped"); //no more moves: mate or stalemate
53 return; //after game ends, no more moves, nothing to do
54 }
55 if (!Array.isArray(compMove)) compMove = [compMove]; //potential multi-move
56 // Small delay for the bot to appear "more human"
57 const delay = Math.max(500 - (Date.now() - this.timeStart), 0);
58 setTimeout(() => {
59 if (this.currentUrl != document.location.href) return; //page change
60 // NOTE: do not animate move if special display (ShowMoves != "all")
61 const animate = V.ShowMoves == "all";
62 const animDelay = animate ? 250 : 0;
63 let moveIdx = 0;
64 let self = this;
65 (function executeMove() {
66 self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
67 if (moveIdx >= compMove.length) {
68 self.compThink = false;
69 if (self.game.score != "*")
70 //user action
71 self.$emit("game-stopped");
72 } else setTimeout(executeMove, 500 + animDelay);
73 })();
74 }, delay);
75 };
76 if (this.gameInfo.fen) this.launchGame();
77 },
78 methods: {
79 launchGame: function() {
80 this.compWorker.postMessage(["scripts", this.gameInfo.vname]);
81 this.compWorker.postMessage(["init", this.gameInfo.fen]);
82 this.vr = new V(this.gameInfo.fen);
83 const mycolor = Math.random() < 0.5 ? "w" : "b";
84 let players = [{ name: "Myself" }, { name: "Computer" }];
85 if (mycolor == "b") players = players.reverse();
86 this.currentUrl = document.location.href; //to avoid playing outside page
87 // NOTE: fen and fenStart are redundant in game object
88 this.game = Object.assign({}, this.gameInfo, {
89 fenStart: this.gameInfo.fen,
90 players: players,
91 mycolor: mycolor,
92 score: "*"
93 });
94 this.compWorker.postMessage(["init", this.gameInfo.fen]);
95 if (mycolor != "w" || this.gameInfo.mode == "auto")
96 this.playComputerMove();
97 },
98 playComputerMove: function() {
99 this.timeStart = Date.now();
100 this.compThink = true;
101 this.compWorker.postMessage(["askmove"]);
102 },
103 processMove: function(move) {
104 if (this.game.score != "*") return;
105 // Send the move to web worker (including his own moves)
106 this.compWorker.postMessage(["newmove", move]);
107 // subTurn condition for Marseille (and Avalanche) rules
108 if (
109 (!this.vr.subTurn || this.vr.subTurn <= 1) &&
110 (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)
111 ) {
112 this.playComputerMove();
113 }
114 },
115 gameOver: function(score, scoreMsg) {
116 this.game.score = score;
117 this.game.scoreMsg = scoreMsg;
118 this.$emit("game-over", score); //bubble up to Rules.vue
119 }
120 }
121 };
122 </script>