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";
17 name: "my-computer-game",
21 // gameInfo: fen + mode + vname
22 // mode: "auto" (game comp vs comp) or "versus" (normal)
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
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);
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(() => self.processMove(compMove), animationLength);
52 self.compThink = false;
53 if (self.game.score != "*")
55 self.$emit("game-stopped");
60 launchGame: function(game) {
61 this.compWorker.postMessage(["scripts", this.gameInfo.vname]);
64 vname: this.gameInfo.vname,
65 fenStart: V.GenRandInitFen(this.st.settings.randomness),
68 game.fen = game.fenStart;
69 if (this.gameInfo.mode == "versus")
70 CompgameStorage.add(game);
72 if (!game.mycolor) game.mycolor = (Math.random() < 0.5 ? "w" : "b");
73 this.compWorker.postMessage(["init", game.fen]);
74 this.vr = new V(game.fen);
75 game.players = [{ name: "Myself" }, { name: "Computer" }];
76 if (game.mycolor == "b") game.players = game.players.reverse();
77 game.score = "*"; //finished games are removed
78 game.mode = this.gameInfo.mode;
79 this.currentUrl = document.location.href; //to avoid playing outside page
81 this.$refs["basegame"].re_setVariables(game);
82 this.compWorker.postMessage(["init", game.fen]);
83 if (this.gameInfo.mode == "auto" || game.mycolor != this.vr.turn)
84 this.playComputerMove();
86 // NOTE: a "goto" action could lead to an error when comp is thinking,
87 // but it's OK because from the user viewpoint the game just stops.
88 playComputerMove: function() {
89 this.timeStart = Date.now();
90 this.compThink = true;
91 this.compWorker.postMessage(["askmove"]);
93 processMove: function(move, scoreObj) {
94 playMove(move, this.vr);
95 // This move could have ended the game:
96 if (!scoreObj) scoreObj = { score: this.vr.getCurrentScore() };
97 if (scoreObj.score != "*") {
98 this.gameOver(scoreObj.score);
101 if (this.game.score != "*")
102 // The game already ended, probably because of a user action
104 // Send the move to web worker (including his own moves)
105 this.compWorker.postMessage(["newmove", move]);
106 if (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)
107 this.playComputerMove();
108 // Finally, update storage:
109 if (this.gameInfo.mode == "versus") {
110 CompgameStorage.update(this.gameInfo.vname, {
111 move: getFilteredMove(move),
112 fen: this.vr.getFen()
116 gameOver: function(score) {
117 this.game.score = score;
118 this.game.scoreMsg = getScoreMessage(score);
119 // If comp is thinking, let him finish:
120 if (!this.compThink) this.$emit("game-stopped");