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