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 | 11 | components: { |
6808d7a1 | 12 | BaseGame |
a6088c90 | 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 |
6808d7a1 | 25 | compWorker: null |
a6088c90 BA |
26 | }; |
27 | }, | |
a6088c90 | 28 | watch: { |
834c202a | 29 | "gameInfo.fen": function() { |
a6088c90 BA |
30 | this.launchGame(); |
31 | }, | |
41cb9b94 | 32 | "gameInfo.score": function(newScore) { |
6808d7a1 | 33 | if (newScore != "*") { |
41cb9b94 | 34 | this.game.score = newScore; //user action |
6808d7a1 | 35 | if (!this.compThink) this.$emit("game-stopped"); //otherwise wait for comp |
834c202a | 36 | } |
6808d7a1 | 37 | } |
a6088c90 BA |
38 | }, |
39 | // Modal end of game, and then sub-components | |
40 | created: function() { | |
41cb9b94 BA |
41 | // Computer moves web worker logic: |
42 | this.compWorker = new Worker(); | |
a6088c90 | 43 | this.compWorker.onmessage = e => { |
a6088c90 | 44 | let compMove = e.data; |
6808d7a1 | 45 | if (!compMove) { |
7b3cf1b7 BA |
46 | this.compThink = false; |
47 | this.$emit("game-stopped"); //no more moves: mate or stalemate | |
41cb9b94 | 48 | return; //after game ends, no more moves, nothing to do |
7b3cf1b7 | 49 | } |
6808d7a1 | 50 | if (!Array.isArray(compMove)) compMove = [compMove]; //to deal with MarseilleRules |
a6088c90 | 51 | // Small delay for the bot to appear "more human" |
6808d7a1 | 52 | const delay = Math.max(500 - (Date.now() - this.timeStart), 0); |
a6088c90 | 53 | setTimeout(() => { |
6808d7a1 | 54 | if (this.currentUrl != document.location.href) return; //page change |
41cb9b94 | 55 | // NOTE: Dark and 2-moves are incompatible |
6808d7a1 BA |
56 | const animate = this.gameInfo.vname != "Dark"; |
57 | const animDelay = animate ? 250 : 0; | |
af32cf62 BA |
58 | let moveIdx = 0; |
59 | let self = this; | |
60 | (function executeMove() { | |
63ca2b89 | 61 | self.$set(self.game, "moveToPlay", compMove[moveIdx++]); |
6808d7a1 | 62 | if (moveIdx >= compMove.length) { |
af32cf62 | 63 | self.compThink = false; |
6808d7a1 BA |
64 | if (self.game.score != "*") |
65 | //user action | |
af32cf62 | 66 | self.$emit("game-stopped"); |
6808d7a1 | 67 | } else setTimeout(executeMove, 500 + animDelay); |
af32cf62 | 68 | })(); |
a6088c90 | 69 | }, delay); |
6808d7a1 BA |
70 | }; |
71 | if (this.gameInfo.fen) this.launchGame(); | |
a6088c90 | 72 | }, |
a6088c90 | 73 | methods: { |
e6a5a8ea | 74 | launchGame: function() { |
6808d7a1 BA |
75 | this.compWorker.postMessage(["scripts", this.gameInfo.vname]); |
76 | this.compWorker.postMessage(["init", this.gameInfo.fen]); | |
834c202a | 77 | this.vr = new V(this.gameInfo.fen); |
6808d7a1 BA |
78 | const mycolor = Math.random() < 0.5 ? "w" : "b"; |
79 | let players = [{ name: "Myself" }, { name: "Computer" }]; | |
80 | if (mycolor == "b") players = players.reverse(); | |
6cd07b4d | 81 | this.currentUrl = document.location.href; //to avoid playing outside page |
41cb9b94 | 82 | // NOTE: fen and fenStart are redundant in game object |
6808d7a1 BA |
83 | this.game = Object.assign({}, this.gameInfo, { |
84 | fenStart: this.gameInfo.fen, | |
85 | players: players, | |
86 | mycolor: mycolor, | |
87 | score: "*" | |
88 | }); | |
89 | this.compWorker.postMessage(["init", this.gameInfo.fen]); | |
834c202a | 90 | if (mycolor != "w" || this.gameInfo.mode == "auto") |
37cdcbf3 | 91 | this.playComputerMove(); |
a6088c90 BA |
92 | }, |
93 | playComputerMove: function() { | |
94 | this.timeStart = Date.now(); | |
41cb9b94 | 95 | this.compThink = true; |
a6088c90 BA |
96 | this.compWorker.postMessage(["askmove"]); |
97 | }, | |
1acda11c | 98 | processMove: function(move) { |
6808d7a1 | 99 | if (this.game.score != "*") return; |
1acda11c | 100 | // Send the move to web worker (including his own moves) |
6808d7a1 | 101 | this.compWorker.postMessage(["newmove", move]); |
1acda11c | 102 | // subTurn condition for Marseille (and Avalanche) rules |
6808d7a1 BA |
103 | if ( |
104 | (!this.vr.subTurn || this.vr.subTurn <= 1) && | |
105 | (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor) | |
106 | ) { | |
1acda11c BA |
107 | this.playComputerMove(); |
108 | } | |
109 | }, | |
430a2038 | 110 | gameOver: function(score, scoreMsg) { |
41cb9b94 | 111 | this.game.score = score; |
430a2038 | 112 | this.game.scoreMsg = scoreMsg; |
41cb9b94 | 113 | this.$emit("game-over", score); //bubble up to Rules.vue |
6808d7a1 BA |
114 | } |
115 | } | |
a6088c90 BA |
116 | }; |
117 | </script> |