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