Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
7aa548e7 | 2 | main |
910d631b BA |
3 | input#modalChat.modal( |
4 | type="checkbox" | |
5 | @click="resetChatColor()" | |
6 | ) | |
7 | div#chatWrap( | |
8 | role="dialog" | |
9 | data-checkbox="modalChat" | |
10 | ) | |
5b4de147 | 11 | .card |
a1c48034 | 12 | label.modal-close(for="modalChat") |
ed06d9e9 | 13 | #participants |
ac8f441c | 14 | span {{ Object.keys(people).length + " " + st.tr["participant(s):"] }} |
910d631b BA |
15 | span( |
16 | v-for="p in Object.values(people)" | |
efdfb4c7 | 17 | v-if="p.name" |
910d631b | 18 | ) |
ed06d9e9 | 19 | | {{ p.name }} |
efdfb4c7 | 20 | span.anonymous(v-if="Object.values(people).some(p => !p.name && p.id === 0)") |
ed06d9e9 | 21 | | + @nonymous |
910d631b | 22 | Chat( |
aae89b49 | 23 | ref="chatcomp" |
910d631b BA |
24 | :players="game.players" |
25 | :pastChats="game.chats" | |
26 | :newChat="newChat" | |
27 | @mychat="processChat" | |
db1f1f9a | 28 | @chatcleared="clearChat" |
910d631b | 29 | ) |
5b4de147 BA |
30 | input#modalConfirm.modal(type="checkbox") |
31 | div#confirmDiv(role="dialog") | |
32 | .card | |
a17ae317 BA |
33 | .diagram( |
34 | v-if="!!vr && ['all','byrow'].includes(vr.showMoves)" | |
35 | v-html="curDiag" | |
36 | ) | |
37 | p.text-center(v-else) | |
38 | span {{ st.tr["Move played:"] + " " }} | |
39 | span.bold {{ moveNotation }} | |
40 | br | |
41 | span {{ st.tr["Are you sure?"] }} | |
5b4de147 BA |
42 | .button-group#buttonsConfirm |
43 | // onClick for acceptBtn: set dynamically | |
44 | button.acceptBtn | |
45 | span {{ st.tr["Validate"] }} | |
46 | button.refuseBtn(@click="cancelMove()") | |
47 | span {{ st.tr["Cancel"] }} | |
7aa548e7 | 48 | .row |
050ae3b5 | 49 | #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2 |
2f258c37 BA |
50 | span.variant-cadence {{ game.cadence }} |
51 | span.variant-name {{ game.vname }} | |
feaf1bf7 BA |
52 | span#nextGame( |
53 | v-if="nextIds.length > 0" | |
54 | @click="showNextGame()" | |
55 | ) | |
56 | | {{ st.tr["Next_g"] }} | |
57 | button#chatBtn.tooltip( | |
58 | onClick="window.doClick('modalChat')" | |
59 | aria-label="Chat" | |
60 | ) | |
61 | img(src="/images/icons/chat.svg") | |
4f518610 | 62 | #actions(v-if="game.score=='*'") |
feaf1bf7 | 63 | button.tooltip( |
910d631b BA |
64 | @click="clickDraw()" |
65 | :class="{['draw-' + drawOffer]: true}" | |
feaf1bf7 | 66 | :aria-label="st.tr['Draw']" |
910d631b | 67 | ) |
feaf1bf7 BA |
68 | img(src="/images/icons/draw.svg") |
69 | button.tooltip( | |
910d631b BA |
70 | v-if="!!game.mycolor" |
71 | @click="abortGame()" | |
feaf1bf7 | 72 | :aria-label="st.tr['Abort']" |
910d631b | 73 | ) |
feaf1bf7 BA |
74 | img(src="/images/icons/abort.svg") |
75 | button.tooltip( | |
910d631b BA |
76 | v-if="!!game.mycolor" |
77 | @click="resign()" | |
feaf1bf7 | 78 | :aria-label="st.tr['Resign']" |
910d631b | 79 | ) |
feaf1bf7 BA |
80 | img(src="/images/icons/resign.svg") |
81 | button.tooltip( | |
82 | v-else-if="!!game.mycolor" | |
83 | @click="rematch()" | |
84 | :aria-label="st.tr['Rematch']" | |
85 | ) | |
86 | img(src="/images/icons/rematch.svg") | |
050ae3b5 BA |
87 | #playersInfo |
88 | p | |
5bcc9b31 BA |
89 | span.name(:class="{connected: isConnected(0)}") |
90 | | {{ game.players[0].name || "@nonymous" }} | |
57eb158f BA |
91 | span.time( |
92 | v-if="game.score=='*'" | |
93 | :class="{yourturn: !!vr && vr.turn == 'w'}" | |
94 | ) | |
95 | span.time-left {{ virtualClocks[0][0] }} | |
96 | span.time-separator(v-if="!!virtualClocks[0][1]") : | |
aae89b49 BA |
97 | span.time-right(v-if="!!virtualClocks[0][1]") |
98 | | {{ virtualClocks[0][1] }} | |
050ae3b5 | 99 | span.split-names - |
5bcc9b31 BA |
100 | span.name(:class="{connected: isConnected(1)}") |
101 | | {{ game.players[1].name || "@nonymous" }} | |
57eb158f BA |
102 | span.time( |
103 | v-if="game.score=='*'" | |
104 | :class="{yourturn: !!vr && vr.turn == 'b'}" | |
105 | ) | |
106 | span.time-left {{ virtualClocks[1][0] }} | |
107 | span.time-separator(v-if="!!virtualClocks[1][1]") : | |
aae89b49 BA |
108 | span.time-right(v-if="!!virtualClocks[1][1]") |
109 | | {{ virtualClocks[1][1] }} | |
910d631b | 110 | BaseGame( |
8477e53d | 111 | ref="basegame" |
910d631b | 112 | :game="game" |
910d631b | 113 | @newmove="processMove" |
910d631b | 114 | ) |
a6088c90 BA |
115 | </template> |
116 | ||
117 | <script> | |
46284a2f | 118 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 119 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 120 | import { store } from "@/store"; |
967a2686 | 121 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 122 | import { ppt } from "@/utils/datetime"; |
23ecf008 | 123 | import { ajax } from "@/utils/ajax"; |
66d03f23 | 124 | import { extractTime } from "@/utils/timeControl"; |
51d87b52 | 125 | import { getRandString } from "@/utils/alea"; |
5aa14a21 BA |
126 | import { getScoreMessage } from "@/utils/scoring"; |
127 | import { getFullNotation } from "@/utils/notation"; | |
5b4de147 | 128 | import { getDiagram } from "@/utils/printDiagram"; |
dcd68c41 | 129 | import { processModalClick } from "@/utils/modalClick"; |
e71161fb | 130 | import { playMove, getFilteredMove } from "@/utils/playUndo"; |
1611a25f | 131 | import { ArrayFun } from "@/utils/array"; |
8418f0d7 | 132 | import params from "@/parameters"; |
a6088c90 | 133 | export default { |
6808d7a1 | 134 | name: "my-game", |
a6088c90 BA |
135 | components: { |
136 | BaseGame, | |
6808d7a1 | 137 | Chat |
a6088c90 | 138 | }, |
a6088c90 BA |
139 | data: function() { |
140 | return { | |
141 | st: store.state, | |
6808d7a1 | 142 | gameRef: { |
1611a25f | 143 | // rid = remote (socket) ID |
4b0384fa BA |
144 | id: "", |
145 | rid: "" | |
146 | }, | |
feaf1bf7 | 147 | nextIds: [], |
aae89b49 BA |
148 | game: {}, //passed to BaseGame |
149 | // virtualClocks will be initialized from true game.clocks | |
150 | virtualClocks: [], | |
6dd02928 | 151 | vr: null, //"variant rules" object initialized from FEN |
dcd68c41 BA |
152 | drawOffer: "", |
153 | people: {}, //players + observers | |
1611a25f | 154 | onMygames: [], //opponents (or me) on "MyGames" page |
760adbce | 155 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 156 | repeat: {}, //detect position repetition |
5b4de147 | 157 | curDiag: "", //for corr moves confirmation |
ac8f441c | 158 | newChat: "", |
8418f0d7 | 159 | conn: null, |
f9c36b2d BA |
160 | roomInitialized: false, |
161 | // If newmove has wrong index: ask fullgame again: | |
57eb158f | 162 | askGameTime: 0, |
dcff8e82 | 163 | gameIsLoading: false, |
f9c36b2d BA |
164 | // If asklastate got no reply, ask again: |
165 | gotLastate: false, | |
e5c1d0fb | 166 | gotMoveIdx: -1, //last move index received |
f9c36b2d BA |
167 | // If newmove got no pingback, send again: |
168 | opponentGotMove: false, | |
51d87b52 | 169 | connexionString: "", |
a17ae317 BA |
170 | // Incomplete info games: show move played |
171 | moveNotation: "", | |
aae89b49 | 172 | // Intervals from setInterval(): |
aae89b49 BA |
173 | askLastate: null, |
174 | retrySendmove: null, | |
175 | clockUpdate: null, | |
51d87b52 BA |
176 | // Related to (killing of) self multi-connects: |
177 | newConnect: {}, | |
6808d7a1 | 178 | killed: {} |
a6088c90 BA |
179 | }; |
180 | }, | |
181 | watch: { | |
aae89b49 BA |
182 | $route: function(to, from) { |
183 | if (from.params["id"] != to.params["id"]) { | |
184 | // Change everything: | |
185 | this.cleanBeforeDestroy(); | |
5aa14a21 BA |
186 | let boardDiv = document.querySelector(".game"); |
187 | if (!!boardDiv) | |
188 | // In case of incomplete information variant: | |
189 | boardDiv.style.visibility = "hidden"; | |
aae89b49 BA |
190 | this.atCreation(); |
191 | } else { | |
192 | // Same game ID | |
193 | this.gameRef.id = to.params["id"]; | |
194 | this.gameRef.rid = to.query["rid"]; | |
195 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); | |
196 | this.loadGame(); | |
197 | } | |
6808d7a1 | 198 | } |
92a523d1 | 199 | }, |
71468011 | 200 | // NOTE: some redundant code with Hall.vue (mostly related to people array) |
a6088c90 | 201 | created: function() { |
aae89b49 | 202 | this.atCreation(); |
cdb34c93 | 203 | }, |
dcd68c41 | 204 | mounted: function() { |
6808d7a1 BA |
205 | document |
206 | .getElementById("chatWrap") | |
207 | .addEventListener("click", processModalClick); | |
5b4de147 BA |
208 | if ("ontouchstart" in window) { |
209 | // Disable tooltips on smartphones: | |
210 | document.getElementsByClassName("tooltip").forEach(elt => { | |
211 | elt.classList.remove("tooltip"); | |
212 | }); | |
213 | } | |
dcd68c41 | 214 | }, |
8418f0d7 | 215 | beforeDestroy: function() { |
aae89b49 | 216 | this.cleanBeforeDestroy(); |
8418f0d7 | 217 | }, |
cdb34c93 | 218 | methods: { |
aae89b49 BA |
219 | atCreation: function() { |
220 | // 0] (Re)Set variables | |
221 | this.gameRef.id = this.$route.params["id"]; | |
222 | // rid = remote ID to find an observed live game, | |
223 | // next = next corr games IDs to navigate faster | |
224 | // (Both might be undefined) | |
225 | this.gameRef.rid = this.$route.query["rid"]; | |
226 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); | |
227 | // Always add myself to players' list | |
228 | const my = this.st.user; | |
229 | this.$set(this.people, my.sid, { id: my.id, name: my.name }); | |
230 | this.game = { | |
231 | players: [{ name: "" }, { name: "" }], | |
232 | chats: [], | |
233 | rendered: false | |
234 | }; | |
235 | let chatComp = this.$refs["chatcomp"]; | |
236 | if (!!chatComp) chatComp.chats = []; | |
237 | this.virtualClocks = [[0,0], [0,0]]; | |
238 | this.vr = null; | |
239 | this.drawOffer = ""; | |
240 | this.onMygames = []; | |
241 | this.lastate = undefined; | |
242 | this.newChat = ""; | |
243 | this.roomInitialized = false; | |
244 | this.askGameTime = 0; | |
245 | this.gameIsLoading = false; | |
246 | this.gotLastate = false; | |
247 | this.gotMoveIdx = -1; | |
248 | this.opponentGotMove = false; | |
aae89b49 BA |
249 | this.askLastate = null; |
250 | this.retrySendmove = null; | |
251 | this.clockUpdate = null; | |
252 | this.newConnect = {}; | |
253 | this.killed = {}; | |
254 | // 1] Initialize connection | |
255 | this.connexionString = | |
256 | params.socketUrl + | |
257 | "/?sid=" + | |
258 | this.st.user.sid + | |
259 | "&tmpId=" + | |
260 | getRandString() + | |
261 | "&page=" + | |
262 | // Discard potential "/?next=[...]" for page indication: | |
263 | encodeURIComponent(this.$route.path.match(/\/game\/[a-zA-Z0-9]+/)[0]); | |
264 | this.conn = new WebSocket(this.connexionString); | |
265 | this.conn.onmessage = this.socketMessageListener; | |
266 | this.conn.onclose = this.socketCloseListener; | |
267 | // Socket init required before loading remote game: | |
268 | const socketInit = callback => { | |
269 | if (!!this.conn && this.conn.readyState == 1) | |
270 | // 1 == OPEN state | |
271 | callback(); | |
272 | else | |
273 | // Socket not ready yet (initial loading) | |
274 | // NOTE: it's important to call callback without arguments, | |
275 | // otherwise first arg is Websocket object and loadGame fails. | |
276 | this.conn.onopen = () => callback(); | |
277 | }; | |
278 | if (!this.gameRef.rid) | |
279 | // Game stored locally or on server | |
280 | this.loadGame(null, () => socketInit(this.roomInit)); | |
281 | else | |
282 | // Game stored remotely: need socket to retrieve it | |
283 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. | |
284 | // --> It will be given when receiving "fullgame" socket event. | |
285 | socketInit(this.loadGame); | |
286 | }, | |
287 | cleanBeforeDestroy: function() { | |
aae89b49 BA |
288 | if (!!this.askLastate) |
289 | clearInterval(this.askLastate); | |
290 | if (!!this.retrySendmove) | |
291 | clearInterval(this.retrySendmove); | |
292 | if (!!this.clockUpdate) | |
293 | clearInterval(this.clockUpdate); | |
294 | this.send("disconnect"); | |
295 | }, | |
760adbce | 296 | roomInit: function() { |
f9c36b2d BA |
297 | if (!this.roomInitialized) { |
298 | // Notify the room only now that I connected, because | |
299 | // messages might be lost otherwise (if game loading is slow) | |
300 | this.send("connect"); | |
301 | this.send("pollclients"); | |
302 | // We may ask fullgame several times if some moves are lost, | |
303 | // but room should be init only once: | |
304 | this.roomInitialized = true; | |
305 | } | |
71468011 BA |
306 | }, |
307 | send: function(code, obj) { | |
f9c36b2d | 308 | if (!!this.conn) |
6808d7a1 | 309 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
5f131484 | 310 | }, |
050ae3b5 | 311 | isConnected: function(index) { |
29ced362 BA |
312 | const player = this.game.players[index]; |
313 | // Is it me ? | |
0a17525e BA |
314 | if (this.st.user.sid == player.sid || this.st.user.id == player.uid) |
315 | // Still have to check for name (because of potential multi-accounts | |
316 | // on same browser, although this should be rare...) | |
317 | return (!this.st.user.name || this.st.user.name == player.name); | |
29ced362 | 318 | // Try to find a match in people: |
6808d7a1 | 319 | return ( |
1611a25f BA |
320 | ( |
321 | player.sid && | |
322 | Object.keys(this.people).some(sid => sid == player.sid) | |
323 | ) | |
324 | || | |
325 | ( | |
326 | player.uid && | |
327 | Object.values(this.people).some(p => p.id == player.uid) | |
328 | ) | |
6808d7a1 | 329 | ); |
050ae3b5 | 330 | }, |
db1f1f9a BA |
331 | resetChatColor: function() { |
332 | // TODO: this is called twice, once on opening an once on closing | |
333 | document.getElementById("chatBtn").classList.remove("somethingnew"); | |
334 | }, | |
335 | processChat: function(chat) { | |
336 | this.send("newchat", { data: chat }); | |
337 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) | |
338 | if (this.game.type == "corr" && this.st.user.id > 0) | |
aae89b49 | 339 | this.updateCorrGame({ chat: chat }); |
db1f1f9a BA |
340 | }, |
341 | clearChat: function() { | |
342 | // Nothing more to do if game is live (chats not recorded) | |
23ecf008 | 343 | if (this.game.type == "corr") { |
e57c4de4 BA |
344 | if (!!this.game.mycolor) { |
345 | ajax( | |
346 | "/chats", | |
347 | "DELETE", | |
348 | { data: { gid: this.game.id } } | |
349 | ); | |
350 | } | |
dcff8e82 | 351 | this.$set(this.game, "chats", []); |
db1f1f9a BA |
352 | } |
353 | }, | |
1611a25f BA |
354 | // Notify turn after a new move (to opponent and me on MyGames page) |
355 | notifyTurn: function(sid) { | |
356 | const player = this.people[sid]; | |
357 | const colorIdx = this.game.players.findIndex( | |
8f4e861c | 358 | p => p.sid == sid || p.uid == player.id); |
1611a25f | 359 | const color = ["w","b"][colorIdx]; |
dcff8e82 | 360 | const movesCount = this.game.moves.length; |
1611a25f | 361 | const yourTurn = |
dcff8e82 BA |
362 | (color == "w" && movesCount % 2 == 0) || |
363 | (color == "b" && movesCount % 2 == 1); | |
1611a25f BA |
364 | this.send("turnchange", { target: sid, yourTurn: yourTurn }); |
365 | }, | |
feaf1bf7 BA |
366 | showNextGame: function() { |
367 | // Did I play in current game? If not, add it to nextIds list | |
368 | if (this.game.score == "*" && this.vr.turn == this.game.mycolor) | |
369 | this.nextIds.unshift(this.game.id); | |
370 | const nextGid = this.nextIds.pop(); | |
371 | this.$router.push( | |
372 | "/game/" + nextGid + "/?next=" + JSON.stringify(this.nextIds)); | |
373 | }, | |
374 | rematch: function() { | |
375 | alert("Unimplemented yet (soon :) )"); | |
376 | // TODO: same logic as for draw, but re-click remove rematch offer (toggle) | |
377 | }, | |
d6f08e56 BA |
378 | askGameAgain: function() { |
379 | this.gameIsLoading = true; | |
aae89b49 | 380 | const currentUrl = document.location.href; |
57eb158f | 381 | const doAskGame = () => { |
e01e086d | 382 | if (document.location.href != currentUrl) return; //page change |
57eb158f BA |
383 | if (!this.gameRef.rid) |
384 | // This is my game: just reload. | |
385 | this.loadGame(); | |
e01e086d | 386 | else |
57eb158f BA |
387 | // Just ask fullgame again (once!), this is much simpler. |
388 | // If this fails, the user could just reload page :/ | |
aae89b49 | 389 | this.send("askfullgame", { target: this.gameRef.rid }); |
57eb158f BA |
390 | }; |
391 | // Delay of at least 2s between two game requests | |
392 | const now = Date.now(); | |
393 | const delay = Math.max(2000 - (now - this.askGameTime), 0); | |
394 | this.askGameTime = now; | |
395 | setTimeout(doAskGame, delay); | |
d6f08e56 | 396 | }, |
cdb34c93 | 397 | socketMessageListener: function(msg) { |
6808d7a1 | 398 | if (!this.conn) return; |
a6088c90 | 399 | const data = JSON.parse(msg.data); |
6808d7a1 | 400 | switch (data.code) { |
5f131484 | 401 | case "pollclients": |
5f131484 | 402 | data.sockIds.forEach(sid => { |
dcff8e82 | 403 | if (sid != this.st.user.sid) |
6808d7a1 | 404 | this.send("askidentity", { target: sid }); |
5f131484 BA |
405 | }); |
406 | break; | |
71468011 | 407 | case "connect": |
efdfb4c7 | 408 | if (!this.people[data.from]) { |
51d87b52 | 409 | this.newConnect[data.from] = true; //for self multi-connects tests |
6808d7a1 | 410 | this.send("askidentity", { target: data.from }); |
51d87b52 | 411 | } |
71468011 BA |
412 | break; |
413 | case "disconnect": | |
414 | this.$delete(this.people, data.from); | |
415 | break; | |
efdfb4c7 | 416 | case "mconnect": { |
1611a25f BA |
417 | // TODO: from MyGames page : send mconnect message with the list of gid (live and corr) |
418 | // Either me (another tab) or opponent | |
419 | const sid = data.from; | |
420 | if (!this.onMygames.some(s => s == sid)) | |
421 | { | |
422 | this.onMygames.push(sid); | |
423 | this.notifyTurn(sid); //TODO: this may require server ID (so, notify after receiving identity) | |
424 | } | |
425 | break; | |
426 | if (!this.people[sid]) | |
427 | this.send("askidentity", { target: sid }); | |
428 | } | |
429 | case "mdisconnect": | |
430 | ArrayFun.remove(this.onMygames, sid => sid == data.from); | |
431 | break; | |
51d87b52 BA |
432 | case "killed": |
433 | // I logged in elsewhere: | |
51d87b52 | 434 | this.conn = null; |
09d37571 | 435 | alert(this.st.tr["New connexion detected: tab now offline"]); |
51d87b52 | 436 | break; |
6808d7a1 | 437 | case "askidentity": { |
efdfb4c7 | 438 | // Request for identification |
51d87b52 BA |
439 | const me = { |
440 | // Decompose to avoid revealing email | |
441 | name: this.st.user.name, | |
442 | sid: this.st.user.sid, | |
6808d7a1 | 443 | id: this.st.user.id |
51d87b52 | 444 | }; |
6808d7a1 | 445 | this.send("identity", { data: me, target: data.from }); |
5f131484 | 446 | break; |
51d87b52 | 447 | } |
6808d7a1 | 448 | case "identity": { |
71468011 | 449 | const user = data.data; |
efdfb4c7 | 450 | this.$set(this.people, user.sid, { name: user.name, id: user.id }); |
f9c36b2d BA |
451 | // If I multi-connect, kill current connexion if no mark (I'm older) |
452 | if (this.newConnect[user.sid]) { | |
6808d7a1 | 453 | if ( |
6808d7a1 BA |
454 | user.id > 0 && |
455 | user.id == this.st.user.id && | |
f9c36b2d BA |
456 | user.sid != this.st.user.sid && |
457 | !this.killed[this.st.user.sid] | |
6808d7a1 | 458 | ) { |
6808d7a1 | 459 | this.send("killme", { sid: this.st.user.sid }); |
51d87b52 | 460 | this.killed[this.st.user.sid] = true; |
51d87b52 | 461 | } |
f9c36b2d | 462 | delete this.newConnect[user.sid]; |
a0c41e7e | 463 | } |
dcff8e82 BA |
464 | if (!this.killed[this.st.user.sid]) { |
465 | // Ask potentially missed last state, if opponent and I play | |
466 | if ( | |
467 | !!this.game.mycolor && | |
468 | this.game.type == "live" && | |
469 | this.game.score == "*" && | |
470 | this.game.players.some(p => p.sid == user.sid) | |
471 | ) { | |
aae89b49 | 472 | this.send("asklastate", { target: user.sid }); |
e01e086d | 473 | let counter = 1; |
aae89b49 BA |
474 | this.askLastate = setInterval( |
475 | () => { | |
e01e086d BA |
476 | // Ask at most 3 times: |
477 | // if no reply after that there should be a network issue. | |
478 | if ( | |
479 | counter < 3 && | |
480 | !this.gotLastate && | |
481 | !!this.people[user.sid] | |
482 | ) { | |
aae89b49 | 483 | this.send("asklastate", { target: user.sid }); |
e01e086d BA |
484 | counter++; |
485 | } else { | |
aae89b49 | 486 | clearInterval(this.askLastate); |
e01e086d | 487 | } |
aae89b49 | 488 | }, |
e01e086d | 489 | 1500 |
aae89b49 | 490 | ); |
dcff8e82 BA |
491 | } |
492 | } | |
a0c41e7e | 493 | break; |
71468011 BA |
494 | } |
495 | case "askgame": | |
496 | // Send current (live) game if not asked by any of the players | |
6808d7a1 BA |
497 | if ( |
498 | this.game.type == "live" && | |
499 | this.game.players.every(p => p.sid != data.from[0]) | |
500 | ) { | |
71468011 BA |
501 | const myGame = { |
502 | id: this.game.id, | |
503 | fen: this.game.fen, | |
504 | players: this.game.players, | |
505 | vid: this.game.vid, | |
506 | cadence: this.game.cadence, | |
507 | score: this.game.score, | |
6808d7a1 | 508 | rid: this.st.user.sid //useful in Hall if I'm an observer |
71468011 | 509 | }; |
6808d7a1 | 510 | this.send("game", { data: myGame, target: data.from }); |
71468011 BA |
511 | } |
512 | break; | |
513 | case "askfullgame": | |
e8da204a BA |
514 | const gameToSend = Object.keys(this.game) |
515 | .filter(k => | |
516 | [ | |
517 | "id","fen","players","vid","cadence","fenStart","vname", | |
518 | "moves","clocks","initime","score","drawOffer" | |
519 | ].includes(k)) | |
520 | .reduce( | |
521 | (obj, k) => { | |
522 | obj[k] = this.game[k]; | |
523 | return obj; | |
524 | }, | |
525 | {} | |
526 | ); | |
527 | this.send("fullgame", { data: gameToSend, target: data.from }); | |
71468011 BA |
528 | break; |
529 | case "fullgame": | |
530 | // Callback "roomInit" to poll clients only after game is loaded | |
dcff8e82 | 531 | this.loadGame(data.data, this.roomInit); |
71468011 | 532 | break; |
a0c41e7e | 533 | case "asklastate": |
dcff8e82 | 534 | // Sending informative last state if I played a move or score != "*" |
6808d7a1 BA |
535 | if ( |
536 | (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || | |
537 | this.game.score != "*" || | |
538 | this.drawOffer == "sent" | |
539 | ) { | |
f41ce580 | 540 | // Send our "last state" informations to opponent |
411d23cd | 541 | const L = this.game.moves.length; |
6808d7a1 | 542 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); |
71468011 | 543 | const myLastate = { |
6808d7a1 | 544 | lastMove: L > 0 ? this.game.moves[L - 1] : undefined, |
e01e086d | 545 | clock: this.game.clocks[myIdx], |
71468011 BA |
546 | // Since we played a move (or abort or resign), |
547 | // only drawOffer=="sent" is possible | |
548 | drawSent: this.drawOffer == "sent", | |
26f3a887 | 549 | score: this.game.score, |
a17ae317 | 550 | score: this.game.scoreMsg, |
71468011 | 551 | movesCount: L, |
6808d7a1 | 552 | initime: this.game.initime[1 - myIdx] //relevant only if I played |
5fd5fb22 | 553 | }; |
6808d7a1 | 554 | this.send("lastate", { data: myLastate, target: data.from }); |
dcff8e82 BA |
555 | } else { |
556 | this.send("lastate", { data: {nothing: true}, target: data.from }); | |
5fd5fb22 | 557 | } |
c6788ecf | 558 | break; |
dcff8e82 BA |
559 | case "lastate": { |
560 | // Got opponent infos about last move | |
561 | this.gotLastate = true; | |
562 | if (!data.data.nothing) { | |
563 | this.lastate = data.data; | |
564 | if (this.game.rendered) | |
565 | // Game is rendered (Board component) | |
566 | this.processLastate(); | |
567 | // Else: will be processed when game is ready | |
568 | } | |
71468011 | 569 | break; |
dcff8e82 | 570 | } |
6808d7a1 | 571 | case "newmove": { |
dcff8e82 BA |
572 | const movePlus = data.data; |
573 | const movesCount = this.game.moves.length; | |
574 | if (movePlus.index > movesCount) { | |
575 | // This can only happen if I'm an observer and missed a move. | |
57eb158f BA |
576 | if (this.gotMoveIdx < movePlus.index) |
577 | this.gotMoveIdx = movePlus.index; | |
d6f08e56 BA |
578 | if (!this.gameIsLoading) this.askGameAgain(); |
579 | } | |
580 | else { | |
f9c36b2d | 581 | if ( |
dcff8e82 BA |
582 | movePlus.index < movesCount || |
583 | this.gotMoveIdx >= movePlus.index | |
f9c36b2d BA |
584 | ) { |
585 | // Opponent re-send but we already have the move: | |
586 | // (maybe he didn't receive our pingback...) | |
dcff8e82 | 587 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
f9c36b2d | 588 | } else { |
dcff8e82 BA |
589 | this.gotMoveIdx = movePlus.index; |
590 | const receiveMyMove = (movePlus.color == this.game.mycolor); | |
f9c36b2d BA |
591 | if (!receiveMyMove && !!this.game.mycolor) |
592 | // Notify opponent that I got the move: | |
dcff8e82 BA |
593 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
594 | if (movePlus.cancelDrawOffer) { | |
f9c36b2d BA |
595 | // Opponent refuses draw |
596 | this.drawOffer = ""; | |
597 | // NOTE for corr games: drawOffer reset by player in turn | |
598 | if ( | |
599 | this.game.type == "live" && | |
600 | !!this.game.mycolor && | |
601 | !receiveMyMove | |
602 | ) { | |
603 | GameStorage.update(this.gameRef.id, { drawOffer: "" }); | |
604 | } | |
605 | } | |
57eb158f BA |
606 | this.$refs["basegame"].play(movePlus.move, "received", null, true); |
607 | this.processMove( | |
dcff8e82 | 608 | movePlus.move, |
f9c36b2d | 609 | { |
e01e086d | 610 | clock: movePlus.clock, |
f9c36b2d BA |
611 | receiveMyMove: receiveMyMove |
612 | } | |
613 | ); | |
614 | } | |
633959bf | 615 | } |
a6088c90 | 616 | break; |
71468011 | 617 | } |
f9c36b2d BA |
618 | case "gotmove": { |
619 | this.opponentGotMove = true; | |
e01e086d BA |
620 | // Now his clock starts running: |
621 | const oppIdx = ['w','b'].indexOf(this.vr.turn); | |
622 | this.game.initime[oppIdx] = Date.now(); | |
623 | this.re_setClocks(); | |
f9c36b2d BA |
624 | break; |
625 | } | |
93d1d7a7 | 626 | case "resign": |
8477e53d BA |
627 | const score = data.side == "b" ? "1-0" : "0-1"; |
628 | const side = data.side == "w" ? "White" : "Black"; | |
629 | this.gameOver(score, side + " surrender"); | |
93d1d7a7 | 630 | break; |
93d1d7a7 | 631 | case "abort": |
8477e53d | 632 | this.gameOver("?", "Stop"); |
93d1d7a7 | 633 | break; |
2cc10cdb | 634 | case "draw": |
71468011 | 635 | this.gameOver("1/2", data.data); |
2cc10cdb BA |
636 | break; |
637 | case "drawoffer": | |
41c80bb6 BA |
638 | // NOTE: observers don't know who offered draw |
639 | this.drawOffer = "received"; | |
6d9f4315 | 640 | break; |
71468011 | 641 | case "newchat": |
bd76b456 | 642 | this.newChat = data.data; |
71468011 | 643 | if (!document.getElementById("modalChat").checked) |
2f258c37 | 644 | document.getElementById("chatBtn").classList.add("somethingnew"); |
a6088c90 BA |
645 | break; |
646 | } | |
cdb34c93 | 647 | }, |
51d87b52 BA |
648 | socketCloseListener: function() { |
649 | this.conn = new WebSocket(this.connexionString); | |
6808d7a1 BA |
650 | this.conn.addEventListener("message", this.socketMessageListener); |
651 | this.conn.addEventListener("close", this.socketCloseListener); | |
51d87b52 | 652 | }, |
5aa14a21 | 653 | updateCorrGame: function(obj, callback) { |
aae89b49 BA |
654 | ajax( |
655 | "/games", | |
656 | "PUT", | |
657 | { | |
e57c4de4 BA |
658 | data: { |
659 | gid: this.gameRef.id, | |
660 | newObj: obj | |
661 | }, | |
662 | success: () => { | |
663 | if (!!callback) callback(); | |
664 | } | |
aae89b49 BA |
665 | } |
666 | ); | |
667 | }, | |
760adbce BA |
668 | // lastate was received, but maybe game wasn't ready yet: |
669 | processLastate: function() { | |
670 | const data = this.lastate; | |
671 | this.lastate = undefined; //security... | |
672 | const L = this.game.moves.length; | |
6808d7a1 | 673 | if (data.movesCount > L) { |
760adbce | 674 | // Just got last move from him |
e01e086d BA |
675 | this.$refs["basegame"].play(data.lastMove, "received", null, true); |
676 | this.processMove(data.lastMove, { clock: data.clock }); | |
a0c41e7e | 677 | } |
6808d7a1 BA |
678 | if (data.drawSent) this.drawOffer = "received"; |
679 | if (data.score != "*") { | |
a0c41e7e | 680 | this.drawOffer = ""; |
5aa14a21 | 681 | if (this.game.score == "*") |
a17ae317 | 682 | this.gameOver(data.score, data.scoreMsg); |
760adbce BA |
683 | } |
684 | }, | |
dcd68c41 | 685 | clickDraw: function() { |
6808d7a1 BA |
686 | if (!this.game.mycolor) return; //I'm just spectator |
687 | if (["received", "threerep"].includes(this.drawOffer)) { | |
688 | if (!confirm(this.st.tr["Accept draw?"])) return; | |
689 | const message = | |
690 | this.drawOffer == "received" | |
691 | ? "Mutual agreement" | |
692 | : "Three repetitions"; | |
693 | this.send("draw", { data: message }); | |
77c50966 | 694 | this.gameOver("1/2", message); |
6808d7a1 | 695 | } else if (this.drawOffer == "") { |
e71161fb | 696 | // No effect if drawOffer == "sent" |
9ee2826a | 697 | if (this.game.mycolor != this.vr.turn) { |
6808d7a1 | 698 | alert(this.st.tr["Draw offer only in your turn"]); |
6fba6e0c | 699 | return; |
6808d7a1 BA |
700 | } |
701 | if (!confirm(this.st.tr["Offer draw?"])) return; | |
760adbce | 702 | this.drawOffer = "sent"; |
71468011 | 703 | this.send("drawoffer"); |
aae89b49 BA |
704 | if (this.game.type == "live") { |
705 | GameStorage.update( | |
706 | this.gameRef.id, | |
707 | { drawOffer: this.game.mycolor } | |
708 | ); | |
709 | } else this.updateCorrGame({ drawOffer: this.game.mycolor }); | |
a6088c90 BA |
710 | } |
711 | }, | |
7f3484bd | 712 | abortGame: function() { |
6808d7a1 | 713 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) return; |
8477e53d | 714 | this.gameOver("?", "Stop"); |
71468011 | 715 | this.send("abort"); |
a6088c90 | 716 | }, |
6808d7a1 | 717 | resign: function() { |
77c50966 | 718 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
a6088c90 | 719 | return; |
6808d7a1 | 720 | this.send("resign", { data: this.game.mycolor }); |
8477e53d BA |
721 | const score = this.game.mycolor == "w" ? "0-1" : "1-0"; |
722 | const side = this.game.mycolor == "w" ? "White" : "Black"; | |
723 | this.gameOver(score, side + " surrender"); | |
a6088c90 | 724 | }, |
967a2686 BA |
725 | // 3 cases for loading a game: |
726 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
727 | // - from server (one correspondance game I play[ed] or not) |
728 | // - from remote peer (one live game I don't play, finished or not) | |
760adbce | 729 | loadGame: function(game, callback) { |
6808d7a1 | 730 | const afterRetrieval = async game => { |
f41ce580 BA |
731 | const vModule = await import("@/variants/" + game.vname + ".js"); |
732 | window.V = vModule.VariantRules; | |
733 | this.vr = new V(game.fen); | |
6808d7a1 | 734 | const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live"; |
71468011 | 735 | const tc = extractTime(game.cadence); |
9ef63965 BA |
736 | const myIdx = game.players.findIndex(p => { |
737 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
738 | }); | |
6808d7a1 BA |
739 | const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers |
740 | if (!game.chats) game.chats = []; //live games don't have chat history | |
741 | if (gtype == "corr") { | |
742 | if (game.players[0].color == "b") { | |
f41ce580 | 743 | // Adopt the same convention for live and corr games: [0] = white |
6808d7a1 BA |
744 | [game.players[0], game.players[1]] = [ |
745 | game.players[1], | |
746 | game.players[0] | |
747 | ]; | |
f41ce580 | 748 | } |
7f3484bd | 749 | // NOTE: clocks in seconds, initime in milliseconds |
6808d7a1 | 750 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
feaf1bf7 | 751 | game.clocks = [tc.mainTime, tc.mainTime]; |
e71161fb | 752 | const L = game.moves.length; |
6808d7a1 | 753 | if (game.score == "*") { |
e71161fb | 754 | // Set clocks + initime |
b7cbbda1 | 755 | game.initime = [0, 0]; |
92240cf0 BA |
756 | if (L >= 1) { |
757 | const gameLastupdate = game.moves[L-1].played; | |
758 | game.initime[L % 2] = gameLastupdate; | |
c3d16e78 BA |
759 | if (L >= 2) { |
760 | game.clocks[L % 2] = | |
761 | tc.mainTime - (Date.now() - gameLastupdate) / 1000; | |
762 | } | |
5f131484 | 763 | } |
92a523d1 | 764 | } |
9ef63965 | 765 | // Sort chat messages from newest to oldest |
6808d7a1 BA |
766 | game.chats.sort((c1, c2) => { |
767 | return c2.added - c1.added; | |
768 | }); | |
0d329b05 | 769 | if (myIdx >= 0 && game.score == "*" && game.chats.length > 0) { |
8477e53d | 770 | // Did a chat message arrive after my last move? |
9ef63965 | 771 | let dtLastMove = 0; |
e71161fb BA |
772 | if (L == 1 && myIdx == 0) |
773 | dtLastMove = game.moves[0].played; | |
774 | else if (L >= 2) { | |
775 | if (L % 2 == 0) { | |
776 | // It's now white turn | |
777 | dtLastMove = game.moves[L-1-(1-myIdx)].played; | |
778 | } else { | |
779 | // Black turn: | |
780 | dtLastMove = game.moves[L-1-myIdx].played; | |
9ef63965 BA |
781 | } |
782 | } | |
783 | if (dtLastMove < game.chats[0].added) | |
784 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
785 | } | |
786 | // Now that we used idx and played, re-format moves as for live games | |
8477e53d | 787 | game.moves = game.moves.map(m => m.squares); |
c0b27606 | 788 | } |
6808d7a1 | 789 | if (gtype == "live" && game.clocks[0] < 0) { |
8477e53d | 790 | // Game is unstarted |
66d03f23 | 791 | game.clocks = [tc.mainTime, tc.mainTime]; |
6808d7a1 | 792 | if (game.score == "*") { |
b7cbbda1 | 793 | game.initime[0] = Date.now(); |
6808d7a1 | 794 | if (myIdx >= 0) { |
b7cbbda1 | 795 | // I play in this live game; corr games don't have clocks+initime |
6808d7a1 | 796 | GameStorage.update(game.id, { |
b7cbbda1 | 797 | clocks: game.clocks, |
6808d7a1 | 798 | initime: game.initime |
b7cbbda1 BA |
799 | }); |
800 | } | |
22efa391 | 801 | } |
66d03f23 | 802 | } |
e8da204a | 803 | if (!!game.drawOffer) { |
6808d7a1 | 804 | if (game.drawOffer == "t") |
8477e53d | 805 | // Three repetitions |
77c50966 | 806 | this.drawOffer = "threerep"; |
6808d7a1 | 807 | else { |
8477e53d | 808 | // Draw offered by any of the players: |
6808d7a1 | 809 | if (myIdx < 0) this.drawOffer = "received"; |
6808d7a1 | 810 | else { |
77c50966 | 811 | // I play in this game: |
6808d7a1 BA |
812 | if ( |
813 | (game.drawOffer == "w" && myIdx == 0) || | |
814 | (game.drawOffer == "b" && myIdx == 1) | |
815 | ) | |
77c50966 | 816 | this.drawOffer = "sent"; |
6808d7a1 | 817 | else this.drawOffer = "received"; |
77c50966 BA |
818 | } |
819 | } | |
820 | } | |
725da57f BA |
821 | this.repeat = {}; //reset: scan past moves' FEN: |
822 | let repIdx = 0; | |
725da57f | 823 | let vr_tmp = new V(game.fenStart); |
725da57f BA |
824 | let curTurn = "n"; |
825 | game.moves.forEach(m => { | |
e71161fb BA |
826 | playMove(m, vr_tmp); |
827 | const fenIdx = vr_tmp.getFen().replace(/ /g, "_"); | |
828 | this.repeat[fenIdx] = this.repeat[fenIdx] | |
829 | ? this.repeat[fenIdx] + 1 | |
725da57f BA |
830 | : 1; |
831 | }); | |
725da57f | 832 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; |
6808d7a1 | 833 | this.game = Object.assign( |
cf742aaf | 834 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 835 | { |
c0b27606 | 836 | type: gtype, |
66d03f23 | 837 | increment: tc.increment, |
9ef63965 | 838 | mycolor: mycolor, |
5f131484 BA |
839 | // opponent sid not strictly required (or available), but easier |
840 | // at least oppsid or oppid is available anyway: | |
6808d7a1 | 841 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, |
e01e086d | 842 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].uid |
e71161fb BA |
843 | }, |
844 | game, | |
4b0384fa | 845 | ); |
dcff8e82 BA |
846 | if (this.gameIsLoading) |
847 | // Re-load game because we missed some moves: | |
57eb158f | 848 | // artificially reset BaseGame (required if moves arrived in wrong order) |
dcff8e82 | 849 | this.$refs["basegame"].re_setVariables(); |
5aa14a21 | 850 | else { |
aae89b49 BA |
851 | // Initial loading: |
852 | this.gotMoveIdx = game.moves.length - 1; | |
5aa14a21 BA |
853 | // If we arrive here after 'nextGame' action, the board might be hidden |
854 | let boardDiv = document.querySelector(".game"); | |
855 | if (!!boardDiv && boardDiv.style.visibility == "hidden") | |
856 | boardDiv.style.visibility = "visible"; | |
857 | } | |
9ef63965 | 858 | this.re_setClocks(); |
a0c41e7e BA |
859 | this.$nextTick(() => { |
860 | this.game.rendered = true; | |
861 | // Did lastate arrive before game was rendered? | |
6808d7a1 | 862 | if (this.lastate) this.processLastate(); |
a0c41e7e | 863 | }); |
d6f08e56 BA |
864 | if (this.gameIsLoading) { |
865 | this.gameIsLoading = false; | |
866 | if (this.gotMoveIdx >= game.moves.length) | |
867 | // Some moves arrived meanwhile... | |
868 | this.askGameAgain(); | |
869 | } | |
dcff8e82 | 870 | if (!!callback) callback(); |
967a2686 | 871 | }; |
f9c36b2d | 872 | if (!!game) { |
6808d7a1 BA |
873 | afterRetrieval(game); |
874 | return; | |
967a2686 | 875 | } |
6808d7a1 BA |
876 | if (this.gameRef.rid) { |
877 | // Remote live game: forgetting about callback func... (TODO: design) | |
878 | this.send("askfullgame", { target: this.gameRef.rid }); | |
879 | } else { | |
aae89b49 | 880 | // Local or corr game on server. |
8477e53d | 881 | // NOTE: afterRetrieval() is never called if game not found |
aae89b49 BA |
882 | const gid = this.gameRef.id; |
883 | if (Number.isInteger(gid) || !isNaN(parseInt(gid))) { | |
884 | // corr games identifiers are integers | |
e57c4de4 BA |
885 | ajax( |
886 | "/games", | |
887 | "GET", | |
888 | { | |
889 | data: { gid: gid }, | |
890 | success: (res) => { | |
891 | let g = res.game; | |
892 | g.moves.forEach(m => { | |
893 | m.squares = JSON.parse(m.squares); | |
894 | }); | |
895 | afterRetrieval(g); | |
896 | } | |
897 | } | |
898 | ); | |
aae89b49 BA |
899 | } |
900 | else | |
901 | // Local game | |
902 | GameStorage.get(this.gameRef.id, afterRetrieval); | |
967a2686 | 903 | } |
a6088c90 | 904 | }, |
9ef63965 | 905 | re_setClocks: function() { |
dcff8e82 | 906 | if (this.game.moves.length < 2 || this.game.score != "*") { |
9ef63965 | 907 | // 1st move not completed yet, or game over: freeze time |
57eb158f | 908 | this.virtualClocks = this.game.clocks.map(s => ppt(s).split(':')); |
9ef63965 BA |
909 | return; |
910 | } | |
911 | const currentTurn = this.vr.turn; | |
8477e53d | 912 | const currentMovesCount = this.game.moves.length; |
6808d7a1 BA |
913 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
914 | let countdown = | |
915 | this.game.clocks[colorIdx] - | |
916 | (Date.now() - this.game.initime[colorIdx]) / 1000; | |
917 | this.virtualClocks = [0, 1].map(i => { | |
918 | const removeTime = | |
919 | i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0; | |
57eb158f | 920 | return ppt(this.game.clocks[i] - removeTime).split(':'); |
9ef63965 | 921 | }); |
e01e086d BA |
922 | this.clockUpdate = setInterval( |
923 | () => { | |
924 | if ( | |
925 | countdown < 0 || | |
926 | this.game.moves.length > currentMovesCount || | |
927 | this.game.score != "*" | |
928 | ) { | |
929 | clearInterval(this.clockUpdate); | |
930 | if (countdown < 0) | |
931 | this.gameOver( | |
932 | currentTurn == "w" ? "0-1" : "1-0", | |
933 | "Time" | |
934 | ); | |
935 | } else | |
936 | this.$set( | |
937 | this.virtualClocks, | |
938 | colorIdx, | |
939 | ppt(Math.max(0, --countdown)).split(':') | |
6808d7a1 | 940 | ); |
e01e086d BA |
941 | }, |
942 | 1000 | |
943 | ); | |
9ef63965 | 944 | }, |
57eb158f | 945 | // Update variables and storage after a move: |
e71161fb | 946 | processMove: function(move, data) { |
e5c1d0fb | 947 | if (!data) data = {}; |
e71161fb BA |
948 | const moveCol = this.vr.turn; |
949 | const doProcessMove = () => { | |
950 | const colorIdx = ["w", "b"].indexOf(moveCol); | |
951 | const nextIdx = 1 - colorIdx; | |
dcff8e82 | 952 | const origMovescount = this.game.moves.length; |
e01e086d | 953 | let addTime = 0; //for live games |
f9c36b2d | 954 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
e71161fb BA |
955 | if (this.drawOffer == "received") |
956 | // I refuse draw | |
957 | this.drawOffer = ""; | |
dcff8e82 | 958 | if (this.game.type == "live" && origMovescount >= 2) { |
e71161fb BA |
959 | const elapsed = Date.now() - this.game.initime[colorIdx]; |
960 | // elapsed time is measured in milliseconds | |
961 | addTime = this.game.increment - elapsed / 1000; | |
962 | } | |
dce792f6 | 963 | } |
dcff8e82 | 964 | // Update current game object: |
e71161fb | 965 | playMove(move, this.vr); |
e01e086d BA |
966 | // The move is played: stop clock |
967 | clearInterval(this.clockUpdate); | |
5aa14a21 BA |
968 | if (!data.score) { |
969 | // Received move, score has not been computed in BaseGame (!!noemit) | |
970 | const score = this.vr.getCurrentScore(); | |
971 | if (score != "*") this.gameOver(score); | |
972 | } | |
f9c36b2d | 973 | // TODO: notifyTurn: "changeturn" message |
dcff8e82 | 974 | this.game.moves.push(move); |
e71161fb | 975 | this.game.fen = this.vr.getFen(); |
e01e086d BA |
976 | if (this.game.type == "live") { |
977 | if (!!data.clock) this.game.clocks[colorIdx] = data.clock; | |
978 | else this.game.clocks[colorIdx] += addTime; | |
979 | } | |
92240cf0 | 980 | // In corr games, just reset clock to mainTime: |
e01e086d BA |
981 | else { |
982 | this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime; | |
983 | } | |
984 | // NOTE: opponent's initime is reset after "gotmove" is received | |
985 | if ( | |
986 | !this.game.mycolor || | |
987 | moveCol != this.game.mycolor || | |
988 | !!data.receiveMyMove | |
989 | ) { | |
990 | this.game.initime[nextIdx] = Date.now(); | |
991 | } | |
e71161fb | 992 | // If repetition detected, consider that a draw offer was received: |
f9c36b2d | 993 | const fenObj = this.vr.getFenForRepeat(); |
e01e086d BA |
994 | this.repeat[fenObj] = |
995 | !!this.repeat[fenObj] | |
996 | ? this.repeat[fenObj] + 1 | |
997 | : 1; | |
f9c36b2d | 998 | if (this.repeat[fenObj] >= 3) this.drawOffer = "threerep"; |
e71161fb | 999 | else if (this.drawOffer == "threerep") this.drawOffer = ""; |
dcff8e82 BA |
1000 | if (!!this.game.mycolor && !data.receiveMyMove) { |
1001 | // NOTE: 'var' to see that variable outside this block | |
1002 | var filtered_move = getFilteredMove(move); | |
1003 | } | |
aae89b49 BA |
1004 | // Since corr games are stored at only one location, update should be |
1005 | // done only by one player for each move: | |
e71161fb | 1006 | if ( |
f9c36b2d BA |
1007 | !!this.game.mycolor && |
1008 | !data.receiveMyMove && | |
e71161fb BA |
1009 | (this.game.type == "live" || moveCol == this.game.mycolor) |
1010 | ) { | |
1011 | let drawCode = ""; | |
1012 | switch (this.drawOffer) { | |
1013 | case "threerep": | |
1014 | drawCode = "t"; | |
1015 | break; | |
1016 | case "sent": | |
1017 | drawCode = this.game.mycolor; | |
1018 | break; | |
1019 | case "received": | |
1020 | drawCode = V.GetOppCol(this.game.mycolor); | |
1021 | break; | |
1022 | } | |
1023 | if (this.game.type == "corr") { | |
aae89b49 BA |
1024 | // corr: only move, fen and score |
1025 | this.updateCorrGame({ | |
e71161fb BA |
1026 | fen: this.game.fen, |
1027 | move: { | |
1028 | squares: filtered_move, | |
1029 | played: Date.now(), | |
dcff8e82 | 1030 | idx: origMovescount |
e71161fb BA |
1031 | }, |
1032 | // Code "n" for "None" to force reset (otherwise it's ignored) | |
1033 | drawOffer: drawCode || "n" | |
1034 | }); | |
1035 | } | |
57eb158f BA |
1036 | else { |
1037 | const updateStorage = () => { | |
1038 | GameStorage.update(this.gameRef.id, { | |
1039 | fen: this.game.fen, | |
1040 | move: filtered_move, | |
1041 | moveIdx: origMovescount, | |
1042 | clocks: this.game.clocks, | |
1043 | initime: this.game.initime, | |
1044 | drawOffer: drawCode | |
1045 | }); | |
1046 | }; | |
1047 | // The active tab can update storage immediately | |
1048 | if (!document.hidden) updateStorage(); | |
1049 | // Small random delay otherwise | |
1050 | else setTimeout(updateStorage, 500 + 1000 * Math.random()); | |
e71161fb | 1051 | } |
e69f159d | 1052 | } |
dcff8e82 BA |
1053 | // Send move ("newmove" event) to people in the room (if our turn) |
1054 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { | |
e01e086d | 1055 | let sendMove = { |
dcff8e82 BA |
1056 | move: filtered_move, |
1057 | index: origMovescount, | |
1058 | // color is required to check if this is my move (if several tabs opened) | |
1059 | color: moveCol, | |
dcff8e82 BA |
1060 | cancelDrawOffer: this.drawOffer == "" |
1061 | }; | |
e01e086d BA |
1062 | if (this.game.type == "live") |
1063 | sendMove["clock"] = this.game.clocks[colorIdx]; | |
dcff8e82 BA |
1064 | this.opponentGotMove = false; |
1065 | this.send("newmove", {data: sendMove}); | |
1066 | // If the opponent doesn't reply gotmove soon enough, re-send move: | |
e01e086d BA |
1067 | // Do this at most 2 times, because mpore would mean network issues, |
1068 | // opponent would then be expected to disconnect/reconnect. | |
1069 | let counter = 1; | |
1070 | const currentUrl = document.location.href; | |
aae89b49 | 1071 | this.retrySendmove = setInterval( |
57eb158f | 1072 | () => { |
e01e086d BA |
1073 | if ( |
1074 | counter >= 3 || | |
1075 | this.opponentGotMove || | |
1076 | document.location.href != currentUrl //page change | |
1077 | ) { | |
aae89b49 | 1078 | clearInterval(this.retrySendmove); |
57eb158f BA |
1079 | return; |
1080 | } | |
1081 | let oppsid = this.game.players[nextIdx].sid; | |
1082 | if (!oppsid) { | |
1083 | oppsid = Object.keys(this.people).find( | |
1084 | sid => this.people[sid].id == this.game.players[nextIdx].uid | |
1085 | ); | |
1086 | } | |
1087 | if (!oppsid || !this.people[oppsid]) | |
1088 | // Opponent is disconnected: he'll ask last state | |
aae89b49 | 1089 | clearInterval(this.retrySendmove); |
e01e086d BA |
1090 | else { |
1091 | this.send("newmove", { data: sendMove, target: oppsid }); | |
1092 | counter++; | |
1093 | } | |
57eb158f | 1094 | }, |
e01e086d | 1095 | 1500 |
57eb158f | 1096 | ); |
dcff8e82 | 1097 | } |
e01e086d BA |
1098 | else |
1099 | // Not my move or I'm an observer: just start other player's clock | |
1100 | this.re_setClocks(); | |
e71161fb | 1101 | }; |
f9c36b2d BA |
1102 | if ( |
1103 | this.game.type == "corr" && | |
1104 | moveCol == this.game.mycolor && | |
1105 | !data.receiveMyMove | |
1106 | ) { | |
a17ae317 | 1107 | let boardDiv = document.querySelector(".game"); |
5aa14a21 BA |
1108 | const afterSetScore = () => { |
1109 | doProcessMove(); | |
1110 | if (this.st.settings.gotonext && this.nextIds.length > 0) | |
1111 | this.showNextGame(); | |
1112 | else { | |
5aa14a21 | 1113 | // The board might have been hidden: |
5aa14a21 BA |
1114 | if (boardDiv.style.visibility == "hidden") |
1115 | boardDiv.style.visibility = "visible"; | |
e71161fb | 1116 | } |
5aa14a21 | 1117 | }; |
a17ae317 BA |
1118 | let el = document.querySelector("#buttonsConfirm > .acceptBtn"); |
1119 | // We may play several moves in a row: in case of, remove listener: | |
1120 | let elClone = el.cloneNode(true); | |
1121 | el.parentNode.replaceChild(elClone, el); | |
1122 | elClone.addEventListener( | |
1123 | "click", | |
1124 | () => { | |
1125 | document.getElementById("modalConfirm").checked = false; | |
1126 | if (!!data.score && data.score != "*") | |
1127 | // Set score first | |
1128 | this.gameOver(data.score, null, afterSetScore); | |
1129 | else afterSetScore(); | |
1130 | } | |
1131 | ); | |
1132 | // PlayOnBoard is enough, and more appropriate for Synchrone Chess | |
1133 | V.PlayOnBoard(this.vr.board, move); | |
1134 | const position = this.vr.getBaseFen(); | |
1135 | V.UndoOnBoard(this.vr.board, move); | |
5aa14a21 | 1136 | if (["all","byrow"].includes(V.ShowMoves)) { |
5aa14a21 BA |
1137 | this.curDiag = getDiagram({ |
1138 | position: position, | |
1139 | orientation: V.CanFlip ? this.game.mycolor : "w" | |
1140 | }); | |
5aa14a21 BA |
1141 | } else { |
1142 | // Incomplete information: just ask confirmation | |
a17ae317 | 1143 | // Hide the board, because otherwise it could reveal infos |
5aa14a21 | 1144 | boardDiv.style.visibility = "hidden"; |
a17ae317 | 1145 | this.moveNotation = getFullNotation(move); |
5aa14a21 | 1146 | } |
a17ae317 | 1147 | document.getElementById("modalConfirm").checked = true; |
6d68309a | 1148 | } |
aae89b49 | 1149 | else { |
5aa14a21 | 1150 | // Normal situation |
5aa14a21 | 1151 | if (!!data.score && data.score != "*") |
e01e086d BA |
1152 | this.gameOver(data.score, null, doProcessMove); |
1153 | else doProcessMove(); | |
aae89b49 | 1154 | } |
b4fb1612 | 1155 | }, |
5b4de147 | 1156 | cancelMove: function() { |
a17ae317 BA |
1157 | let boardDiv = document.querySelector(".game"); |
1158 | if (boardDiv.style.visibility == "hidden") | |
1159 | boardDiv.style.visibility = "visible"; | |
5b4de147 BA |
1160 | document.getElementById("modalConfirm").checked = false; |
1161 | this.$refs["basegame"].cancelLastMove(); | |
1162 | }, | |
5aa14a21 BA |
1163 | // In corr games, callback to change page only after score is set: |
1164 | gameOver: function(score, scoreMsg, callback) { | |
430a2038 | 1165 | this.game.score = score; |
5aa14a21 | 1166 | if (!scoreMsg) scoreMsg = getScoreMessage(score); |
a17ae317 | 1167 | this.game.scoreMsg = scoreMsg; |
5aa14a21 | 1168 | this.$set(this.game, "scoreMsg", scoreMsg); |
ab6f48ea BA |
1169 | const myIdx = this.game.players.findIndex(p => { |
1170 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
1171 | }); | |
6808d7a1 | 1172 | if (myIdx >= 0) { |
8477e53d | 1173 | // OK, I play in this game |
aae89b49 | 1174 | const scoreObj = { |
6808d7a1 BA |
1175 | score: score, |
1176 | scoreMsg: scoreMsg | |
aae89b49 | 1177 | }; |
5aa14a21 | 1178 | if (this.game.type == "live") { |
aae89b49 | 1179 | GameStorage.update(this.gameRef.id, scoreObj); |
5aa14a21 BA |
1180 | if (!!callback) callback(); |
1181 | } | |
1182 | else this.updateCorrGame(scoreObj, callback); | |
48ab808f | 1183 | // Notify the score to main Hall. TODO: only one player (currently double send) |
6808d7a1 | 1184 | this.send("result", { gid: this.game.id, score: score }); |
dcd68c41 | 1185 | } |
5aa14a21 | 1186 | else if (!!callback) callback(); |
6808d7a1 BA |
1187 | } |
1188 | } | |
a6088c90 BA |
1189 | }; |
1190 | </script> | |
7e1a1fe9 | 1191 | |
41c80bb6 | 1192 | <style lang="sass" scoped> |
72ccbd67 | 1193 | .connected |
050ae3b5 | 1194 | background-color: lightgreen |
72ccbd67 | 1195 | |
ed06d9e9 BA |
1196 | #participants |
1197 | margin-left: 5px | |
1198 | ||
1199 | .anonymous | |
1200 | color: grey | |
1201 | font-style: italic | |
1202 | ||
ec905cbc BA |
1203 | #playersInfo > p |
1204 | margin: 0 | |
1205 | ||
430a2038 BA |
1206 | @media screen and (min-width: 768px) |
1207 | #actions | |
1208 | width: 300px | |
1209 | @media screen and (max-width: 767px) | |
1210 | .game | |
1211 | width: 100% | |
72ccbd67 | 1212 | |
430a2038 | 1213 | #actions |
cf94b843 | 1214 | display: inline-block |
1d6d7b1d | 1215 | margin: 0 |
feaf1bf7 BA |
1216 | |
1217 | button | |
1218 | display: inline-block | |
1219 | margin: 0 | |
1220 | display: inline-flex | |
1221 | img | |
1222 | height: 24px | |
1223 | display: flex | |
1224 | @media screen and (max-width: 767px) | |
1225 | height: 18px | |
a1c48034 | 1226 | |
050ae3b5 BA |
1227 | @media screen and (max-width: 767px) |
1228 | #aboveBoard | |
1229 | text-align: center | |
885d93a7 BA |
1230 | @media screen and (min-width: 768px) |
1231 | #aboveBoard | |
1232 | margin-left: 30% | |
050ae3b5 | 1233 | |
2f258c37 BA |
1234 | .variant-cadence |
1235 | padding-right: 10px | |
1236 | ||
1237 | .variant-name | |
8c5f5390 | 1238 | font-weight: bold |
77c50966 | 1239 | padding-right: 10px |
77c50966 | 1240 | |
feaf1bf7 BA |
1241 | span#nextGame |
1242 | background-color: #edda99 | |
1243 | cursor: pointer | |
1244 | display: inline-block | |
1245 | margin-right: 10px | |
1246 | ||
57eb158f | 1247 | span.name |
050ae3b5 | 1248 | font-size: 1.5rem |
57eb158f | 1249 | padding: 0 3px |
050ae3b5 | 1250 | |
57eb158f | 1251 | span.time |
050ae3b5 BA |
1252 | font-size: 2rem |
1253 | display: inline-block | |
57eb158f BA |
1254 | .time-left |
1255 | margin-left: 10px | |
1256 | .time-right | |
1257 | margin-left: 5px | |
1258 | .time-separator | |
1259 | margin-left: 5px | |
1260 | position: relative | |
1261 | top: -1px | |
1262 | ||
1263 | span.yourturn | |
1264 | color: #831B1B | |
1265 | .time-separator | |
1266 | animation: blink-animation 2s steps(3, start) infinite | |
1267 | @keyframes blink-animation | |
1268 | to | |
1269 | visibility: hidden | |
050ae3b5 BA |
1270 | |
1271 | .split-names | |
1272 | display: inline-block | |
1273 | margin: 0 15px | |
1274 | ||
5b4de147 | 1275 | #chatWrap > .card |
a1c48034 | 1276 | padding-top: 20px |
a154d45e | 1277 | max-width: 767px |
5b4de147 BA |
1278 | border: none |
1279 | ||
1280 | #confirmDiv > .card | |
1281 | max-width: 767px | |
1282 | max-height: 100% | |
cf94b843 | 1283 | |
dcd68c41 BA |
1284 | .draw-sent, .draw-sent:hover |
1285 | background-color: lightyellow | |
1286 | ||
1287 | .draw-received, .draw-received:hover | |
1288 | background-color: lightgreen | |
1289 | ||
1290 | .draw-threerep, .draw-threerep:hover | |
1291 | background-color: #e4d1fc | |
2f258c37 BA |
1292 | |
1293 | .somethingnew | |
1294 | background-color: #c5fefe | |
5b4de147 BA |
1295 | |
1296 | .diagram | |
1297 | margin: 0 auto | |
1298 | max-width: 400px | |
1299 | // width: 100% required for Firefox | |
1300 | width: 100% | |
1301 | ||
1302 | #buttonsConfirm | |
1303 | margin: 0 | |
1304 | & > button > span | |
1305 | width: 100% | |
1306 | text-align: center | |
1307 | ||
1308 | button.acceptBtn | |
1309 | background-color: lightgreen | |
1310 | button.refuseBtn | |
1311 | background-color: red | |
7e1a1fe9 | 1312 | </style> |