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