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