Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
7aa548e7 | 2 | main |
a1c48034 | 3 | input#modalChat.modal(type="checkbox" @change="toggleChat") |
dcd68c41 BA |
4 | div#chatWrap(role="dialog" data-checkbox="modalChat" |
5 | aria-labelledby="inputChat") | |
a1c48034 BA |
6 | #chat.card |
7 | label.modal-close(for="modalChat") | |
3837d4f7 | 8 | Chat(:players="game.players" :pastChats="game.chats" |
a1c48034 | 9 | @newchat-sent="finishSendChat" @newchat-received="processChat") |
7aa548e7 | 10 | .row |
050ae3b5 | 11 | #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2 |
cf94b843 | 12 | button#chatBtn(onClick="doClick('modalChat')") Chat |
4f518610 | 13 | #actions(v-if="game.score=='*'") |
dcd68c41 | 14 | button(@click="clickDraw" :class="{['draw-' + drawOffer]: true}") Draw |
7aa548e7 BA |
15 | button(@click="abortGame") Abort |
16 | button(@click="resign") Resign | |
050ae3b5 BA |
17 | #playersInfo |
18 | p | |
5bcc9b31 BA |
19 | span.name(:class="{connected: isConnected(0)}") |
20 | | {{ game.players[0].name || "@nonymous" }} | |
050ae3b5 BA |
21 | span.time(v-if="game.score=='*'") {{ virtualClocks[0] }} |
22 | span.split-names - | |
5bcc9b31 BA |
23 | span.name(:class="{connected: isConnected(1)}") |
24 | | {{ game.players[1].name || "@nonymous" }} | |
050ae3b5 | 25 | span.time(v-if="game.score=='*'") {{ virtualClocks[1] }} |
430a2038 BA |
26 | BaseGame(:game="game" :vr="vr" ref="basegame" |
27 | @newmove="processMove" @gameover="gameOver") | |
a6088c90 BA |
28 | </template> |
29 | ||
30 | <script> | |
46284a2f | 31 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 32 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 33 | import { store } from "@/store"; |
967a2686 | 34 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 35 | import { ppt } from "@/utils/datetime"; |
66d03f23 | 36 | import { extractTime } from "@/utils/timeControl"; |
f41ce580 | 37 | import { ArrayFun } from "@/utils/array"; |
dcd68c41 | 38 | import { processModalClick } from "@/utils/modalClick"; |
a6088c90 BA |
39 | |
40 | export default { | |
41 | name: 'my-game', | |
42 | components: { | |
43 | BaseGame, | |
5c8e044f | 44 | Chat, |
a6088c90 | 45 | }, |
f7121527 | 46 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
47 | data: function() { |
48 | return { | |
49 | st: store.state, | |
4b0384fa BA |
50 | gameRef: { //given in URL (rid = remote ID) |
51 | id: "", | |
52 | rid: "" | |
53 | }, | |
f41ce580 | 54 | game: {players:[{name:""},{name:""}]}, //passed to BaseGame |
809ba2aa | 55 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 56 | vr: null, //"variant rules" object initialized from FEN |
dcd68c41 BA |
57 | drawOffer: "", |
58 | people: {}, //players + observers | |
760adbce | 59 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 60 | repeat: {}, //detect position repetition |
a6088c90 BA |
61 | }; |
62 | }, | |
63 | watch: { | |
5f131484 BA |
64 | "$route": function(to, from) { |
65 | this.gameRef.id = to.params["id"]; | |
66 | this.gameRef.rid = to.query["rid"]; | |
67 | this.loadGame(); | |
a6088c90 | 68 | }, |
5b87454c | 69 | "game.clocks": function(newState) { |
b7cbbda1 | 70 | if (this.game.moves.length < 2 || this.game.score != "*") |
dce792f6 | 71 | { |
b7cbbda1 | 72 | // 1st move not completed yet, or game over: freeze time |
dce792f6 BA |
73 | this.virtualClocks = newState.map(s => ppt(s)); |
74 | return; | |
75 | } | |
809ba2aa | 76 | const currentTurn = this.vr.turn; |
6fba6e0c | 77 | const colorIdx = ["w","b"].indexOf(currentTurn); |
809ba2aa BA |
78 | let countdown = newState[colorIdx] - |
79 | (Date.now() - this.game.initime[colorIdx])/1000; | |
c0b27606 BA |
80 | this.virtualClocks = [0,1].map(i => { |
81 | const removeTime = i == colorIdx | |
82 | ? (Date.now() - this.game.initime[colorIdx])/1000 | |
83 | : 0; | |
84 | return ppt(newState[i] - removeTime); | |
85 | }); | |
809ba2aa | 86 | let clockUpdate = setInterval(() => { |
e69f159d | 87 | if (countdown < 0 || this.vr.turn != currentTurn || this.game.score != "*") |
809ba2aa BA |
88 | { |
89 | clearInterval(clockUpdate); | |
e69f159d | 90 | if (countdown < 0) |
430a2038 | 91 | this.gameOver(this.vr.turn=="w" ? "0-1" : "1-0", "Time"); |
809ba2aa | 92 | } |
9aa229f3 BA |
93 | else |
94 | { | |
95 | // TODO: with Vue 3, just do this.virtualClocks[colorIdx] = ppt(--countdown) | |
96 | this.$set(this.virtualClocks, colorIdx, ppt(Math.max(0, --countdown))); | |
97 | } | |
809ba2aa | 98 | }, 1000); |
5b87454c | 99 | }, |
92a523d1 | 100 | }, |
dcd68c41 | 101 | // NOTE: some redundant code with Hall.vue (related to people array) |
a6088c90 | 102 | created: function() { |
5f131484 BA |
103 | // Always add myself to players' list |
104 | const my = this.st.user; | |
dcd68c41 | 105 | this.$set(this.people, my.sid, {id:my.id, name:my.name}); |
dc284d90 BA |
106 | this.gameRef.id = this.$route.params["id"]; |
107 | this.gameRef.rid = this.$route.query["rid"]; //may be undefined | |
760adbce | 108 | // Define socket .onmessage() and .onclose() events: |
5f131484 | 109 | this.st.conn.onmessage = this.socketMessageListener; |
cdb34c93 BA |
110 | const socketCloseListener = () => { |
111 | store.socketCloseListener(); //reinitialize connexion (in store.js) | |
a9b131f1 | 112 | this.st.conn.addEventListener('message', this.socketMessageListener); |
cdb34c93 BA |
113 | this.st.conn.addEventListener('close', socketCloseListener); |
114 | }; | |
cdb34c93 | 115 | this.st.conn.onclose = socketCloseListener; |
760adbce BA |
116 | // Socket init required before loading remote game: |
117 | const socketInit = (callback) => { | |
118 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state | |
119 | callback(); | |
120 | else //socket not ready yet (initial loading) | |
121 | this.st.conn.onopen = callback; | |
122 | }; | |
123 | if (!this.gameRef.rid) //game stored locally or on server | |
124 | this.loadGame(null, () => socketInit(this.roomInit)); | |
125 | else //game stored remotely: need socket to retrieve it | |
126 | { | |
127 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. | |
128 | // --> It will be given when receiving "fullgame" socket event. | |
129 | // A more general approach would be to store it somewhere. | |
130 | socketInit(this.loadGame); | |
131 | } | |
cdb34c93 | 132 | }, |
dcd68c41 BA |
133 | mounted: function() { |
134 | document.getElementById("chatWrap").addEventListener( | |
135 | "click", processModalClick); | |
136 | }, | |
cdb34c93 | 137 | methods: { |
760adbce BA |
138 | // O.1] Ask server for room composition: |
139 | roomInit: function() { | |
41c80bb6 BA |
140 | // Notify the room only now that I connected, because |
141 | // messages might be lost otherwise (if game loading is slow) | |
142 | this.st.conn.send(JSON.stringify({code:"connect"})); | |
760adbce | 143 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
5f131484 | 144 | }, |
050ae3b5 BA |
145 | isConnected: function(index) { |
146 | const name = this.game.players[index].name; | |
147 | if (this.st.user.name == name) | |
148 | return true; | |
dcd68c41 | 149 | return Object.values(this.people).some(p => p.name == name); |
050ae3b5 | 150 | }, |
cdb34c93 | 151 | socketMessageListener: function(msg) { |
a6088c90 | 152 | const data = JSON.parse(msg.data); |
a6088c90 BA |
153 | switch (data.code) |
154 | { | |
6d9f4315 BA |
155 | case "duplicate": |
156 | alert("Warning: duplicate 'offline' connection"); | |
157 | break; | |
5f131484 BA |
158 | // 0.2] Receive clients list (just socket IDs) |
159 | case "pollclients": | |
160 | { | |
161 | data.sockIds.forEach(sid => { | |
dcd68c41 BA |
162 | // TODO: understand clearly what happens here, problems when a |
163 | // game is quit, and then launch a new game from hall. | |
164 | if (!!this.people[sid]) | |
165 | return; | |
166 | this.$set(this.people, sid, {id:0, name:""}); | |
5f131484 BA |
167 | // Ask only identity |
168 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); | |
169 | }); | |
170 | break; | |
171 | } | |
172 | case "askidentity": | |
173 | { | |
174 | // Request for identification: reply if I'm not anonymous | |
175 | if (this.st.user.id > 0) | |
176 | { | |
dcd68c41 BA |
177 | this.st.conn.send(JSON.stringify({code:"identity", |
178 | user: { | |
179 | // NOTE: decompose to avoid revealing email | |
180 | name: this.st.user.name, | |
181 | sid: this.st.user.sid, | |
182 | id: this.st.user.id, | |
183 | }, | |
184 | target:data.from})); | |
5f131484 BA |
185 | } |
186 | break; | |
187 | } | |
188 | case "identity": | |
189 | { | |
cd0d7743 BA |
190 | // NOTE: sometimes player.id fails because player is undefined... |
191 | // Probably because the event was meant for Hall? | |
41c80bb6 | 192 | if (!this.people[data.user.sid]) |
cd0d7743 | 193 | return; |
41c80bb6 BA |
194 | this.$set(this.people, data.user.sid, |
195 | {id: data.user.id, name: data.user.name}); | |
f41ce580 | 196 | // Sending last state only for live games: corr games are complete |
dcd68c41 | 197 | if (this.game.type == "live" && this.game.oppsid == data.user.sid) |
411d23cd | 198 | { |
f41ce580 | 199 | // Send our "last state" informations to opponent |
411d23cd | 200 | const L = this.game.moves.length; |
6d9f4315 BA |
201 | let lastMove = (L>0 ? this.game.moves[L-1] : undefined); |
202 | if (!!lastMove && this.drawOffer == "sent") | |
203 | lastMove.draw = true; | |
411d23cd BA |
204 | this.st.conn.send(JSON.stringify({ |
205 | code: "lastate", | |
dcd68c41 | 206 | target: data.user.sid, |
f41ce580 BA |
207 | state: |
208 | { | |
6d9f4315 | 209 | lastMove: lastMove, |
f41ce580 BA |
210 | score: this.game.score, |
211 | movesCount: L, | |
f41ce580 BA |
212 | clocks: this.game.clocks, |
213 | } | |
411d23cd BA |
214 | })); |
215 | } | |
a6088c90 | 216 | break; |
6fba6e0c | 217 | } |
c6788ecf | 218 | case "askgame": |
5bcc9b31 BA |
219 | // Send current (live) game if not asked by opponent (!) |
220 | if (this.game.players.some(p => p.sid == data.from)) | |
221 | return; | |
c6788ecf BA |
222 | const myGame = |
223 | { | |
224 | // Minimal game informations: | |
225 | id: this.game.id, | |
ab6f48ea | 226 | players: this.game.players.map(p => { return {name:p.name}; }), |
c6788ecf BA |
227 | vid: this.game.vid, |
228 | timeControl: this.game.timeControl, | |
229 | }; | |
230 | this.st.conn.send(JSON.stringify({code:"game", | |
231 | game:myGame, target:data.from})); | |
232 | break; | |
f41ce580 | 233 | case "newmove": |
41c80bb6 | 234 | this.$set(this.game, "moveToPlay", data.move); |
f41ce580 | 235 | break; |
a6088c90 | 236 | case "lastate": //got opponent infos about last move |
6fba6e0c | 237 | { |
760adbce BA |
238 | this.lastate = data; |
239 | if (!!this.game.type) //game is loaded | |
240 | this.processLastate(); | |
241 | //else: will be processed when game is ready | |
a6088c90 | 242 | break; |
6fba6e0c | 243 | } |
93d1d7a7 | 244 | case "resign": |
430a2038 | 245 | this.gameOver(data.side=="b" ? "1-0" : "0-1", "Resign"); |
93d1d7a7 | 246 | break; |
93d1d7a7 | 247 | case "abort": |
430a2038 | 248 | this.gameOver("?", "Abort"); |
93d1d7a7 | 249 | break; |
2cc10cdb | 250 | case "draw": |
dcd68c41 | 251 | this.gameOver("1/2", data.message); |
2cc10cdb BA |
252 | break; |
253 | case "drawoffer": | |
41c80bb6 BA |
254 | // NOTE: observers don't know who offered draw |
255 | this.drawOffer = "received"; | |
6d9f4315 | 256 | break; |
7e1a1fe9 | 257 | case "askfullgame": |
41c80bb6 BA |
258 | this.st.conn.send(JSON.stringify({code:"fullgame", |
259 | game:this.game, target:data.from})); | |
7e1a1fe9 | 260 | break; |
5f131484 | 261 | case "fullgame": |
760adbce BA |
262 | // Callback "roomInit" to poll clients only after game is loaded |
263 | this.loadGame(data.game, this.roomInit); | |
5f131484 | 264 | break; |
5f131484 BA |
265 | case "connect": |
266 | { | |
dcd68c41 BA |
267 | // TODO: next condition is probably not required. See note line 150 |
268 | if (!this.people[data.from]) | |
269 | { | |
270 | this.$set(this.people, data.from, {name:"", id:0}); | |
271 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); | |
272 | } | |
5f131484 BA |
273 | break; |
274 | } | |
275 | case "disconnect": | |
dcd68c41 | 276 | this.$delete(this.people, data.from); |
a6088c90 BA |
277 | break; |
278 | } | |
cdb34c93 | 279 | }, |
760adbce BA |
280 | // lastate was received, but maybe game wasn't ready yet: |
281 | processLastate: function() { | |
282 | const data = this.lastate; | |
283 | this.lastate = undefined; //security... | |
284 | const L = this.game.moves.length; | |
285 | if (data.movesCount > L) | |
286 | { | |
287 | // Just got last move from him | |
06e79b07 | 288 | this.$set(this.game, "moveToPlay", data.lastMove); |
760adbce BA |
289 | if (data.score != "*" && this.game.score == "*") |
290 | { | |
291 | // Opponent resigned or aborted game, or accepted draw offer | |
292 | // (this is not a stalemate or checkmate) | |
430a2038 | 293 | this.gameOver(data.score, "Opponent action"); |
760adbce BA |
294 | } |
295 | this.game.clocks = data.clocks; //TODO: check this? | |
296 | if (!!data.lastMove.draw) | |
297 | this.drawOffer = "received"; | |
298 | } | |
299 | }, | |
dcd68c41 | 300 | clickDraw: function() { |
a1c48034 | 301 | if (["received","threerep"].includes(this.drawOffer)) |
6fba6e0c | 302 | { |
2cc10cdb | 303 | if (!confirm("Accept draw?")) |
6fba6e0c | 304 | return; |
a1c48034 BA |
305 | const message = (this.drawOffer == "received" |
306 | ? "Mutual agreement" | |
307 | : "Three repetitions"); | |
dcd68c41 BA |
308 | Object.keys(this.people).forEach(sid => { |
309 | if (sid != this.st.user.sid) | |
310 | { | |
311 | this.st.conn.send(JSON.stringify({code:"draw", | |
312 | message:message, target:sid})); | |
313 | } | |
314 | }); | |
a1c48034 | 315 | this.gameOver("1/2", message); |
2cc10cdb | 316 | } |
41c80bb6 | 317 | else if (this.drawOffer == "") //no effect if drawOffer == "sent" |
6fba6e0c BA |
318 | { |
319 | if (!confirm("Offer draw?")) | |
320 | return; | |
760adbce | 321 | this.drawOffer = "sent"; |
dcd68c41 BA |
322 | Object.keys(this.people).forEach(sid => { |
323 | if (sid != this.st.user.sid) | |
324 | this.st.conn.send(JSON.stringify({code:"drawoffer", target:sid})); | |
760adbce | 325 | }); |
41c80bb6 | 326 | GameStorage.update(this.gameRef.id, {drawOffer: true}); |
a6088c90 BA |
327 | } |
328 | }, | |
7f3484bd BA |
329 | abortGame: function() { |
330 | if (!confirm(this.st.tr["Terminate game?"])) | |
331 | return; | |
430a2038 | 332 | this.gameOver("?", "Abort"); |
dcd68c41 BA |
333 | Object.keys(this.people).forEach(sid => { |
334 | if (sid != this.st.user.sid) | |
5f131484 BA |
335 | { |
336 | this.st.conn.send(JSON.stringify({ | |
337 | code: "abort", | |
dcd68c41 | 338 | target: sid, |
5f131484 BA |
339 | })); |
340 | } | |
7f3484bd | 341 | }); |
a6088c90 BA |
342 | }, |
343 | resign: function(e) { | |
344 | if (!confirm("Resign the game?")) | |
345 | return; | |
dcd68c41 BA |
346 | Object.keys(this.people).forEach(sid => { |
347 | if (sid != this.st.user.sid) | |
760adbce BA |
348 | { |
349 | this.st.conn.send(JSON.stringify({code:"resign", | |
dcd68c41 | 350 | side:this.game.mycolor, target:sid})); |
760adbce BA |
351 | } |
352 | }); | |
430a2038 | 353 | this.gameOver(this.game.mycolor=="w" ? "0-1" : "1-0", "Resign"); |
a6088c90 | 354 | }, |
967a2686 BA |
355 | // 3 cases for loading a game: |
356 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
357 | // - from server (one correspondance game I play[ed] or not) |
358 | // - from remote peer (one live game I don't play, finished or not) | |
760adbce | 359 | loadGame: function(game, callback) { |
967a2686 | 360 | const afterRetrieval = async (game) => { |
f41ce580 BA |
361 | const vModule = await import("@/variants/" + game.vname + ".js"); |
362 | window.V = vModule.VariantRules; | |
363 | this.vr = new V(game.fen); | |
c0b27606 | 364 | const gtype = (game.timeControl.indexOf('d') >= 0 ? "corr" : "live"); |
92a523d1 | 365 | const tc = extractTime(game.timeControl); |
c0b27606 BA |
366 | if (gtype == "corr") |
367 | { | |
f41ce580 BA |
368 | if (game.players[0].color == "b") |
369 | { | |
370 | // Adopt the same convention for live and corr games: [0] = white | |
371 | [ game.players[0], game.players[1] ] = | |
372 | [ game.players[1], game.players[0] ]; | |
373 | } | |
c0b27606 | 374 | // corr game: needs to compute the clocks + initime |
7f3484bd | 375 | // NOTE: clocks in seconds, initime in milliseconds |
92a523d1 | 376 | game.clocks = [tc.mainTime, tc.mainTime]; |
92b82def | 377 | game.moves.sort((m1,m2) => m1.idx - m2.idx); //in case of |
b7cbbda1 | 378 | if (game.score == "*") //otherwise no need to bother with time |
92a523d1 | 379 | { |
b7cbbda1 BA |
380 | game.initime = [0, 0]; |
381 | const L = game.moves.length; | |
382 | if (L >= 3) | |
5f131484 | 383 | { |
b7cbbda1 BA |
384 | let addTime = [0, 0]; |
385 | for (let i=2; i<L; i++) | |
386 | { | |
387 | addTime[i%2] += tc.increment - | |
388 | (game.moves[i].played - game.moves[i-1].played) / 1000; | |
389 | } | |
390 | for (let i=0; i<=1; i++) | |
391 | game.clocks[i] += addTime[i]; | |
5f131484 | 392 | } |
b7cbbda1 BA |
393 | if (L >= 1) |
394 | game.initime[L%2] = game.moves[L-1].played; | |
395 | if (game.drawOffer) | |
396 | this.drawOffer = "received"; | |
92a523d1 | 397 | } |
92b82def | 398 | // Now that we used idx and played, re-format moves as for live games |
6d68309a | 399 | game.moves = game.moves.map( (m) => { |
92b82def | 400 | const s = m.squares; |
6d68309a | 401 | return { |
92b82def BA |
402 | appear: s.appear, |
403 | vanish: s.vanish, | |
404 | start: s.start, | |
405 | end: s.end, | |
92b82def BA |
406 | }; |
407 | }); | |
3837d4f7 BA |
408 | // Also sort chat messages (if any) |
409 | game.chats.sort( (c1,c2) => { return c2.added - c1.added; }); | |
c0b27606 | 410 | } |
f41ce580 BA |
411 | const myIdx = game.players.findIndex(p => { |
412 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
413 | }); | |
92a523d1 | 414 | if (gtype == "live" && game.clocks[0] < 0) //game unstarted |
66d03f23 BA |
415 | { |
416 | game.clocks = [tc.mainTime, tc.mainTime]; | |
b7cbbda1 | 417 | if (game.score == "*") |
22efa391 | 418 | { |
b7cbbda1 BA |
419 | game.initime[0] = Date.now(); |
420 | if (myIdx >= 0) | |
22efa391 | 421 | { |
b7cbbda1 BA |
422 | // I play in this live game; corr games don't have clocks+initime |
423 | GameStorage.update(game.id, | |
424 | { | |
425 | clocks: game.clocks, | |
426 | initime: game.initime, | |
427 | }); | |
428 | } | |
22efa391 | 429 | } |
66d03f23 | 430 | } |
4b0384fa BA |
431 | this.game = Object.assign({}, |
432 | game, | |
cf742aaf | 433 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 434 | { |
c0b27606 | 435 | type: gtype, |
66d03f23 | 436 | increment: tc.increment, |
6fba6e0c | 437 | mycolor: [undefined,"w","b"][myIdx+1], |
5f131484 BA |
438 | // opponent sid not strictly required (or available), but easier |
439 | // at least oppsid or oppid is available anyway: | |
440 | oppsid: (myIdx < 0 ? undefined : game.players[1-myIdx].sid), | |
441 | oppid: (myIdx < 0 ? undefined : game.players[1-myIdx].uid), | |
6fba6e0c | 442 | } |
4b0384fa | 443 | ); |
72ccbd67 | 444 | this.repeat = {}; //reset |
760adbce BA |
445 | if (!!this.lastate) //lastate arrived before game was loaded: |
446 | this.processLastate(); | |
447 | callback(); | |
967a2686 BA |
448 | }; |
449 | if (!!game) | |
dc284d90 | 450 | return afterRetrieval(game); |
967a2686 BA |
451 | if (!!this.gameRef.rid) |
452 | { | |
760adbce | 453 | // Remote live game: forgetting about callback func... (TODO: design) |
5f131484 BA |
454 | this.st.conn.send(JSON.stringify( |
455 | {code:"askfullgame", target:this.gameRef.rid})); | |
967a2686 BA |
456 | } |
457 | else | |
458 | { | |
f41ce580 | 459 | // Local or corr game |
11667c79 | 460 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 461 | } |
a6088c90 | 462 | }, |
9d54ab89 | 463 | // Post-process a move (which was just played) |
ce87ac6a | 464 | processMove: function(move) { |
a1c48034 | 465 | // Update storage (corr or live) if I play in the game |
6fba6e0c | 466 | const colorIdx = ["w","b"].indexOf(move.color); |
9d54ab89 | 467 | // https://stackoverflow.com/a/38750895 |
a1c48034 BA |
468 | if (!!this.game.mycolor) |
469 | { | |
470 | const allowed_fields = ["appear", "vanish", "start", "end"]; | |
471 | // NOTE: 'var' to see this variable outside this block | |
472 | var filtered_move = Object.keys(move) | |
473 | .filter(key => allowed_fields.includes(key)) | |
474 | .reduce((obj, key) => { | |
475 | obj[key] = move[key]; | |
476 | return obj; | |
477 | }, {}); | |
478 | } | |
dc284d90 | 479 | // Send move ("newmove" event) to people in the room (if our turn) |
dce792f6 | 480 | let addTime = 0; |
9d54ab89 | 481 | if (move.color == this.game.mycolor) |
b4fb1612 | 482 | { |
dce792f6 BA |
483 | if (this.game.moves.length >= 2) //after first move |
484 | { | |
485 | const elapsed = Date.now() - this.game.initime[colorIdx]; | |
486 | // elapsed time is measured in milliseconds | |
487 | addTime = this.game.increment - elapsed/1000; | |
488 | } | |
6d68309a | 489 | let sendMove = Object.assign({}, filtered_move, {addTime: addTime}); |
dcd68c41 BA |
490 | Object.keys(this.people).forEach(sid => { |
491 | if (sid != this.st.user.sid) | |
dc284d90 BA |
492 | { |
493 | this.st.conn.send(JSON.stringify({ | |
494 | code: "newmove", | |
dcd68c41 | 495 | target: sid, |
dc284d90 BA |
496 | move: sendMove, |
497 | })); | |
498 | } | |
499 | }); | |
b4fb1612 | 500 | } |
5b87454c BA |
501 | else |
502 | addTime = move.addTime; //supposed transmitted | |
6fba6e0c | 503 | const nextIdx = ["w","b"].indexOf(this.vr.turn); |
6d68309a BA |
504 | // Since corr games are stored at only one location, update should be |
505 | // done only by one player for each move: | |
a1c48034 BA |
506 | if (!!this.game.mycolor && |
507 | (this.game.type == "live" || move.color == this.game.mycolor)) | |
967a2686 | 508 | { |
e69f159d | 509 | if (this.game.type == "corr") |
f41ce580 | 510 | { |
e69f159d | 511 | GameStorage.update(this.gameRef.id, |
6d68309a | 512 | { |
e69f159d BA |
513 | fen: move.fen, |
514 | move: | |
515 | { | |
516 | squares: filtered_move, | |
e69f159d BA |
517 | played: Date.now(), //TODO: on server? |
518 | idx: this.game.moves.length, | |
519 | }, | |
520 | }); | |
521 | } | |
522 | else //live | |
523 | { | |
524 | GameStorage.update(this.gameRef.id, | |
525 | { | |
526 | fen: move.fen, | |
527 | move: filtered_move, | |
528 | clocks: this.game.clocks.map((t,i) => i==colorIdx | |
529 | ? this.game.clocks[i] + addTime | |
530 | : this.game.clocks[i]), | |
531 | initime: this.game.initime.map((t,i) => i==nextIdx | |
532 | ? Date.now() | |
533 | : this.game.initime[i]), | |
534 | }); | |
535 | } | |
6d68309a | 536 | } |
967a2686 BA |
537 | // Also update current game object: |
538 | this.game.moves.push(move); | |
539 | this.game.fen = move.fen; | |
760adbce | 540 | //TODO: (Vue3) just this.game.clocks[colorIdx] += addTime; |
66d03f23 | 541 | this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime); |
809ba2aa | 542 | this.game.initime[nextIdx] = Date.now(); |
72ccbd67 BA |
543 | // If repetition detected, consider that a draw offer was received: |
544 | const fenObj = V.ParseFen(move.fen); | |
545 | let repIdx = fenObj.position + "_" + fenObj.turn; | |
546 | if (!!fenObj.flags) | |
547 | repIdx += "_" + fenObj.flags; | |
548 | this.repeat[repIdx] = (!!this.repeat[repIdx] | |
549 | ? this.repeat[repIdx]+1 | |
550 | : 1); | |
551 | if (this.repeat[repIdx] >= 3) | |
a1c48034 | 552 | this.drawOffer = "threerep"; |
b4fb1612 | 553 | }, |
a1c48034 BA |
554 | toggleChat: function() { |
555 | document.getElementById("chatBtn").style.backgroundColor = "#e2e2e2"; | |
556 | }, | |
557 | finishSendChat: function(chat) { | |
0e16cb26 BA |
558 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) |
559 | if (this.game.type == "corr" && this.st.user.id > 0) | |
63ca2b89 BA |
560 | GameStorage.update(this.gameRef.id, {chat: chat}); |
561 | }, | |
a1c48034 BA |
562 | processChat: function() { |
563 | if (!document.getElementById("inputChat").checked) | |
564 | document.getElementById("chatBtn").style.backgroundColor = "#c5fefe"; | |
565 | }, | |
430a2038 | 566 | gameOver: function(score, scoreMsg) { |
430a2038 BA |
567 | this.game.score = score; |
568 | this.game.scoreMsg = scoreMsg; | |
ab6f48ea BA |
569 | const myIdx = this.game.players.findIndex(p => { |
570 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
571 | }); | |
572 | if (myIdx >= 0) //OK, I play in this game | |
dcd68c41 BA |
573 | { |
574 | GameStorage.update(this.gameRef.id, | |
575 | {score: score, scoreMsg: scoreMsg}); | |
576 | } | |
ce87ac6a | 577 | }, |
a6088c90 BA |
578 | }, |
579 | }; | |
580 | </script> | |
7e1a1fe9 | 581 | |
41c80bb6 | 582 | <style lang="sass" scoped> |
72ccbd67 | 583 | .connected |
050ae3b5 | 584 | background-color: lightgreen |
72ccbd67 | 585 | |
430a2038 BA |
586 | @media screen and (min-width: 768px) |
587 | #actions | |
588 | width: 300px | |
589 | @media screen and (max-width: 767px) | |
590 | .game | |
591 | width: 100% | |
72ccbd67 | 592 | |
430a2038 | 593 | #actions |
cf94b843 | 594 | display: inline-block |
430a2038 | 595 | margin-top: 10px |
430a2038 BA |
596 | button |
597 | display: inline-block | |
598 | width: 33% | |
599 | margin: 0 | |
a1c48034 | 600 | |
050ae3b5 BA |
601 | @media screen and (max-width: 767px) |
602 | #aboveBoard | |
603 | text-align: center | |
604 | ||
605 | .name | |
606 | font-size: 1.5rem | |
607 | padding: 1px | |
608 | ||
609 | .time | |
610 | font-size: 2rem | |
611 | display: inline-block | |
612 | margin-left: 10px | |
613 | ||
614 | .split-names | |
615 | display: inline-block | |
616 | margin: 0 15px | |
617 | ||
430a2038 | 618 | #chat |
a1c48034 BA |
619 | padding-top: 20px |
620 | max-width: 600px | |
430a2038 | 621 | border: none; |
cf94b843 BA |
622 | |
623 | #chatBtn | |
624 | margin: 0 10px 0 0 | |
dcd68c41 BA |
625 | |
626 | .draw-sent, .draw-sent:hover | |
627 | background-color: lightyellow | |
628 | ||
629 | .draw-received, .draw-received:hover | |
630 | background-color: lightgreen | |
631 | ||
632 | .draw-threerep, .draw-threerep:hover | |
633 | background-color: #e4d1fc | |
7e1a1fe9 | 634 | </style> |