Smooth scrolling in moves, template to render moveList
[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
a6088c90 17 // mode: "auto" (game comp vs comp), "versus" (normal) or "analyze"
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
834c202a 38 this.game.mode = "analyze";
41cb9b94
BA
39 if (!this.compThink)
40 this.$emit("game-stopped"); //otherwise wait for comp
834c202a
BA
41 }
42 },
a6088c90
BA
43 },
44 // Modal end of game, and then sub-components
45 created: function() {
41cb9b94
BA
46 // Computer moves web worker logic:
47 this.compWorker = new Worker();
a6088c90 48 this.compWorker.onmessage = e => {
a6088c90 49 let compMove = e.data;
41cb9b94 50 if (!compMove)
7b3cf1b7
BA
51 {
52 this.compThink = false;
53 this.$emit("game-stopped"); //no more moves: mate or stalemate
41cb9b94 54 return; //after game ends, no more moves, nothing to do
7b3cf1b7 55 }
a6088c90
BA
56 if (!Array.isArray(compMove))
57 compMove = [compMove]; //to deal with MarseilleRules
58 // Small delay for the bot to appear "more human"
af32cf62 59 const delay = Math.max(500-(Date.now()-this.timeStart), 0);
a6088c90 60 setTimeout(() => {
6cd07b4d
BA
61 if (this.currentUrl != document.location.href)
62 return; //page change
41cb9b94 63 // NOTE: Dark and 2-moves are incompatible
6dd02928 64 const animate = (this.gameInfo.vname != "Dark");
af32cf62
BA
65 const animDelay = (animate ? 250 : 0);
66 let moveIdx = 0;
67 let self = this;
68 (function executeMove() {
69 self.$refs.basegame.play(compMove[moveIdx++], animate);
70 if (moveIdx >= compMove.length)
71 {
72 self.compThink = false;
73 if (self.game.score != "*") //user action
74 self.$emit("game-stopped");
75 }
76 else
77 setTimeout(executeMove, 500 + animDelay);
78 })();
a6088c90
BA
79 }, delay);
80 }
834c202a 81 if (!!this.gameInfo.fen)
b7c32f1a 82 this.launchGame();
a6088c90
BA
83 },
84 // dans variant.js (plutôt room.js) conn gère aussi les challenges
85 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
86 methods: {
87 launchGame: async function() {
6dd02928 88 const vModule = await import("@/variants/" + this.gameInfo.vname + ".js");
a6088c90 89 window.V = vModule.VariantRules;
6dd02928 90 this.compWorker.postMessage(["scripts",this.gameInfo.vname]);
834c202a
BA
91 this.compWorker.postMessage(["init",this.gameInfo.fen]);
92 this.vr = new V(this.gameInfo.fen);
93 const mycolor = (Math.random() < 0.5 ? "w" : "b");
41cb9b94 94 let players = [{name:"Myself"},{name:"Computer"}];
834c202a
BA
95 if (mycolor == "b")
96 players = players.reverse();
6cd07b4d 97 this.currentUrl = document.location.href; //to avoid playing outside page
41cb9b94 98 // NOTE: fen and fenStart are redundant in game object
834c202a
BA
99 this.game = Object.assign({},
100 this.gameInfo,
101 {
102 fenStart: this.gameInfo.fen,
103 players: players,
104 mycolor: mycolor,
2c6cb25e 105 score: "*",
834c202a
BA
106 });
107 this.compWorker.postMessage(["init",this.gameInfo.fen]);
108 if (mycolor != "w" || this.gameInfo.mode == "auto")
37cdcbf3 109 this.playComputerMove();
a6088c90
BA
110 },
111 playComputerMove: function() {
112 this.timeStart = Date.now();
41cb9b94 113 this.compThink = true;
a6088c90
BA
114 this.compWorker.postMessage(["askmove"]);
115 },
1acda11c
BA
116 processMove: function(move) {
117 // Send the move to web worker (including his own moves)
118 this.compWorker.postMessage(["newmove",move]);
119 // subTurn condition for Marseille (and Avalanche) rules
120 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
834c202a 121 && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor))
1acda11c
BA
122 {
123 this.playComputerMove();
124 }
125 },
430a2038 126 gameOver: function(score, scoreMsg) {
41cb9b94 127 this.game.score = score;
430a2038 128 this.game.scoreMsg = scoreMsg;
834c202a 129 this.game.mode = "analyze";
41cb9b94 130 this.$emit("game-over", score); //bubble up to Rules.vue
834c202a 131 },
a6088c90
BA
132 },
133};
134</script>