Fixing computer play
[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]);
1c9f093d 78 this.compWorker.postMessage(["init",this.fen]);
a6088c90
BA
79 this.newGameFromFen(this.fen);
80 },
81 newGameFromFen: function(fen) {
82 this.vr = new V(fen);
83 this.moves = [];
84 this.cursor = -1;
85 this.fenStart = fen;
86 this.score = "*";
87 if (this.mode == "analyze")
88 {
89 this.mycolor = V.ParseFen(fen).turn;
90 this.orientation = this.mycolor;
91 }
92 else if (this.mode == "computer") //only other alternative (HH with gameId)
93 {
94 this.mycolor = (Math.random() < 0.5 ? "w" : "b");
95 this.orientation = this.mycolor;
96 this.compWorker.postMessage(["init",fen]);
1c9f093d 97 if (this.mycolor != "w" || this.mode == "auto")
a6088c90
BA
98 this.playComputerMove();
99 }
100 },
101 playComputerMove: function() {
102 this.timeStart = Date.now();
103 this.compWorker.postMessage(["askmove"]);
104 },
1acda11c
BA
105 // TODO: do not process if game is over (check score ?)
106 processMove: function(move) {
107 // Send the move to web worker (including his own moves)
108 this.compWorker.postMessage(["newmove",move]);
109 // subTurn condition for Marseille (and Avalanche) rules
110 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
111 && (this.mode == "auto" || this.vr.turn != this.mycolor))
112 {
113 this.playComputerMove();
114 }
115 },
a6088c90
BA
116 },
117};
118</script>