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"] }} | |
656b1878 | 11 | button(@click="abortGame") {{ st.tr["Opponent is gone"] }} |
b4fb1612 BA |
12 | BaseGame(:game="game" :vr="vr" ref="basegame" |
13 | @newmove="processMove" @gameover="gameOver") | |
f41ce580 | 14 | div Names: {{ game.players[0].name }} - {{ game.players[1].name }} |
afd3240d | 15 | div(v-if="game.score=='*'") 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 |
9330b976 | 20 | textarea(v-if="game.score=='*'" v-model="corrMsg") |
5c8e044f | 21 | Chat(:players="game.players") |
a6088c90 BA |
22 | </template> |
23 | ||
24 | <script> | |
46284a2f | 25 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 26 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 27 | import { store } from "@/store"; |
967a2686 | 28 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 29 | import { ppt } from "@/utils/datetime"; |
66d03f23 | 30 | import { extractTime } from "@/utils/timeControl"; |
f41ce580 | 31 | import { ArrayFun } from "@/utils/array"; |
a6088c90 BA |
32 | |
33 | export default { | |
34 | name: 'my-game', | |
35 | components: { | |
36 | BaseGame, | |
5c8e044f | 37 | Chat, |
a6088c90 | 38 | }, |
f7121527 | 39 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
40 | data: function() { |
41 | return { | |
42 | st: store.state, | |
4b0384fa BA |
43 | gameRef: { //given in URL (rid = remote ID) |
44 | id: "", | |
45 | rid: "" | |
46 | }, | |
f41ce580 | 47 | game: {players:[{name:""},{name:""}]}, //passed to BaseGame |
6fba6e0c | 48 | corrMsg: "", //to send offline messages in corr games |
809ba2aa | 49 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 50 | vr: null, //"variant rules" object initialized from FEN |
6fba6e0c | 51 | drawOffer: "", //TODO: use for button style |
92a523d1 | 52 | people: [], //players + observers |
a6088c90 BA |
53 | }; |
54 | }, | |
55 | watch: { | |
5f131484 BA |
56 | "$route": function(to, from) { |
57 | this.gameRef.id = to.params["id"]; | |
58 | this.gameRef.rid = to.query["rid"]; | |
59 | this.loadGame(); | |
a6088c90 | 60 | }, |
5b87454c | 61 | "game.clocks": function(newState) { |
dce792f6 BA |
62 | if (this.game.moves.length < 2) |
63 | { | |
64 | // 1st move not completed yet: freeze time | |
65 | this.virtualClocks = newState.map(s => ppt(s)); | |
66 | return; | |
67 | } | |
809ba2aa | 68 | const currentTurn = this.vr.turn; |
6fba6e0c | 69 | const colorIdx = ["w","b"].indexOf(currentTurn); |
809ba2aa BA |
70 | let countdown = newState[colorIdx] - |
71 | (Date.now() - this.game.initime[colorIdx])/1000; | |
c0b27606 BA |
72 | this.virtualClocks = [0,1].map(i => { |
73 | const removeTime = i == colorIdx | |
74 | ? (Date.now() - this.game.initime[colorIdx])/1000 | |
75 | : 0; | |
76 | return ppt(newState[i] - removeTime); | |
77 | }); | |
809ba2aa | 78 | let clockUpdate = setInterval(() => { |
e69f159d | 79 | if (countdown < 0 || this.vr.turn != currentTurn || this.game.score != "*") |
809ba2aa BA |
80 | { |
81 | clearInterval(clockUpdate); | |
e69f159d | 82 | if (countdown < 0) |
809ba2aa BA |
83 | { |
84 | this.$refs["basegame"].endGame( | |
e69f159d | 85 | this.vr.turn=="w" ? "0-1" : "1-0", "Time"); |
809ba2aa BA |
86 | } |
87 | } | |
9aa229f3 BA |
88 | else |
89 | { | |
90 | // TODO: with Vue 3, just do this.virtualClocks[colorIdx] = ppt(--countdown) | |
91 | this.$set(this.virtualClocks, colorIdx, ppt(Math.max(0, --countdown))); | |
92 | } | |
809ba2aa | 93 | }, 1000); |
5b87454c | 94 | }, |
92a523d1 | 95 | }, |
5f131484 | 96 | // TODO: redundant code with Hall.vue (related to people array) |
a6088c90 | 97 | created: function() { |
5f131484 BA |
98 | // Always add myself to players' list |
99 | const my = this.st.user; | |
100 | this.people.push({sid:my.sid, id:my.id, name:my.name}); | |
dc284d90 BA |
101 | this.gameRef.id = this.$route.params["id"]; |
102 | this.gameRef.rid = this.$route.query["rid"]; //may be undefined | |
103 | if (!this.gameRef.rid) | |
104 | this.loadGame(); //local or corr: can load from now | |
afd3240d BA |
105 | // TODO: mode analyse (/analyze/Atomic/rn |
106 | // ... fen = query[], vname=params[] ... | |
5f131484 | 107 | // 0.1] Ask server for room composition: |
dc284d90 BA |
108 | const initialize = () => { |
109 | // Poll clients + load game if stored remotely | |
5f131484 | 110 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
dc284d90 BA |
111 | if (!!this.gameRef.rid) |
112 | this.loadGame(); | |
5f131484 BA |
113 | }; |
114 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state | |
dc284d90 | 115 | initialize(); |
5f131484 | 116 | else //socket not ready yet (initial loading) |
dc284d90 | 117 | this.st.conn.onopen = initialize; |
5f131484 | 118 | this.st.conn.onmessage = this.socketMessageListener; |
cdb34c93 BA |
119 | const socketCloseListener = () => { |
120 | store.socketCloseListener(); //reinitialize connexion (in store.js) | |
a9b131f1 | 121 | this.st.conn.addEventListener('message', this.socketMessageListener); |
cdb34c93 BA |
122 | this.st.conn.addEventListener('close', socketCloseListener); |
123 | }; | |
cdb34c93 BA |
124 | this.st.conn.onclose = socketCloseListener; |
125 | }, | |
126 | methods: { | |
5f131484 BA |
127 | getOppSid: function() { |
128 | if (!!this.game.oppsid) | |
129 | return this.game.oppsid; | |
130 | const opponent = this.people.find(p => p.id == this.game.oppid); | |
131 | return (!!opponent ? opponent.sid : null); | |
132 | }, | |
cdb34c93 | 133 | socketMessageListener: function(msg) { |
a6088c90 | 134 | const data = JSON.parse(msg.data); |
a6088c90 BA |
135 | switch (data.code) |
136 | { | |
5f131484 BA |
137 | // 0.2] Receive clients list (just socket IDs) |
138 | case "pollclients": | |
139 | { | |
140 | data.sockIds.forEach(sid => { | |
141 | this.people.push({sid:sid, id:0, name:""}); | |
142 | // Ask only identity | |
143 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); | |
144 | }); | |
145 | break; | |
146 | } | |
147 | case "askidentity": | |
148 | { | |
149 | // Request for identification: reply if I'm not anonymous | |
150 | if (this.st.user.id > 0) | |
151 | { | |
152 | this.st.conn.send(JSON.stringify( | |
153 | // people[0] instead of st.user to avoid sending email | |
154 | {code:"identity", user:this.people[0], target:data.from})); | |
155 | } | |
156 | break; | |
157 | } | |
158 | case "identity": | |
159 | { | |
f41ce580 | 160 | let player = this.people.find(p => p.sid == data.user.sid); |
cd0d7743 BA |
161 | // NOTE: sometimes player.id fails because player is undefined... |
162 | // Probably because the event was meant for Hall? | |
163 | if (!player) | |
164 | return; | |
f41ce580 BA |
165 | player.id = data.user.id; |
166 | player.name = data.user.name; | |
167 | // Sending last state only for live games: corr games are complete | |
168 | if (this.game.type == "live" && this.game.oppsid == player.sid) | |
411d23cd | 169 | { |
f41ce580 | 170 | // Send our "last state" informations to opponent |
411d23cd BA |
171 | const L = this.game.moves.length; |
172 | this.st.conn.send(JSON.stringify({ | |
173 | code: "lastate", | |
f41ce580 BA |
174 | target: player.sid, |
175 | state: | |
176 | { | |
177 | lastMove: (L>0 ? this.game.moves[L-1] : undefined), | |
178 | score: this.game.score, | |
179 | movesCount: L, | |
180 | drawOffer: this.drawOffer, | |
181 | clocks: this.game.clocks, | |
182 | } | |
411d23cd BA |
183 | })); |
184 | } | |
a6088c90 | 185 | break; |
6fba6e0c | 186 | } |
c6788ecf BA |
187 | case "askgame": |
188 | // Send current (live) game | |
189 | const myGame = | |
190 | { | |
191 | // Minimal game informations: | |
192 | id: this.game.id, | |
ab6f48ea | 193 | players: this.game.players.map(p => { return {name:p.name}; }), |
c6788ecf BA |
194 | vid: this.game.vid, |
195 | timeControl: this.game.timeControl, | |
196 | }; | |
197 | this.st.conn.send(JSON.stringify({code:"game", | |
198 | game:myGame, target:data.from})); | |
199 | break; | |
f41ce580 BA |
200 | case "newmove": |
201 | // NOTE: this call to play() will trigger processMove() | |
202 | this.$refs["basegame"].play(data.move, | |
203 | "receive", this.game.vname!="Dark" ? "animate" : null); | |
204 | break; | |
a6088c90 | 205 | case "lastate": //got opponent infos about last move |
6fba6e0c BA |
206 | { |
207 | const L = this.game.moves.length; | |
6fba6e0c | 208 | if (data.movesCount > L) |
a6088c90 | 209 | { |
6fba6e0c | 210 | // Just got last move from him |
f41ce580 BA |
211 | this.$refs["basegame"].play(data.lastMove, |
212 | "receive", this.game.vname!="Dark" ? "animate" : null); | |
6fba6e0c BA |
213 | if (data.score != "*" && this.game.score == "*") |
214 | { | |
215 | // Opponent resigned or aborted game, or accepted draw offer | |
216 | // (this is not a stalemate or checkmate) | |
217 | this.$refs["basegame"].endGame(data.score, "Opponent action"); | |
218 | } | |
f41ce580 BA |
219 | this.game.clocks = data.clocks; //TODO: check this? |
220 | this.drawOffer = data.drawOffer; //does opponent offer draw? | |
a6088c90 | 221 | } |
a6088c90 | 222 | break; |
6fba6e0c | 223 | } |
93d1d7a7 BA |
224 | case "resign": |
225 | this.$refs["basegame"].endGame( | |
226 | this.game.mycolor=="w" ? "1-0" : "0-1", "Resign"); | |
227 | break; | |
93d1d7a7 BA |
228 | case "abort": |
229 | this.$refs["basegame"].endGame("?", "Abort: " + data.msg); | |
230 | break; | |
2cc10cdb BA |
231 | case "draw": |
232 | this.$refs["basegame"].endGame("1/2", "Mutual agreement"); | |
233 | break; | |
234 | case "drawoffer": | |
235 | this.drawOffer = "received"; | |
236 | break; | |
7e1a1fe9 | 237 | case "askfullgame": |
dc284d90 BA |
238 | // TODO: use data.id to retrieve game in indexedDB (but for now only one running game so OK) |
239 | this.st.conn.send(JSON.stringify({code:"fullgame", game:this.game, target:data.from})); | |
7e1a1fe9 | 240 | break; |
5f131484 | 241 | case "fullgame": |
5f131484 BA |
242 | this.loadGame(data.game); |
243 | break; | |
244 | // TODO: drawaccepted (click draw button before sending move | |
245 | // ==> draw offer in move) | |
246 | // ==> on "newmove", check "drawOffer" field | |
247 | case "connect": | |
248 | { | |
c6788ecf BA |
249 | this.people.push({name:"", id:0, sid:data.from}); |
250 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); | |
5f131484 BA |
251 | break; |
252 | } | |
253 | case "disconnect": | |
c6788ecf | 254 | ArrayFun.remove(this.people, p => p.sid == data.from); |
a6088c90 BA |
255 | break; |
256 | } | |
cdb34c93 | 257 | }, |
a6088c90 | 258 | offerDraw: function() { |
2cc10cdb | 259 | // TODO: also for corr games |
6fba6e0c BA |
260 | if (this.drawOffer == "received") |
261 | { | |
2cc10cdb | 262 | if (!confirm("Accept draw?")) |
6fba6e0c | 263 | return; |
5f131484 BA |
264 | const oppsid = this.getOppSid(); |
265 | if (!!oppsid) | |
266 | this.st.conn.send(JSON.stringify({code:"draw", target:oppsid})); | |
2cc10cdb BA |
267 | this.$refs["basegame"].endGame("1/2", "Mutual agreement"); |
268 | } | |
6fba6e0c BA |
269 | else if (this.drawOffer == "sent") |
270 | this.drawOffer = ""; | |
271 | else | |
272 | { | |
273 | if (!confirm("Offer draw?")) | |
274 | return; | |
5f131484 BA |
275 | const oppsid = this.getOppSid(); |
276 | if (!!oppsid) | |
277 | this.st.conn.send(JSON.stringify({code:"drawoffer", target:oppsid})); | |
a6088c90 BA |
278 | } |
279 | }, | |
280 | // + conn handling: "draw" message ==> agree for draw (if we have "drawOffered" at true) | |
281 | receiveDrawOffer: function() { | |
282 | //if (...) | |
283 | // TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers" | |
284 | // if accept: send message "draw" | |
285 | }, | |
b988c726 | 286 | abortGame: function(event) { |
9aa229f3 | 287 | let modalBox = document.getElementById("modalAbort"); |
b988c726 BA |
288 | if (!event) |
289 | { | |
290 | // First call show options: | |
b988c726 BA |
291 | modalBox.checked = true; |
292 | } | |
293 | else | |
294 | { | |
9aa229f3 BA |
295 | modalBox.checked = false; //decision made: box disappear |
296 | const message = event.target.innerText; | |
d4036efe | 297 | // Next line will trigger a "gameover" event, bubbling up till here |
9aa229f3 | 298 | this.$refs["basegame"].endGame("?", "Abort: " + message); |
5f131484 BA |
299 | const oppsid = this.getOppSid(); |
300 | if (!!oppsid) | |
301 | { | |
302 | this.st.conn.send(JSON.stringify({ | |
303 | code: "abort", | |
304 | msg: message, | |
305 | target: oppsid, | |
306 | })); | |
307 | } | |
b988c726 | 308 | } |
a6088c90 BA |
309 | }, |
310 | resign: function(e) { | |
311 | if (!confirm("Resign the game?")) | |
312 | return; | |
5f131484 BA |
313 | const oppsid = this.getOppSid(); |
314 | if (!!oppsid) | |
315 | { | |
316 | this.st.conn.send(JSON.stringify({ | |
317 | code: "resign", | |
318 | target: oppsid, | |
319 | })); | |
320 | } | |
93d1d7a7 | 321 | // Next line will trigger a "gameover" event, bubbling up till here |
809ba2aa BA |
322 | this.$refs["basegame"].endGame( |
323 | this.game.mycolor=="w" ? "0-1" : "1-0", "Resign"); | |
a6088c90 | 324 | }, |
967a2686 BA |
325 | // 3 cases for loading a game: |
326 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
327 | // - from server (one correspondance game I play[ed] or not) |
328 | // - from remote peer (one live game I don't play, finished or not) | |
967a2686 BA |
329 | loadGame: function(game) { |
330 | const afterRetrieval = async (game) => { | |
f41ce580 BA |
331 | const vModule = await import("@/variants/" + game.vname + ".js"); |
332 | window.V = vModule.VariantRules; | |
333 | this.vr = new V(game.fen); | |
c0b27606 | 334 | const gtype = (game.timeControl.indexOf('d') >= 0 ? "corr" : "live"); |
92a523d1 | 335 | const tc = extractTime(game.timeControl); |
c0b27606 BA |
336 | if (gtype == "corr") |
337 | { | |
f41ce580 BA |
338 | if (game.players[0].color == "b") |
339 | { | |
340 | // Adopt the same convention for live and corr games: [0] = white | |
341 | [ game.players[0], game.players[1] ] = | |
342 | [ game.players[1], game.players[0] ]; | |
343 | } | |
c0b27606 | 344 | // corr game: needs to compute the clocks + initime |
92a523d1 | 345 | game.clocks = [tc.mainTime, tc.mainTime]; |
c0b27606 | 346 | game.initime = [0, 0]; |
5f131484 | 347 | const L = game.moves.length; |
92b82def | 348 | game.moves.sort((m1,m2) => m1.idx - m2.idx); //in case of |
5f131484 | 349 | if (L >= 3) |
92a523d1 | 350 | { |
5f131484 BA |
351 | let addTime = [0, 0]; |
352 | for (let i=2; i<L; i++) | |
353 | { | |
354 | addTime[i%2] += tc.increment - | |
355 | (game.moves[i].played - game.moves[i-1].played); | |
356 | } | |
357 | for (let i=0; i<=1; i++) | |
358 | game.clocks[i] += addTime[i]; | |
92a523d1 | 359 | } |
5f131484 BA |
360 | if (L >= 1) |
361 | game.initime[L%2] = game.moves[L-1].played; | |
92b82def | 362 | // Now that we used idx and played, re-format moves as for live games |
6d68309a | 363 | game.moves = game.moves.map( (m) => { |
92b82def | 364 | const s = m.squares; |
6d68309a | 365 | return { |
92b82def BA |
366 | appear: s.appear, |
367 | vanish: s.vanish, | |
368 | start: s.start, | |
369 | end: s.end, | |
370 | message: m.message, | |
371 | }; | |
372 | }); | |
c0b27606 | 373 | } |
f41ce580 BA |
374 | const myIdx = game.players.findIndex(p => { |
375 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
376 | }); | |
92a523d1 | 377 | if (gtype == "live" && game.clocks[0] < 0) //game unstarted |
66d03f23 BA |
378 | { |
379 | game.clocks = [tc.mainTime, tc.mainTime]; | |
380 | game.initime[0] = Date.now(); | |
92a523d1 | 381 | if (myIdx >= 0) |
22efa391 | 382 | { |
cf742aaf | 383 | // I play in this live game; corr games don't have clocks+initime |
d18bfa12 | 384 | GameStorage.update(game.id, |
22efa391 BA |
385 | { |
386 | clocks: game.clocks, | |
387 | initime: game.initime, | |
388 | }); | |
389 | } | |
66d03f23 | 390 | } |
4b0384fa BA |
391 | this.game = Object.assign({}, |
392 | game, | |
cf742aaf | 393 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 394 | { |
c0b27606 | 395 | type: gtype, |
66d03f23 | 396 | increment: tc.increment, |
6fba6e0c | 397 | mycolor: [undefined,"w","b"][myIdx+1], |
5f131484 BA |
398 | // opponent sid not strictly required (or available), but easier |
399 | // at least oppsid or oppid is available anyway: | |
400 | oppsid: (myIdx < 0 ? undefined : game.players[1-myIdx].sid), | |
401 | oppid: (myIdx < 0 ? undefined : game.players[1-myIdx].uid), | |
6fba6e0c | 402 | } |
4b0384fa | 403 | ); |
967a2686 BA |
404 | }; |
405 | if (!!game) | |
dc284d90 | 406 | return afterRetrieval(game); |
967a2686 BA |
407 | if (!!this.gameRef.rid) |
408 | { | |
f41ce580 | 409 | // Remote live game |
dc284d90 BA |
410 | // (TODO: send game ID as well, and receiver would pick the corresponding |
411 | // game in his current games; if allowed to play several) | |
5f131484 BA |
412 | this.st.conn.send(JSON.stringify( |
413 | {code:"askfullgame", target:this.gameRef.rid})); | |
967a2686 | 414 | // (send moves updates + resign/abort/draw actions) |
967a2686 BA |
415 | } |
416 | else | |
417 | { | |
f41ce580 | 418 | // Local or corr game |
11667c79 | 419 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 420 | } |
a6088c90 | 421 | }, |
9d54ab89 | 422 | // Post-process a move (which was just played) |
ce87ac6a | 423 | processMove: function(move) { |
b4fb1612 BA |
424 | if (!this.game.mycolor) |
425 | return; //I'm just an observer | |
9d54ab89 | 426 | // Update storage (corr or live) |
6fba6e0c | 427 | const colorIdx = ["w","b"].indexOf(move.color); |
9d54ab89 BA |
428 | // https://stackoverflow.com/a/38750895 |
429 | const allowed_fields = ["appear", "vanish", "start", "end"]; | |
430 | const filtered_move = Object.keys(move) | |
431 | .filter(key => allowed_fields.includes(key)) | |
432 | .reduce((obj, key) => { | |
8a7452b5 | 433 | obj[key] = move[key]; |
9d54ab89 BA |
434 | return obj; |
435 | }, {}); | |
dc284d90 | 436 | // Send move ("newmove" event) to people in the room (if our turn) |
dce792f6 | 437 | let addTime = 0; |
9d54ab89 | 438 | if (move.color == this.game.mycolor) |
b4fb1612 | 439 | { |
dce792f6 BA |
440 | if (this.game.moves.length >= 2) //after first move |
441 | { | |
442 | const elapsed = Date.now() - this.game.initime[colorIdx]; | |
443 | // elapsed time is measured in milliseconds | |
444 | addTime = this.game.increment - elapsed/1000; | |
445 | } | |
6d68309a BA |
446 | let sendMove = Object.assign({}, filtered_move, {addTime: addTime}); |
447 | if (this.game.type == "corr") | |
448 | sendMove.message = this.corrMsg; | |
a4480041 | 449 | const oppsid = this.getOppSid(); |
dc284d90 BA |
450 | this.people.forEach(p => { |
451 | if (p.sid != this.st.user.sid) | |
452 | { | |
453 | this.st.conn.send(JSON.stringify({ | |
454 | code: "newmove", | |
455 | target: p.sid, | |
456 | move: sendMove, | |
457 | })); | |
458 | } | |
459 | }); | |
a4480041 BA |
460 | if (this.game.type == "corr" && this.corrMsg != "") |
461 | { | |
462 | // Add message to last move in BaseGame: | |
463 | // TODO: not very good style... | |
464 | this.$refs["basegame"].setCurrentMessage(this.corrMsg); | |
465 | } | |
b4fb1612 | 466 | } |
5b87454c BA |
467 | else |
468 | addTime = move.addTime; //supposed transmitted | |
6fba6e0c | 469 | const nextIdx = ["w","b"].indexOf(this.vr.turn); |
6d68309a BA |
470 | // Since corr games are stored at only one location, update should be |
471 | // done only by one player for each move: | |
472 | if (this.game.type == "live" || move.color == this.game.mycolor) | |
967a2686 | 473 | { |
e69f159d | 474 | if (this.game.type == "corr") |
f41ce580 | 475 | { |
e69f159d | 476 | GameStorage.update(this.gameRef.id, |
6d68309a | 477 | { |
e69f159d BA |
478 | fen: move.fen, |
479 | move: | |
480 | { | |
481 | squares: filtered_move, | |
482 | message: this.corrMsg, | |
483 | played: Date.now(), //TODO: on server? | |
484 | idx: this.game.moves.length, | |
485 | }, | |
486 | }); | |
487 | } | |
488 | else //live | |
489 | { | |
490 | GameStorage.update(this.gameRef.id, | |
491 | { | |
492 | fen: move.fen, | |
493 | move: filtered_move, | |
494 | clocks: this.game.clocks.map((t,i) => i==colorIdx | |
495 | ? this.game.clocks[i] + addTime | |
496 | : this.game.clocks[i]), | |
497 | initime: this.game.initime.map((t,i) => i==nextIdx | |
498 | ? Date.now() | |
499 | : this.game.initime[i]), | |
500 | }); | |
501 | } | |
6d68309a | 502 | } |
967a2686 BA |
503 | // Also update current game object: |
504 | this.game.moves.push(move); | |
505 | this.game.fen = move.fen; | |
66d03f23 BA |
506 | //TODO: just this.game.clocks[colorIdx] += addTime; |
507 | this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime); | |
809ba2aa | 508 | this.game.initime[nextIdx] = Date.now(); |
6d68309a BA |
509 | // Finally reset curMoveMessage if needed |
510 | if (this.game.type == "corr" && move.color == this.game.mycolor) | |
511 | this.corrMsg = ""; | |
b4fb1612 BA |
512 | }, |
513 | gameOver: function(score) { | |
93d1d7a7 | 514 | this.game.mode = "analyze"; |
d18bfa12 | 515 | this.game.score = score; |
ab6f48ea BA |
516 | const myIdx = this.game.players.findIndex(p => { |
517 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
518 | }); | |
519 | if (myIdx >= 0) //OK, I play in this game | |
520 | GameStorage.update(this.gameRef.id, { score: score }); | |
ce87ac6a | 521 | }, |
a6088c90 BA |
522 | }, |
523 | }; | |
524 | </script> | |
7e1a1fe9 BA |
525 | |
526 | <style lang="sass"> | |
527 | // TODO | |
528 | </style> |