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