Fix parseInt() usage, rename Doubleorda --> Ordamirror, implement Clorange variant
[vchess.git] / client / src / components / ComputerGame.vue
1 <template lang="pug">
2 BaseGame(
3 ref="basegame"
4 :game="game"
5 @newmove="processMove"
6 )
7 </template>
8
9 <script>
10 import BaseGame from "@/components/BaseGame.vue";
11 import { store } from "@/store";
12 import { CompgameStorage } from "@/utils/compgameStorage";
13 import { getScoreMessage } from "@/utils/scoring";
14 import { playMove, getFilteredMove } from "@/utils/playUndo";
15 import Worker from "worker-loader!@/playCompMove";
16 export default {
17 name: "my-computer-game",
18 components: {
19 BaseGame
20 },
21 // gameInfo: fen + mode + vname
22 // mode: "auto" (game comp vs comp) or "versus" (normal)
23 props: ["gameInfo"],
24 data: function() {
25 return {
26 st: store.state,
27 game: {},
28 vr: null,
29 // Web worker to play computer moves without freezing interface:
30 timeStart: undefined, //time when computer starts thinking
31 compThink: false, //avoid asking a new move while one is being searched
32 compWorker: null
33 };
34 },
35 created: function() {
36 // Computer moves web worker logic:
37 this.compWorker = new Worker();
38 this.compWorker.onmessage = e => {
39 let compMove = e.data;
40 // Small delay for the bot to appear "more human"
41 const minDelay = this.gameInfo.mode == "versus" ? 500 : 1000;
42 const delay = Math.max(minDelay - (Date.now() - this.timeStart), 0);
43 let self = this;
44 setTimeout(() => {
45 if (this.currentUrl != document.location.href) return; //page change
46 self.$refs["basegame"].play(compMove, "received");
47 const animationLength =
48 // 250 = length of animation, 500 = delay between sub-moves
49 // TODO: a callback would be cleaner.
50 250 + (Array.isArray(compMove) ? (compMove.length - 1) * 750 : 0);
51 setTimeout(
52 () => {
53 self.compThink = false;
54 self.processMove(compMove);
55 },
56 animationLength
57 );
58 }, delay);
59 };
60 },
61 methods: {
62 launchGame: function(game) {
63 this.compWorker.postMessage(["scripts", this.gameInfo.vname]);
64 if (!game) {
65 game = {
66 vname: this.gameInfo.vname,
67 fenStart: V.GenRandInitFen(this.st.settings.randomness),
68 moves: []
69 };
70 game.fen = game.fenStart;
71 if (this.gameInfo.mode == "versus") CompgameStorage.add(game);
72 }
73 if (!game.mycolor) game.mycolor = (Math.random() < 0.5 ? "w" : "b");
74 this.compWorker.postMessage(["init", game.fen]);
75 this.vr = new V(game.fen);
76 game.players = [{ name: "Computer" }, { name: "Computer" }];
77 if (this.gameInfo.mode == "versus")
78 game.players[game.mycolor == 'w' ? 0 : 1] = { name: "Myself" };
79 game.score = "*"; //finished games are removed
80 game.mode = this.gameInfo.mode;
81 this.currentUrl = document.location.href; //to avoid playing outside page
82 this.game = game;
83 this.$refs["basegame"].re_setVariables(game);
84 this.compWorker.postMessage(["init", game.fen]);
85 if (this.gameInfo.mode == "auto" || game.mycolor != this.vr.turn)
86 this.playComputerMove();
87 },
88 // NOTE: a "goto" action could lead to an error when comp is thinking,
89 // but it's OK because from the user viewpoint the game just stops.
90 playComputerMove: function() {
91 this.timeStart = Date.now();
92 this.compThink = true;
93 this.compWorker.postMessage(["askmove"]);
94 },
95 processMove: function(move, scoreObj) {
96 playMove(move, this.vr);
97 // Maybe the user stopped the game:
98 if (this.game.score != "*") {
99 this.$emit("game-stopped");
100 return;
101 }
102 // This move could have ended the game
103 if (!scoreObj) scoreObj = { score: this.vr.getCurrentScore() };
104 if (scoreObj.score != "*") {
105 this.gameOver(scoreObj.score);
106 return;
107 }
108 // Send the move to web worker (including his own moves)
109 this.compWorker.postMessage(["newmove", move]);
110 if (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)
111 this.playComputerMove();
112 // Finally, update storage:
113 if (this.gameInfo.mode == "versus") {
114 CompgameStorage.update(this.gameInfo.vname, {
115 move: getFilteredMove(move),
116 fen: this.vr.getFen()
117 });
118 }
119 },
120 gameOver: function(score) {
121 this.game.score = score;
122 this.game.scoreMsg = getScoreMessage(score);
123 // If comp is thinking, let him finish:
124 if (!this.compThink) this.$emit("game-stopped");
125 }
126 }
127 };
128 </script>