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