Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
604b951e | 2 | BaseGame(:game="game" :vr="vr" @newmove="processMove" @gameover="gameOver") |
a6088c90 BA |
3 | </template> |
4 | ||
5 | <script> | |
6 | import BaseGame from "@/components/BaseGame.vue"; | |
7 | import { store } from "@/store"; | |
41cb9b94 | 8 | import Worker from "worker-loader!@/playCompMove"; |
a6088c90 | 9 | export default { |
41cb9b94 | 10 | name: "my-computer-game", |
a6088c90 BA |
11 | components: { |
12 | BaseGame, | |
13 | }, | |
6dd02928 | 14 | // gameInfo: fen + mode + vname |
63ca2b89 | 15 | // mode: "auto" (game comp vs comp) or "versus" (normal) |
6dd02928 | 16 | props: ["gameInfo"], |
a6088c90 BA |
17 | data: function() { |
18 | return { | |
19 | st: store.state, | |
41cb9b94 | 20 | game: {}, |
b7c32f1a | 21 | vr: null, |
a6088c90 BA |
22 | // Web worker to play computer moves without freezing interface: |
23 | timeStart: undefined, //time when computer starts thinking | |
41cb9b94 | 24 | compThink: false, //avoid asking a new move while one is being searched |
a6088c90 | 25 | compWorker: null, |
a6088c90 BA |
26 | }; |
27 | }, | |
a6088c90 | 28 | watch: { |
834c202a | 29 | "gameInfo.fen": function() { |
a6088c90 BA |
30 | this.launchGame(); |
31 | }, | |
41cb9b94 BA |
32 | "gameInfo.score": function(newScore) { |
33 | if (newScore != "*") | |
834c202a | 34 | { |
41cb9b94 | 35 | this.game.score = newScore; //user action |
41cb9b94 BA |
36 | if (!this.compThink) |
37 | this.$emit("game-stopped"); //otherwise wait for comp | |
834c202a BA |
38 | } |
39 | }, | |
a6088c90 BA |
40 | }, |
41 | // Modal end of game, and then sub-components | |
42 | created: function() { | |
41cb9b94 BA |
43 | // Computer moves web worker logic: |
44 | this.compWorker = new Worker(); | |
a6088c90 | 45 | this.compWorker.onmessage = e => { |
a6088c90 | 46 | let compMove = e.data; |
41cb9b94 | 47 | if (!compMove) |
7b3cf1b7 BA |
48 | { |
49 | this.compThink = false; | |
50 | this.$emit("game-stopped"); //no more moves: mate or stalemate | |
41cb9b94 | 51 | return; //after game ends, no more moves, nothing to do |
7b3cf1b7 | 52 | } |
a6088c90 BA |
53 | if (!Array.isArray(compMove)) |
54 | compMove = [compMove]; //to deal with MarseilleRules | |
55 | // Small delay for the bot to appear "more human" | |
af32cf62 | 56 | const delay = Math.max(500-(Date.now()-this.timeStart), 0); |
a6088c90 | 57 | setTimeout(() => { |
6cd07b4d BA |
58 | if (this.currentUrl != document.location.href) |
59 | return; //page change | |
41cb9b94 | 60 | // NOTE: Dark and 2-moves are incompatible |
6dd02928 | 61 | const animate = (this.gameInfo.vname != "Dark"); |
af32cf62 BA |
62 | const animDelay = (animate ? 250 : 0); |
63 | let moveIdx = 0; | |
64 | let self = this; | |
65 | (function executeMove() { | |
63ca2b89 | 66 | self.$set(self.game, "moveToPlay", compMove[moveIdx++]); |
af32cf62 BA |
67 | if (moveIdx >= compMove.length) |
68 | { | |
69 | self.compThink = false; | |
70 | if (self.game.score != "*") //user action | |
71 | self.$emit("game-stopped"); | |
72 | } | |
73 | else | |
74 | setTimeout(executeMove, 500 + animDelay); | |
75 | })(); | |
a6088c90 BA |
76 | }, delay); |
77 | } | |
834c202a | 78 | if (!!this.gameInfo.fen) |
b7c32f1a | 79 | this.launchGame(); |
a6088c90 | 80 | }, |
a6088c90 | 81 | methods: { |
e6a5a8ea | 82 | launchGame: function() { |
6dd02928 | 83 | this.compWorker.postMessage(["scripts",this.gameInfo.vname]); |
834c202a BA |
84 | this.compWorker.postMessage(["init",this.gameInfo.fen]); |
85 | this.vr = new V(this.gameInfo.fen); | |
86 | const mycolor = (Math.random() < 0.5 ? "w" : "b"); | |
41cb9b94 | 87 | let players = [{name:"Myself"},{name:"Computer"}]; |
834c202a BA |
88 | if (mycolor == "b") |
89 | players = players.reverse(); | |
6cd07b4d | 90 | this.currentUrl = document.location.href; //to avoid playing outside page |
41cb9b94 | 91 | // NOTE: fen and fenStart are redundant in game object |
834c202a BA |
92 | this.game = Object.assign({}, |
93 | this.gameInfo, | |
94 | { | |
95 | fenStart: this.gameInfo.fen, | |
96 | players: players, | |
97 | mycolor: mycolor, | |
2c6cb25e | 98 | score: "*", |
834c202a BA |
99 | }); |
100 | this.compWorker.postMessage(["init",this.gameInfo.fen]); | |
101 | if (mycolor != "w" || this.gameInfo.mode == "auto") | |
37cdcbf3 | 102 | this.playComputerMove(); |
a6088c90 BA |
103 | }, |
104 | playComputerMove: function() { | |
105 | this.timeStart = Date.now(); | |
41cb9b94 | 106 | this.compThink = true; |
a6088c90 BA |
107 | this.compWorker.postMessage(["askmove"]); |
108 | }, | |
1acda11c | 109 | processMove: function(move) { |
0e16cb26 BA |
110 | if (this.game.score != "*") |
111 | return; | |
1acda11c BA |
112 | // Send the move to web worker (including his own moves) |
113 | this.compWorker.postMessage(["newmove",move]); | |
114 | // subTurn condition for Marseille (and Avalanche) rules | |
115 | if ((!this.vr.subTurn || this.vr.subTurn <= 1) | |
834c202a | 116 | && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)) |
1acda11c BA |
117 | { |
118 | this.playComputerMove(); | |
119 | } | |
120 | }, | |
430a2038 | 121 | gameOver: function(score, scoreMsg) { |
41cb9b94 | 122 | this.game.score = score; |
430a2038 | 123 | this.game.scoreMsg = scoreMsg; |
41cb9b94 | 124 | this.$emit("game-over", score); //bubble up to Rules.vue |
834c202a | 125 | }, |
a6088c90 BA |
126 | }, |
127 | }; | |
128 | </script> |