'update'
[vchess.git] / client / src / components / ComputerGame.vue
CommitLineData
a6088c90 1<template lang="pug">
7aa548e7
BA
2BaseGame(:game="game" :vr="vr" ref="basegame"
3 @newmove="processMove" @gameover="gameOver")
a6088c90
BA
4</template>
5
6<script>
7import BaseGame from "@/components/BaseGame.vue";
8import { store } from "@/store";
41cb9b94 9import Worker from "worker-loader!@/playCompMove";
a6088c90
BA
10
11export default {
41cb9b94 12 name: "my-computer-game",
a6088c90
BA
13 components: {
14 BaseGame,
15 },
6dd02928 16 // gameInfo: fen + mode + vname
63ca2b89 17 // mode: "auto" (game comp vs comp) or "versus" (normal)
6dd02928 18 props: ["gameInfo"],
a6088c90
BA
19 data: function() {
20 return {
21 st: store.state,
41cb9b94 22 game: {},
b7c32f1a 23 vr: null,
a6088c90
BA
24 // Web worker to play computer moves without freezing interface:
25 timeStart: undefined, //time when computer starts thinking
41cb9b94 26 compThink: false, //avoid asking a new move while one is being searched
a6088c90 27 compWorker: null,
a6088c90
BA
28 };
29 },
a6088c90 30 watch: {
834c202a 31 "gameInfo.fen": function() {
a6088c90
BA
32 this.launchGame();
33 },
41cb9b94
BA
34 "gameInfo.score": function(newScore) {
35 if (newScore != "*")
834c202a 36 {
41cb9b94 37 this.game.score = newScore; //user action
41cb9b94
BA
38 if (!this.compThink)
39 this.$emit("game-stopped"); //otherwise wait for comp
834c202a
BA
40 }
41 },
a6088c90
BA
42 },
43 // Modal end of game, and then sub-components
44 created: function() {
41cb9b94
BA
45 // Computer moves web worker logic:
46 this.compWorker = new Worker();
a6088c90 47 this.compWorker.onmessage = e => {
a6088c90 48 let compMove = e.data;
41cb9b94 49 if (!compMove)
7b3cf1b7
BA
50 {
51 this.compThink = false;
52 this.$emit("game-stopped"); //no more moves: mate or stalemate
41cb9b94 53 return; //after game ends, no more moves, nothing to do
7b3cf1b7 54 }
a6088c90
BA
55 if (!Array.isArray(compMove))
56 compMove = [compMove]; //to deal with MarseilleRules
57 // Small delay for the bot to appear "more human"
af32cf62 58 const delay = Math.max(500-(Date.now()-this.timeStart), 0);
a6088c90 59 setTimeout(() => {
6cd07b4d
BA
60 if (this.currentUrl != document.location.href)
61 return; //page change
41cb9b94 62 // NOTE: Dark and 2-moves are incompatible
6dd02928 63 const animate = (this.gameInfo.vname != "Dark");
af32cf62
BA
64 const animDelay = (animate ? 250 : 0);
65 let moveIdx = 0;
66 let self = this;
67 (function executeMove() {
63ca2b89 68 self.$set(self.game, "moveToPlay", compMove[moveIdx++]);
af32cf62
BA
69 if (moveIdx >= compMove.length)
70 {
71 self.compThink = false;
72 if (self.game.score != "*") //user action
73 self.$emit("game-stopped");
74 }
75 else
76 setTimeout(executeMove, 500 + animDelay);
77 })();
a6088c90
BA
78 }, delay);
79 }
834c202a 80 if (!!this.gameInfo.fen)
b7c32f1a 81 this.launchGame();
a6088c90
BA
82 },
83 // dans variant.js (plutôt room.js) conn gère aussi les challenges
84 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
85 methods: {
86 launchGame: async function() {
6dd02928 87 const vModule = await import("@/variants/" + this.gameInfo.vname + ".js");
a6088c90 88 window.V = vModule.VariantRules;
6dd02928 89 this.compWorker.postMessage(["scripts",this.gameInfo.vname]);
834c202a
BA
90 this.compWorker.postMessage(["init",this.gameInfo.fen]);
91 this.vr = new V(this.gameInfo.fen);
92 const mycolor = (Math.random() < 0.5 ? "w" : "b");
41cb9b94 93 let players = [{name:"Myself"},{name:"Computer"}];
834c202a
BA
94 if (mycolor == "b")
95 players = players.reverse();
6cd07b4d 96 this.currentUrl = document.location.href; //to avoid playing outside page
41cb9b94 97 // NOTE: fen and fenStart are redundant in game object
834c202a
BA
98 this.game = Object.assign({},
99 this.gameInfo,
100 {
101 fenStart: this.gameInfo.fen,
102 players: players,
103 mycolor: mycolor,
2c6cb25e 104 score: "*",
834c202a
BA
105 });
106 this.compWorker.postMessage(["init",this.gameInfo.fen]);
107 if (mycolor != "w" || this.gameInfo.mode == "auto")
37cdcbf3 108 this.playComputerMove();
a6088c90
BA
109 },
110 playComputerMove: function() {
111 this.timeStart = Date.now();
41cb9b94 112 this.compThink = true;
a6088c90
BA
113 this.compWorker.postMessage(["askmove"]);
114 },
1acda11c
BA
115 processMove: function(move) {
116 // Send the move to web worker (including his own moves)
117 this.compWorker.postMessage(["newmove",move]);
118 // subTurn condition for Marseille (and Avalanche) rules
119 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
834c202a 120 && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor))
1acda11c
BA
121 {
122 this.playComputerMove();
123 }
124 },
430a2038 125 gameOver: function(score, scoreMsg) {
41cb9b94 126 this.game.score = score;
430a2038 127 this.game.scoreMsg = scoreMsg;
41cb9b94 128 this.$emit("game-over", score); //bubble up to Rules.vue
834c202a 129 },
a6088c90
BA
130 },
131};
132</script>