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