Commit | Line | Data |
---|---|---|
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 | |
834c202a BA |
4 | BaseGame(:game="game" :vr="vr" ref="basegame" |
5 | @newmove="processMove" @gameover="gameOver") | |
a6088c90 BA |
6 | </template> |
7 | ||
8 | <script> | |
9 | import BaseGame from "@/components/BaseGame.vue"; | |
10 | import { store } from "@/store"; | |
41cb9b94 | 11 | import Worker from "worker-loader!@/playCompMove"; |
a6088c90 BA |
12 | |
13 | export default { | |
41cb9b94 | 14 | name: "my-computer-game", |
a6088c90 BA |
15 | components: { |
16 | BaseGame, | |
17 | }, | |
6dd02928 | 18 | // gameInfo: fen + mode + vname |
a6088c90 | 19 | // mode: "auto" (game comp vs comp), "versus" (normal) or "analyze" |
6dd02928 | 20 | props: ["gameInfo"], |
a6088c90 BA |
21 | data: function() { |
22 | return { | |
23 | st: store.state, | |
41cb9b94 | 24 | game: {}, |
b7c32f1a | 25 | vr: null, |
a6088c90 BA |
26 | // Web worker to play computer moves without freezing interface: |
27 | timeStart: undefined, //time when computer starts thinking | |
41cb9b94 | 28 | compThink: false, //avoid asking a new move while one is being searched |
a6088c90 | 29 | compWorker: null, |
a6088c90 BA |
30 | }; |
31 | }, | |
a6088c90 | 32 | watch: { |
834c202a | 33 | "gameInfo.fen": function() { |
a6088c90 BA |
34 | this.launchGame(); |
35 | }, | |
41cb9b94 BA |
36 | "gameInfo.score": function(newScore) { |
37 | if (newScore != "*") | |
834c202a | 38 | { |
41cb9b94 | 39 | this.game.score = newScore; //user action |
834c202a | 40 | this.game.mode = "analyze"; |
41cb9b94 BA |
41 | if (!this.compThink) |
42 | this.$emit("game-stopped"); //otherwise wait for comp | |
834c202a BA |
43 | } |
44 | }, | |
a6088c90 BA |
45 | }, |
46 | // Modal end of game, and then sub-components | |
47 | created: function() { | |
41cb9b94 BA |
48 | // Computer moves web worker logic: |
49 | this.compWorker = new Worker(); | |
a6088c90 | 50 | this.compWorker.onmessage = e => { |
a6088c90 | 51 | let compMove = e.data; |
41cb9b94 BA |
52 | if (!compMove) |
53 | return; //after game ends, no more moves, nothing to do | |
a6088c90 BA |
54 | if (!Array.isArray(compMove)) |
55 | compMove = [compMove]; //to deal with MarseilleRules | |
56 | // Small delay for the bot to appear "more human" | |
af32cf62 | 57 | const delay = Math.max(500-(Date.now()-this.timeStart), 0); |
a6088c90 | 58 | setTimeout(() => { |
41cb9b94 | 59 | // NOTE: Dark and 2-moves are incompatible |
6dd02928 | 60 | const animate = (this.gameInfo.vname != "Dark"); |
af32cf62 BA |
61 | const animDelay = (animate ? 250 : 0); |
62 | let moveIdx = 0; | |
63 | let self = this; | |
64 | (function executeMove() { | |
65 | self.$refs.basegame.play(compMove[moveIdx++], animate); | |
66 | if (moveIdx >= compMove.length) | |
67 | { | |
68 | self.compThink = false; | |
69 | if (self.game.score != "*") //user action | |
70 | self.$emit("game-stopped"); | |
71 | } | |
72 | else | |
73 | setTimeout(executeMove, 500 + animDelay); | |
74 | })(); | |
a6088c90 BA |
75 | }, delay); |
76 | } | |
834c202a | 77 | if (!!this.gameInfo.fen) |
b7c32f1a | 78 | this.launchGame(); |
a6088c90 BA |
79 | }, |
80 | // dans variant.js (plutôt room.js) conn gère aussi les challenges | |
81 | // et les chats dans chat.js. Puis en webRTC, repenser tout ça. | |
82 | methods: { | |
83 | launchGame: async function() { | |
6dd02928 | 84 | const vModule = await import("@/variants/" + this.gameInfo.vname + ".js"); |
a6088c90 | 85 | window.V = vModule.VariantRules; |
6dd02928 | 86 | this.compWorker.postMessage(["scripts",this.gameInfo.vname]); |
834c202a BA |
87 | this.compWorker.postMessage(["init",this.gameInfo.fen]); |
88 | this.vr = new V(this.gameInfo.fen); | |
89 | const mycolor = (Math.random() < 0.5 ? "w" : "b"); | |
41cb9b94 | 90 | let players = [{name:"Myself"},{name:"Computer"}]; |
834c202a BA |
91 | if (mycolor == "b") |
92 | players = players.reverse(); | |
41cb9b94 | 93 | // NOTE: fen and fenStart are redundant in game object |
834c202a BA |
94 | this.game = Object.assign({}, |
95 | this.gameInfo, | |
96 | { | |
97 | fenStart: this.gameInfo.fen, | |
98 | players: players, | |
99 | mycolor: mycolor, | |
2c6cb25e | 100 | score: "*", |
834c202a BA |
101 | }); |
102 | this.compWorker.postMessage(["init",this.gameInfo.fen]); | |
103 | if (mycolor != "w" || this.gameInfo.mode == "auto") | |
37cdcbf3 | 104 | this.playComputerMove(); |
a6088c90 BA |
105 | }, |
106 | playComputerMove: function() { | |
107 | this.timeStart = Date.now(); | |
41cb9b94 | 108 | this.compThink = true; |
a6088c90 BA |
109 | this.compWorker.postMessage(["askmove"]); |
110 | }, | |
1acda11c BA |
111 | processMove: function(move) { |
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 | }, | |
93d1d7a7 | 121 | gameOver: function(score) { |
41cb9b94 | 122 | this.game.score = score; |
834c202a | 123 | this.game.mode = "analyze"; |
41cb9b94 | 124 | this.$emit("game-over", score); //bubble up to Rules.vue |
834c202a | 125 | }, |
a6088c90 BA |
126 | }, |
127 | }; | |
128 | </script> |