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 |
7aa548e7 | 5 | Chat(:players="game.players") |
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] }} | |
14 | div(v-if="game.type=='corr'") {{ game.corrMsg }} | |
15 | textarea(v-if="game.score=='*'" v-model="corrMsg") | |
430a2038 BA |
16 | BaseGame(:game="game" :vr="vr" ref="basegame" |
17 | @newmove="processMove" @gameover="gameOver") | |
a6088c90 BA |
18 | </template> |
19 | ||
20 | <script> | |
46284a2f | 21 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 22 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 23 | import { store } from "@/store"; |
967a2686 | 24 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 25 | import { ppt } from "@/utils/datetime"; |
66d03f23 | 26 | import { extractTime } from "@/utils/timeControl"; |
f41ce580 | 27 | import { ArrayFun } from "@/utils/array"; |
a6088c90 BA |
28 | |
29 | export default { | |
30 | name: 'my-game', | |
31 | components: { | |
32 | BaseGame, | |
5c8e044f | 33 | Chat, |
a6088c90 | 34 | }, |
f7121527 | 35 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
36 | data: function() { |
37 | return { | |
38 | st: store.state, | |
4b0384fa BA |
39 | gameRef: { //given in URL (rid = remote ID) |
40 | id: "", | |
41 | rid: "" | |
42 | }, | |
f41ce580 | 43 | game: {players:[{name:""},{name:""}]}, //passed to BaseGame |
6fba6e0c | 44 | corrMsg: "", //to send offline messages in corr games |
809ba2aa | 45 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 46 | vr: null, //"variant rules" object initialized from FEN |
6fba6e0c | 47 | drawOffer: "", //TODO: use for button style |
92a523d1 | 48 | people: [], //players + observers |
760adbce | 49 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 50 | repeat: {}, //detect position repetition |
a6088c90 BA |
51 | }; |
52 | }, | |
53 | watch: { | |
5f131484 BA |
54 | "$route": function(to, from) { |
55 | this.gameRef.id = to.params["id"]; | |
56 | this.gameRef.rid = to.query["rid"]; | |
57 | this.loadGame(); | |
a6088c90 | 58 | }, |
5b87454c | 59 | "game.clocks": function(newState) { |
b7cbbda1 | 60 | if (this.game.moves.length < 2 || this.game.score != "*") |
dce792f6 | 61 | { |
b7cbbda1 | 62 | // 1st move not completed yet, or game over: freeze time |
dce792f6 BA |
63 | this.virtualClocks = newState.map(s => ppt(s)); |
64 | return; | |
65 | } | |
809ba2aa | 66 | const currentTurn = this.vr.turn; |
6fba6e0c | 67 | const colorIdx = ["w","b"].indexOf(currentTurn); |
809ba2aa BA |
68 | let countdown = newState[colorIdx] - |
69 | (Date.now() - this.game.initime[colorIdx])/1000; | |
c0b27606 BA |
70 | this.virtualClocks = [0,1].map(i => { |
71 | const removeTime = i == colorIdx | |
72 | ? (Date.now() - this.game.initime[colorIdx])/1000 | |
73 | : 0; | |
74 | return ppt(newState[i] - removeTime); | |
75 | }); | |
809ba2aa | 76 | let clockUpdate = setInterval(() => { |
e69f159d | 77 | if (countdown < 0 || this.vr.turn != currentTurn || this.game.score != "*") |
809ba2aa BA |
78 | { |
79 | clearInterval(clockUpdate); | |
e69f159d | 80 | if (countdown < 0) |
430a2038 | 81 | this.gameOver(this.vr.turn=="w" ? "0-1" : "1-0", "Time"); |
809ba2aa | 82 | } |
9aa229f3 BA |
83 | else |
84 | { | |
85 | // TODO: with Vue 3, just do this.virtualClocks[colorIdx] = ppt(--countdown) | |
86 | this.$set(this.virtualClocks, colorIdx, ppt(Math.max(0, --countdown))); | |
87 | } | |
809ba2aa | 88 | }, 1000); |
5b87454c | 89 | }, |
92a523d1 | 90 | }, |
5f131484 | 91 | // TODO: redundant code with Hall.vue (related to people array) |
a6088c90 | 92 | created: function() { |
5f131484 BA |
93 | // Always add myself to players' list |
94 | const my = this.st.user; | |
95 | this.people.push({sid:my.sid, id:my.id, name:my.name}); | |
dc284d90 BA |
96 | this.gameRef.id = this.$route.params["id"]; |
97 | this.gameRef.rid = this.$route.query["rid"]; //may be undefined | |
760adbce | 98 | // Define socket .onmessage() and .onclose() events: |
5f131484 | 99 | this.st.conn.onmessage = this.socketMessageListener; |
cdb34c93 BA |
100 | const socketCloseListener = () => { |
101 | store.socketCloseListener(); //reinitialize connexion (in store.js) | |
a9b131f1 | 102 | this.st.conn.addEventListener('message', this.socketMessageListener); |
cdb34c93 BA |
103 | this.st.conn.addEventListener('close', socketCloseListener); |
104 | }; | |
cdb34c93 | 105 | this.st.conn.onclose = socketCloseListener; |
760adbce BA |
106 | // Socket init required before loading remote game: |
107 | const socketInit = (callback) => { | |
108 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state | |
109 | callback(); | |
110 | else //socket not ready yet (initial loading) | |
111 | this.st.conn.onopen = callback; | |
112 | }; | |
113 | if (!this.gameRef.rid) //game stored locally or on server | |
114 | this.loadGame(null, () => socketInit(this.roomInit)); | |
115 | else //game stored remotely: need socket to retrieve it | |
116 | { | |
117 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. | |
118 | // --> It will be given when receiving "fullgame" socket event. | |
119 | // A more general approach would be to store it somewhere. | |
120 | socketInit(this.loadGame); | |
121 | } | |
cdb34c93 BA |
122 | }, |
123 | methods: { | |
760adbce BA |
124 | // O.1] Ask server for room composition: |
125 | roomInit: function() { | |
126 | this.st.conn.send(JSON.stringify({code:"pollclients"})); | |
5f131484 | 127 | }, |
cdb34c93 | 128 | socketMessageListener: function(msg) { |
a6088c90 | 129 | const data = JSON.parse(msg.data); |
a6088c90 BA |
130 | switch (data.code) |
131 | { | |
6d9f4315 BA |
132 | case "duplicate": |
133 | alert("Warning: duplicate 'offline' connection"); | |
134 | break; | |
5f131484 BA |
135 | // 0.2] Receive clients list (just socket IDs) |
136 | case "pollclients": | |
137 | { | |
138 | data.sockIds.forEach(sid => { | |
139 | this.people.push({sid:sid, id:0, name:""}); | |
140 | // Ask only identity | |
141 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); | |
142 | }); | |
143 | break; | |
144 | } | |
145 | case "askidentity": | |
146 | { | |
147 | // Request for identification: reply if I'm not anonymous | |
148 | if (this.st.user.id > 0) | |
149 | { | |
150 | this.st.conn.send(JSON.stringify( | |
151 | // people[0] instead of st.user to avoid sending email | |
152 | {code:"identity", user:this.people[0], target:data.from})); | |
153 | } | |
154 | break; | |
155 | } | |
156 | case "identity": | |
157 | { | |
f41ce580 | 158 | let player = this.people.find(p => p.sid == data.user.sid); |
cd0d7743 BA |
159 | // NOTE: sometimes player.id fails because player is undefined... |
160 | // Probably because the event was meant for Hall? | |
161 | if (!player) | |
162 | return; | |
f41ce580 BA |
163 | player.id = data.user.id; |
164 | player.name = data.user.name; | |
165 | // Sending last state only for live games: corr games are complete | |
166 | if (this.game.type == "live" && this.game.oppsid == player.sid) | |
411d23cd | 167 | { |
f41ce580 | 168 | // Send our "last state" informations to opponent |
411d23cd | 169 | const L = this.game.moves.length; |
6d9f4315 BA |
170 | let lastMove = (L>0 ? this.game.moves[L-1] : undefined); |
171 | if (!!lastMove && this.drawOffer == "sent") | |
172 | lastMove.draw = true; | |
411d23cd BA |
173 | this.st.conn.send(JSON.stringify({ |
174 | code: "lastate", | |
f41ce580 BA |
175 | target: player.sid, |
176 | state: | |
177 | { | |
6d9f4315 | 178 | lastMove: lastMove, |
f41ce580 BA |
179 | score: this.game.score, |
180 | movesCount: L, | |
f41ce580 BA |
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 | 200 | case "newmove": |
7e355d68 | 201 | this.corrMsg = data.move.message; //may be empty |
06e79b07 | 202 | this.$set(this.game, "moveToPlay", data.move); //TODO: Vue3... |
f41ce580 | 203 | break; |
a6088c90 | 204 | case "lastate": //got opponent infos about last move |
6fba6e0c | 205 | { |
760adbce BA |
206 | this.lastate = data; |
207 | if (!!this.game.type) //game is loaded | |
208 | this.processLastate(); | |
209 | //else: will be processed when game is ready | |
a6088c90 | 210 | break; |
6fba6e0c | 211 | } |
93d1d7a7 | 212 | case "resign": |
430a2038 | 213 | this.gameOver(data.side=="b" ? "1-0" : "0-1", "Resign"); |
93d1d7a7 | 214 | break; |
93d1d7a7 | 215 | case "abort": |
430a2038 | 216 | this.gameOver("?", "Abort"); |
93d1d7a7 | 217 | break; |
2cc10cdb | 218 | case "draw": |
430a2038 | 219 | this.gameOver("1/2", "Mutual agreement"); |
2cc10cdb BA |
220 | break; |
221 | case "drawoffer": | |
760adbce | 222 | this.drawOffer = "received"; //TODO: observers don't know who offered draw |
6d9f4315 | 223 | break; |
7e1a1fe9 | 224 | case "askfullgame": |
dc284d90 | 225 | this.st.conn.send(JSON.stringify({code:"fullgame", game:this.game, target:data.from})); |
7e1a1fe9 | 226 | break; |
5f131484 | 227 | case "fullgame": |
760adbce BA |
228 | // Callback "roomInit" to poll clients only after game is loaded |
229 | this.loadGame(data.game, this.roomInit); | |
5f131484 | 230 | break; |
5f131484 BA |
231 | case "connect": |
232 | { | |
c6788ecf BA |
233 | this.people.push({name:"", id:0, sid:data.from}); |
234 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); | |
5f131484 BA |
235 | break; |
236 | } | |
237 | case "disconnect": | |
c6788ecf | 238 | ArrayFun.remove(this.people, p => p.sid == data.from); |
a6088c90 BA |
239 | break; |
240 | } | |
cdb34c93 | 241 | }, |
760adbce BA |
242 | // lastate was received, but maybe game wasn't ready yet: |
243 | processLastate: function() { | |
244 | const data = this.lastate; | |
245 | this.lastate = undefined; //security... | |
246 | const L = this.game.moves.length; | |
247 | if (data.movesCount > L) | |
248 | { | |
249 | // Just got last move from him | |
06e79b07 | 250 | this.$set(this.game, "moveToPlay", data.lastMove); |
760adbce BA |
251 | if (data.score != "*" && this.game.score == "*") |
252 | { | |
253 | // Opponent resigned or aborted game, or accepted draw offer | |
254 | // (this is not a stalemate or checkmate) | |
430a2038 | 255 | this.gameOver(data.score, "Opponent action"); |
760adbce BA |
256 | } |
257 | this.game.clocks = data.clocks; //TODO: check this? | |
258 | if (!!data.lastMove.draw) | |
259 | this.drawOffer = "received"; | |
260 | } | |
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 | }); | |
430a2038 | 271 | this.gameOver("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; | |
430a2038 | 295 | this.gameOver("?", "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 | }); | |
430a2038 | 316 | this.gameOver(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 | ); |
72ccbd67 | 406 | this.repeat = {}; //reset |
760adbce BA |
407 | if (!!this.lastate) //lastate arrived before game was loaded: |
408 | this.processLastate(); | |
409 | callback(); | |
967a2686 BA |
410 | }; |
411 | if (!!game) | |
dc284d90 | 412 | return afterRetrieval(game); |
967a2686 BA |
413 | if (!!this.gameRef.rid) |
414 | { | |
760adbce | 415 | // Remote live game: forgetting about callback func... (TODO: design) |
5f131484 BA |
416 | this.st.conn.send(JSON.stringify( |
417 | {code:"askfullgame", target:this.gameRef.rid})); | |
967a2686 BA |
418 | } |
419 | else | |
420 | { | |
f41ce580 | 421 | // Local or corr game |
11667c79 | 422 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 423 | } |
a6088c90 | 424 | }, |
9d54ab89 | 425 | // Post-process a move (which was just played) |
ce87ac6a | 426 | processMove: function(move) { |
b4fb1612 BA |
427 | if (!this.game.mycolor) |
428 | return; //I'm just an observer | |
9d54ab89 | 429 | // Update storage (corr or live) |
6fba6e0c | 430 | const colorIdx = ["w","b"].indexOf(move.color); |
9d54ab89 BA |
431 | // https://stackoverflow.com/a/38750895 |
432 | const allowed_fields = ["appear", "vanish", "start", "end"]; | |
433 | const filtered_move = Object.keys(move) | |
434 | .filter(key => allowed_fields.includes(key)) | |
435 | .reduce((obj, key) => { | |
8a7452b5 | 436 | obj[key] = move[key]; |
9d54ab89 BA |
437 | return obj; |
438 | }, {}); | |
dc284d90 | 439 | // Send move ("newmove" event) to people in the room (if our turn) |
dce792f6 | 440 | let addTime = 0; |
9d54ab89 | 441 | if (move.color == this.game.mycolor) |
b4fb1612 | 442 | { |
dce792f6 BA |
443 | if (this.game.moves.length >= 2) //after first move |
444 | { | |
445 | const elapsed = Date.now() - this.game.initime[colorIdx]; | |
446 | // elapsed time is measured in milliseconds | |
447 | addTime = this.game.increment - elapsed/1000; | |
448 | } | |
6d68309a BA |
449 | let sendMove = Object.assign({}, filtered_move, {addTime: addTime}); |
450 | if (this.game.type == "corr") | |
451 | sendMove.message = this.corrMsg; | |
dc284d90 BA |
452 | this.people.forEach(p => { |
453 | if (p.sid != this.st.user.sid) | |
454 | { | |
455 | this.st.conn.send(JSON.stringify({ | |
456 | code: "newmove", | |
457 | target: p.sid, | |
458 | move: sendMove, | |
459 | })); | |
460 | } | |
461 | }); | |
b4fb1612 | 462 | } |
5b87454c BA |
463 | else |
464 | addTime = move.addTime; //supposed transmitted | |
6fba6e0c | 465 | const nextIdx = ["w","b"].indexOf(this.vr.turn); |
6d68309a BA |
466 | // Since corr games are stored at only one location, update should be |
467 | // done only by one player for each move: | |
468 | if (this.game.type == "live" || move.color == this.game.mycolor) | |
967a2686 | 469 | { |
e69f159d | 470 | if (this.game.type == "corr") |
f41ce580 | 471 | { |
e69f159d | 472 | GameStorage.update(this.gameRef.id, |
6d68309a | 473 | { |
e69f159d | 474 | fen: move.fen, |
7e355d68 | 475 | message: this.corrMsg, |
e69f159d BA |
476 | move: |
477 | { | |
478 | squares: filtered_move, | |
e69f159d BA |
479 | played: Date.now(), //TODO: on server? |
480 | idx: this.game.moves.length, | |
481 | }, | |
482 | }); | |
483 | } | |
484 | else //live | |
485 | { | |
486 | GameStorage.update(this.gameRef.id, | |
487 | { | |
488 | fen: move.fen, | |
489 | move: filtered_move, | |
490 | clocks: this.game.clocks.map((t,i) => i==colorIdx | |
491 | ? this.game.clocks[i] + addTime | |
492 | : this.game.clocks[i]), | |
493 | initime: this.game.initime.map((t,i) => i==nextIdx | |
494 | ? Date.now() | |
495 | : this.game.initime[i]), | |
496 | }); | |
497 | } | |
6d68309a | 498 | } |
967a2686 BA |
499 | // Also update current game object: |
500 | this.game.moves.push(move); | |
501 | this.game.fen = move.fen; | |
760adbce | 502 | //TODO: (Vue3) just this.game.clocks[colorIdx] += addTime; |
66d03f23 | 503 | this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime); |
809ba2aa | 504 | this.game.initime[nextIdx] = Date.now(); |
72ccbd67 BA |
505 | // If repetition detected, consider that a draw offer was received: |
506 | const fenObj = V.ParseFen(move.fen); | |
507 | let repIdx = fenObj.position + "_" + fenObj.turn; | |
508 | if (!!fenObj.flags) | |
509 | repIdx += "_" + fenObj.flags; | |
510 | this.repeat[repIdx] = (!!this.repeat[repIdx] | |
511 | ? this.repeat[repIdx]+1 | |
512 | : 1); | |
513 | if (this.repeat[repIdx] >= 3) | |
514 | this.drawOffer = "received"; //TODO: will print "mutual agreement"... | |
b4fb1612 | 515 | }, |
430a2038 | 516 | gameOver: function(score, scoreMsg) { |
93d1d7a7 | 517 | this.game.mode = "analyze"; |
430a2038 BA |
518 | this.game.score = score; |
519 | this.game.scoreMsg = scoreMsg; | |
ab6f48ea BA |
520 | const myIdx = this.game.players.findIndex(p => { |
521 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
522 | }); | |
523 | if (myIdx >= 0) //OK, I play in this game | |
524 | GameStorage.update(this.gameRef.id, { score: score }); | |
ce87ac6a | 525 | }, |
a6088c90 BA |
526 | }, |
527 | }; | |
528 | </script> | |
7e1a1fe9 BA |
529 | |
530 | <style lang="sass"> | |
72ccbd67 BA |
531 | .connected |
532 | background-color: green | |
72ccbd67 BA |
533 | .disconnected |
534 | background-color: red | |
535 | ||
430a2038 BA |
536 | @media screen and (min-width: 768px) |
537 | #actions | |
538 | width: 300px | |
539 | @media screen and (max-width: 767px) | |
540 | .game | |
541 | width: 100% | |
72ccbd67 | 542 | |
430a2038 BA |
543 | #actions |
544 | margin-top: 10px | |
545 | margin-left: auto | |
546 | margin-right: auto | |
547 | button | |
548 | display: inline-block | |
549 | width: 33% | |
550 | margin: 0 | |
551 | #chat | |
552 | margin-top: 5px | |
553 | margin-bottom: 5px | |
554 | >.card | |
555 | max-width: 100% | |
556 | margin: 0; | |
557 | border: none; | |
7e1a1fe9 | 558 | </style> |