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