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