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