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