Draft of a problems section + news system
[vchess.git] / client / src / components / ComputerGame.vue
1 <template lang="pug">
2 BaseGame(:game="game" :vr="vr" @newmove="processMove" @gameover="gameOver")
3 </template>
4
5 <script>
6 import BaseGame from "@/components/BaseGame.vue";
7 import { store } from "@/store";
8 import Worker from "worker-loader!@/playCompMove";
9 export default {
10 name: "my-computer-game",
11 components: {
12 BaseGame,
13 },
14 // gameInfo: fen + mode + vname
15 // mode: "auto" (game comp vs comp) or "versus" (normal)
16 props: ["gameInfo"],
17 data: function() {
18 return {
19 st: store.state,
20 game: {},
21 vr: null,
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
25 compWorker: null,
26 };
27 },
28 watch: {
29 "gameInfo.fen": function() {
30 this.launchGame();
31 },
32 "gameInfo.score": function(newScore) {
33 if (newScore != "*")
34 {
35 this.game.score = newScore; //user action
36 if (!this.compThink)
37 this.$emit("game-stopped"); //otherwise wait for comp
38 }
39 },
40 },
41 // Modal end of game, and then sub-components
42 created: function() {
43 // Computer moves web worker logic:
44 this.compWorker = new Worker();
45 this.compWorker.onmessage = e => {
46 let compMove = e.data;
47 if (!compMove)
48 {
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
52 }
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);
57 setTimeout(() => {
58 if (this.currentUrl != document.location.href)
59 return; //page change
60 // NOTE: Dark and 2-moves are incompatible
61 const animate = (this.gameInfo.vname != "Dark");
62 const animDelay = (animate ? 250 : 0);
63 let moveIdx = 0;
64 let self = this;
65 (function executeMove() {
66 self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
67 if (moveIdx >= compMove.length)
68 {
69 self.compThink = false;
70 if (self.game.score != "*") //user action
71 self.$emit("game-stopped");
72 }
73 else
74 setTimeout(executeMove, 500 + animDelay);
75 })();
76 }, delay);
77 }
78 if (!!this.gameInfo.fen)
79 this.launchGame();
80 },
81 methods: {
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"}];
88 if (mycolor == "b")
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({},
93 this.gameInfo,
94 {
95 fenStart: this.gameInfo.fen,
96 players: players,
97 mycolor: mycolor,
98 score: "*",
99 });
100 this.compWorker.postMessage(["init",this.gameInfo.fen]);
101 if (mycolor != "w" || this.gameInfo.mode == "auto")
102 this.playComputerMove();
103 },
104 playComputerMove: function() {
105 this.timeStart = Date.now();
106 this.compThink = true;
107 this.compWorker.postMessage(["askmove"]);
108 },
109 processMove: function(move) {
110 if (this.game.score != "*")
111 return;
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))
117 {
118 this.playComputerMove();
119 }
120 },
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
125 },
126 },
127 };
128 </script>