12 import BaseGame from "@/components/BaseGame.vue";
13 import { store } from "@/store";
14 import { CompgameStorage } from "@/utils/compgameStorage";
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;
41 this.compThink = false;
42 this.$emit("game-stopped"); //no more moves: mate or stalemate
43 return; //after game ends, no more moves, nothing to do
45 if (!Array.isArray(compMove)) compMove = [compMove]; //potential multi-move
46 // Small delay for the bot to appear "more human"
47 const delay = Math.max(500 - (Date.now() - this.timeStart), 0);
49 if (this.currentUrl != document.location.href) return; //page change
50 // NOTE: do not animate move if special display (ShowMoves != "all")
51 const animate = V.ShowMoves == "all";
52 const animDelay = animate ? 250 : 0;
55 (function executeMove() {
56 // NOTE: BaseGame::play() will trigger processMove() here
57 self.$refs["basegame"].play(compMove[moveIdx++], "received");
58 if (moveIdx >= compMove.length) {
59 self.compThink = false;
60 if (self.game.score != "*")
62 self.$emit("game-stopped");
63 } else setTimeout(executeMove, 500 + animDelay);
69 launchGame: function(game) {
70 this.compWorker.postMessage(["scripts", this.gameInfo.vname]);
73 vname: this.gameInfo.vname,
74 fenStart: V.GenRandInitFen(),
77 game.fen = game.fenStart;
78 if (this.gameInfo.mode == "versus")
79 CompgameStorage.add(game);
81 if (this.gameInfo.mode == "versus" && !game.mycolor)
82 game.mycolor = Math.random() < 0.5 ? "w" : "b";
83 this.compWorker.postMessage(["init", game.fen]);
84 this.vr = new V(game.fen);
85 game.players = [{ name: "Myself" }, { name: "Computer" }];
86 if (game.myColor == "b") game.players = game.players.reverse();
87 game.score = "*"; //finished games are removed
88 this.currentUrl = document.location.href; //to avoid playing outside page
90 this.compWorker.postMessage(["init", game.fen]);
91 if (this.gameInfo.mode == "auto" || game.mycolor != this.vr.turn)
92 this.playComputerMove();
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.
96 playComputerMove: function() {
97 this.timeStart = Date.now();
98 this.compThink = true;
99 this.compWorker.postMessage(["askmove"]);
101 processMove: function(move) {
102 if (this.game.score != "*") return;
103 // Send the move to web worker (including his own moves)
104 this.compWorker.postMessage(["newmove", move]);
105 // subTurn condition for Marseille (and Avalanche) rules
107 (!this.vr.subTurn || this.vr.subTurn <= 1) &&
108 (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)
110 this.playComputerMove();
112 // Finally, update storage:
113 if (this.gameInfo.mode == "versus") {
114 const allowed_fields = ["appear", "vanish", "start", "end"];
115 const filtered_move = Object.keys(move)
116 .filter(key => allowed_fields.includes(key))
117 .reduce((obj, key) => {
118 obj[key] = move[key];
121 CompgameStorage.update(this.gameInfo.vname, {
127 gameOver: function(score, scoreMsg) {
128 this.game.score = score;
129 this.game.scoreMsg = scoreMsg;
130 if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp