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