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