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) {
35 this.game.score = newScore; //user action
37 this.$emit("game-stopped"); //otherwise wait for comp
41 // Modal end of game, and then sub-components
43 // Computer moves web worker logic:
44 this.compWorker = new Worker();
45 this.compWorker.onmessage = e => {
46 let compMove = e.data;
49 this.compThink = false;
50 this.$emit("game-stopped"); //no more moves: mate or stalemate
51 return; //after game ends, no more moves, nothing to do
53 if (!Array.isArray(compMove))
54 compMove = [compMove]; //to deal with MarseilleRules
55 // Small delay for the bot to appear "more human"
56 const delay = Math.max(500-(Date.now()-this.timeStart), 0);
58 if (this.currentUrl != document.location.href)
60 // NOTE: Dark and 2-moves are incompatible
61 const animate = (this.gameInfo.vname != "Dark");
62 const animDelay = (animate ? 250 : 0);
65 (function executeMove() {
66 self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
67 if (moveIdx >= compMove.length)
69 self.compThink = false;
70 if (self.game.score != "*") //user action
71 self.$emit("game-stopped");
74 setTimeout(executeMove, 500 + animDelay);
78 if (!!this.gameInfo.fen)
82 launchGame: function() {
83 this.compWorker.postMessage(["scripts",this.gameInfo.vname]);
84 this.compWorker.postMessage(["init",this.gameInfo.fen]);
85 this.vr = new V(this.gameInfo.fen);
86 const mycolor = (Math.random() < 0.5 ? "w" : "b");
87 let players = [{name:"Myself"},{name:"Computer"}];
89 players = players.reverse();
90 this.currentUrl = document.location.href; //to avoid playing outside page
91 // NOTE: fen and fenStart are redundant in game object
92 this.game = Object.assign({},
95 fenStart: this.gameInfo.fen,
100 this.compWorker.postMessage(["init",this.gameInfo.fen]);
101 if (mycolor != "w" || this.gameInfo.mode == "auto")
102 this.playComputerMove();
104 playComputerMove: function() {
105 this.timeStart = Date.now();
106 this.compThink = true;
107 this.compWorker.postMessage(["askmove"]);
109 processMove: function(move) {
110 if (this.game.score != "*")
112 // Send the move to web worker (including his own moves)
113 this.compWorker.postMessage(["newmove",move]);
114 // subTurn condition for Marseille (and Avalanche) rules
115 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
116 && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor))
118 this.playComputerMove();
121 gameOver: function(score, scoreMsg) {
122 this.game.score = score;
123 this.game.scoreMsg = scoreMsg;
124 this.$emit("game-over", score); //bubble up to Rules.vue