2 BaseGame(:game="game" :vr="vr" @newmove="processMove" @gameover="gameOver")
6 import BaseGame from "@/components/BaseGame.vue";
7 import { store } from "@/store";
8 import Worker from "worker-loader!@/playCompMove";
10 name: "my-computer-game",
14 // gameInfo: fen + mode + vname
15 // mode: "auto" (game comp vs comp) or "versus" (normal)
22 // Web worker to play computer moves without freezing interface:
23 timeStart: undefined, //time when computer starts thinking
24 compThink: false, //avoid asking a new move while one is being searched
29 "gameInfo.fen": function() {
32 "gameInfo.score": function(newScore) {
33 if (newScore != "*") {
34 this.game.score = newScore; //user action
35 if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp
39 // Modal end of game, and then sub-components
41 // Computer moves web worker logic:
42 this.compWorker = new Worker();
43 this.compWorker.onmessage = e => {
44 let compMove = e.data;
46 this.compThink = false;
47 this.$emit("game-stopped"); //no more moves: mate or stalemate
48 return; //after game ends, no more moves, nothing to do
50 if (!Array.isArray(compMove)) compMove = [compMove]; //to deal with MarseilleRules
51 // Small delay for the bot to appear "more human"
52 const delay = Math.max(500 - (Date.now() - this.timeStart), 0);
54 if (this.currentUrl != document.location.href) return; //page change
55 // NOTE: Dark and 2-moves are incompatible
56 const animate = this.gameInfo.vname != "Dark";
57 const animDelay = animate ? 250 : 0;
60 (function executeMove() {
61 self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
62 if (moveIdx >= compMove.length) {
63 self.compThink = false;
64 if (self.game.score != "*")
66 self.$emit("game-stopped");
67 } else setTimeout(executeMove, 500 + animDelay);
71 if (this.gameInfo.fen) this.launchGame();
74 launchGame: function() {
75 this.compWorker.postMessage(["scripts", this.gameInfo.vname]);
76 this.compWorker.postMessage(["init", this.gameInfo.fen]);
77 this.vr = new V(this.gameInfo.fen);
78 const mycolor = Math.random() < 0.5 ? "w" : "b";
79 let players = [{ name: "Myself" }, { name: "Computer" }];
80 if (mycolor == "b") players = players.reverse();
81 this.currentUrl = document.location.href; //to avoid playing outside page
82 // NOTE: fen and fenStart are redundant in game object
83 this.game = Object.assign({}, this.gameInfo, {
84 fenStart: this.gameInfo.fen,
89 this.compWorker.postMessage(["init", this.gameInfo.fen]);
90 if (mycolor != "w" || this.gameInfo.mode == "auto")
91 this.playComputerMove();
93 playComputerMove: function() {
94 this.timeStart = Date.now();
95 this.compThink = true;
96 this.compWorker.postMessage(["askmove"]);
98 processMove: function(move) {
99 if (this.game.score != "*") return;
100 // Send the move to web worker (including his own moves)
101 this.compWorker.postMessage(["newmove", move]);
102 // subTurn condition for Marseille (and Avalanche) rules
104 (!this.vr.subTurn || this.vr.subTurn <= 1) &&
105 (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)
107 this.playComputerMove();
110 gameOver: function(score, scoreMsg) {
111 this.game.score = score;
112 this.game.scoreMsg = scoreMsg;
113 this.$emit("game-over", score); //bubble up to Rules.vue