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