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