Fix play comp move in ComputerGame component
[vchess.git] / client / src / components / ComputerGame.vue
CommitLineData
a6088c90
BA
1<template lang="pug">
2.row
3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
b7c32f1a 4 BaseGame(:vname="vname" :analyze="analyze"
1acda11c
BA
5 :vr="vr" :fen-start="fenStart" :players="players" :mycolor="mycolor"
6 ref="basegame" @newmove="processMove")
a6088c90
BA
7</template>
8
9<script>
10import BaseGame from "@/components/BaseGame.vue";
11import { store } from "@/store";
12import Worker from 'worker-loader!@/playCompMove';
13
14export default {
15 name: 'my-computer-game',
16 components: {
17 BaseGame,
18 },
19 // mode: "auto" (game comp vs comp), "versus" (normal) or "analyze"
b7c32f1a 20 props: ["fen","mode","vname"],
a6088c90
BA
21 data: function() {
22 return {
23 st: store.state,
b7c32f1a
BA
24 // variables passed to BaseGame:
25 fenStart: "",
26 vr: null,
27 players: ["Myself","Computer"], //always playing white for now
28 mycolor: "w",
a6088c90
BA
29 // Web worker to play computer moves without freezing interface:
30 timeStart: undefined, //time when computer starts thinking
31 lockCompThink: false, //to avoid some ghost moves
a6088c90 32 compWorker: null,
a6088c90
BA
33 };
34 },
35 computed: {
36 analyze: function() {
37 return this.mode == "analyze";
38 },
39 },
40 watch: {
41 fen: function() {
42 // (Security) No effect if a computer move is in progress:
43 if (this.lockCompThink)
44 return this.$emit("computer-think");
45 this.launchGame();
46 },
47 },
48 // Modal end of game, and then sub-components
49 created: function() {
a6088c90
BA
50 // Computer moves web worker logic: (TODO: also for observers in HH games ?)
51 this.compWorker = new Worker(); //'/javascripts/playCompMove.js'),
52 this.compWorker.onmessage = e => {
53 this.lockCompThink = true; //to avoid some ghost moves
54 let compMove = e.data;
55 if (!Array.isArray(compMove))
56 compMove = [compMove]; //to deal with MarseilleRules
57 // Small delay for the bot to appear "more human"
58 const delay = Math.max(500-(Date.now()-this.timeStart), 0);
59 setTimeout(() => {
1acda11c
BA
60 const animate = (this.vname != "Dark");
61 this.$refs.basegame.play(compMove[0], animate);
a6088c90 62 if (compMove.length == 2)
1acda11c 63 setTimeout( () => { this.$refs.basegame.play(compMove[1], animate); }, 750);
a6088c90
BA
64 else //250 == length of animation (TODO: should be a constant somewhere)
65 setTimeout( () => this.lockCompThink = false, 250);
66 }, delay);
67 }
b7c32f1a
BA
68 if (!!this.fen)
69 this.launchGame();
a6088c90
BA
70 },
71 // dans variant.js (plutôt room.js) conn gère aussi les challenges
72 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
73 methods: {
74 launchGame: async function() {
b7c32f1a 75 const vModule = await import("@/variants/" + this.vname + ".js");
a6088c90 76 window.V = vModule.VariantRules;
b7c32f1a 77 this.compWorker.postMessage(["scripts",this.vname]);
a6088c90
BA
78 this.newGameFromFen(this.fen);
79 },
80 newGameFromFen: function(fen) {
81 this.vr = new V(fen);
82 this.moves = [];
83 this.cursor = -1;
84 this.fenStart = fen;
85 this.score = "*";
86 if (this.mode == "analyze")
87 {
88 this.mycolor = V.ParseFen(fen).turn;
89 this.orientation = this.mycolor;
90 }
91 else if (this.mode == "computer") //only other alternative (HH with gameId)
92 {
93 this.mycolor = (Math.random() < 0.5 ? "w" : "b");
94 this.orientation = this.mycolor;
95 this.compWorker.postMessage(["init",fen]);
96 if (this.mycolor != "w" || this.subMode == "auto")
97 this.playComputerMove();
98 }
99 },
100 playComputerMove: function() {
101 this.timeStart = Date.now();
102 this.compWorker.postMessage(["askmove"]);
103 },
1acda11c
BA
104 // TODO: do not process if game is over (check score ?)
105 processMove: function(move) {
106 // Send the move to web worker (including his own moves)
107 this.compWorker.postMessage(["newmove",move]);
108 // subTurn condition for Marseille (and Avalanche) rules
109 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
110 && (this.mode == "auto" || this.vr.turn != this.mycolor))
111 {
112 this.playComputerMove();
113 }
114 },
a6088c90
BA
115 },
116};
117</script>