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 | |
b988c726 BA |
4 | input#modalAbort.modal(type="checkbox") |
5 | div(role="dialog" aria-labelledby="abortBoxTitle") | |
6 | .card.smallpad.small-modal.text-center | |
7 | label.modal-close(for="modalAbort") | |
8 | h3#abortBoxTitle.section {{ st.tr["Terminate game?"] }} | |
9 | button(@click="abortGame") {{ st.tr["Sorry I have to go"] }} | |
10 | button(@click="abortGame") {{ st.tr["Game seems over"] }} | |
11 | button(@click="abortGame") {{ st.tr["Game is too boring"] }} | |
b4fb1612 BA |
12 | BaseGame(:game="game" :vr="vr" ref="basegame" |
13 | @newmove="processMove" @gameover="gameOver") | |
6fba6e0c | 14 | // TODO: also show players names |
809ba2aa | 15 | div Time: {{ virtualClocks[0] }} - {{ virtualClocks[1] }} |
d4036efe | 16 | .button-group(v-if="game.mode!='analyze' && game.score=='*'") |
a6088c90 | 17 | button(@click="offerDraw") Draw |
b988c726 | 18 | button(@click="() => abortGame()") Abort |
a6088c90 | 19 | button(@click="resign") Resign |
6dd02928 | 20 | div(v-if="game.mode=='corr'") |
4b0384fa | 21 | textarea(v-show="score=='*' && vr.turn==game.mycolor" v-model="corrMsg") |
a6088c90 BA |
22 | div(v-show="cursor>=0") {{ moves[cursor].message }} |
23 | </template> | |
24 | ||
9aa229f3 BA |
25 | <!-- |
26 | // TODO: movelist dans basegame et chat ici | |
9aa229f3 BA |
27 | // ==> après, implémenter/vérifier les passages de challenges + parties en cours |
28 | // observer, | |
29 | // + problèmes, habiller et publier. (+ corr...) | |
6fba6e0c | 30 | // when send to chat (or a move), reach only this group (send gid along) |
9aa229f3 BA |
31 | --> |
32 | ||
a6088c90 | 33 | <script> |
46284a2f | 34 | import BaseGame from "@/components/BaseGame.vue"; |
a6088c90 BA |
35 | //import Chat from "@/components/Chat.vue"; |
36 | //import MoveList from "@/components/MoveList.vue"; | |
37 | import { store } from "@/store"; | |
967a2686 | 38 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 39 | import { ppt } from "@/utils/datetime"; |
66d03f23 | 40 | import { extractTime } from "@/utils/timeControl"; |
a6088c90 BA |
41 | |
42 | export default { | |
43 | name: 'my-game', | |
44 | components: { | |
45 | BaseGame, | |
46 | }, | |
f7121527 | 47 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
48 | data: function() { |
49 | return { | |
50 | st: store.state, | |
4b0384fa BA |
51 | gameRef: { //given in URL (rid = remote ID) |
52 | id: "", | |
53 | rid: "" | |
54 | }, | |
92a523d1 | 55 | game: {}, //passed to BaseGame |
6fba6e0c | 56 | corrMsg: "", //to send offline messages in corr games |
809ba2aa | 57 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 58 | vr: null, //"variant rules" object initialized from FEN |
6fba6e0c | 59 | drawOffer: "", //TODO: use for button style |
92a523d1 | 60 | people: [], //players + observers |
a6088c90 BA |
61 | }; |
62 | }, | |
63 | watch: { | |
f7121527 | 64 | '$route' (to, from) { |
4fe5664d BA |
65 | if (!!to.params["id"]) |
66 | { | |
67 | this.gameRef.id = to.params["id"]; | |
68 | this.gameRef.rid = to.query["rid"]; | |
69 | this.loadGame(); | |
70 | } | |
a6088c90 | 71 | }, |
5b87454c | 72 | "game.clocks": function(newState) { |
dce792f6 BA |
73 | if (this.game.moves.length < 2) |
74 | { | |
75 | // 1st move not completed yet: freeze time | |
76 | this.virtualClocks = newState.map(s => ppt(s)); | |
77 | return; | |
78 | } | |
809ba2aa | 79 | const currentTurn = this.vr.turn; |
6fba6e0c | 80 | const colorIdx = ["w","b"].indexOf(currentTurn); |
809ba2aa BA |
81 | let countdown = newState[colorIdx] - |
82 | (Date.now() - this.game.initime[colorIdx])/1000; | |
c0b27606 BA |
83 | this.virtualClocks = [0,1].map(i => { |
84 | const removeTime = i == colorIdx | |
85 | ? (Date.now() - this.game.initime[colorIdx])/1000 | |
86 | : 0; | |
87 | return ppt(newState[i] - removeTime); | |
88 | }); | |
809ba2aa | 89 | const myTurn = (currentTurn == this.game.mycolor); |
809ba2aa | 90 | let clockUpdate = setInterval(() => { |
d18bfa12 | 91 | if (countdown <= 0 || this.vr.turn != currentTurn || this.game.score != "*") |
809ba2aa BA |
92 | { |
93 | clearInterval(clockUpdate); | |
94 | if (countdown <= 0 && myTurn) | |
95 | { | |
96 | this.$refs["basegame"].endGame( | |
97 | this.game.mycolor=="w" ? "0-1" : "1-0", "Time"); | |
6fba6e0c BA |
98 | this.st.conn.send(JSON.stringify({ |
99 | code: "timeover", | |
100 | target: this.game.oppid, | |
101 | })); | |
809ba2aa BA |
102 | } |
103 | } | |
9aa229f3 BA |
104 | else |
105 | { | |
106 | // TODO: with Vue 3, just do this.virtualClocks[colorIdx] = ppt(--countdown) | |
107 | this.$set(this.virtualClocks, colorIdx, ppt(Math.max(0, --countdown))); | |
108 | } | |
809ba2aa | 109 | }, 1000); |
5b87454c | 110 | }, |
fd7aea36 BA |
111 | // In case variants array was't loaded when game was retrieved |
112 | "st.variants": function(variantArray) { | |
113 | if (!!this.game.vname && this.game.vname == "") | |
114 | this.game.vname = variantArray.filter(v => v.id == this.game.vid)[0].name; | |
115 | }, | |
a6088c90 | 116 | }, |
92a523d1 BA |
117 | computed: { |
118 | oppConnected: function() { | |
119 | return this.people.indexOf(p => p.id == this.game.oppid) >= 0; | |
120 | }, | |
121 | }, | |
a6088c90 | 122 | created: function() { |
f7121527 | 123 | if (!!this.$route.params["id"]) |
a6088c90 | 124 | { |
f7121527 BA |
125 | this.gameRef.id = this.$route.params["id"]; |
126 | this.gameRef.rid = this.$route.query["rid"]; | |
b196f8ea | 127 | this.loadGame(); |
f7121527 | 128 | } |
cdb34c93 BA |
129 | // TODO: onopen, ask lastState informations + update observers and players status |
130 | const socketCloseListener = () => { | |
131 | store.socketCloseListener(); //reinitialize connexion (in store.js) | |
a9b131f1 | 132 | this.st.conn.addEventListener('message', this.socketMessageListener); |
cdb34c93 BA |
133 | this.st.conn.addEventListener('close', socketCloseListener); |
134 | }; | |
135 | this.st.conn.onmessage = this.socketMessageListener; | |
136 | this.st.conn.onclose = socketCloseListener; | |
137 | }, | |
138 | methods: { | |
139 | socketMessageListener: function(msg) { | |
a6088c90 | 140 | const data = JSON.parse(msg.data); |
a6088c90 BA |
141 | switch (data.code) |
142 | { | |
f7121527 | 143 | case "newmove": |
c4f91d3f BA |
144 | // NOTE: next call will trigger processMove() |
145 | this.$refs["basegame"].play(data.move, | |
146 | "receive", this.game.vname!="Dark" ? "animate" : null); | |
a6088c90 BA |
147 | break; |
148 | case "pong": //received if we sent a ping (game still alive on our side) | |
6fba6e0c | 149 | { |
a6088c90 | 150 | this.oppConnected = true; |
411d23cd BA |
151 | if (this.game.type == "live") //corr games are always complete |
152 | { | |
153 | // Send our "last state" informations to opponent(s) | |
154 | const L = this.game.moves.length; | |
155 | this.st.conn.send(JSON.stringify({ | |
156 | code: "lastate", | |
157 | target: this.game.oppid, | |
158 | gameId: this.gameRef.id, | |
159 | lastMove: (L>0 ? this.game.moves[L-1] : undefined), | |
160 | score: this.game.score, | |
161 | movesCount: L, | |
162 | drawOffer: this.drawOffer, | |
163 | clocks: this.game.clocks, | |
164 | })); | |
165 | } | |
a6088c90 | 166 | break; |
6fba6e0c | 167 | } |
a6088c90 | 168 | case "lastate": //got opponent infos about last move |
6fba6e0c BA |
169 | { |
170 | const L = this.game.moves.length; | |
a6088c90 BA |
171 | if (this.gameRef.id != data.gameId) |
172 | break; //games IDs don't match: nothing we can do... | |
173 | // OK, opponent still in game (which might be over) | |
6fba6e0c | 174 | if (data.movesCount > L) |
a6088c90 | 175 | { |
6fba6e0c BA |
176 | // Just got last move from him |
177 | this.$refs["basegame"].play(data.lastMove, "receive"); | |
178 | if (data.score != "*" && this.game.score == "*") | |
179 | { | |
180 | // Opponent resigned or aborted game, or accepted draw offer | |
181 | // (this is not a stalemate or checkmate) | |
182 | this.$refs["basegame"].endGame(data.score, "Opponent action"); | |
183 | } | |
184 | this.game.clocks = data.clocks; | |
185 | this.drawOffer = data.drawOffer; | |
a6088c90 | 186 | } |
a6088c90 BA |
187 | else if (data.movesCount < L) |
188 | { | |
189 | // We must tell last move to opponent | |
4b0384fa | 190 | this.st.conn.send(JSON.stringify({ |
a6088c90 | 191 | code: "lastate", |
6fba6e0c | 192 | target: this.game.oppid, |
a6088c90 | 193 | gameId: this.gameRef.id, |
6fba6e0c BA |
194 | lastMove: (L>0 ? this.game.moves[L-1] : undefined), |
195 | score: this.game.score, | |
a6088c90 | 196 | movesCount: L, |
6fba6e0c BA |
197 | drawOffer: this.drawOffer, |
198 | clocks: this.game.clocks, | |
a6088c90 BA |
199 | })); |
200 | } | |
a6088c90 | 201 | break; |
6fba6e0c | 202 | } |
93d1d7a7 BA |
203 | case "resign": |
204 | this.$refs["basegame"].endGame( | |
205 | this.game.mycolor=="w" ? "1-0" : "0-1", "Resign"); | |
206 | break; | |
207 | case "timeover": | |
208 | this.$refs["basegame"].endGame( | |
209 | this.game.mycolor=="w" ? "1-0" : "0-1", "Time"); | |
a6088c90 | 210 | break; |
93d1d7a7 BA |
211 | case "abort": |
212 | this.$refs["basegame"].endGame("?", "Abort: " + data.msg); | |
213 | break; | |
2cc10cdb BA |
214 | case "draw": |
215 | this.$refs["basegame"].endGame("1/2", "Mutual agreement"); | |
216 | break; | |
217 | case "drawoffer": | |
218 | this.drawOffer = "received"; | |
219 | break; | |
7e1a1fe9 BA |
220 | case "askfullgame": |
221 | // TODO: just give game; observers are listed here anyway: | |
222 | // gameconnect? | |
223 | break; | |
93d1d7a7 BA |
224 | // TODO: drawaccepted (click draw button before sending move ==> draw offer in move) |
225 | // ==> on "newmove", check "drawOffer" field | |
a6088c90 BA |
226 | // TODO: also use (dis)connect info to count online players? |
227 | case "gameconnect": | |
228 | case "gamedisconnect": | |
6fba6e0c BA |
229 | const online = (data.code == "gameconnect"); |
230 | // If this is an opponent ? | |
231 | if (this.game.oppid == data.id) | |
232 | this.oppConnected = true; | |
233 | else | |
a6088c90 | 234 | { |
6fba6e0c BA |
235 | // Or an observer ? |
236 | if (!online) | |
237 | delete this.people[data.id]; | |
a6088c90 | 238 | else |
6fba6e0c | 239 | this.people[data.id] = data.name; |
a6088c90 BA |
240 | } |
241 | break; | |
242 | } | |
cdb34c93 | 243 | }, |
a6088c90 | 244 | offerDraw: function() { |
2cc10cdb | 245 | // TODO: also for corr games |
6fba6e0c BA |
246 | if (this.drawOffer == "received") |
247 | { | |
2cc10cdb | 248 | if (!confirm("Accept draw?")) |
6fba6e0c BA |
249 | return; |
250 | this.st.conn.send(JSON.stringify({code:"draw", target:this.game.oppid})); | |
2cc10cdb BA |
251 | this.$refs["basegame"].endGame("1/2", "Mutual agreement"); |
252 | } | |
6fba6e0c BA |
253 | else if (this.drawOffer == "sent") |
254 | this.drawOffer = ""; | |
255 | else | |
256 | { | |
257 | if (!confirm("Offer draw?")) | |
258 | return; | |
2cc10cdb | 259 | this.st.conn.send(JSON.stringify({code:"drawoffer", target:this.game.oppid})); |
a6088c90 BA |
260 | } |
261 | }, | |
262 | // + conn handling: "draw" message ==> agree for draw (if we have "drawOffered" at true) | |
263 | receiveDrawOffer: function() { | |
264 | //if (...) | |
265 | // TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers" | |
266 | // if accept: send message "draw" | |
267 | }, | |
b988c726 | 268 | abortGame: function(event) { |
9aa229f3 | 269 | let modalBox = document.getElementById("modalAbort"); |
b988c726 BA |
270 | if (!event) |
271 | { | |
272 | // First call show options: | |
b988c726 BA |
273 | modalBox.checked = true; |
274 | } | |
275 | else | |
276 | { | |
9aa229f3 BA |
277 | modalBox.checked = false; //decision made: box disappear |
278 | const message = event.target.innerText; | |
d4036efe | 279 | // Next line will trigger a "gameover" event, bubbling up till here |
9aa229f3 | 280 | this.$refs["basegame"].endGame("?", "Abort: " + message); |
6fba6e0c BA |
281 | this.st.conn.send(JSON.stringify({ |
282 | code: "abort", | |
283 | msg: message, | |
284 | target: this.game.oppid, | |
285 | })); | |
b988c726 | 286 | } |
a6088c90 BA |
287 | }, |
288 | resign: function(e) { | |
289 | if (!confirm("Resign the game?")) | |
290 | return; | |
6fba6e0c BA |
291 | this.st.conn.send(JSON.stringify({ |
292 | code: "resign", | |
293 | target: this.game.oppid, | |
294 | })); | |
93d1d7a7 | 295 | // Next line will trigger a "gameover" event, bubbling up till here |
809ba2aa BA |
296 | this.$refs["basegame"].endGame( |
297 | this.game.mycolor=="w" ? "0-1" : "1-0", "Resign"); | |
a6088c90 | 298 | }, |
967a2686 BA |
299 | // 3 cases for loading a game: |
300 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
301 | // - from server (one correspondance game I play[ed] or not) |
302 | // - from remote peer (one live game I don't play, finished or not) | |
967a2686 BA |
303 | loadGame: function(game) { |
304 | const afterRetrieval = async (game) => { | |
fd7aea36 BA |
305 | // NOTE: variants array might not be available yet, thus the two next lines |
306 | const variantCell = this.st.variants.filter(v => v.id == game.vid); | |
307 | const vname = (variantCell.length > 0 ? variantCell[0].name : ""); | |
308 | if (!game.fen) | |
309 | game.fen = game.fenStart; //game wasn't started | |
c0b27606 | 310 | const gtype = (game.timeControl.indexOf('d') >= 0 ? "corr" : "live"); |
92a523d1 | 311 | const tc = extractTime(game.timeControl); |
c0b27606 BA |
312 | if (gtype == "corr") |
313 | { | |
314 | // corr game: needs to compute the clocks + initime | |
92a523d1 | 315 | game.clocks = [tc.mainTime, tc.mainTime]; |
c0b27606 | 316 | game.initime = [0, 0]; |
92a523d1 BA |
317 | let addTime = [0, 0]; |
318 | for (let i=2; i<game.moves.length; i++) | |
319 | { | |
320 | addTime[i%2] += tc.increment - | |
321 | (game.moves[i].played - game.moves[i-1].played); | |
322 | } | |
323 | for (let i=0; i<=1; i++) | |
324 | game.clocks[i] += addTime[i]; | |
325 | const L = game.moves.length; | |
326 | game.initime[L%2] = game.moves[L-1].played; | |
c0b27606 | 327 | } |
3d55deea BA |
328 | // TODO: this is not really beautiful (uid on corr players...) |
329 | if (gtype == "corr" && game.players[0].color == "b") | |
330 | [ game.players[0], game.players[1] ] = [ game.players[1], game.players[0] ]; | |
331 | const myIdx = game.players.findIndex(p => { | |
332 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
333 | }); | |
92a523d1 | 334 | if (gtype == "live" && game.clocks[0] < 0) //game unstarted |
66d03f23 BA |
335 | { |
336 | game.clocks = [tc.mainTime, tc.mainTime]; | |
337 | game.initime[0] = Date.now(); | |
92a523d1 | 338 | if (myIdx >= 0) |
22efa391 | 339 | { |
cf742aaf | 340 | // I play in this live game; corr games don't have clocks+initime |
d18bfa12 | 341 | GameStorage.update(game.id, |
22efa391 BA |
342 | { |
343 | clocks: game.clocks, | |
344 | initime: game.initime, | |
345 | }); | |
346 | } | |
66d03f23 | 347 | } |
a9b131f1 | 348 | const vModule = await import("@/variants/" + vname + ".js"); |
d4036efe | 349 | window.V = vModule.VariantRules; |
809ba2aa | 350 | this.vr = new V(game.fen); |
d18bfa12 BA |
351 | |
352 | ||
353 | ||
354 | //TODO: people, on connect, search for opponent....... | |
355 | console.log(myIdx + " " + game.players[1-myIdx].sid); //otherwise this is undefined: | |
356 | ||
357 | ||
358 | ||
4b0384fa BA |
359 | this.game = Object.assign({}, |
360 | game, | |
cf742aaf | 361 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 362 | { |
c0b27606 | 363 | type: gtype, |
66d03f23 | 364 | increment: tc.increment, |
a9b131f1 | 365 | vname: vname, |
6fba6e0c BA |
366 | mycolor: [undefined,"w","b"][myIdx+1], |
367 | // opponent sid not strictly required, but easier | |
368 | oppid: (myIdx < 0 ? undefined : game.players[1-myIdx].sid), | |
369 | } | |
4b0384fa | 370 | ); |
6fba6e0c BA |
371 | if (!!this.game.oppid) |
372 | { | |
373 | // Send ping to server (answer pong if players[s] are connected) | |
a36a09c0 | 374 | this.st.conn.send(JSON.stringify({code:"ping", target:this.game.oppid})); |
6fba6e0c | 375 | } |
967a2686 BA |
376 | }; |
377 | if (!!game) | |
378 | return afterRetrival(game); | |
379 | if (!!this.gameRef.rid) | |
380 | { | |
7e1a1fe9 | 381 | this.st.conn.send(JSON.stringify({code:"askfullgame", target:this.gameRef.rid})); |
967a2686 BA |
382 | // TODO: just send a game request message to the remote player, |
383 | // and when receiving answer just call loadGame(received_game) | |
384 | // + remote peer should have registered us as an observer | |
385 | // (send moves updates + resign/abort/draw actions) | |
967a2686 BA |
386 | } |
387 | else | |
388 | { | |
389 | GameStorage.get(this.gameRef.id, async (game) => { | |
390 | afterRetrieval(game); | |
391 | }); | |
392 | } | |
a6088c90 | 393 | }, |
9d54ab89 | 394 | // Post-process a move (which was just played) |
ce87ac6a | 395 | processMove: function(move) { |
b4fb1612 BA |
396 | if (!this.game.mycolor) |
397 | return; //I'm just an observer | |
9d54ab89 | 398 | // Update storage (corr or live) |
6fba6e0c | 399 | const colorIdx = ["w","b"].indexOf(move.color); |
9d54ab89 BA |
400 | // https://stackoverflow.com/a/38750895 |
401 | const allowed_fields = ["appear", "vanish", "start", "end"]; | |
402 | const filtered_move = Object.keys(move) | |
403 | .filter(key => allowed_fields.includes(key)) | |
404 | .reduce((obj, key) => { | |
8a7452b5 | 405 | obj[key] = move[key]; |
9d54ab89 BA |
406 | return obj; |
407 | }, {}); | |
b4fb1612 | 408 | // Send move ("newmove" event) to opponent(s) (if ours) |
dce792f6 | 409 | let addTime = 0; |
9d54ab89 | 410 | if (move.color == this.game.mycolor) |
b4fb1612 | 411 | { |
dce792f6 BA |
412 | if (this.game.moves.length >= 2) //after first move |
413 | { | |
414 | const elapsed = Date.now() - this.game.initime[colorIdx]; | |
415 | // elapsed time is measured in milliseconds | |
416 | addTime = this.game.increment - elapsed/1000; | |
417 | } | |
6fba6e0c BA |
418 | this.st.conn.send(JSON.stringify({ |
419 | code: "newmove", | |
420 | target: this.game.oppid, | |
421 | move: Object.assign({}, filtered_move, {addTime: addTime}), | |
422 | })); | |
b4fb1612 | 423 | } |
5b87454c BA |
424 | else |
425 | addTime = move.addTime; //supposed transmitted | |
6fba6e0c | 426 | const nextIdx = ["w","b"].indexOf(this.vr.turn); |
967a2686 BA |
427 | GameStorage.update(this.gameRef.id, |
428 | { | |
9d54ab89 BA |
429 | move: filtered_move, |
430 | fen: move.fen, | |
22efa391 BA |
431 | clocks: this.game.clocks.map((t,i) => i==colorIdx |
432 | ? this.game.clocks[i] + addTime | |
433 | : this.game.clocks[i]), | |
434 | initime: this.game.initime.map((t,i) => i==nextIdx | |
435 | ? Date.now() | |
436 | : this.game.initime[i]), | |
9d54ab89 | 437 | }); |
967a2686 BA |
438 | // Also update current game object: |
439 | this.game.moves.push(move); | |
440 | this.game.fen = move.fen; | |
66d03f23 BA |
441 | //TODO: just this.game.clocks[colorIdx] += addTime; |
442 | this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime); | |
809ba2aa | 443 | this.game.initime[nextIdx] = Date.now(); |
b4fb1612 | 444 | }, |
93d1d7a7 | 445 | // TODO: this update function should also work for corr games |
b4fb1612 | 446 | gameOver: function(score) { |
93d1d7a7 | 447 | this.game.mode = "analyze"; |
d18bfa12 | 448 | this.game.score = score; |
22efa391 | 449 | GameStorage.update(this.gameRef.id, { score: score }); |
ce87ac6a | 450 | }, |
a6088c90 BA |
451 | }, |
452 | }; | |
453 | </script> | |
7e1a1fe9 BA |
454 | |
455 | <style lang="sass"> | |
456 | // TODO | |
457 | </style> |