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"; | |
11 | import Worker from 'worker-loader!@/playCompMove'; | |
12 | ||
13 | export default { | |
14 | name: 'my-computer-game', | |
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, | |
834c202a | 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 | |
28 | lockCompThink: false, //to avoid some ghost moves | |
a6088c90 | 29 | compWorker: null, |
a6088c90 BA |
30 | }; |
31 | }, | |
a6088c90 | 32 | watch: { |
834c202a | 33 | "gameInfo.fen": function() { |
a6088c90 BA |
34 | // (Security) No effect if a computer move is in progress: |
35 | if (this.lockCompThink) | |
36 | return this.$emit("computer-think"); | |
37 | this.launchGame(); | |
38 | }, | |
834c202a BA |
39 | "gameInfo.userStop": function() { |
40 | if (this.gameInfo.userStop) | |
41 | { | |
42 | // User stopped the game: unknown result | |
43 | this.game.mode = "analyze"; | |
44 | } | |
45 | }, | |
a6088c90 BA |
46 | }, |
47 | // Modal end of game, and then sub-components | |
48 | created: function() { | |
a6088c90 BA |
49 | // Computer moves web worker logic: (TODO: also for observers in HH games ?) |
50 | this.compWorker = new Worker(); //'/javascripts/playCompMove.js'), | |
51 | this.compWorker.onmessage = e => { | |
52 | this.lockCompThink = true; //to avoid some ghost moves | |
53 | let compMove = e.data; | |
54 | if (!Array.isArray(compMove)) | |
55 | compMove = [compMove]; //to deal with MarseilleRules | |
56 | // Small delay for the bot to appear "more human" | |
57 | const delay = Math.max(500-(Date.now()-this.timeStart), 0); | |
58 | setTimeout(() => { | |
6dd02928 | 59 | const animate = (this.gameInfo.vname != "Dark"); |
1acda11c | 60 | this.$refs.basegame.play(compMove[0], animate); |
a6088c90 | 61 | if (compMove.length == 2) |
1acda11c | 62 | setTimeout( () => { this.$refs.basegame.play(compMove[1], animate); }, 750); |
a6088c90 BA |
63 | else //250 == length of animation (TODO: should be a constant somewhere) |
64 | setTimeout( () => this.lockCompThink = false, 250); | |
65 | }, delay); | |
66 | } | |
834c202a | 67 | if (!!this.gameInfo.fen) |
b7c32f1a | 68 | this.launchGame(); |
a6088c90 BA |
69 | }, |
70 | // dans variant.js (plutôt room.js) conn gère aussi les challenges | |
71 | // et les chats dans chat.js. Puis en webRTC, repenser tout ça. | |
72 | methods: { | |
73 | launchGame: async function() { | |
6dd02928 | 74 | const vModule = await import("@/variants/" + this.gameInfo.vname + ".js"); |
a6088c90 | 75 | window.V = vModule.VariantRules; |
6dd02928 | 76 | this.compWorker.postMessage(["scripts",this.gameInfo.vname]); |
834c202a BA |
77 | this.compWorker.postMessage(["init",this.gameInfo.fen]); |
78 | this.vr = new V(this.gameInfo.fen); | |
79 | const mycolor = (Math.random() < 0.5 ? "w" : "b"); | |
80 | let players = ["Myself","Computer"]; | |
81 | if (mycolor == "b") | |
82 | players = players.reverse(); | |
83 | // NOTE: (TODO?) fen and fenStart are redundant in game object | |
84 | this.game = Object.assign({}, | |
85 | this.gameInfo, | |
86 | { | |
87 | fenStart: this.gameInfo.fen, | |
88 | players: players, | |
89 | mycolor: mycolor, | |
90 | }); | |
91 | this.compWorker.postMessage(["init",this.gameInfo.fen]); | |
92 | if (mycolor != "w" || this.gameInfo.mode == "auto") | |
37cdcbf3 | 93 | this.playComputerMove(); |
a6088c90 BA |
94 | }, |
95 | playComputerMove: function() { | |
7667307f BA |
96 | |
97 | console.log("call comp move"); | |
98 | ||
a6088c90 BA |
99 | this.timeStart = Date.now(); |
100 | this.compWorker.postMessage(["askmove"]); | |
101 | }, | |
1acda11c BA |
102 | // TODO: do not process if game is over (check score ?) |
103 | processMove: function(move) { | |
7667307f BA |
104 | console.log("play move"); |
105 | console.log(move); | |
1acda11c BA |
106 | // Send the move to web worker (including his own moves) |
107 | this.compWorker.postMessage(["newmove",move]); | |
108 | // subTurn condition for Marseille (and Avalanche) rules | |
109 | if ((!this.vr.subTurn || this.vr.subTurn <= 1) | |
834c202a | 110 | && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor)) |
1acda11c | 111 | { |
7667307f BA |
112 | |
113 | console.log("ask new comp move"); | |
114 | ||
1acda11c BA |
115 | this.playComputerMove(); |
116 | } | |
117 | }, | |
93d1d7a7 BA |
118 | gameOver: function(score) { |
119 | // Just switch to analyze mode: no user action can set score | |
834c202a BA |
120 | this.game.mode = "analyze"; |
121 | }, | |
a6088c90 BA |
122 | }, |
123 | }; | |
124 | </script> |