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