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