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 | ) | |
a1c48034 BA |
11 | #chat.card |
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 BA |
22 | Chat( |
23 | :players="game.players" | |
24 | :pastChats="game.chats" | |
25 | :newChat="newChat" | |
26 | @mychat="processChat" | |
db1f1f9a | 27 | @chatcleared="clearChat" |
910d631b | 28 | ) |
7aa548e7 | 29 | .row |
050ae3b5 | 30 | #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2 |
2f258c37 BA |
31 | span.variant-cadence {{ game.cadence }} |
32 | span.variant-name {{ game.vname }} | |
910d631b | 33 | button#chatBtn(onClick="window.doClick('modalChat')") Chat |
4f518610 | 34 | #actions(v-if="game.score=='*'") |
910d631b BA |
35 | button( |
36 | @click="clickDraw()" | |
37 | :class="{['draw-' + drawOffer]: true}" | |
38 | ) | |
602d6bef | 39 | | {{ st.tr["Draw"] }} |
910d631b BA |
40 | button( |
41 | v-if="!!game.mycolor" | |
42 | @click="abortGame()" | |
43 | ) | |
44 | | {{ st.tr["Abort"] }} | |
45 | button( | |
46 | v-if="!!game.mycolor" | |
47 | @click="resign()" | |
48 | ) | |
49 | | {{ st.tr["Resign"] }} | |
050ae3b5 BA |
50 | #playersInfo |
51 | p | |
5bcc9b31 BA |
52 | span.name(:class="{connected: isConnected(0)}") |
53 | | {{ game.players[0].name || "@nonymous" }} | |
050ae3b5 BA |
54 | span.time(v-if="game.score=='*'") {{ virtualClocks[0] }} |
55 | span.split-names - | |
5bcc9b31 BA |
56 | span.name(:class="{connected: isConnected(1)}") |
57 | | {{ game.players[1].name || "@nonymous" }} | |
050ae3b5 | 58 | span.time(v-if="game.score=='*'") {{ virtualClocks[1] }} |
910d631b | 59 | BaseGame( |
8477e53d | 60 | ref="basegame" |
910d631b | 61 | :game="game" |
910d631b BA |
62 | @newmove="processMove" |
63 | @gameover="gameOver" | |
64 | ) | |
a6088c90 BA |
65 | </template> |
66 | ||
67 | <script> | |
46284a2f | 68 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 69 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 70 | import { store } from "@/store"; |
967a2686 | 71 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 72 | import { ppt } from "@/utils/datetime"; |
23ecf008 | 73 | import { ajax } from "@/utils/ajax"; |
66d03f23 | 74 | import { extractTime } from "@/utils/timeControl"; |
51d87b52 | 75 | import { getRandString } from "@/utils/alea"; |
dcd68c41 | 76 | import { processModalClick } from "@/utils/modalClick"; |
e71161fb BA |
77 | import { getFullNotation } from "@/utils/notation"; |
78 | import { playMove, getFilteredMove } from "@/utils/playUndo"; | |
77c50966 | 79 | import { getScoreMessage } from "@/utils/scoring"; |
1611a25f | 80 | import { ArrayFun } from "@/utils/array"; |
8418f0d7 | 81 | import params from "@/parameters"; |
a6088c90 | 82 | export default { |
6808d7a1 | 83 | name: "my-game", |
a6088c90 BA |
84 | components: { |
85 | BaseGame, | |
6808d7a1 | 86 | Chat |
a6088c90 | 87 | }, |
f7121527 | 88 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
89 | data: function() { |
90 | return { | |
91 | st: store.state, | |
6808d7a1 | 92 | gameRef: { |
1611a25f | 93 | // rid = remote (socket) ID |
4b0384fa BA |
94 | id: "", |
95 | rid: "" | |
96 | }, | |
6808d7a1 | 97 | game: { |
1611a25f | 98 | // Passed to BaseGame |
6808d7a1 | 99 | players: [{ name: "" }, { name: "" }], |
9a3049f3 | 100 | chats: [], |
6808d7a1 | 101 | rendered: false |
a0c41e7e | 102 | }, |
809ba2aa | 103 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 104 | vr: null, //"variant rules" object initialized from FEN |
dcd68c41 BA |
105 | drawOffer: "", |
106 | people: {}, //players + observers | |
1611a25f | 107 | onMygames: [], //opponents (or me) on "MyGames" page |
760adbce | 108 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 109 | repeat: {}, //detect position repetition |
ac8f441c | 110 | newChat: "", |
8418f0d7 | 111 | conn: null, |
f9c36b2d BA |
112 | roomInitialized: false, |
113 | // If newmove has wrong index: ask fullgame again: | |
dcff8e82 | 114 | gameIsLoading: false, |
f9c36b2d BA |
115 | // If asklastate got no reply, ask again: |
116 | gotLastate: false, | |
e5c1d0fb | 117 | gotMoveIdx: -1, //last move index received |
f9c36b2d BA |
118 | // If newmove got no pingback, send again: |
119 | opponentGotMove: false, | |
51d87b52 BA |
120 | connexionString: "", |
121 | // Related to (killing of) self multi-connects: | |
122 | newConnect: {}, | |
6808d7a1 | 123 | killed: {} |
a6088c90 BA |
124 | }; |
125 | }, | |
126 | watch: { | |
6808d7a1 | 127 | $route: function(to) { |
5f131484 BA |
128 | this.gameRef.id = to.params["id"]; |
129 | this.gameRef.rid = to.query["rid"]; | |
130 | this.loadGame(); | |
6808d7a1 | 131 | } |
92a523d1 | 132 | }, |
71468011 | 133 | // NOTE: some redundant code with Hall.vue (mostly related to people array) |
a6088c90 | 134 | created: function() { |
5f131484 BA |
135 | // Always add myself to players' list |
136 | const my = this.st.user; | |
6808d7a1 | 137 | this.$set(this.people, my.sid, { id: my.id, name: my.name }); |
dc284d90 BA |
138 | this.gameRef.id = this.$route.params["id"]; |
139 | this.gameRef.rid = this.$route.query["rid"]; //may be undefined | |
8418f0d7 | 140 | // Initialize connection |
6808d7a1 BA |
141 | this.connexionString = |
142 | params.socketUrl + | |
143 | "/?sid=" + | |
144 | this.st.user.sid + | |
145 | "&tmpId=" + | |
146 | getRandString() + | |
147 | "&page=" + | |
148 | encodeURIComponent(this.$route.path); | |
51d87b52 | 149 | this.conn = new WebSocket(this.connexionString); |
8418f0d7 | 150 | this.conn.onmessage = this.socketMessageListener; |
51d87b52 | 151 | this.conn.onclose = this.socketCloseListener; |
760adbce | 152 | // Socket init required before loading remote game: |
6808d7a1 BA |
153 | const socketInit = callback => { |
154 | if (!!this.conn && this.conn.readyState == 1) | |
8477e53d | 155 | // 1 == OPEN state |
760adbce | 156 | callback(); |
1611a25f | 157 | else |
8477e53d | 158 | // Socket not ready yet (initial loading) |
7f36b53a BA |
159 | // NOTE: it's important to call callback without arguments, |
160 | // otherwise first arg is Websocket object and loadGame fails. | |
1611a25f | 161 | this.conn.onopen = () => callback(); |
760adbce | 162 | }; |
6808d7a1 | 163 | if (!this.gameRef.rid) |
8477e53d | 164 | // Game stored locally or on server |
760adbce | 165 | this.loadGame(null, () => socketInit(this.roomInit)); |
1611a25f | 166 | else |
8477e53d | 167 | // Game stored remotely: need socket to retrieve it |
760adbce BA |
168 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. |
169 | // --> It will be given when receiving "fullgame" socket event. | |
760adbce | 170 | socketInit(this.loadGame); |
cdb34c93 | 171 | }, |
dcd68c41 | 172 | mounted: function() { |
6808d7a1 BA |
173 | document |
174 | .getElementById("chatWrap") | |
175 | .addEventListener("click", processModalClick); | |
dcd68c41 | 176 | }, |
8418f0d7 | 177 | beforeDestroy: function() { |
71468011 | 178 | this.send("disconnect"); |
8418f0d7 | 179 | }, |
cdb34c93 | 180 | methods: { |
760adbce | 181 | roomInit: function() { |
f9c36b2d BA |
182 | if (!this.roomInitialized) { |
183 | // Notify the room only now that I connected, because | |
184 | // messages might be lost otherwise (if game loading is slow) | |
185 | this.send("connect"); | |
186 | this.send("pollclients"); | |
187 | // We may ask fullgame several times if some moves are lost, | |
188 | // but room should be init only once: | |
189 | this.roomInitialized = true; | |
190 | } | |
71468011 BA |
191 | }, |
192 | send: function(code, obj) { | |
f9c36b2d | 193 | if (!!this.conn) |
6808d7a1 | 194 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
5f131484 | 195 | }, |
050ae3b5 | 196 | isConnected: function(index) { |
29ced362 BA |
197 | const player = this.game.players[index]; |
198 | // Is it me ? | |
199 | if (this.st.user.sid == player.sid || this.st.user.id == player.uid) | |
050ae3b5 | 200 | return true; |
29ced362 | 201 | // Try to find a match in people: |
6808d7a1 | 202 | return ( |
1611a25f BA |
203 | ( |
204 | player.sid && | |
205 | Object.keys(this.people).some(sid => sid == player.sid) | |
206 | ) | |
207 | || | |
208 | ( | |
209 | player.uid && | |
210 | Object.values(this.people).some(p => p.id == player.uid) | |
211 | ) | |
6808d7a1 | 212 | ); |
050ae3b5 | 213 | }, |
db1f1f9a BA |
214 | resetChatColor: function() { |
215 | // TODO: this is called twice, once on opening an once on closing | |
216 | document.getElementById("chatBtn").classList.remove("somethingnew"); | |
217 | }, | |
218 | processChat: function(chat) { | |
219 | this.send("newchat", { data: chat }); | |
220 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) | |
221 | if (this.game.type == "corr" && this.st.user.id > 0) | |
222 | GameStorage.update(this.gameRef.id, { chat: chat }); | |
223 | }, | |
224 | clearChat: function() { | |
225 | // Nothing more to do if game is live (chats not recorded) | |
23ecf008 | 226 | if (this.game.type == "corr") { |
f9c36b2d | 227 | if (!!this.game.mycolor) |
23ecf008 | 228 | ajax("/chats", "DELETE", {gid: this.game.id}); |
dcff8e82 | 229 | this.$set(this.game, "chats", []); |
db1f1f9a BA |
230 | } |
231 | }, | |
1611a25f BA |
232 | // Notify turn after a new move (to opponent and me on MyGames page) |
233 | notifyTurn: function(sid) { | |
234 | const player = this.people[sid]; | |
235 | const colorIdx = this.game.players.findIndex( | |
236 | p => p.sid == sid || p.id == player.id); | |
237 | const color = ["w","b"][colorIdx]; | |
dcff8e82 | 238 | const movesCount = this.game.moves.length; |
1611a25f | 239 | const yourTurn = |
dcff8e82 BA |
240 | (color == "w" && movesCount % 2 == 0) || |
241 | (color == "b" && movesCount % 2 == 1); | |
1611a25f BA |
242 | this.send("turnchange", { target: sid, yourTurn: yourTurn }); |
243 | }, | |
cdb34c93 | 244 | socketMessageListener: function(msg) { |
6808d7a1 | 245 | if (!this.conn) return; |
a6088c90 | 246 | const data = JSON.parse(msg.data); |
6808d7a1 | 247 | switch (data.code) { |
5f131484 | 248 | case "pollclients": |
5f131484 | 249 | data.sockIds.forEach(sid => { |
dcff8e82 | 250 | if (sid != this.st.user.sid) |
6808d7a1 | 251 | this.send("askidentity", { target: sid }); |
5f131484 BA |
252 | }); |
253 | break; | |
71468011 | 254 | case "connect": |
efdfb4c7 | 255 | if (!this.people[data.from]) { |
51d87b52 | 256 | this.newConnect[data.from] = true; //for self multi-connects tests |
6808d7a1 | 257 | this.send("askidentity", { target: data.from }); |
51d87b52 | 258 | } |
71468011 BA |
259 | break; |
260 | case "disconnect": | |
261 | this.$delete(this.people, data.from); | |
262 | break; | |
efdfb4c7 | 263 | case "mconnect": { |
1611a25f BA |
264 | // TODO: from MyGames page : send mconnect message with the list of gid (live and corr) |
265 | // Either me (another tab) or opponent | |
266 | const sid = data.from; | |
267 | if (!this.onMygames.some(s => s == sid)) | |
268 | { | |
269 | this.onMygames.push(sid); | |
270 | this.notifyTurn(sid); //TODO: this may require server ID (so, notify after receiving identity) | |
271 | } | |
272 | break; | |
273 | if (!this.people[sid]) | |
274 | this.send("askidentity", { target: sid }); | |
275 | } | |
276 | case "mdisconnect": | |
277 | ArrayFun.remove(this.onMygames, sid => sid == data.from); | |
278 | break; | |
51d87b52 BA |
279 | case "killed": |
280 | // I logged in elsewhere: | |
51d87b52 | 281 | this.conn = null; |
09d37571 | 282 | alert(this.st.tr["New connexion detected: tab now offline"]); |
51d87b52 | 283 | break; |
6808d7a1 | 284 | case "askidentity": { |
efdfb4c7 | 285 | // Request for identification |
51d87b52 BA |
286 | const me = { |
287 | // Decompose to avoid revealing email | |
288 | name: this.st.user.name, | |
289 | sid: this.st.user.sid, | |
6808d7a1 | 290 | id: this.st.user.id |
51d87b52 | 291 | }; |
6808d7a1 | 292 | this.send("identity", { data: me, target: data.from }); |
5f131484 | 293 | break; |
51d87b52 | 294 | } |
6808d7a1 | 295 | case "identity": { |
71468011 | 296 | const user = data.data; |
efdfb4c7 | 297 | this.$set(this.people, user.sid, { name: user.name, id: user.id }); |
f9c36b2d BA |
298 | // If I multi-connect, kill current connexion if no mark (I'm older) |
299 | if (this.newConnect[user.sid]) { | |
6808d7a1 | 300 | if ( |
6808d7a1 BA |
301 | user.id > 0 && |
302 | user.id == this.st.user.id && | |
f9c36b2d BA |
303 | user.sid != this.st.user.sid && |
304 | !this.killed[this.st.user.sid] | |
6808d7a1 | 305 | ) { |
6808d7a1 | 306 | this.send("killme", { sid: this.st.user.sid }); |
51d87b52 | 307 | this.killed[this.st.user.sid] = true; |
51d87b52 | 308 | } |
f9c36b2d | 309 | delete this.newConnect[user.sid]; |
a0c41e7e | 310 | } |
dcff8e82 BA |
311 | if (!this.killed[this.st.user.sid]) { |
312 | // Ask potentially missed last state, if opponent and I play | |
313 | if ( | |
314 | !!this.game.mycolor && | |
315 | this.game.type == "live" && | |
316 | this.game.score == "*" && | |
317 | this.game.players.some(p => p.sid == user.sid) | |
318 | ) { | |
319 | let self = this; | |
320 | (function askLastate() { | |
321 | self.send("asklastate", { target: user.sid }); | |
322 | setTimeout( | |
323 | () => { | |
324 | // Ask until we got a reply (or opponent disconnect): | |
325 | if (!self.gotLastate && !!self.people[user.sid]) | |
326 | askLastate(); | |
327 | }, | |
328 | 750 | |
329 | ); | |
330 | })(); | |
331 | } | |
332 | } | |
a0c41e7e | 333 | break; |
71468011 BA |
334 | } |
335 | case "askgame": | |
336 | // Send current (live) game if not asked by any of the players | |
6808d7a1 BA |
337 | if ( |
338 | this.game.type == "live" && | |
339 | this.game.players.every(p => p.sid != data.from[0]) | |
340 | ) { | |
71468011 BA |
341 | const myGame = { |
342 | id: this.game.id, | |
343 | fen: this.game.fen, | |
344 | players: this.game.players, | |
345 | vid: this.game.vid, | |
346 | cadence: this.game.cadence, | |
347 | score: this.game.score, | |
6808d7a1 | 348 | rid: this.st.user.sid //useful in Hall if I'm an observer |
71468011 | 349 | }; |
6808d7a1 | 350 | this.send("game", { data: myGame, target: data.from }); |
71468011 BA |
351 | } |
352 | break; | |
353 | case "askfullgame": | |
6808d7a1 | 354 | this.send("fullgame", { data: this.game, target: data.from }); |
71468011 BA |
355 | break; |
356 | case "fullgame": | |
357 | // Callback "roomInit" to poll clients only after game is loaded | |
dcff8e82 | 358 | this.loadGame(data.data, this.roomInit); |
71468011 | 359 | break; |
a0c41e7e | 360 | case "asklastate": |
dcff8e82 | 361 | // Sending informative last state if I played a move or score != "*" |
6808d7a1 BA |
362 | if ( |
363 | (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || | |
364 | this.game.score != "*" || | |
365 | this.drawOffer == "sent" | |
366 | ) { | |
f41ce580 | 367 | // Send our "last state" informations to opponent |
411d23cd | 368 | const L = this.game.moves.length; |
6808d7a1 | 369 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); |
71468011 | 370 | const myLastate = { |
6808d7a1 | 371 | lastMove: L > 0 ? this.game.moves[L - 1] : undefined, |
dcff8e82 | 372 | addTime: L > 0 ? this.game.addTimes[L - 1] : undefined, |
71468011 BA |
373 | // Since we played a move (or abort or resign), |
374 | // only drawOffer=="sent" is possible | |
375 | drawSent: this.drawOffer == "sent", | |
26f3a887 | 376 | score: this.game.score, |
71468011 | 377 | movesCount: L, |
6808d7a1 | 378 | initime: this.game.initime[1 - myIdx] //relevant only if I played |
5fd5fb22 | 379 | }; |
6808d7a1 | 380 | this.send("lastate", { data: myLastate, target: data.from }); |
dcff8e82 BA |
381 | } else { |
382 | this.send("lastate", { data: {nothing: true}, target: data.from }); | |
5fd5fb22 | 383 | } |
c6788ecf | 384 | break; |
dcff8e82 BA |
385 | case "lastate": { |
386 | // Got opponent infos about last move | |
387 | this.gotLastate = true; | |
388 | if (!data.data.nothing) { | |
389 | this.lastate = data.data; | |
390 | if (this.game.rendered) | |
391 | // Game is rendered (Board component) | |
392 | this.processLastate(); | |
393 | // Else: will be processed when game is ready | |
394 | } | |
71468011 | 395 | break; |
dcff8e82 | 396 | } |
6808d7a1 | 397 | case "newmove": { |
dcff8e82 BA |
398 | const movePlus = data.data; |
399 | const movesCount = this.game.moves.length; | |
400 | if (movePlus.index > movesCount) { | |
401 | // This can only happen if I'm an observer and missed a move. | |
402 | if (!this.gameIsLoading) { | |
403 | this.gameIsLoading = true; | |
404 | if (!this.gameRef.rid) | |
405 | // This is my game: just reload. | |
406 | this.loadGame(); | |
407 | else { | |
408 | // Just ask fullgame again (once!), this is much simpler. | |
409 | // If this fails, the user could just reload page :/ | |
410 | let self = this; | |
411 | (function askIfPeerConnected() { | |
412 | if (!!self.people[self.gameRef.rid]) | |
413 | self.send("askfullgame", { target: self.gameRef.rid }); | |
414 | else setTimeout(askIfPeerConnected, 1000); | |
415 | })(); | |
416 | } | |
417 | } | |
f9c36b2d BA |
418 | } else { |
419 | if ( | |
dcff8e82 BA |
420 | movePlus.index < movesCount || |
421 | this.gotMoveIdx >= movePlus.index | |
f9c36b2d BA |
422 | ) { |
423 | // Opponent re-send but we already have the move: | |
424 | // (maybe he didn't receive our pingback...) | |
dcff8e82 | 425 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
f9c36b2d | 426 | } else { |
dcff8e82 BA |
427 | this.gotMoveIdx = movePlus.index; |
428 | const receiveMyMove = (movePlus.color == this.game.mycolor); | |
f9c36b2d BA |
429 | if (!receiveMyMove && !!this.game.mycolor) |
430 | // Notify opponent that I got the move: | |
dcff8e82 BA |
431 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
432 | if (movePlus.cancelDrawOffer) { | |
f9c36b2d BA |
433 | // Opponent refuses draw |
434 | this.drawOffer = ""; | |
435 | // NOTE for corr games: drawOffer reset by player in turn | |
436 | if ( | |
437 | this.game.type == "live" && | |
438 | !!this.game.mycolor && | |
439 | !receiveMyMove | |
440 | ) { | |
441 | GameStorage.update(this.gameRef.id, { drawOffer: "" }); | |
442 | } | |
443 | } | |
444 | this.$refs["basegame"].play( | |
dcff8e82 | 445 | movePlus.move, |
f9c36b2d BA |
446 | "received", |
447 | null, | |
448 | { | |
dcff8e82 | 449 | addTime: movePlus.addTime, |
f9c36b2d BA |
450 | receiveMyMove: receiveMyMove |
451 | } | |
452 | ); | |
453 | } | |
633959bf | 454 | } |
a6088c90 | 455 | break; |
71468011 | 456 | } |
f9c36b2d BA |
457 | case "gotmove": { |
458 | this.opponentGotMove = true; | |
459 | break; | |
460 | } | |
93d1d7a7 | 461 | case "resign": |
8477e53d BA |
462 | const score = data.side == "b" ? "1-0" : "0-1"; |
463 | const side = data.side == "w" ? "White" : "Black"; | |
464 | this.gameOver(score, side + " surrender"); | |
93d1d7a7 | 465 | break; |
93d1d7a7 | 466 | case "abort": |
8477e53d | 467 | this.gameOver("?", "Stop"); |
93d1d7a7 | 468 | break; |
2cc10cdb | 469 | case "draw": |
71468011 | 470 | this.gameOver("1/2", data.data); |
2cc10cdb BA |
471 | break; |
472 | case "drawoffer": | |
41c80bb6 BA |
473 | // NOTE: observers don't know who offered draw |
474 | this.drawOffer = "received"; | |
6d9f4315 | 475 | break; |
71468011 | 476 | case "newchat": |
bd76b456 | 477 | this.newChat = data.data; |
71468011 | 478 | if (!document.getElementById("modalChat").checked) |
2f258c37 | 479 | document.getElementById("chatBtn").classList.add("somethingnew"); |
a6088c90 BA |
480 | break; |
481 | } | |
cdb34c93 | 482 | }, |
51d87b52 BA |
483 | socketCloseListener: function() { |
484 | this.conn = new WebSocket(this.connexionString); | |
6808d7a1 BA |
485 | this.conn.addEventListener("message", this.socketMessageListener); |
486 | this.conn.addEventListener("close", this.socketCloseListener); | |
51d87b52 | 487 | }, |
760adbce BA |
488 | // lastate was received, but maybe game wasn't ready yet: |
489 | processLastate: function() { | |
490 | const data = this.lastate; | |
491 | this.lastate = undefined; //security... | |
492 | const L = this.game.moves.length; | |
6808d7a1 | 493 | if (data.movesCount > L) { |
760adbce | 494 | // Just got last move from him |
8477e53d | 495 | this.$refs["basegame"].play( |
dcff8e82 | 496 | data.lastMove, |
e71161fb BA |
497 | "received", |
498 | null, | |
dcff8e82 BA |
499 | {addTime: data.addTime, initime: data.initime} |
500 | ); | |
a0c41e7e | 501 | } |
6808d7a1 BA |
502 | if (data.drawSent) this.drawOffer = "received"; |
503 | if (data.score != "*") { | |
a0c41e7e | 504 | this.drawOffer = ""; |
6808d7a1 | 505 | if (this.game.score == "*") this.gameOver(data.score); |
760adbce BA |
506 | } |
507 | }, | |
dcd68c41 | 508 | clickDraw: function() { |
6808d7a1 BA |
509 | if (!this.game.mycolor) return; //I'm just spectator |
510 | if (["received", "threerep"].includes(this.drawOffer)) { | |
511 | if (!confirm(this.st.tr["Accept draw?"])) return; | |
512 | const message = | |
513 | this.drawOffer == "received" | |
514 | ? "Mutual agreement" | |
515 | : "Three repetitions"; | |
516 | this.send("draw", { data: message }); | |
77c50966 | 517 | this.gameOver("1/2", message); |
6808d7a1 | 518 | } else if (this.drawOffer == "") { |
e71161fb | 519 | // No effect if drawOffer == "sent" |
f9c36b2d | 520 | if (!!this.game.mycolor != this.vr.turn) { |
6808d7a1 | 521 | alert(this.st.tr["Draw offer only in your turn"]); |
6fba6e0c | 522 | return; |
6808d7a1 BA |
523 | } |
524 | if (!confirm(this.st.tr["Offer draw?"])) return; | |
760adbce | 525 | this.drawOffer = "sent"; |
71468011 | 526 | this.send("drawoffer"); |
6808d7a1 | 527 | GameStorage.update(this.gameRef.id, { drawOffer: this.game.mycolor }); |
a6088c90 BA |
528 | } |
529 | }, | |
7f3484bd | 530 | abortGame: function() { |
6808d7a1 | 531 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) return; |
8477e53d | 532 | this.gameOver("?", "Stop"); |
71468011 | 533 | this.send("abort"); |
a6088c90 | 534 | }, |
6808d7a1 | 535 | resign: function() { |
77c50966 | 536 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
a6088c90 | 537 | return; |
6808d7a1 | 538 | this.send("resign", { data: this.game.mycolor }); |
8477e53d BA |
539 | const score = this.game.mycolor == "w" ? "0-1" : "1-0"; |
540 | const side = this.game.mycolor == "w" ? "White" : "Black"; | |
541 | this.gameOver(score, side + " surrender"); | |
a6088c90 | 542 | }, |
967a2686 BA |
543 | // 3 cases for loading a game: |
544 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
545 | // - from server (one correspondance game I play[ed] or not) |
546 | // - from remote peer (one live game I don't play, finished or not) | |
760adbce | 547 | loadGame: function(game, callback) { |
6808d7a1 | 548 | const afterRetrieval = async game => { |
f41ce580 BA |
549 | const vModule = await import("@/variants/" + game.vname + ".js"); |
550 | window.V = vModule.VariantRules; | |
551 | this.vr = new V(game.fen); | |
6808d7a1 | 552 | const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live"; |
71468011 | 553 | const tc = extractTime(game.cadence); |
9ef63965 BA |
554 | const myIdx = game.players.findIndex(p => { |
555 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
556 | }); | |
6808d7a1 BA |
557 | const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers |
558 | if (!game.chats) game.chats = []; //live games don't have chat history | |
559 | if (gtype == "corr") { | |
560 | if (game.players[0].color == "b") { | |
f41ce580 | 561 | // Adopt the same convention for live and corr games: [0] = white |
6808d7a1 BA |
562 | [game.players[0], game.players[1]] = [ |
563 | game.players[1], | |
564 | game.players[0] | |
565 | ]; | |
f41ce580 | 566 | } |
7f3484bd | 567 | // NOTE: clocks in seconds, initime in milliseconds |
6808d7a1 | 568 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
e71161fb | 569 | const L = game.moves.length; |
6808d7a1 | 570 | if (game.score == "*") { |
e71161fb | 571 | // Set clocks + initime |
92240cf0 | 572 | game.clocks = [tc.mainTime, tc.mainTime]; |
b7cbbda1 | 573 | game.initime = [0, 0]; |
92240cf0 BA |
574 | if (L >= 1) { |
575 | const gameLastupdate = game.moves[L-1].played; | |
576 | game.initime[L % 2] = gameLastupdate; | |
c3d16e78 BA |
577 | if (L >= 2) { |
578 | game.clocks[L % 2] = | |
579 | tc.mainTime - (Date.now() - gameLastupdate) / 1000; | |
580 | } | |
5f131484 | 581 | } |
92a523d1 | 582 | } |
9ef63965 | 583 | // Sort chat messages from newest to oldest |
6808d7a1 BA |
584 | game.chats.sort((c1, c2) => { |
585 | return c2.added - c1.added; | |
586 | }); | |
0d329b05 | 587 | if (myIdx >= 0 && game.score == "*" && game.chats.length > 0) { |
8477e53d | 588 | // Did a chat message arrive after my last move? |
9ef63965 | 589 | let dtLastMove = 0; |
e71161fb BA |
590 | if (L == 1 && myIdx == 0) |
591 | dtLastMove = game.moves[0].played; | |
592 | else if (L >= 2) { | |
593 | if (L % 2 == 0) { | |
594 | // It's now white turn | |
595 | dtLastMove = game.moves[L-1-(1-myIdx)].played; | |
596 | } else { | |
597 | // Black turn: | |
598 | dtLastMove = game.moves[L-1-myIdx].played; | |
9ef63965 BA |
599 | } |
600 | } | |
601 | if (dtLastMove < game.chats[0].added) | |
602 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
603 | } | |
604 | // Now that we used idx and played, re-format moves as for live games | |
8477e53d | 605 | game.moves = game.moves.map(m => m.squares); |
c0b27606 | 606 | } |
6808d7a1 | 607 | if (gtype == "live" && game.clocks[0] < 0) { |
8477e53d | 608 | // Game is unstarted |
66d03f23 | 609 | game.clocks = [tc.mainTime, tc.mainTime]; |
6808d7a1 | 610 | if (game.score == "*") { |
b7cbbda1 | 611 | game.initime[0] = Date.now(); |
6808d7a1 | 612 | if (myIdx >= 0) { |
b7cbbda1 | 613 | // I play in this live game; corr games don't have clocks+initime |
6808d7a1 | 614 | GameStorage.update(game.id, { |
b7cbbda1 | 615 | clocks: game.clocks, |
6808d7a1 | 616 | initime: game.initime |
b7cbbda1 BA |
617 | }); |
618 | } | |
22efa391 | 619 | } |
66d03f23 | 620 | } |
6808d7a1 BA |
621 | if (game.drawOffer) { |
622 | if (game.drawOffer == "t") | |
8477e53d | 623 | // Three repetitions |
77c50966 | 624 | this.drawOffer = "threerep"; |
6808d7a1 | 625 | else { |
8477e53d | 626 | // Draw offered by any of the players: |
6808d7a1 | 627 | if (myIdx < 0) this.drawOffer = "received"; |
6808d7a1 | 628 | else { |
77c50966 | 629 | // I play in this game: |
6808d7a1 BA |
630 | if ( |
631 | (game.drawOffer == "w" && myIdx == 0) || | |
632 | (game.drawOffer == "b" && myIdx == 1) | |
633 | ) | |
77c50966 | 634 | this.drawOffer = "sent"; |
6808d7a1 | 635 | else this.drawOffer = "received"; |
77c50966 BA |
636 | } |
637 | } | |
638 | } | |
725da57f BA |
639 | this.repeat = {}; //reset: scan past moves' FEN: |
640 | let repIdx = 0; | |
725da57f | 641 | let vr_tmp = new V(game.fenStart); |
725da57f BA |
642 | let curTurn = "n"; |
643 | game.moves.forEach(m => { | |
e71161fb BA |
644 | playMove(m, vr_tmp); |
645 | const fenIdx = vr_tmp.getFen().replace(/ /g, "_"); | |
646 | this.repeat[fenIdx] = this.repeat[fenIdx] | |
647 | ? this.repeat[fenIdx] + 1 | |
725da57f BA |
648 | : 1; |
649 | }); | |
725da57f | 650 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; |
6808d7a1 | 651 | this.game = Object.assign( |
cf742aaf | 652 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 653 | { |
c0b27606 | 654 | type: gtype, |
66d03f23 | 655 | increment: tc.increment, |
9ef63965 | 656 | mycolor: mycolor, |
5f131484 BA |
657 | // opponent sid not strictly required (or available), but easier |
658 | // at least oppsid or oppid is available anyway: | |
6808d7a1 | 659 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, |
725da57f | 660 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].uid, |
dcff8e82 | 661 | addTimes: [], //used for live games |
e71161fb BA |
662 | }, |
663 | game, | |
4b0384fa | 664 | ); |
dcff8e82 BA |
665 | if (this.gameIsLoading) |
666 | // Re-load game because we missed some moves: | |
667 | // artificially reset BaseGame (required if moves arrive too quickly) | |
668 | this.$refs["basegame"].re_setVariables(); | |
9ef63965 | 669 | this.re_setClocks(); |
a0c41e7e BA |
670 | this.$nextTick(() => { |
671 | this.game.rendered = true; | |
672 | // Did lastate arrive before game was rendered? | |
6808d7a1 | 673 | if (this.lastate) this.processLastate(); |
a0c41e7e | 674 | }); |
dcff8e82 BA |
675 | this.gameIsLoading = false; |
676 | if (!!callback) callback(); | |
967a2686 | 677 | }; |
f9c36b2d | 678 | if (!!game) { |
6808d7a1 BA |
679 | afterRetrieval(game); |
680 | return; | |
967a2686 | 681 | } |
6808d7a1 BA |
682 | if (this.gameRef.rid) { |
683 | // Remote live game: forgetting about callback func... (TODO: design) | |
684 | this.send("askfullgame", { target: this.gameRef.rid }); | |
685 | } else { | |
f41ce580 | 686 | // Local or corr game |
8477e53d | 687 | // NOTE: afterRetrieval() is never called if game not found |
11667c79 | 688 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 689 | } |
a6088c90 | 690 | }, |
9ef63965 | 691 | re_setClocks: function() { |
dcff8e82 | 692 | if (this.game.moves.length < 2 || this.game.score != "*") { |
9ef63965 BA |
693 | // 1st move not completed yet, or game over: freeze time |
694 | this.virtualClocks = this.game.clocks.map(s => ppt(s)); | |
695 | return; | |
696 | } | |
697 | const currentTurn = this.vr.turn; | |
8477e53d | 698 | const currentMovesCount = this.game.moves.length; |
6808d7a1 BA |
699 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
700 | let countdown = | |
701 | this.game.clocks[colorIdx] - | |
702 | (Date.now() - this.game.initime[colorIdx]) / 1000; | |
703 | this.virtualClocks = [0, 1].map(i => { | |
704 | const removeTime = | |
705 | i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0; | |
9ef63965 BA |
706 | return ppt(this.game.clocks[i] - removeTime); |
707 | }); | |
708 | let clockUpdate = setInterval(() => { | |
6808d7a1 BA |
709 | if ( |
710 | countdown < 0 || | |
8477e53d | 711 | this.game.moves.length > currentMovesCount || |
6808d7a1 BA |
712 | this.game.score != "*" |
713 | ) { | |
9ef63965 BA |
714 | clearInterval(clockUpdate); |
715 | if (countdown < 0) | |
6808d7a1 | 716 | this.gameOver( |
8477e53d | 717 | currentTurn == "w" ? "0-1" : "1-0", |
00c07ba3 | 718 | "Time" |
6808d7a1 BA |
719 | ); |
720 | } else | |
721 | this.$set( | |
722 | this.virtualClocks, | |
723 | colorIdx, | |
724 | ppt(Math.max(0, --countdown)) | |
725 | ); | |
9ef63965 BA |
726 | }, 1000); |
727 | }, | |
8477e53d | 728 | // Post-process a (potentially partial) move (which was just played in BaseGame) |
e71161fb | 729 | processMove: function(move, data) { |
e5c1d0fb | 730 | if (!data) data = {}; |
e71161fb BA |
731 | const moveCol = this.vr.turn; |
732 | const doProcessMove = () => { | |
733 | const colorIdx = ["w", "b"].indexOf(moveCol); | |
734 | const nextIdx = 1 - colorIdx; | |
dcff8e82 BA |
735 | const origMovescount = this.game.moves.length; |
736 | let addTime = | |
737 | this.game.type == "live" | |
738 | ? (data.addTime || 0) | |
739 | : undefined; | |
f9c36b2d | 740 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
e71161fb BA |
741 | if (this.drawOffer == "received") |
742 | // I refuse draw | |
743 | this.drawOffer = ""; | |
dcff8e82 | 744 | if (this.game.type == "live" && origMovescount >= 2) { |
e71161fb BA |
745 | const elapsed = Date.now() - this.game.initime[colorIdx]; |
746 | // elapsed time is measured in milliseconds | |
747 | addTime = this.game.increment - elapsed / 1000; | |
748 | } | |
dce792f6 | 749 | } |
dcff8e82 | 750 | // Update current game object: |
e71161fb | 751 | playMove(move, this.vr); |
f9c36b2d | 752 | // TODO: notifyTurn: "changeturn" message |
dcff8e82 | 753 | this.game.moves.push(move); |
e71161fb | 754 | // (add)Time indication: useful in case of lastate infos requested |
dcff8e82 BA |
755 | if (this.game.type == "live") |
756 | this.game.addTimes.push(addTime); | |
e71161fb | 757 | this.game.fen = this.vr.getFen(); |
92240cf0 BA |
758 | if (this.game.type == "live") this.game.clocks[colorIdx] += addTime; |
759 | // In corr games, just reset clock to mainTime: | |
760 | else this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime; | |
e71161fb | 761 | // data.initime is set only when I receive a "lastate" move from opponent |
e5c1d0fb | 762 | this.game.initime[nextIdx] = data.initime || Date.now(); |
e71161fb BA |
763 | this.re_setClocks(); |
764 | // If repetition detected, consider that a draw offer was received: | |
f9c36b2d BA |
765 | const fenObj = this.vr.getFenForRepeat(); |
766 | this.repeat[fenObj] = this.repeat[fenObj] ? this.repeat[fenObj] + 1 : 1; | |
767 | if (this.repeat[fenObj] >= 3) this.drawOffer = "threerep"; | |
e71161fb BA |
768 | else if (this.drawOffer == "threerep") this.drawOffer = ""; |
769 | // Since corr games are stored at only one location, update should be | |
770 | // done only by one player for each move: | |
dcff8e82 BA |
771 | if (!!this.game.mycolor && !data.receiveMyMove) { |
772 | // NOTE: 'var' to see that variable outside this block | |
773 | var filtered_move = getFilteredMove(move); | |
774 | } | |
e71161fb | 775 | if ( |
f9c36b2d BA |
776 | !!this.game.mycolor && |
777 | !data.receiveMyMove && | |
e71161fb BA |
778 | (this.game.type == "live" || moveCol == this.game.mycolor) |
779 | ) { | |
780 | let drawCode = ""; | |
781 | switch (this.drawOffer) { | |
782 | case "threerep": | |
783 | drawCode = "t"; | |
784 | break; | |
785 | case "sent": | |
786 | drawCode = this.game.mycolor; | |
787 | break; | |
788 | case "received": | |
789 | drawCode = V.GetOppCol(this.game.mycolor); | |
790 | break; | |
791 | } | |
792 | if (this.game.type == "corr") { | |
793 | GameStorage.update(this.gameRef.id, { | |
794 | fen: this.game.fen, | |
795 | move: { | |
796 | squares: filtered_move, | |
797 | played: Date.now(), | |
dcff8e82 | 798 | idx: origMovescount |
e71161fb BA |
799 | }, |
800 | // Code "n" for "None" to force reset (otherwise it's ignored) | |
801 | drawOffer: drawCode || "n" | |
802 | }); | |
803 | } | |
dcff8e82 BA |
804 | else if (!document.hidden) { |
805 | // Live game: consider only the active tab | |
e71161fb BA |
806 | GameStorage.update(this.gameRef.id, { |
807 | fen: this.game.fen, | |
808 | move: filtered_move, | |
809 | clocks: this.game.clocks, | |
810 | initime: this.game.initime, | |
811 | drawOffer: drawCode | |
812 | }); | |
813 | } | |
e69f159d | 814 | } |
dcff8e82 BA |
815 | // Send move ("newmove" event) to people in the room (if our turn) |
816 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { | |
817 | const sendMove = { | |
818 | move: filtered_move, | |
819 | index: origMovescount, | |
820 | // color is required to check if this is my move (if several tabs opened) | |
821 | color: moveCol, | |
822 | addTime: addTime, //undefined for corr games | |
823 | cancelDrawOffer: this.drawOffer == "" | |
824 | }; | |
825 | this.opponentGotMove = false; | |
826 | this.send("newmove", {data: sendMove}); | |
827 | // If the opponent doesn't reply gotmove soon enough, re-send move: | |
828 | let retrySendmove = setInterval( () => { | |
829 | if (this.opponentGotMove) { | |
830 | clearInterval(retrySendmove); | |
831 | return; | |
832 | } | |
833 | let oppsid = this.game.players[nextIdx].sid; | |
834 | if (!oppsid) { | |
835 | oppsid = Object.keys(this.people).find( | |
836 | sid => this.people[sid].id == this.game.players[nextIdx].uid | |
837 | ); | |
838 | } | |
839 | if (!oppsid || !this.people[oppsid]) | |
840 | // Opponent is disconnected: he'll ask last state | |
841 | clearInterval(retrySendmove); | |
842 | else this.send("newmove", {data: sendMove, target: oppsid}); | |
843 | }, 750); | |
844 | } | |
e71161fb | 845 | }; |
f9c36b2d BA |
846 | if ( |
847 | this.game.type == "corr" && | |
848 | moveCol == this.game.mycolor && | |
849 | !data.receiveMyMove | |
850 | ) { | |
e71161fb | 851 | setTimeout(() => { |
f9c36b2d BA |
852 | // TODO: remplacer cette confirm box par qqch de plus discret |
853 | // (et de même pour challenge accepté / refusé) | |
e71161fb BA |
854 | if ( |
855 | !confirm( | |
856 | this.st.tr["Move played:"] + | |
857 | " " + | |
858 | getFullNotation(move) + | |
859 | "\n" + | |
860 | this.st.tr["Are you sure?"] | |
861 | ) | |
862 | ) { | |
863 | this.$refs["basegame"].cancelLastMove(); | |
864 | return; | |
865 | } | |
866 | doProcessMove(); | |
867 | // Let small time to finish drawing current move attempt: | |
868 | }, 500); | |
6d68309a | 869 | } |
e71161fb | 870 | else doProcessMove(); |
b4fb1612 | 871 | }, |
430a2038 | 872 | gameOver: function(score, scoreMsg) { |
430a2038 | 873 | this.game.score = score; |
8477e53d | 874 | this.$set(this.game, "scoreMsg", scoreMsg || getScoreMessage(score)); |
ab6f48ea BA |
875 | const myIdx = this.game.players.findIndex(p => { |
876 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
877 | }); | |
6808d7a1 | 878 | if (myIdx >= 0) { |
8477e53d | 879 | // OK, I play in this game |
6808d7a1 BA |
880 | GameStorage.update(this.gameRef.id, { |
881 | score: score, | |
882 | scoreMsg: scoreMsg | |
883 | }); | |
48ab808f | 884 | // Notify the score to main Hall. TODO: only one player (currently double send) |
6808d7a1 | 885 | this.send("result", { gid: this.game.id, score: score }); |
dcd68c41 | 886 | } |
6808d7a1 BA |
887 | } |
888 | } | |
a6088c90 BA |
889 | }; |
890 | </script> | |
7e1a1fe9 | 891 | |
41c80bb6 | 892 | <style lang="sass" scoped> |
72ccbd67 | 893 | .connected |
050ae3b5 | 894 | background-color: lightgreen |
72ccbd67 | 895 | |
ed06d9e9 BA |
896 | #participants |
897 | margin-left: 5px | |
898 | ||
899 | .anonymous | |
900 | color: grey | |
901 | font-style: italic | |
902 | ||
ec905cbc BA |
903 | #playersInfo > p |
904 | margin: 0 | |
905 | ||
430a2038 BA |
906 | @media screen and (min-width: 768px) |
907 | #actions | |
908 | width: 300px | |
909 | @media screen and (max-width: 767px) | |
910 | .game | |
911 | width: 100% | |
72ccbd67 | 912 | |
430a2038 | 913 | #actions |
cf94b843 | 914 | display: inline-block |
1d6d7b1d | 915 | margin: 0 |
430a2038 BA |
916 | button |
917 | display: inline-block | |
430a2038 | 918 | margin: 0 |
a1c48034 | 919 | |
050ae3b5 BA |
920 | @media screen and (max-width: 767px) |
921 | #aboveBoard | |
922 | text-align: center | |
885d93a7 BA |
923 | @media screen and (min-width: 768px) |
924 | #aboveBoard | |
925 | margin-left: 30% | |
050ae3b5 | 926 | |
2f258c37 BA |
927 | .variant-cadence |
928 | padding-right: 10px | |
929 | ||
930 | .variant-name | |
8c5f5390 | 931 | font-weight: bold |
77c50966 | 932 | padding-right: 10px |
77c50966 | 933 | |
050ae3b5 BA |
934 | .name |
935 | font-size: 1.5rem | |
936 | padding: 1px | |
937 | ||
938 | .time | |
939 | font-size: 2rem | |
940 | display: inline-block | |
941 | margin-left: 10px | |
942 | ||
943 | .split-names | |
944 | display: inline-block | |
945 | margin: 0 15px | |
946 | ||
430a2038 | 947 | #chat |
a1c48034 | 948 | padding-top: 20px |
a154d45e | 949 | max-width: 767px |
430a2038 | 950 | border: none; |
cf94b843 BA |
951 | |
952 | #chatBtn | |
953 | margin: 0 10px 0 0 | |
dcd68c41 BA |
954 | |
955 | .draw-sent, .draw-sent:hover | |
956 | background-color: lightyellow | |
957 | ||
958 | .draw-received, .draw-received:hover | |
959 | background-color: lightgreen | |
960 | ||
961 | .draw-threerep, .draw-threerep:hover | |
962 | background-color: #e4d1fc | |
2f258c37 BA |
963 | |
964 | .somethingnew | |
965 | background-color: #c5fefe | |
7e1a1fe9 | 966 | </style> |