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