Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
7aa548e7 | 2 | main |
c292ebb2 BA |
3 | input#modalInfo.modal(type="checkbox") |
4 | div#infoDiv( | |
5 | role="dialog" | |
6 | data-checkbox="modalInfo" | |
7 | ) | |
8 | .card.text-center | |
9 | label.modal-close(for="modalInfo") | |
f14572c4 BA |
10 | a( |
11 | :href="'#/game/' + rematchId" | |
12 | onClick="document.getElementById('modalInfo').checked=false" | |
13 | ) | |
14 | | {{ st.tr["Rematch in progress"] }} | |
910d631b BA |
15 | input#modalChat.modal( |
16 | type="checkbox" | |
8be8238c | 17 | @click="toggleChat()" |
910d631b BA |
18 | ) |
19 | div#chatWrap( | |
20 | role="dialog" | |
21 | data-checkbox="modalChat" | |
22 | ) | |
5b4de147 | 23 | .card |
a1c48034 | 24 | label.modal-close(for="modalChat") |
ed06d9e9 | 25 | #participants |
afde7666 | 26 | span {{ st.tr["Participant(s):"] }} |
910d631b BA |
27 | span( |
28 | v-for="p in Object.values(people)" | |
7ebc0408 | 29 | v-if="participateInChat(p)" |
910d631b | 30 | ) |
ed06d9e9 | 31 | | {{ p.name }} |
7ebc0408 | 32 | span.anonymous(v-if="someAnonymousPresent()") + @nonymous |
910d631b | 33 | Chat( |
aae89b49 | 34 | ref="chatcomp" |
910d631b BA |
35 | :players="game.players" |
36 | :pastChats="game.chats" | |
910d631b | 37 | @mychat="processChat" |
db1f1f9a | 38 | @chatcleared="clearChat" |
910d631b | 39 | ) |
5b4de147 BA |
40 | input#modalConfirm.modal(type="checkbox") |
41 | div#confirmDiv(role="dialog") | |
42 | .card | |
a17ae317 BA |
43 | .diagram( |
44 | v-if="!!vr && ['all','byrow'].includes(vr.showMoves)" | |
45 | v-html="curDiag" | |
46 | ) | |
47 | p.text-center(v-else) | |
48 | span {{ st.tr["Move played:"] + " " }} | |
49 | span.bold {{ moveNotation }} | |
50 | br | |
51 | span {{ st.tr["Are you sure?"] }} | |
5b4de147 BA |
52 | .button-group#buttonsConfirm |
53 | // onClick for acceptBtn: set dynamically | |
54 | button.acceptBtn | |
55 | span {{ st.tr["Validate"] }} | |
56 | button.refuseBtn(@click="cancelMove()") | |
57 | span {{ st.tr["Cancel"] }} | |
7aa548e7 | 58 | .row |
050ae3b5 | 59 | #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2 |
2f258c37 BA |
60 | span.variant-cadence {{ game.cadence }} |
61 | span.variant-name {{ game.vname }} | |
feaf1bf7 BA |
62 | span#nextGame( |
63 | v-if="nextIds.length > 0" | |
64 | @click="showNextGame()" | |
65 | ) | |
66 | | {{ st.tr["Next_g"] }} | |
67 | button#chatBtn.tooltip( | |
68 | onClick="window.doClick('modalChat')" | |
69 | aria-label="Chat" | |
70 | ) | |
71 | img(src="/images/icons/chat.svg") | |
4f518610 | 72 | #actions(v-if="game.score=='*'") |
feaf1bf7 | 73 | button.tooltip( |
910d631b BA |
74 | @click="clickDraw()" |
75 | :class="{['draw-' + drawOffer]: true}" | |
feaf1bf7 | 76 | :aria-label="st.tr['Draw']" |
910d631b | 77 | ) |
feaf1bf7 BA |
78 | img(src="/images/icons/draw.svg") |
79 | button.tooltip( | |
910d631b BA |
80 | v-if="!!game.mycolor" |
81 | @click="abortGame()" | |
feaf1bf7 | 82 | :aria-label="st.tr['Abort']" |
910d631b | 83 | ) |
feaf1bf7 BA |
84 | img(src="/images/icons/abort.svg") |
85 | button.tooltip( | |
910d631b BA |
86 | v-if="!!game.mycolor" |
87 | @click="resign()" | |
feaf1bf7 | 88 | :aria-label="st.tr['Resign']" |
910d631b | 89 | ) |
feaf1bf7 BA |
90 | img(src="/images/icons/resign.svg") |
91 | button.tooltip( | |
28b32b4f | 92 | v-else |
c292ebb2 BA |
93 | @click="clickRematch()" |
94 | :class="{['rematch-' + rematchOffer]: true}" | |
feaf1bf7 BA |
95 | :aria-label="st.tr['Rematch']" |
96 | ) | |
97 | img(src="/images/icons/rematch.svg") | |
050ae3b5 BA |
98 | #playersInfo |
99 | p | |
5bcc9b31 BA |
100 | span.name(:class="{connected: isConnected(0)}") |
101 | | {{ game.players[0].name || "@nonymous" }} | |
57eb158f BA |
102 | span.time( |
103 | v-if="game.score=='*'" | |
104 | :class="{yourturn: !!vr && vr.turn == 'w'}" | |
105 | ) | |
106 | span.time-left {{ virtualClocks[0][0] }} | |
107 | span.time-separator(v-if="!!virtualClocks[0][1]") : | |
aae89b49 BA |
108 | span.time-right(v-if="!!virtualClocks[0][1]") |
109 | | {{ virtualClocks[0][1] }} | |
050ae3b5 | 110 | span.split-names - |
5bcc9b31 BA |
111 | span.name(:class="{connected: isConnected(1)}") |
112 | | {{ game.players[1].name || "@nonymous" }} | |
57eb158f BA |
113 | span.time( |
114 | v-if="game.score=='*'" | |
115 | :class="{yourturn: !!vr && vr.turn == 'b'}" | |
116 | ) | |
117 | span.time-left {{ virtualClocks[1][0] }} | |
118 | span.time-separator(v-if="!!virtualClocks[1][1]") : | |
aae89b49 BA |
119 | span.time-right(v-if="!!virtualClocks[1][1]") |
120 | | {{ virtualClocks[1][1] }} | |
910d631b | 121 | BaseGame( |
8477e53d | 122 | ref="basegame" |
910d631b | 123 | :game="game" |
910d631b | 124 | @newmove="processMove" |
910d631b | 125 | ) |
a6088c90 BA |
126 | </template> |
127 | ||
128 | <script> | |
46284a2f | 129 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 130 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 131 | import { store } from "@/store"; |
967a2686 | 132 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 133 | import { ppt } from "@/utils/datetime"; |
f5768809 | 134 | import { notify } from "@/utils/notifications"; |
23ecf008 | 135 | import { ajax } from "@/utils/ajax"; |
66d03f23 | 136 | import { extractTime } from "@/utils/timeControl"; |
51d87b52 | 137 | import { getRandString } from "@/utils/alea"; |
5aa14a21 BA |
138 | import { getScoreMessage } from "@/utils/scoring"; |
139 | import { getFullNotation } from "@/utils/notation"; | |
5b4de147 | 140 | import { getDiagram } from "@/utils/printDiagram"; |
dcd68c41 | 141 | import { processModalClick } from "@/utils/modalClick"; |
e71161fb | 142 | import { playMove, getFilteredMove } from "@/utils/playUndo"; |
1611a25f | 143 | import { ArrayFun } from "@/utils/array"; |
8418f0d7 | 144 | import params from "@/parameters"; |
a6088c90 | 145 | export default { |
6808d7a1 | 146 | name: "my-game", |
a6088c90 BA |
147 | components: { |
148 | BaseGame, | |
6808d7a1 | 149 | Chat |
a6088c90 | 150 | }, |
a6088c90 BA |
151 | data: function() { |
152 | return { | |
153 | st: store.state, | |
f54f4c26 BA |
154 | // gameRef can point to a corr game, local game or remote live game |
155 | gameRef: "", | |
feaf1bf7 | 156 | nextIds: [], |
aae89b49 | 157 | game: {}, //passed to BaseGame |
f5768809 | 158 | focus: false, |
aae89b49 BA |
159 | // virtualClocks will be initialized from true game.clocks |
160 | virtualClocks: [], | |
6dd02928 | 161 | vr: null, //"variant rules" object initialized from FEN |
dcd68c41 | 162 | drawOffer: "", |
585d0955 | 163 | rematchId: "", |
c292ebb2 | 164 | rematchOffer: "", |
584f81b9 | 165 | lastateAsked: false, |
dcd68c41 | 166 | people: {}, //players + observers |
760adbce | 167 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 168 | repeat: {}, //detect position repetition |
5b4de147 | 169 | curDiag: "", //for corr moves confirmation |
8418f0d7 | 170 | conn: null, |
f9c36b2d BA |
171 | roomInitialized: false, |
172 | // If newmove has wrong index: ask fullgame again: | |
57eb158f | 173 | askGameTime: 0, |
dcff8e82 | 174 | gameIsLoading: false, |
f9c36b2d BA |
175 | // If asklastate got no reply, ask again: |
176 | gotLastate: false, | |
e5c1d0fb | 177 | gotMoveIdx: -1, //last move index received |
f9c36b2d BA |
178 | // If newmove got no pingback, send again: |
179 | opponentGotMove: false, | |
51d87b52 | 180 | connexionString: "", |
882ec6fe | 181 | socketCloseListener: 0, |
a17ae317 BA |
182 | // Incomplete info games: show move played |
183 | moveNotation: "", | |
aae89b49 | 184 | // Intervals from setInterval(): |
aae89b49 BA |
185 | askLastate: null, |
186 | retrySendmove: null, | |
187 | clockUpdate: null, | |
51d87b52 BA |
188 | // Related to (killing of) self multi-connects: |
189 | newConnect: {}, | |
6808d7a1 | 190 | killed: {} |
a6088c90 BA |
191 | }; |
192 | }, | |
193 | watch: { | |
aae89b49 | 194 | $route: function(to, from) { |
7ebc0408 | 195 | if (to.path.length < 6 || to.path.substr(0, 6) != "/game/") |
1112f1fd BA |
196 | // Page change |
197 | this.cleanBeforeDestroy(); | |
198 | else if (from.params["id"] != to.params["id"]) { | |
aae89b49 BA |
199 | // Change everything: |
200 | this.cleanBeforeDestroy(); | |
5aa14a21 BA |
201 | let boardDiv = document.querySelector(".game"); |
202 | if (!!boardDiv) | |
203 | // In case of incomplete information variant: | |
204 | boardDiv.style.visibility = "hidden"; | |
aae89b49 | 205 | this.atCreation(); |
8f16069a | 206 | } else |
aae89b49 | 207 | // Same game ID |
aae89b49 | 208 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); |
6808d7a1 | 209 | } |
92a523d1 | 210 | }, |
71468011 | 211 | // NOTE: some redundant code with Hall.vue (mostly related to people array) |
a6088c90 | 212 | created: function() { |
aae89b49 | 213 | this.atCreation(); |
cdb34c93 | 214 | }, |
dcd68c41 | 215 | mounted: function() { |
42a92848 BA |
216 | ["chatWrap", "infoDiv"].forEach(eltName => { |
217 | document.getElementById(eltName) | |
218 | .addEventListener("click", processModalClick); | |
219 | }); | |
5b4de147 BA |
220 | if ("ontouchstart" in window) { |
221 | // Disable tooltips on smartphones: | |
abb5917e | 222 | document.querySelectorAll("#aboveBoard .tooltip").forEach(elt => { |
407342b6 | 223 | elt.classList.remove("tooltip"); |
5b4de147 BA |
224 | }); |
225 | } | |
dcd68c41 | 226 | }, |
8418f0d7 | 227 | beforeDestroy: function() { |
aae89b49 | 228 | this.cleanBeforeDestroy(); |
8418f0d7 | 229 | }, |
cdb34c93 | 230 | methods: { |
1112f1fd | 231 | cleanBeforeDestroy: function() { |
882ec6fe | 232 | clearInterval(this.socketCloseListener); |
1112f1fd | 233 | document.removeEventListener('visibilitychange', this.visibilityChange); |
f5768809 BA |
234 | window.removeEventListener('focus', this.onFocus); |
235 | window.removeEventListener('blur', this.onBlur); | |
7ebc0408 BA |
236 | if (!!this.askLastate) clearInterval(this.askLastate); |
237 | if (!!this.retrySendmove) clearInterval(this.retrySendmove); | |
238 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); | |
68e3aa8c | 239 | this.conn.removeEventListener("message", this.socketMessageListener); |
1112f1fd | 240 | this.send("disconnect"); |
68e3aa8c | 241 | this.conn = null; |
1112f1fd | 242 | }, |
a041d5d8 BA |
243 | visibilityChange: function() { |
244 | // TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27 | |
f5768809 BA |
245 | this.focus = (document.visibilityState == "visible"); |
246 | if (!this.focus && !!this.rematchOffer) { | |
247 | this.rematchOffer = ""; | |
248 | this.send("rematchoffer", { data: false }); | |
249 | // Do not remove rematch offer from (local) storage | |
250 | } | |
251 | this.send(this.focus ? "getfocus" : "losefocus"); | |
252 | }, | |
253 | onFocus: function() { | |
254 | this.focus = true; | |
255 | this.send("getfocus"); | |
256 | }, | |
257 | onBlur: function() { | |
258 | this.focus = false; | |
259 | if (!!this.rematchOffer) { | |
260 | this.rematchOffer = ""; | |
261 | this.send("rematchoffer", { data: false }); | |
262 | } | |
263 | this.send("losefocus"); | |
a041d5d8 | 264 | }, |
7ebc0408 BA |
265 | participateInChat: function(p) { |
266 | return Object.keys(p.tmpIds).some(x => p.tmpIds[x].focus) && !!p.name; | |
267 | }, | |
268 | someAnonymousPresent: function() { | |
269 | return ( | |
270 | Object.values(this.people).some(p => | |
271 | !p.name && Object.keys(p.tmpIds).some(x => p.tmpIds[x].focus) | |
272 | ) | |
273 | ); | |
274 | }, | |
aae89b49 | 275 | atCreation: function() { |
1112f1fd | 276 | document.addEventListener('visibilitychange', this.visibilityChange); |
f5768809 BA |
277 | window.addEventListener('focus', this.onFocus); |
278 | window.addEventListener('blur', this.onBlur); | |
aae89b49 | 279 | // 0] (Re)Set variables |
f54f4c26 BA |
280 | this.gameRef = this.$route.params["id"]; |
281 | // next = next corr games IDs to navigate faster (if applicable) | |
aae89b49 BA |
282 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); |
283 | // Always add myself to players' list | |
284 | const my = this.st.user; | |
7ebc0408 | 285 | const tmpId = getRandString(); |
a041d5d8 BA |
286 | this.$set( |
287 | this.people, | |
288 | my.sid, | |
289 | { | |
290 | id: my.id, | |
291 | name: my.name, | |
7ebc0408 BA |
292 | tmpIds: { |
293 | tmpId: { focus: true } | |
294 | } | |
a041d5d8 BA |
295 | } |
296 | ); | |
aae89b49 BA |
297 | this.game = { |
298 | players: [{ name: "" }, { name: "" }], | |
299 | chats: [], | |
300 | rendered: false | |
301 | }; | |
302 | let chatComp = this.$refs["chatcomp"]; | |
303 | if (!!chatComp) chatComp.chats = []; | |
304 | this.virtualClocks = [[0,0], [0,0]]; | |
305 | this.vr = null; | |
306 | this.drawOffer = ""; | |
584f81b9 | 307 | this.lastateAsked = false; |
c292ebb2 | 308 | this.rematchOffer = ""; |
aae89b49 | 309 | this.lastate = undefined; |
aae89b49 BA |
310 | this.roomInitialized = false; |
311 | this.askGameTime = 0; | |
312 | this.gameIsLoading = false; | |
313 | this.gotLastate = false; | |
314 | this.gotMoveIdx = -1; | |
315 | this.opponentGotMove = false; | |
aae89b49 BA |
316 | this.askLastate = null; |
317 | this.retrySendmove = null; | |
318 | this.clockUpdate = null; | |
319 | this.newConnect = {}; | |
320 | this.killed = {}; | |
321 | // 1] Initialize connection | |
322 | this.connexionString = | |
323 | params.socketUrl + | |
7ebc0408 BA |
324 | "/?sid=" + this.st.user.sid + |
325 | "&id=" + this.st.user.id + | |
326 | "&tmpId=" + tmpId + | |
aae89b49 BA |
327 | "&page=" + |
328 | // Discard potential "/?next=[...]" for page indication: | |
329 | encodeURIComponent(this.$route.path.match(/\/game\/[a-zA-Z0-9]+/)[0]); | |
330 | this.conn = new WebSocket(this.connexionString); | |
3f22c2c3 | 331 | this.conn.addEventListener("message", this.socketMessageListener); |
882ec6fe BA |
332 | this.socketCloseListener = setInterval( |
333 | () => { | |
334 | if (this.conn.readyState == 3) { | |
335 | this.conn.removeEventListener("message", this.socketMessageListener); | |
336 | this.conn = new WebSocket(this.connexionString); | |
337 | this.conn.addEventListener("message", this.socketMessageListener); | |
338 | } | |
339 | }, | |
340 | 1000 | |
341 | ); | |
aae89b49 BA |
342 | // Socket init required before loading remote game: |
343 | const socketInit = callback => { | |
3f22c2c3 | 344 | if (this.conn.readyState == 1) |
aae89b49 BA |
345 | // 1 == OPEN state |
346 | callback(); | |
347 | else | |
348 | // Socket not ready yet (initial loading) | |
f54f4c26 | 349 | // NOTE: first arg is Websocket object, unused here: |
aae89b49 BA |
350 | this.conn.onopen = () => callback(); |
351 | }; | |
f54f4c26 BA |
352 | this.fetchGame((game) => { |
353 | if (!!game) | |
354 | this.loadVariantThenGame(game, () => socketInit(this.roomInit)); | |
355 | else | |
356 | // Live game stored remotely: need socket to retrieve it | |
357 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. | |
358 | // --> It will be given when receiving "fullgame" socket event. | |
359 | socketInit(() => { this.send("askfullgame"); }); | |
360 | }); | |
aae89b49 | 361 | }, |
760adbce | 362 | roomInit: function() { |
f9c36b2d BA |
363 | if (!this.roomInitialized) { |
364 | // Notify the room only now that I connected, because | |
365 | // messages might be lost otherwise (if game loading is slow) | |
366 | this.send("connect"); | |
367 | this.send("pollclients"); | |
368 | // We may ask fullgame several times if some moves are lost, | |
369 | // but room should be init only once: | |
370 | this.roomInitialized = true; | |
371 | } | |
71468011 BA |
372 | }, |
373 | send: function(code, obj) { | |
f9c36b2d | 374 | if (!!this.conn) |
6808d7a1 | 375 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
5f131484 | 376 | }, |
050ae3b5 | 377 | isConnected: function(index) { |
29ced362 | 378 | const player = this.game.players[index]; |
a041d5d8 | 379 | // Is it me ? In this case no need to bother with focus |
0234201f | 380 | if (this.st.user.sid == player.sid || this.st.user.id == player.id) |
0a17525e BA |
381 | // Still have to check for name (because of potential multi-accounts |
382 | // on same browser, although this should be rare...) | |
383 | return (!this.st.user.name || this.st.user.name == player.name); | |
29ced362 | 384 | // Try to find a match in people: |
6808d7a1 | 385 | return ( |
1611a25f | 386 | ( |
a041d5d8 | 387 | !!player.sid && |
7ebc0408 BA |
388 | Object.keys(this.people).some(sid => { |
389 | return ( | |
390 | sid == player.sid && | |
391 | Object.values(this.people[sid].tmpIds).some(v => v.focus) | |
392 | ); | |
393 | }) | |
1611a25f BA |
394 | ) |
395 | || | |
396 | ( | |
f14572c4 | 397 | !!player.id && |
7ebc0408 BA |
398 | Object.values(this.people).some(p => { |
399 | return ( | |
400 | p.id == player.id && | |
401 | Object.values(p.tmpIds).some(v => v.focus) | |
402 | ); | |
403 | }) | |
1611a25f | 404 | ) |
6808d7a1 | 405 | ); |
050ae3b5 | 406 | }, |
c292ebb2 BA |
407 | getOppsid: function() { |
408 | let oppsid = this.game.oppsid; | |
409 | if (!oppsid) { | |
410 | oppsid = Object.keys(this.people).find( | |
411 | sid => this.people[sid].id == this.game.oppid | |
412 | ); | |
413 | } | |
414 | // oppsid is useful only if opponent is online: | |
415 | if (!!oppsid && !!this.people[oppsid]) return oppsid; | |
416 | return null; | |
417 | }, | |
8be8238c BA |
418 | toggleChat: function() { |
419 | if (document.getElementById("modalChat").checked) | |
420 | // Entering chat | |
421 | document.getElementById("inputChat").focus(); | |
422 | // TODO: next line is only required when exiting chat, | |
423 | // but the event for now isn't well detected. | |
db1f1f9a BA |
424 | document.getElementById("chatBtn").classList.remove("somethingnew"); |
425 | }, | |
426 | processChat: function(chat) { | |
427 | this.send("newchat", { data: chat }); | |
428 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) | |
429 | if (this.game.type == "corr" && this.st.user.id > 0) | |
aae89b49 | 430 | this.updateCorrGame({ chat: chat }); |
1a021529 BA |
431 | else if (this.game.type == "live") { |
432 | chat.added = Date.now(); | |
433 | GameStorage.update(this.gameRef, { chat: chat }); | |
434 | } | |
db1f1f9a BA |
435 | }, |
436 | clearChat: function() { | |
1a021529 BA |
437 | if (!!this.game.mycolor) { |
438 | if (this.game.type == "corr") { | |
e57c4de4 BA |
439 | ajax( |
440 | "/chats", | |
441 | "DELETE", | |
442 | { data: { gid: this.game.id } } | |
443 | ); | |
1a021529 BA |
444 | } else { |
445 | // Live game | |
446 | GameStorage.update(this.gameRef, { delchat: true }); | |
e57c4de4 | 447 | } |
dcff8e82 | 448 | this.$set(this.game, "chats", []); |
db1f1f9a BA |
449 | } |
450 | }, | |
584f81b9 BA |
451 | getGameType: function(game) { |
452 | return game.cadence.indexOf("d") >= 0 ? "corr" : "live"; | |
453 | }, | |
cafe0166 BA |
454 | // Notify something after a new move (to opponent and me on MyGames page) |
455 | notifyMyGames: function(thing, data) { | |
456 | this.send( | |
457 | "notify" + thing, | |
458 | { | |
459 | data: data, | |
460 | targets: this.game.players.map(p => { | |
0234201f | 461 | return { sid: p.sid, id: p.id }; |
cafe0166 BA |
462 | }) |
463 | } | |
464 | ); | |
1611a25f | 465 | }, |
feaf1bf7 BA |
466 | showNextGame: function() { |
467 | // Did I play in current game? If not, add it to nextIds list | |
468 | if (this.game.score == "*" && this.vr.turn == this.game.mycolor) | |
469 | this.nextIds.unshift(this.game.id); | |
470 | const nextGid = this.nextIds.pop(); | |
471 | this.$router.push( | |
472 | "/game/" + nextGid + "/?next=" + JSON.stringify(this.nextIds)); | |
473 | }, | |
d6f08e56 BA |
474 | askGameAgain: function() { |
475 | this.gameIsLoading = true; | |
aae89b49 | 476 | const currentUrl = document.location.href; |
57eb158f | 477 | const doAskGame = () => { |
e01e086d | 478 | if (document.location.href != currentUrl) return; //page change |
f54f4c26 BA |
479 | this.fetchGame((game) => { |
480 | if (!!game) | |
481 | // This is my game: just reload. | |
482 | this.loadGame(game); | |
483 | else | |
484 | // Just ask fullgame again (once!), this is much simpler. | |
485 | // If this fails, the user could just reload page :/ | |
486 | this.send("askfullgame"); | |
487 | }); | |
57eb158f BA |
488 | }; |
489 | // Delay of at least 2s between two game requests | |
490 | const now = Date.now(); | |
491 | const delay = Math.max(2000 - (now - this.askGameTime), 0); | |
492 | this.askGameTime = now; | |
493 | setTimeout(doAskGame, delay); | |
d6f08e56 | 494 | }, |
cdb34c93 | 495 | socketMessageListener: function(msg) { |
6808d7a1 | 496 | if (!this.conn) return; |
a6088c90 | 497 | const data = JSON.parse(msg.data); |
6808d7a1 | 498 | switch (data.code) { |
5f131484 | 499 | case "pollclients": |
7ebc0408 BA |
500 | // TODO: shuffling and random filtering on server, |
501 | // if the room is really crowded. | |
502 | Object.keys(data.sockIds).forEach(sid => { | |
a041d5d8 | 503 | if (sid != this.st.user.sid) { |
6808d7a1 | 504 | this.send("askidentity", { target: sid }); |
0d5335de BA |
505 | this.people[sid] = { tmpIds: data.sockIds[sid] }; |
506 | } else { | |
507 | // Complete my tmpIds: | |
508 | Object.assign(this.people[sid].tmpIds, data.sockIds[sid]); | |
a041d5d8 | 509 | } |
5f131484 BA |
510 | }); |
511 | break; | |
71468011 | 512 | case "connect": |
7ebc0408 BA |
513 | if (!this.people[data.from[0]]) { |
514 | // focus depends on the tmpId (e.g. tab) | |
515 | this.$set( | |
516 | this.people, | |
517 | data.from[0], | |
518 | { | |
519 | tmpIds: { | |
520 | [data.from[1]]: { focus: true } | |
521 | } | |
522 | } | |
523 | ); | |
51d87b52 | 524 | this.newConnect[data.from] = true; //for self multi-connects tests |
7ebc0408 BA |
525 | this.send("askidentity", { target: data.from[0] }); |
526 | } else { | |
527 | this.people[data.from[0]].tmpIds[data.from[1]] = { focus: true }; | |
a2bd587a | 528 | this.$forceUpdate(); //TODO: shouldn't be required |
51d87b52 | 529 | } |
71468011 BA |
530 | break; |
531 | case "disconnect": | |
7ebc0408 BA |
532 | if (!this.people[data.from[0]]) return; |
533 | delete this.people[data.from[0]].tmpIds[data.from[1]]; | |
534 | if (Object.keys(this.people[data.from[0]].tmpIds).length == 0) | |
535 | this.$delete(this.people, data.from[0]); | |
536 | else this.$forceUpdate(); //TODO: shouldn't be required | |
71468011 | 537 | break; |
a041d5d8 | 538 | case "getfocus": { |
7ebc0408 | 539 | let player = this.people[data.from[0]]; |
a041d5d8 | 540 | if (!!player) { |
7ebc0408 | 541 | player.tmpIds[data.from[1]].focus = true; |
a041d5d8 BA |
542 | this.$forceUpdate(); //TODO: shouldn't be required |
543 | } | |
544 | break; | |
545 | } | |
546 | case "losefocus": { | |
7ebc0408 | 547 | let player = this.people[data.from[0]]; |
a041d5d8 | 548 | if (!!player) { |
7ebc0408 | 549 | player.tmpIds[data.from[1]].focus = false; |
a041d5d8 BA |
550 | this.$forceUpdate(); //TODO: shouldn't be required |
551 | } | |
552 | break; | |
553 | } | |
51d87b52 BA |
554 | case "killed": |
555 | // I logged in elsewhere: | |
3f22c2c3 BA |
556 | this.conn.removeEventListener("message", this.socketMessageListener); |
557 | this.conn.removeEventListener("close", this.socketCloseListener); | |
51d87b52 | 558 | this.conn = null; |
09d37571 | 559 | alert(this.st.tr["New connexion detected: tab now offline"]); |
51d87b52 | 560 | break; |
6808d7a1 | 561 | case "askidentity": { |
efdfb4c7 | 562 | // Request for identification |
51d87b52 BA |
563 | const me = { |
564 | // Decompose to avoid revealing email | |
565 | name: this.st.user.name, | |
566 | sid: this.st.user.sid, | |
6808d7a1 | 567 | id: this.st.user.id |
51d87b52 | 568 | }; |
6808d7a1 | 569 | this.send("identity", { data: me, target: data.from }); |
5f131484 | 570 | break; |
51d87b52 | 571 | } |
6808d7a1 | 572 | case "identity": { |
71468011 | 573 | const user = data.data; |
a041d5d8 | 574 | let player = this.people[user.sid]; |
7ebc0408 | 575 | // player.tmpIds is already set |
a041d5d8 BA |
576 | player.name = user.name; |
577 | player.id = user.id; | |
578 | this.$forceUpdate(); //TODO: shouldn't be required | |
f9c36b2d BA |
579 | // If I multi-connect, kill current connexion if no mark (I'm older) |
580 | if (this.newConnect[user.sid]) { | |
6808d7a1 | 581 | if ( |
6808d7a1 BA |
582 | user.id > 0 && |
583 | user.id == this.st.user.id && | |
f9c36b2d BA |
584 | user.sid != this.st.user.sid && |
585 | !this.killed[this.st.user.sid] | |
6808d7a1 | 586 | ) { |
6808d7a1 | 587 | this.send("killme", { sid: this.st.user.sid }); |
51d87b52 | 588 | this.killed[this.st.user.sid] = true; |
51d87b52 | 589 | } |
f9c36b2d | 590 | delete this.newConnect[user.sid]; |
a0c41e7e | 591 | } |
dcff8e82 BA |
592 | if (!this.killed[this.st.user.sid]) { |
593 | // Ask potentially missed last state, if opponent and I play | |
594 | if ( | |
6d963536 | 595 | !this.gotLastate && |
dcff8e82 BA |
596 | !!this.game.mycolor && |
597 | this.game.type == "live" && | |
598 | this.game.score == "*" && | |
599 | this.game.players.some(p => p.sid == user.sid) | |
600 | ) { | |
aae89b49 | 601 | this.send("asklastate", { target: user.sid }); |
e01e086d | 602 | let counter = 1; |
aae89b49 BA |
603 | this.askLastate = setInterval( |
604 | () => { | |
e01e086d BA |
605 | // Ask at most 3 times: |
606 | // if no reply after that there should be a network issue. | |
607 | if ( | |
608 | counter < 3 && | |
609 | !this.gotLastate && | |
610 | !!this.people[user.sid] | |
611 | ) { | |
aae89b49 | 612 | this.send("asklastate", { target: user.sid }); |
e01e086d BA |
613 | counter++; |
614 | } else { | |
aae89b49 | 615 | clearInterval(this.askLastate); |
e01e086d | 616 | } |
aae89b49 | 617 | }, |
e01e086d | 618 | 1500 |
aae89b49 | 619 | ); |
dcff8e82 BA |
620 | } |
621 | } | |
a0c41e7e | 622 | break; |
71468011 BA |
623 | } |
624 | case "askgame": | |
625 | // Send current (live) game if not asked by any of the players | |
6808d7a1 BA |
626 | if ( |
627 | this.game.type == "live" && | |
628 | this.game.players.every(p => p.sid != data.from[0]) | |
629 | ) { | |
71468011 BA |
630 | const myGame = { |
631 | id: this.game.id, | |
632 | fen: this.game.fen, | |
633 | players: this.game.players, | |
634 | vid: this.game.vid, | |
635 | cadence: this.game.cadence, | |
f54f4c26 | 636 | score: this.game.score |
71468011 | 637 | }; |
6808d7a1 | 638 | this.send("game", { data: myGame, target: data.from }); |
71468011 BA |
639 | } |
640 | break; | |
641 | case "askfullgame": | |
e8da204a BA |
642 | const gameToSend = Object.keys(this.game) |
643 | .filter(k => | |
644 | [ | |
645 | "id","fen","players","vid","cadence","fenStart","vname", | |
3f22c2c3 | 646 | "moves","clocks","score","drawOffer","rematchOffer" |
e8da204a BA |
647 | ].includes(k)) |
648 | .reduce( | |
649 | (obj, k) => { | |
650 | obj[k] = this.game[k]; | |
651 | return obj; | |
652 | }, | |
653 | {} | |
654 | ); | |
655 | this.send("fullgame", { data: gameToSend, target: data.from }); | |
71468011 BA |
656 | break; |
657 | case "fullgame": | |
c7550017 BA |
658 | if (!!data.data.empty) { |
659 | alert(this.st.tr["The game should be in another tab"]); | |
660 | this.$router.go(-1); | |
661 | } | |
662 | else | |
663 | // Callback "roomInit" to poll clients only after game is loaded | |
664 | this.loadVariantThenGame(data.data, this.roomInit); | |
71468011 | 665 | break; |
a0c41e7e | 666 | case "asklastate": |
dcff8e82 | 667 | // Sending informative last state if I played a move or score != "*" |
584f81b9 | 668 | // If the game or moves aren't loaded yet, delay the sending: |
7ebc0408 | 669 | // TODO: since socket init after game load, the game is supposedly ready |
584f81b9 BA |
670 | if (!this.game || !this.game.moves) this.lastateAsked = true; |
671 | else this.sendLastate(data.from); | |
c6788ecf | 672 | break; |
dcff8e82 BA |
673 | case "lastate": { |
674 | // Got opponent infos about last move | |
675 | this.gotLastate = true; | |
3f22c2c3 BA |
676 | this.lastate = data.data; |
677 | if (this.game.rendered) | |
678 | // Game is rendered (Board component) | |
679 | this.processLastate(); | |
680 | // Else: will be processed when game is ready | |
71468011 | 681 | break; |
dcff8e82 | 682 | } |
6808d7a1 | 683 | case "newmove": { |
dcff8e82 BA |
684 | const movePlus = data.data; |
685 | const movesCount = this.game.moves.length; | |
686 | if (movePlus.index > movesCount) { | |
687 | // This can only happen if I'm an observer and missed a move. | |
57eb158f BA |
688 | if (this.gotMoveIdx < movePlus.index) |
689 | this.gotMoveIdx = movePlus.index; | |
d6f08e56 BA |
690 | if (!this.gameIsLoading) this.askGameAgain(); |
691 | } | |
692 | else { | |
f9c36b2d | 693 | if ( |
dcff8e82 BA |
694 | movePlus.index < movesCount || |
695 | this.gotMoveIdx >= movePlus.index | |
f9c36b2d BA |
696 | ) { |
697 | // Opponent re-send but we already have the move: | |
698 | // (maybe he didn't receive our pingback...) | |
dcff8e82 | 699 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
f9c36b2d | 700 | } else { |
dcff8e82 BA |
701 | this.gotMoveIdx = movePlus.index; |
702 | const receiveMyMove = (movePlus.color == this.game.mycolor); | |
f5768809 BA |
703 | const moveColIdx = ["w", "b"].indexOf(movePlus.color); |
704 | if (!receiveMyMove && !!this.game.mycolor) { | |
f9c36b2d | 705 | // Notify opponent that I got the move: |
dcff8e82 | 706 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
f5768809 BA |
707 | // And myself if I'm elsewhere: |
708 | if (!this.focus) { | |
709 | notify( | |
710 | "New move", | |
711 | { | |
712 | body: | |
713 | (this.game.players[moveColIdx].name || "@nonymous") + | |
714 | " just played." | |
715 | } | |
716 | ); | |
717 | } | |
718 | } | |
dcff8e82 | 719 | if (movePlus.cancelDrawOffer) { |
f9c36b2d BA |
720 | // Opponent refuses draw |
721 | this.drawOffer = ""; | |
722 | // NOTE for corr games: drawOffer reset by player in turn | |
723 | if ( | |
724 | this.game.type == "live" && | |
725 | !!this.game.mycolor && | |
726 | !receiveMyMove | |
727 | ) { | |
f54f4c26 | 728 | GameStorage.update(this.gameRef, { drawOffer: "" }); |
f9c36b2d BA |
729 | } |
730 | } | |
57eb158f | 731 | this.$refs["basegame"].play(movePlus.move, "received", null, true); |
3f22c2c3 | 732 | this.game.clocks[moveColIdx] = movePlus.clock; |
57eb158f | 733 | this.processMove( |
dcff8e82 | 734 | movePlus.move, |
3f22c2c3 | 735 | { receiveMyMove: receiveMyMove } |
f9c36b2d BA |
736 | ); |
737 | } | |
633959bf | 738 | } |
a6088c90 | 739 | break; |
71468011 | 740 | } |
f9c36b2d BA |
741 | case "gotmove": { |
742 | this.opponentGotMove = true; | |
3f22c2c3 | 743 | // Now his clock starts running on my side: |
e01e086d | 744 | const oppIdx = ['w','b'].indexOf(this.vr.turn); |
7ebc0408 BA |
745 | // NOTE: next line to avoid multi-resetClocks when several tabs |
746 | // on same game, resulting in a faster countdown. | |
747 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); | |
e01e086d | 748 | this.re_setClocks(); |
f9c36b2d BA |
749 | break; |
750 | } | |
93d1d7a7 | 751 | case "resign": |
059228c9 BA |
752 | const score = (data.data == "b" ? "1-0" : "0-1"); |
753 | const side = (data.data == "w" ? "White" : "Black"); | |
8477e53d | 754 | this.gameOver(score, side + " surrender"); |
93d1d7a7 | 755 | break; |
93d1d7a7 | 756 | case "abort": |
8477e53d | 757 | this.gameOver("?", "Stop"); |
93d1d7a7 | 758 | break; |
2cc10cdb | 759 | case "draw": |
71468011 | 760 | this.gameOver("1/2", data.data); |
2cc10cdb BA |
761 | break; |
762 | case "drawoffer": | |
41c80bb6 BA |
763 | // NOTE: observers don't know who offered draw |
764 | this.drawOffer = "received"; | |
f54f4c26 BA |
765 | if (this.game.type == "live") { |
766 | GameStorage.update( | |
767 | this.gameRef, | |
768 | { drawOffer: V.GetOppCol(this.game.mycolor) } | |
769 | ); | |
770 | } | |
6d9f4315 | 771 | break; |
c292ebb2 BA |
772 | case "rematchoffer": |
773 | // NOTE: observers don't know who offered rematch | |
774 | this.rematchOffer = data.data ? "received" : ""; | |
f54f4c26 BA |
775 | if (this.game.type == "live") { |
776 | GameStorage.update( | |
777 | this.gameRef, | |
778 | { rematchOffer: V.GetOppCol(this.game.mycolor) } | |
779 | ); | |
780 | } | |
c292ebb2 BA |
781 | break; |
782 | case "newgame": { | |
783 | // A game started, redirect if I'm playing in | |
784 | const gameInfo = data.data; | |
584f81b9 | 785 | const gameType = this.getGameType(gameInfo); |
c292ebb2 | 786 | if ( |
584f81b9 BA |
787 | gameType == "live" && |
788 | gameInfo.players.some(p => p.sid == this.st.user.sid) | |
789 | ) { | |
790 | this.addAndGotoLiveGame(gameInfo); | |
791 | } else if ( | |
792 | gameType == "corr" && | |
0234201f | 793 | gameInfo.players.some(p => p.id == this.st.user.id) |
c292ebb2 BA |
794 | ) { |
795 | this.$router.push("/game/" + gameInfo.id); | |
796 | } else { | |
f54f4c26 | 797 | this.rematchId = gameInfo.id; |
c292ebb2 BA |
798 | document.getElementById("modalInfo").checked = true; |
799 | } | |
800 | break; | |
801 | } | |
1a021529 BA |
802 | case "newchat": { |
803 | let chat = data.data; | |
804 | this.$refs["chatcomp"].newChat(chat); | |
805 | if (this.game.type == "live") { | |
806 | chat.added = Date.now(); | |
807 | GameStorage.update(this.gameRef, { chat: chat }); | |
808 | } | |
71468011 | 809 | if (!document.getElementById("modalChat").checked) |
2f258c37 | 810 | document.getElementById("chatBtn").classList.add("somethingnew"); |
a6088c90 | 811 | break; |
1a021529 | 812 | } |
a6088c90 | 813 | } |
cdb34c93 | 814 | }, |
5aa14a21 | 815 | updateCorrGame: function(obj, callback) { |
aae89b49 BA |
816 | ajax( |
817 | "/games", | |
818 | "PUT", | |
819 | { | |
e57c4de4 | 820 | data: { |
f54f4c26 | 821 | gid: this.gameRef, |
e57c4de4 BA |
822 | newObj: obj |
823 | }, | |
824 | success: () => { | |
825 | if (!!callback) callback(); | |
826 | } | |
aae89b49 BA |
827 | } |
828 | ); | |
829 | }, | |
584f81b9 | 830 | sendLastate: function(target) { |
3f22c2c3 BA |
831 | // Send our "last state" informations to opponent |
832 | const L = this.game.moves.length; | |
833 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); | |
834 | const myLastate = { | |
835 | lastMove: | |
836 | (L > 0 && this.vr.turn != this.game.mycolor) | |
837 | ? this.game.moves[L - 1] | |
838 | : undefined, | |
839 | clock: this.game.clocks[myIdx], | |
840 | // Since we played a move (or abort or resign), | |
841 | // only drawOffer=="sent" is possible | |
842 | drawSent: this.drawOffer == "sent", | |
843 | rematchSent: this.rematchOffer == "sent", | |
844 | score: this.game.score != "*" ? this.game.score : undefined, | |
845 | scoreMsg: this.game.score != "*" ? this.game.scoreMsg : undefined, | |
846 | movesCount: L | |
847 | }; | |
848 | this.send("lastate", { data: myLastate, target: target }); | |
584f81b9 | 849 | }, |
760adbce BA |
850 | // lastate was received, but maybe game wasn't ready yet: |
851 | processLastate: function() { | |
852 | const data = this.lastate; | |
853 | this.lastate = undefined; //security... | |
854 | const L = this.game.moves.length; | |
3f22c2c3 BA |
855 | const oppIdx = 1 - ["w", "b"].indexOf(this.game.mycolor); |
856 | this.game.clocks[oppIdx] = data.clock; | |
6808d7a1 | 857 | if (data.movesCount > L) { |
760adbce | 858 | // Just got last move from him |
e01e086d | 859 | this.$refs["basegame"].play(data.lastMove, "received", null, true); |
3f22c2c3 BA |
860 | this.processMove(data.lastMove); |
861 | } else { | |
7ebc0408 | 862 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); |
3f22c2c3 | 863 | this.re_setClocks(); |
a0c41e7e | 864 | } |
6808d7a1 | 865 | if (data.drawSent) this.drawOffer = "received"; |
c292ebb2 | 866 | if (data.rematchSent) this.rematchOffer = "received"; |
3f22c2c3 | 867 | if (!!data.score) { |
a0c41e7e | 868 | this.drawOffer = ""; |
5aa14a21 | 869 | if (this.game.score == "*") |
a17ae317 | 870 | this.gameOver(data.score, data.scoreMsg); |
760adbce BA |
871 | } |
872 | }, | |
dcd68c41 | 873 | clickDraw: function() { |
6808d7a1 BA |
874 | if (!this.game.mycolor) return; //I'm just spectator |
875 | if (["received", "threerep"].includes(this.drawOffer)) { | |
876 | if (!confirm(this.st.tr["Accept draw?"])) return; | |
877 | const message = | |
878 | this.drawOffer == "received" | |
879 | ? "Mutual agreement" | |
880 | : "Three repetitions"; | |
881 | this.send("draw", { data: message }); | |
77c50966 | 882 | this.gameOver("1/2", message); |
6808d7a1 | 883 | } else if (this.drawOffer == "") { |
e71161fb | 884 | // No effect if drawOffer == "sent" |
9ee2826a | 885 | if (this.game.mycolor != this.vr.turn) { |
6808d7a1 | 886 | alert(this.st.tr["Draw offer only in your turn"]); |
6fba6e0c | 887 | return; |
6808d7a1 BA |
888 | } |
889 | if (!confirm(this.st.tr["Offer draw?"])) return; | |
760adbce | 890 | this.drawOffer = "sent"; |
71468011 | 891 | this.send("drawoffer"); |
aae89b49 BA |
892 | if (this.game.type == "live") { |
893 | GameStorage.update( | |
f54f4c26 | 894 | this.gameRef, |
aae89b49 BA |
895 | { drawOffer: this.game.mycolor } |
896 | ); | |
897 | } else this.updateCorrGame({ drawOffer: this.game.mycolor }); | |
a6088c90 BA |
898 | } |
899 | }, | |
584f81b9 BA |
900 | addAndGotoLiveGame: function(gameInfo, callback) { |
901 | const game = Object.assign( | |
902 | {}, | |
903 | gameInfo, | |
904 | { | |
905 | // (other) Game infos: constant | |
906 | fenStart: gameInfo.fen, | |
907 | vname: this.game.vname, | |
908 | created: Date.now(), | |
909 | // Game state (including FEN): will be updated | |
910 | moves: [], | |
911 | clocks: [-1, -1], //-1 = unstarted | |
584f81b9 BA |
912 | score: "*" |
913 | } | |
914 | ); | |
915 | GameStorage.add(game, (err) => { | |
916 | // No error expected. | |
917 | if (!err) { | |
918 | if (this.st.settings.sound) | |
919 | new Audio("/sounds/newgame.flac").play().catch(() => {}); | |
585d0955 | 920 | if (!!callback) callback(); |
584f81b9 BA |
921 | this.$router.push("/game/" + gameInfo.id); |
922 | } | |
923 | }); | |
924 | }, | |
c292ebb2 BA |
925 | clickRematch: function() { |
926 | if (!this.game.mycolor) return; //I'm just spectator | |
927 | if (this.rematchOffer == "received") { | |
928 | // Start a new game! | |
929 | let gameInfo = { | |
930 | id: getRandString(), //ignored if corr | |
931 | fen: V.GenRandInitFen(this.game.randomness), | |
932 | players: this.game.players.reverse(), | |
933 | vid: this.game.vid, | |
934 | cadence: this.game.cadence | |
935 | }; | |
584f81b9 | 936 | const notifyNewGame = () => { |
f14572c4 | 937 | const oppsid = this.getOppsid(); //may be null |
584f81b9 | 938 | this.send("rnewgame", { data: gameInfo, oppsid: oppsid }); |
f14572c4 BA |
939 | // To main Hall if corr game: |
940 | if (this.game.type == "corr") | |
7ebc0408 | 941 | this.send("newgame", { data: gameInfo, page: "/" }); |
cafe0166 BA |
942 | // Also to MyGames page: |
943 | this.notifyMyGames("newgame", gameInfo); | |
584f81b9 BA |
944 | }; |
945 | if (this.game.type == "live") | |
946 | this.addAndGotoLiveGame(gameInfo, notifyNewGame); | |
c292ebb2 BA |
947 | else { |
948 | // corr game | |
949 | ajax( | |
950 | "/games", | |
951 | "POST", | |
952 | { | |
953 | // cid is useful to delete the challenge: | |
954 | data: { gameInfo: gameInfo }, | |
955 | success: (response) => { | |
956 | gameInfo.id = response.gameId; | |
584f81b9 | 957 | notifyNewGame(); |
c292ebb2 BA |
958 | this.$router.push("/game/" + response.gameId); |
959 | } | |
960 | } | |
961 | ); | |
962 | } | |
963 | } else if (this.rematchOffer == "") { | |
964 | this.rematchOffer = "sent"; | |
965 | this.send("rematchoffer", { data: true }); | |
966 | if (this.game.type == "live") { | |
967 | GameStorage.update( | |
f54f4c26 | 968 | this.gameRef, |
c292ebb2 BA |
969 | { rematchOffer: this.game.mycolor } |
970 | ); | |
971 | } else this.updateCorrGame({ rematchOffer: this.game.mycolor }); | |
972 | } else if (this.rematchOffer == "sent") { | |
973 | // Toggle rematch offer (on --> off) | |
974 | this.rematchOffer = ""; | |
975 | this.send("rematchoffer", { data: false }); | |
976 | if (this.game.type == "live") { | |
977 | GameStorage.update( | |
f54f4c26 | 978 | this.gameRef, |
c292ebb2 BA |
979 | { rematchOffer: '' } |
980 | ); | |
981 | } else this.updateCorrGame({ rematchOffer: 'n' }); | |
982 | } | |
983 | }, | |
7f3484bd | 984 | abortGame: function() { |
6808d7a1 | 985 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) return; |
8477e53d | 986 | this.gameOver("?", "Stop"); |
71468011 | 987 | this.send("abort"); |
a6088c90 | 988 | }, |
6808d7a1 | 989 | resign: function() { |
77c50966 | 990 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
a6088c90 | 991 | return; |
6808d7a1 | 992 | this.send("resign", { data: this.game.mycolor }); |
059228c9 BA |
993 | const score = (this.game.mycolor == "w" ? "0-1" : "1-0"); |
994 | const side = (this.game.mycolor == "w" ? "White" : "Black"); | |
8477e53d | 995 | this.gameOver(score, side + " surrender"); |
a6088c90 | 996 | }, |
760adbce | 997 | loadGame: function(game, callback) { |
32f6285e BA |
998 | this.vr = new V(game.fen); |
999 | const gtype = this.getGameType(game); | |
1000 | const tc = extractTime(game.cadence); | |
1001 | const myIdx = game.players.findIndex(p => { | |
1002 | return p.sid == this.st.user.sid || p.id == this.st.user.id; | |
1003 | }); | |
1004 | const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers | |
1a021529 BA |
1005 | // Live games before 26/03/2020 don't have chat history. TODO: remove next line |
1006 | if (!game.chats) game.chats = []; | |
1007 | // Sort chat messages from newest to oldest | |
1008 | game.chats.sort((c1, c2) => c2.added - c1.added); | |
32f6285e | 1009 | if (gtype == "corr") { |
3f22c2c3 | 1010 | // NOTE: clocks in seconds |
32f6285e BA |
1011 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
1012 | game.clocks = [tc.mainTime, tc.mainTime]; | |
1013 | const L = game.moves.length; | |
1014 | if (game.score == "*") { | |
3f22c2c3 BA |
1015 | // Adjust clocks |
1016 | if (L >= 2) { | |
1017 | game.clocks[L % 2] -= | |
1018 | (Date.now() - game.moves[L-1].played) / 1000; | |
1019 | } | |
66d03f23 | 1020 | } |
32f6285e BA |
1021 | if (myIdx >= 0 && game.score == "*" && game.chats.length > 0) { |
1022 | // Did a chat message arrive after my last move? | |
1023 | let dtLastMove = 0; | |
1024 | if (L == 1 && myIdx == 0) | |
1025 | dtLastMove = game.moves[0].played; | |
1026 | else if (L >= 2) { | |
1027 | if (L % 2 == 0) { | |
1028 | // It's now white turn | |
1029 | dtLastMove = game.moves[L-1-(1-myIdx)].played; | |
1030 | } else { | |
1031 | // Black turn: | |
1032 | dtLastMove = game.moves[L-1-myIdx].played; | |
77c50966 BA |
1033 | } |
1034 | } | |
32f6285e BA |
1035 | if (dtLastMove < game.chats[0].added) |
1036 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
1037 | } | |
1038 | // Now that we used idx and played, re-format moves as for live games | |
1039 | game.moves = game.moves.map(m => m.squares); | |
1040 | } | |
3f22c2c3 | 1041 | if (gtype == "live") { |
1a021529 BA |
1042 | if ( |
1043 | game.chats.length > 0 && | |
1044 | (!game.initime || game.initime < game.chats[0].added) | |
1045 | ) { | |
1046 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
1047 | } | |
3f22c2c3 BA |
1048 | if (game.clocks[0] < 0) { |
1049 | // Game is unstarted. clock is ignored until move 2 | |
1050 | game.clocks = [tc.mainTime, tc.mainTime]; | |
1051 | if (myIdx >= 0) { | |
1052 | // I play in this live game | |
1053 | GameStorage.update(game.id, { | |
1054 | clocks: game.clocks | |
1055 | }); | |
1056 | } | |
1057 | } else { | |
1058 | if (!!game.initime) | |
1059 | // It's my turn: clocks not updated yet | |
1060 | game.clocks[myIdx] -= (Date.now() - game.initime) / 1000; | |
77c50966 | 1061 | } |
32f6285e BA |
1062 | } |
1063 | // TODO: merge next 2 "if" conditions | |
1064 | if (!!game.drawOffer) { | |
1065 | if (game.drawOffer == "t") | |
1066 | // Three repetitions | |
1067 | this.drawOffer = "threerep"; | |
1068 | else { | |
1069 | // Draw offered by any of the players: | |
1070 | if (myIdx < 0) this.drawOffer = "received"; | |
c292ebb2 BA |
1071 | else { |
1072 | // I play in this game: | |
1073 | if ( | |
32f6285e BA |
1074 | (game.drawOffer == "w" && myIdx == 0) || |
1075 | (game.drawOffer == "b" && myIdx == 1) | |
c292ebb2 | 1076 | ) |
32f6285e BA |
1077 | this.drawOffer = "sent"; |
1078 | else this.drawOffer = "received"; | |
c292ebb2 BA |
1079 | } |
1080 | } | |
32f6285e BA |
1081 | } |
1082 | if (!!game.rematchOffer) { | |
1083 | if (myIdx < 0) this.rematchOffer = "received"; | |
1084 | else { | |
1085 | // I play in this game: | |
1086 | if ( | |
1087 | (game.rematchOffer == "w" && myIdx == 0) || | |
1088 | (game.rematchOffer == "b" && myIdx == 1) | |
1089 | ) | |
1090 | this.rematchOffer = "sent"; | |
1091 | else this.rematchOffer = "received"; | |
5aa14a21 | 1092 | } |
32f6285e BA |
1093 | } |
1094 | this.repeat = {}; //reset: scan past moves' FEN: | |
1095 | let repIdx = 0; | |
1096 | let vr_tmp = new V(game.fenStart); | |
1097 | let curTurn = "n"; | |
1098 | game.moves.forEach(m => { | |
1099 | playMove(m, vr_tmp); | |
1100 | const fenIdx = vr_tmp.getFen().replace(/ /g, "_"); | |
1101 | this.repeat[fenIdx] = this.repeat[fenIdx] | |
1102 | ? this.repeat[fenIdx] + 1 | |
1103 | : 1; | |
1104 | }); | |
1105 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; | |
1106 | this.game = Object.assign( | |
1107 | // NOTE: assign mycolor here, since BaseGame could also be VS computer | |
1108 | { | |
1109 | type: gtype, | |
1110 | increment: tc.increment, | |
1111 | mycolor: mycolor, | |
1112 | // opponent sid not strictly required (or available), but easier | |
1113 | // at least oppsid or oppid is available anyway: | |
1114 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, | |
1115 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].id | |
1116 | }, | |
1117 | game | |
1118 | ); | |
1119 | this.$refs["basegame"].re_setVariables(this.game); | |
1120 | if (!this.gameIsLoading) { | |
1121 | // Initial loading: | |
1122 | this.gotMoveIdx = game.moves.length - 1; | |
1123 | // If we arrive here after 'nextGame' action, the board might be hidden | |
1124 | let boardDiv = document.querySelector(".game"); | |
1125 | if (!!boardDiv && boardDiv.style.visibility == "hidden") | |
1126 | boardDiv.style.visibility = "visible"; | |
1127 | } | |
1128 | this.re_setClocks(); | |
1129 | this.$nextTick(() => { | |
1130 | this.game.rendered = true; | |
1131 | // Did lastate arrive before game was rendered? | |
1132 | if (this.lastate) this.processLastate(); | |
1133 | }); | |
1134 | if (this.lastateAsked) { | |
1135 | this.lastateAsked = false; | |
1136 | this.sendLastate(game.oppsid); | |
1137 | } | |
1138 | if (this.gameIsLoading) { | |
1139 | this.gameIsLoading = false; | |
1140 | if (this.gotMoveIdx >= game.moves.length) | |
1141 | // Some moves arrived meanwhile... | |
1142 | this.askGameAgain(); | |
1143 | } | |
1144 | if (!!callback) callback(); | |
1145 | }, | |
f54f4c26 BA |
1146 | loadVariantThenGame: async function(game, callback) { |
1147 | await import("@/variants/" + game.vname + ".js") | |
1148 | .then((vModule) => { | |
1149 | window.V = vModule[game.vname + "Rules"]; | |
1150 | this.loadGame(game, callback); | |
1151 | }); | |
1152 | }, | |
1153 | // 3 cases for loading a game: | |
1154 | // - from indexedDB (running or completed live game I play) | |
1155 | // - from server (one correspondance game I play[ed] or not) | |
1156 | // - from remote peer (one live game I don't play, finished or not) | |
1157 | fetchGame: function(callback) { | |
1158 | if (Number.isInteger(this.gameRef) || !isNaN(parseInt(this.gameRef))) { | |
1159 | // corr games identifiers are integers | |
1160 | ajax( | |
1161 | "/games", | |
1162 | "GET", | |
1163 | { | |
1164 | data: { gid: this.gameRef }, | |
1165 | success: (res) => { | |
1166 | res.game.moves.forEach(m => { | |
1167 | m.squares = JSON.parse(m.squares); | |
1168 | }); | |
1169 | callback(res.game); | |
e57c4de4 | 1170 | } |
f54f4c26 BA |
1171 | } |
1172 | ); | |
1173 | } else | |
1174 | // Local game (or live remote) | |
1175 | GameStorage.get(this.gameRef, callback); | |
a6088c90 | 1176 | }, |
9ef63965 | 1177 | re_setClocks: function() { |
3f22c2c3 | 1178 | this.virtualClocks = this.game.clocks.map(s => ppt(s).split(':')); |
dcff8e82 | 1179 | if (this.game.moves.length < 2 || this.game.score != "*") { |
9ef63965 | 1180 | // 1st move not completed yet, or game over: freeze time |
9ef63965 BA |
1181 | return; |
1182 | } | |
1183 | const currentTurn = this.vr.turn; | |
8477e53d | 1184 | const currentMovesCount = this.game.moves.length; |
6808d7a1 | 1185 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
e01e086d BA |
1186 | this.clockUpdate = setInterval( |
1187 | () => { | |
1188 | if ( | |
3f22c2c3 | 1189 | this.game.clocks[colorIdx] < 0 || |
e01e086d BA |
1190 | this.game.moves.length > currentMovesCount || |
1191 | this.game.score != "*" | |
1192 | ) { | |
1193 | clearInterval(this.clockUpdate); | |
7ebc0408 | 1194 | this.clockUpdate = null; |
3f22c2c3 | 1195 | if (this.game.clocks[colorIdx] < 0) |
e01e086d BA |
1196 | this.gameOver( |
1197 | currentTurn == "w" ? "0-1" : "1-0", | |
1198 | "Time" | |
1199 | ); | |
3f22c2c3 | 1200 | } else { |
e01e086d BA |
1201 | this.$set( |
1202 | this.virtualClocks, | |
1203 | colorIdx, | |
3f22c2c3 | 1204 | ppt(Math.max(0, --this.game.clocks[colorIdx])).split(':') |
6808d7a1 | 1205 | ); |
3f22c2c3 | 1206 | } |
e01e086d BA |
1207 | }, |
1208 | 1000 | |
1209 | ); | |
9ef63965 | 1210 | }, |
57eb158f | 1211 | // Update variables and storage after a move: |
e71161fb | 1212 | processMove: function(move, data) { |
e5c1d0fb | 1213 | if (!data) data = {}; |
e71161fb | 1214 | const moveCol = this.vr.turn; |
8f16069a BA |
1215 | const colorIdx = ["w", "b"].indexOf(moveCol); |
1216 | const nextIdx = 1 - colorIdx; | |
e71161fb | 1217 | const doProcessMove = () => { |
dcff8e82 | 1218 | const origMovescount = this.game.moves.length; |
3f22c2c3 BA |
1219 | // The move is (about to be) played: stop clock |
1220 | clearInterval(this.clockUpdate); | |
7ebc0408 | 1221 | this.clockUpdate = null; |
f9c36b2d | 1222 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
e71161fb BA |
1223 | if (this.drawOffer == "received") |
1224 | // I refuse draw | |
1225 | this.drawOffer = ""; | |
dcff8e82 | 1226 | if (this.game.type == "live" && origMovescount >= 2) { |
3f22c2c3 BA |
1227 | this.game.clocks[colorIdx] += this.game.increment; |
1228 | // For a correct display in casqe of disconnected opponent: | |
1229 | this.$set( | |
1230 | this.virtualClocks, | |
1231 | colorIdx, | |
1232 | ppt(this.game.clocks[colorIdx]).split(':') | |
1233 | ); | |
1234 | GameStorage.update(this.gameRef, { | |
1235 | // It's not my turn anymore: | |
1236 | initime: null | |
1237 | }); | |
e71161fb | 1238 | } |
dce792f6 | 1239 | } |
dcff8e82 | 1240 | // Update current game object: |
e71161fb | 1241 | playMove(move, this.vr); |
f54f4c26 BA |
1242 | if (!data.score) |
1243 | // Received move, score is computed in BaseGame, but maybe not yet. | |
1244 | // ==> Compute it here, although this is redundant (TODO) | |
1245 | data.score = this.vr.getCurrentScore(); | |
1246 | if (data.score != "*") this.gameOver(data.score); | |
dcff8e82 | 1247 | this.game.moves.push(move); |
e71161fb | 1248 | this.game.fen = this.vr.getFen(); |
3f22c2c3 | 1249 | if (this.game.type == "corr") { |
8be8238c | 1250 | // In corr games, just reset clock to mainTime: |
e01e086d BA |
1251 | this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime; |
1252 | } | |
e71161fb | 1253 | // If repetition detected, consider that a draw offer was received: |
f9c36b2d | 1254 | const fenObj = this.vr.getFenForRepeat(); |
e01e086d BA |
1255 | this.repeat[fenObj] = |
1256 | !!this.repeat[fenObj] | |
1257 | ? this.repeat[fenObj] + 1 | |
1258 | : 1; | |
f9c36b2d | 1259 | if (this.repeat[fenObj] >= 3) this.drawOffer = "threerep"; |
e71161fb | 1260 | else if (this.drawOffer == "threerep") this.drawOffer = ""; |
dcff8e82 BA |
1261 | if (!!this.game.mycolor && !data.receiveMyMove) { |
1262 | // NOTE: 'var' to see that variable outside this block | |
1263 | var filtered_move = getFilteredMove(move); | |
1264 | } | |
cafe0166 BA |
1265 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
1266 | // Notify turn on MyGames page: | |
1267 | this.notifyMyGames( | |
1268 | "turn", | |
1269 | { | |
f54f4c26 | 1270 | gid: this.gameRef, |
cafe0166 BA |
1271 | turn: this.vr.turn |
1272 | } | |
1273 | ); | |
1274 | } | |
aae89b49 BA |
1275 | // Since corr games are stored at only one location, update should be |
1276 | // done only by one player for each move: | |
3f22c2c3 BA |
1277 | if ( |
1278 | this.game.type == "live" && | |
1279 | !!this.game.mycolor && | |
1280 | moveCol != this.game.mycolor && | |
1281 | this.game.moves.length >= 2 | |
1282 | ) { | |
1283 | // Receive a move: update initime | |
1284 | this.game.initime = Date.now(); | |
1285 | GameStorage.update(this.gameRef, { | |
1286 | // It's my turn now! | |
1287 | initime: this.game.initime | |
1288 | }); | |
1289 | } | |
e71161fb | 1290 | if ( |
f9c36b2d BA |
1291 | !!this.game.mycolor && |
1292 | !data.receiveMyMove && | |
e71161fb BA |
1293 | (this.game.type == "live" || moveCol == this.game.mycolor) |
1294 | ) { | |
1295 | let drawCode = ""; | |
1296 | switch (this.drawOffer) { | |
1297 | case "threerep": | |
1298 | drawCode = "t"; | |
1299 | break; | |
1300 | case "sent": | |
1301 | drawCode = this.game.mycolor; | |
1302 | break; | |
1303 | case "received": | |
1304 | drawCode = V.GetOppCol(this.game.mycolor); | |
1305 | break; | |
1306 | } | |
1307 | if (this.game.type == "corr") { | |
aae89b49 BA |
1308 | // corr: only move, fen and score |
1309 | this.updateCorrGame({ | |
e71161fb BA |
1310 | fen: this.game.fen, |
1311 | move: { | |
1312 | squares: filtered_move, | |
dcff8e82 | 1313 | idx: origMovescount |
e71161fb BA |
1314 | }, |
1315 | // Code "n" for "None" to force reset (otherwise it's ignored) | |
1316 | drawOffer: drawCode || "n" | |
1317 | }); | |
1318 | } | |
57eb158f BA |
1319 | else { |
1320 | const updateStorage = () => { | |
f54f4c26 | 1321 | GameStorage.update(this.gameRef, { |
57eb158f BA |
1322 | fen: this.game.fen, |
1323 | move: filtered_move, | |
1324 | moveIdx: origMovescount, | |
1325 | clocks: this.game.clocks, | |
57eb158f BA |
1326 | drawOffer: drawCode |
1327 | }); | |
1328 | }; | |
1329 | // The active tab can update storage immediately | |
f5768809 | 1330 | if (this.focus) updateStorage(); |
57eb158f BA |
1331 | // Small random delay otherwise |
1332 | else setTimeout(updateStorage, 500 + 1000 * Math.random()); | |
e71161fb | 1333 | } |
e69f159d | 1334 | } |
dcff8e82 BA |
1335 | // Send move ("newmove" event) to people in the room (if our turn) |
1336 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { | |
e01e086d | 1337 | let sendMove = { |
dcff8e82 BA |
1338 | move: filtered_move, |
1339 | index: origMovescount, | |
1340 | // color is required to check if this is my move (if several tabs opened) | |
1341 | color: moveCol, | |
dcff8e82 BA |
1342 | cancelDrawOffer: this.drawOffer == "" |
1343 | }; | |
e01e086d BA |
1344 | if (this.game.type == "live") |
1345 | sendMove["clock"] = this.game.clocks[colorIdx]; | |
8f16069a | 1346 | // (Live) Clocks will re-start when the opponent pingback arrive |
dcff8e82 BA |
1347 | this.opponentGotMove = false; |
1348 | this.send("newmove", {data: sendMove}); | |
1349 | // If the opponent doesn't reply gotmove soon enough, re-send move: | |
e01e086d BA |
1350 | // Do this at most 2 times, because mpore would mean network issues, |
1351 | // opponent would then be expected to disconnect/reconnect. | |
1352 | let counter = 1; | |
1353 | const currentUrl = document.location.href; | |
aae89b49 | 1354 | this.retrySendmove = setInterval( |
57eb158f | 1355 | () => { |
e01e086d BA |
1356 | if ( |
1357 | counter >= 3 || | |
1358 | this.opponentGotMove || | |
1359 | document.location.href != currentUrl //page change | |
1360 | ) { | |
aae89b49 | 1361 | clearInterval(this.retrySendmove); |
57eb158f BA |
1362 | return; |
1363 | } | |
c292ebb2 BA |
1364 | const oppsid = this.getOppsid(); |
1365 | if (!oppsid) | |
57eb158f | 1366 | // Opponent is disconnected: he'll ask last state |
aae89b49 | 1367 | clearInterval(this.retrySendmove); |
e01e086d BA |
1368 | else { |
1369 | this.send("newmove", { data: sendMove, target: oppsid }); | |
1370 | counter++; | |
1371 | } | |
57eb158f | 1372 | }, |
e01e086d | 1373 | 1500 |
57eb158f | 1374 | ); |
dcff8e82 | 1375 | } |
e01e086d BA |
1376 | else |
1377 | // Not my move or I'm an observer: just start other player's clock | |
1378 | this.re_setClocks(); | |
e71161fb | 1379 | }; |
f9c36b2d BA |
1380 | if ( |
1381 | this.game.type == "corr" && | |
1382 | moveCol == this.game.mycolor && | |
1383 | !data.receiveMyMove | |
1384 | ) { | |
a17ae317 | 1385 | let boardDiv = document.querySelector(".game"); |
5aa14a21 BA |
1386 | const afterSetScore = () => { |
1387 | doProcessMove(); | |
1388 | if (this.st.settings.gotonext && this.nextIds.length > 0) | |
1389 | this.showNextGame(); | |
1390 | else { | |
5aa14a21 | 1391 | // The board might have been hidden: |
5aa14a21 BA |
1392 | if (boardDiv.style.visibility == "hidden") |
1393 | boardDiv.style.visibility = "visible"; | |
3f22c2c3 | 1394 | if (data.score == "*") this.re_setClocks(); |
e71161fb | 1395 | } |
5aa14a21 | 1396 | }; |
a17ae317 BA |
1397 | let el = document.querySelector("#buttonsConfirm > .acceptBtn"); |
1398 | // We may play several moves in a row: in case of, remove listener: | |
1399 | let elClone = el.cloneNode(true); | |
1400 | el.parentNode.replaceChild(elClone, el); | |
1401 | elClone.addEventListener( | |
1402 | "click", | |
1403 | () => { | |
1404 | document.getElementById("modalConfirm").checked = false; | |
1405 | if (!!data.score && data.score != "*") | |
1406 | // Set score first | |
1407 | this.gameOver(data.score, null, afterSetScore); | |
1408 | else afterSetScore(); | |
1409 | } | |
1410 | ); | |
1411 | // PlayOnBoard is enough, and more appropriate for Synchrone Chess | |
1412 | V.PlayOnBoard(this.vr.board, move); | |
1413 | const position = this.vr.getBaseFen(); | |
1414 | V.UndoOnBoard(this.vr.board, move); | |
5aa14a21 | 1415 | if (["all","byrow"].includes(V.ShowMoves)) { |
5aa14a21 BA |
1416 | this.curDiag = getDiagram({ |
1417 | position: position, | |
1418 | orientation: V.CanFlip ? this.game.mycolor : "w" | |
1419 | }); | |
f3fe29d8 BA |
1420 | document.querySelector("#confirmDiv > .card").style.width = |
1421 | boardDiv.offsetWidth + "px"; | |
5aa14a21 BA |
1422 | } else { |
1423 | // Incomplete information: just ask confirmation | |
a17ae317 | 1424 | // Hide the board, because otherwise it could reveal infos |
5aa14a21 | 1425 | boardDiv.style.visibility = "hidden"; |
a17ae317 | 1426 | this.moveNotation = getFullNotation(move); |
5aa14a21 | 1427 | } |
a17ae317 | 1428 | document.getElementById("modalConfirm").checked = true; |
6d68309a | 1429 | } |
aae89b49 | 1430 | else { |
5aa14a21 | 1431 | // Normal situation |
5aa14a21 | 1432 | if (!!data.score && data.score != "*") |
e01e086d BA |
1433 | this.gameOver(data.score, null, doProcessMove); |
1434 | else doProcessMove(); | |
aae89b49 | 1435 | } |
b4fb1612 | 1436 | }, |
5b4de147 | 1437 | cancelMove: function() { |
a17ae317 BA |
1438 | let boardDiv = document.querySelector(".game"); |
1439 | if (boardDiv.style.visibility == "hidden") | |
1440 | boardDiv.style.visibility = "visible"; | |
5b4de147 BA |
1441 | document.getElementById("modalConfirm").checked = false; |
1442 | this.$refs["basegame"].cancelLastMove(); | |
1443 | }, | |
5aa14a21 BA |
1444 | // In corr games, callback to change page only after score is set: |
1445 | gameOver: function(score, scoreMsg, callback) { | |
430a2038 | 1446 | this.game.score = score; |
5aa14a21 | 1447 | if (!scoreMsg) scoreMsg = getScoreMessage(score); |
a17ae317 | 1448 | this.game.scoreMsg = scoreMsg; |
5aa14a21 | 1449 | this.$set(this.game, "scoreMsg", scoreMsg); |
ab6f48ea | 1450 | const myIdx = this.game.players.findIndex(p => { |
0234201f | 1451 | return p.sid == this.st.user.sid || p.id == this.st.user.id; |
ab6f48ea | 1452 | }); |
6808d7a1 | 1453 | if (myIdx >= 0) { |
8477e53d | 1454 | // OK, I play in this game |
aae89b49 | 1455 | const scoreObj = { |
6808d7a1 BA |
1456 | score: score, |
1457 | scoreMsg: scoreMsg | |
aae89b49 | 1458 | }; |
5aa14a21 | 1459 | if (this.game.type == "live") { |
f54f4c26 | 1460 | GameStorage.update(this.gameRef, scoreObj); |
f5768809 BA |
1461 | // Notify myself locally if I'm elsewhere: |
1462 | if (!this.focus) { | |
1463 | notify( | |
1464 | "Game over", | |
1465 | { body: score + " : " + scoreMsg } | |
1466 | ); | |
1467 | } | |
5aa14a21 BA |
1468 | if (!!callback) callback(); |
1469 | } | |
1470 | else this.updateCorrGame(scoreObj, callback); | |
48ab808f | 1471 | // Notify the score to main Hall. TODO: only one player (currently double send) |
6808d7a1 | 1472 | this.send("result", { gid: this.game.id, score: score }); |
cafe0166 BA |
1473 | // Also to MyGames page (TODO: doubled as well...) |
1474 | this.notifyMyGames( | |
1475 | "score", | |
1476 | { | |
f54f4c26 | 1477 | gid: this.gameRef, |
cafe0166 BA |
1478 | score: score |
1479 | } | |
1480 | ); | |
dcd68c41 | 1481 | } |
5aa14a21 | 1482 | else if (!!callback) callback(); |
6808d7a1 BA |
1483 | } |
1484 | } | |
a6088c90 BA |
1485 | }; |
1486 | </script> | |
7e1a1fe9 | 1487 | |
41c80bb6 | 1488 | <style lang="sass" scoped> |
c292ebb2 BA |
1489 | #infoDiv > .card |
1490 | padding: 15px 0 | |
1491 | max-width: 430px | |
1492 | ||
72ccbd67 | 1493 | .connected |
050ae3b5 | 1494 | background-color: lightgreen |
72ccbd67 | 1495 | |
ed06d9e9 BA |
1496 | #participants |
1497 | margin-left: 5px | |
1498 | ||
1499 | .anonymous | |
1500 | color: grey | |
1501 | font-style: italic | |
1502 | ||
ec905cbc BA |
1503 | #playersInfo > p |
1504 | margin: 0 | |
1505 | ||
430a2038 BA |
1506 | @media screen and (min-width: 768px) |
1507 | #actions | |
1508 | width: 300px | |
1509 | @media screen and (max-width: 767px) | |
1510 | .game | |
1511 | width: 100% | |
72ccbd67 | 1512 | |
430a2038 | 1513 | #actions |
cf94b843 | 1514 | display: inline-block |
1d6d7b1d | 1515 | margin: 0 |
feaf1bf7 BA |
1516 | |
1517 | button | |
1518 | display: inline-block | |
1519 | margin: 0 | |
1520 | display: inline-flex | |
1521 | img | |
54ec15eb | 1522 | height: 22px |
feaf1bf7 BA |
1523 | display: flex |
1524 | @media screen and (max-width: 767px) | |
1525 | height: 18px | |
a1c48034 | 1526 | |
050ae3b5 BA |
1527 | @media screen and (max-width: 767px) |
1528 | #aboveBoard | |
1529 | text-align: center | |
885d93a7 BA |
1530 | @media screen and (min-width: 768px) |
1531 | #aboveBoard | |
1532 | margin-left: 30% | |
050ae3b5 | 1533 | |
2f258c37 BA |
1534 | .variant-cadence |
1535 | padding-right: 10px | |
1536 | ||
1537 | .variant-name | |
8c5f5390 | 1538 | font-weight: bold |
77c50966 | 1539 | padding-right: 10px |
77c50966 | 1540 | |
feaf1bf7 BA |
1541 | span#nextGame |
1542 | background-color: #edda99 | |
1543 | cursor: pointer | |
1544 | display: inline-block | |
1545 | margin-right: 10px | |
1546 | ||
57eb158f | 1547 | span.name |
050ae3b5 | 1548 | font-size: 1.5rem |
57eb158f | 1549 | padding: 0 3px |
050ae3b5 | 1550 | |
57eb158f | 1551 | span.time |
050ae3b5 BA |
1552 | font-size: 2rem |
1553 | display: inline-block | |
57eb158f BA |
1554 | .time-left |
1555 | margin-left: 10px | |
1556 | .time-right | |
1557 | margin-left: 5px | |
1558 | .time-separator | |
1559 | margin-left: 5px | |
1560 | position: relative | |
1561 | top: -1px | |
1562 | ||
1563 | span.yourturn | |
1564 | color: #831B1B | |
1565 | .time-separator | |
1566 | animation: blink-animation 2s steps(3, start) infinite | |
1567 | @keyframes blink-animation | |
1568 | to | |
1569 | visibility: hidden | |
050ae3b5 BA |
1570 | |
1571 | .split-names | |
1572 | display: inline-block | |
1573 | margin: 0 15px | |
1574 | ||
5b4de147 | 1575 | #chatWrap > .card |
a1c48034 | 1576 | padding-top: 20px |
a154d45e | 1577 | max-width: 767px |
5b4de147 BA |
1578 | border: none |
1579 | ||
1580 | #confirmDiv > .card | |
1581 | max-width: 767px | |
1582 | max-height: 100% | |
cf94b843 | 1583 | |
dcd68c41 BA |
1584 | .draw-sent, .draw-sent:hover |
1585 | background-color: lightyellow | |
1586 | ||
1587 | .draw-received, .draw-received:hover | |
1588 | background-color: lightgreen | |
1589 | ||
1590 | .draw-threerep, .draw-threerep:hover | |
1591 | background-color: #e4d1fc | |
2f258c37 | 1592 | |
c292ebb2 BA |
1593 | .rematch-sent, .rematch-sent:hover |
1594 | background-color: lightyellow | |
1595 | ||
1596 | .rematch-received, .rematch-received:hover | |
1597 | background-color: lightgreen | |
1598 | ||
2f258c37 BA |
1599 | .somethingnew |
1600 | background-color: #c5fefe | |
5b4de147 BA |
1601 | |
1602 | .diagram | |
1603 | margin: 0 auto | |
5b4de147 BA |
1604 | width: 100% |
1605 | ||
1606 | #buttonsConfirm | |
1607 | margin: 0 | |
1608 | & > button > span | |
1609 | width: 100% | |
1610 | text-align: center | |
1611 | ||
1612 | button.acceptBtn | |
1613 | background-color: lightgreen | |
1614 | button.refuseBtn | |
1615 | background-color: red | |
7e1a1fe9 | 1616 | </style> |