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: | |
114 | fullGamerequested: false, | |
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}); |
1611a25f | 229 | 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]; | |
238 | const yourTurn = | |
239 | ( | |
240 | color == "w" && | |
241 | this.game.movesCount % 2 == 0 | |
242 | ) | |
243 | || | |
244 | ( | |
245 | color == "b" && | |
246 | this.game.movesCount % 2 == 1 | |
247 | ); | |
248 | this.send("turnchange", { target: sid, yourTurn: yourTurn }); | |
249 | }, | |
cdb34c93 | 250 | socketMessageListener: function(msg) { |
6808d7a1 | 251 | if (!this.conn) return; |
a6088c90 | 252 | const data = JSON.parse(msg.data); |
6808d7a1 | 253 | switch (data.code) { |
5f131484 | 254 | case "pollclients": |
5f131484 | 255 | data.sockIds.forEach(sid => { |
6808d7a1 BA |
256 | if (sid != this.st.user.sid) { |
257 | this.send("askidentity", { target: sid }); | |
51d87b52 | 258 | // Ask potentially missed last state, if opponent and I play |
6808d7a1 | 259 | if ( |
f9c36b2d | 260 | !!this.game.mycolor && |
6808d7a1 BA |
261 | this.game.type == "live" && |
262 | this.game.score == "*" && | |
263 | this.game.players.some(p => p.sid == sid) | |
264 | ) { | |
265 | this.send("asklastate", { target: sid }); | |
51d87b52 BA |
266 | } |
267 | } | |
5f131484 BA |
268 | }); |
269 | break; | |
71468011 | 270 | case "connect": |
efdfb4c7 | 271 | if (!this.people[data.from]) { |
51d87b52 | 272 | this.newConnect[data.from] = true; //for self multi-connects tests |
6808d7a1 | 273 | this.send("askidentity", { target: data.from }); |
51d87b52 | 274 | } |
71468011 BA |
275 | break; |
276 | case "disconnect": | |
277 | this.$delete(this.people, data.from); | |
278 | break; | |
efdfb4c7 | 279 | case "mconnect": { |
1611a25f BA |
280 | // TODO: from MyGames page : send mconnect message with the list of gid (live and corr) |
281 | // Either me (another tab) or opponent | |
282 | const sid = data.from; | |
283 | if (!this.onMygames.some(s => s == sid)) | |
284 | { | |
285 | this.onMygames.push(sid); | |
286 | this.notifyTurn(sid); //TODO: this may require server ID (so, notify after receiving identity) | |
287 | } | |
288 | break; | |
289 | if (!this.people[sid]) | |
290 | this.send("askidentity", { target: sid }); | |
291 | } | |
292 | case "mdisconnect": | |
293 | ArrayFun.remove(this.onMygames, sid => sid == data.from); | |
294 | break; | |
51d87b52 BA |
295 | case "killed": |
296 | // I logged in elsewhere: | |
51d87b52 | 297 | this.conn = null; |
09d37571 | 298 | alert(this.st.tr["New connexion detected: tab now offline"]); |
51d87b52 | 299 | break; |
6808d7a1 | 300 | case "askidentity": { |
efdfb4c7 | 301 | // Request for identification |
51d87b52 BA |
302 | const me = { |
303 | // Decompose to avoid revealing email | |
304 | name: this.st.user.name, | |
305 | sid: this.st.user.sid, | |
6808d7a1 | 306 | id: this.st.user.id |
51d87b52 | 307 | }; |
6808d7a1 | 308 | this.send("identity", { data: me, target: data.from }); |
5f131484 | 309 | break; |
51d87b52 | 310 | } |
6808d7a1 | 311 | case "identity": { |
71468011 | 312 | const user = data.data; |
efdfb4c7 | 313 | this.$set(this.people, user.sid, { name: user.name, id: user.id }); |
f9c36b2d BA |
314 | // If I multi-connect, kill current connexion if no mark (I'm older) |
315 | if (this.newConnect[user.sid]) { | |
6808d7a1 | 316 | if ( |
6808d7a1 BA |
317 | user.id > 0 && |
318 | user.id == this.st.user.id && | |
f9c36b2d BA |
319 | user.sid != this.st.user.sid && |
320 | !this.killed[this.st.user.sid] | |
6808d7a1 | 321 | ) { |
6808d7a1 | 322 | this.send("killme", { sid: this.st.user.sid }); |
51d87b52 | 323 | this.killed[this.st.user.sid] = true; |
51d87b52 | 324 | } |
f9c36b2d | 325 | delete this.newConnect[user.sid]; |
a0c41e7e BA |
326 | } |
327 | break; | |
71468011 BA |
328 | } |
329 | case "askgame": | |
330 | // Send current (live) game if not asked by any of the players | |
6808d7a1 BA |
331 | if ( |
332 | this.game.type == "live" && | |
333 | this.game.players.every(p => p.sid != data.from[0]) | |
334 | ) { | |
71468011 BA |
335 | const myGame = { |
336 | id: this.game.id, | |
337 | fen: this.game.fen, | |
338 | players: this.game.players, | |
339 | vid: this.game.vid, | |
340 | cadence: this.game.cadence, | |
341 | score: this.game.score, | |
6808d7a1 | 342 | rid: this.st.user.sid //useful in Hall if I'm an observer |
71468011 | 343 | }; |
6808d7a1 | 344 | this.send("game", { data: myGame, target: data.from }); |
71468011 BA |
345 | } |
346 | break; | |
347 | case "askfullgame": | |
6808d7a1 | 348 | this.send("fullgame", { data: this.game, target: data.from }); |
71468011 BA |
349 | break; |
350 | case "fullgame": | |
351 | // Callback "roomInit" to poll clients only after game is loaded | |
d1be8046 BA |
352 | let game = data.data; |
353 | // Move format isn't the same in storage and in browser, | |
354 | // because of the 'addTime' field. | |
355 | game.moves = game.moves.map(m => { return m.move || m; }); | |
356 | this.loadGame(game, this.roomInit); | |
71468011 | 357 | break; |
a0c41e7e | 358 | case "asklastate": |
a0c41e7e | 359 | // Sending last state if I played a move or score != "*" |
6808d7a1 BA |
360 | if ( |
361 | (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || | |
362 | this.game.score != "*" || | |
363 | this.drawOffer == "sent" | |
364 | ) { | |
f41ce580 | 365 | // Send our "last state" informations to opponent |
411d23cd | 366 | const L = this.game.moves.length; |
6808d7a1 | 367 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); |
71468011 BA |
368 | const myLastate = { |
369 | // NOTE: lastMove (when defined) includes addTime | |
6808d7a1 | 370 | lastMove: L > 0 ? this.game.moves[L - 1] : undefined, |
71468011 BA |
371 | // Since we played a move (or abort or resign), |
372 | // only drawOffer=="sent" is possible | |
373 | drawSent: this.drawOffer == "sent", | |
26f3a887 | 374 | score: this.game.score, |
71468011 | 375 | movesCount: L, |
6808d7a1 | 376 | initime: this.game.initime[1 - myIdx] //relevant only if I played |
5fd5fb22 | 377 | }; |
6808d7a1 | 378 | this.send("lastate", { data: myLastate, target: data.from }); |
5fd5fb22 | 379 | } |
c6788ecf | 380 | break; |
71468011 BA |
381 | case "lastate": //got opponent infos about last move |
382 | this.lastate = data.data; | |
6808d7a1 | 383 | if (this.game.rendered) |
e71161fb | 384 | // Game is rendered (Board component) |
71468011 | 385 | this.processLastate(); |
e71161fb | 386 | // Else: will be processed when game is ready |
71468011 | 387 | break; |
6808d7a1 | 388 | case "newmove": { |
e5c1d0fb BA |
389 | |
390 | console.log(data.data); | |
391 | ||
71468011 | 392 | const move = data.data; |
f9c36b2d BA |
393 | if (move.index > this.game.movesCount && !this.fullGameRequested) { |
394 | // This can only happen if I'm an observer and missed a move: | |
395 | // just ask fullgame again, this is much simpler. | |
396 | (function askIfPeerConnected() { | |
397 | if (!!this.people[this.gameRef.rid]) | |
398 | this.send("askfullgame", { target: this.gameRef.rid }); | |
399 | else setTimeout(askIfPeerConnected, 1000); | |
400 | })(); | |
401 | this.fullGameRequested = true; | |
402 | } else { | |
403 | if ( | |
404 | move.index < this.game.movesCount || | |
405 | this.gotMoveIdx >= move.index | |
406 | ) { | |
407 | // Opponent re-send but we already have the move: | |
408 | // (maybe he didn't receive our pingback...) | |
e5c1d0fb | 409 | this.send("gotmove", {data: move.index, target: data.from}); |
f9c36b2d | 410 | } else { |
e5c1d0fb | 411 | this.gotMoveIdx = move.index; |
f9c36b2d BA |
412 | const receiveMyMove = ( |
413 | !!this.game.mycolor && | |
414 | move.index == this.game.movesCount | |
415 | ); | |
416 | if (!receiveMyMove && !!this.game.mycolor) | |
417 | // Notify opponent that I got the move: | |
e5c1d0fb | 418 | this.send("gotmove", {data: move.index, target: data.from}); |
f9c36b2d BA |
419 | if (move.cancelDrawOffer) { |
420 | // Opponent refuses draw | |
421 | this.drawOffer = ""; | |
422 | // NOTE for corr games: drawOffer reset by player in turn | |
423 | if ( | |
424 | this.game.type == "live" && | |
425 | !!this.game.mycolor && | |
426 | !receiveMyMove | |
427 | ) { | |
428 | GameStorage.update(this.gameRef.id, { drawOffer: "" }); | |
429 | } | |
430 | } | |
431 | this.$refs["basegame"].play( | |
432 | move.move, | |
433 | "received", | |
434 | null, | |
435 | { | |
436 | addTime: move.addTime, | |
437 | receiveMyMove: receiveMyMove | |
438 | } | |
439 | ); | |
440 | } | |
633959bf | 441 | } |
a6088c90 | 442 | break; |
71468011 | 443 | } |
f9c36b2d BA |
444 | case "gotmove": { |
445 | this.opponentGotMove = true; | |
446 | break; | |
447 | } | |
448 | /// TODO: same strategy for askLastate | |
449 | // --> the message could not have been received, | |
450 | // or maybe we ddn't receive it back. | |
451 | ||
452 | ||
93d1d7a7 | 453 | case "resign": |
8477e53d BA |
454 | const score = data.side == "b" ? "1-0" : "0-1"; |
455 | const side = data.side == "w" ? "White" : "Black"; | |
456 | this.gameOver(score, side + " surrender"); | |
93d1d7a7 | 457 | break; |
93d1d7a7 | 458 | case "abort": |
8477e53d | 459 | this.gameOver("?", "Stop"); |
93d1d7a7 | 460 | break; |
2cc10cdb | 461 | case "draw": |
71468011 | 462 | this.gameOver("1/2", data.data); |
2cc10cdb BA |
463 | break; |
464 | case "drawoffer": | |
41c80bb6 BA |
465 | // NOTE: observers don't know who offered draw |
466 | this.drawOffer = "received"; | |
6d9f4315 | 467 | break; |
71468011 | 468 | case "newchat": |
bd76b456 | 469 | this.newChat = data.data; |
71468011 | 470 | if (!document.getElementById("modalChat").checked) |
2f258c37 | 471 | document.getElementById("chatBtn").classList.add("somethingnew"); |
a6088c90 BA |
472 | break; |
473 | } | |
cdb34c93 | 474 | }, |
51d87b52 BA |
475 | socketCloseListener: function() { |
476 | this.conn = new WebSocket(this.connexionString); | |
6808d7a1 BA |
477 | this.conn.addEventListener("message", this.socketMessageListener); |
478 | this.conn.addEventListener("close", this.socketCloseListener); | |
51d87b52 | 479 | }, |
760adbce BA |
480 | // lastate was received, but maybe game wasn't ready yet: |
481 | processLastate: function() { | |
482 | const data = this.lastate; | |
483 | this.lastate = undefined; //security... | |
484 | const L = this.game.moves.length; | |
6808d7a1 | 485 | if (data.movesCount > L) { |
760adbce | 486 | // Just got last move from him |
8477e53d | 487 | this.$refs["basegame"].play( |
e71161fb BA |
488 | data.lastMove.move, |
489 | "received", | |
490 | null, | |
92240cf0 | 491 | {addTime: data.lastMove.addTime, initime: data.initime}); |
a0c41e7e | 492 | } |
6808d7a1 BA |
493 | if (data.drawSent) this.drawOffer = "received"; |
494 | if (data.score != "*") { | |
a0c41e7e | 495 | this.drawOffer = ""; |
6808d7a1 | 496 | if (this.game.score == "*") this.gameOver(data.score); |
760adbce BA |
497 | } |
498 | }, | |
dcd68c41 | 499 | clickDraw: function() { |
6808d7a1 BA |
500 | if (!this.game.mycolor) return; //I'm just spectator |
501 | if (["received", "threerep"].includes(this.drawOffer)) { | |
502 | if (!confirm(this.st.tr["Accept draw?"])) return; | |
503 | const message = | |
504 | this.drawOffer == "received" | |
505 | ? "Mutual agreement" | |
506 | : "Three repetitions"; | |
507 | this.send("draw", { data: message }); | |
77c50966 | 508 | this.gameOver("1/2", message); |
6808d7a1 | 509 | } else if (this.drawOffer == "") { |
e71161fb | 510 | // No effect if drawOffer == "sent" |
f9c36b2d | 511 | if (!!this.game.mycolor != this.vr.turn) { |
6808d7a1 | 512 | alert(this.st.tr["Draw offer only in your turn"]); |
6fba6e0c | 513 | return; |
6808d7a1 BA |
514 | } |
515 | if (!confirm(this.st.tr["Offer draw?"])) return; | |
760adbce | 516 | this.drawOffer = "sent"; |
71468011 | 517 | this.send("drawoffer"); |
6808d7a1 | 518 | GameStorage.update(this.gameRef.id, { drawOffer: this.game.mycolor }); |
a6088c90 BA |
519 | } |
520 | }, | |
7f3484bd | 521 | abortGame: function() { |
6808d7a1 | 522 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) return; |
8477e53d | 523 | this.gameOver("?", "Stop"); |
71468011 | 524 | this.send("abort"); |
a6088c90 | 525 | }, |
6808d7a1 | 526 | resign: function() { |
77c50966 | 527 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
a6088c90 | 528 | return; |
6808d7a1 | 529 | this.send("resign", { data: this.game.mycolor }); |
8477e53d BA |
530 | const score = this.game.mycolor == "w" ? "0-1" : "1-0"; |
531 | const side = this.game.mycolor == "w" ? "White" : "Black"; | |
532 | this.gameOver(score, side + " surrender"); | |
a6088c90 | 533 | }, |
967a2686 BA |
534 | // 3 cases for loading a game: |
535 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
536 | // - from server (one correspondance game I play[ed] or not) |
537 | // - from remote peer (one live game I don't play, finished or not) | |
760adbce | 538 | loadGame: function(game, callback) { |
6808d7a1 | 539 | const afterRetrieval = async game => { |
f41ce580 BA |
540 | const vModule = await import("@/variants/" + game.vname + ".js"); |
541 | window.V = vModule.VariantRules; | |
542 | this.vr = new V(game.fen); | |
6808d7a1 | 543 | const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live"; |
71468011 | 544 | const tc = extractTime(game.cadence); |
9ef63965 BA |
545 | const myIdx = game.players.findIndex(p => { |
546 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
547 | }); | |
6808d7a1 BA |
548 | const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers |
549 | if (!game.chats) game.chats = []; //live games don't have chat history | |
550 | if (gtype == "corr") { | |
551 | if (game.players[0].color == "b") { | |
f41ce580 | 552 | // Adopt the same convention for live and corr games: [0] = white |
6808d7a1 BA |
553 | [game.players[0], game.players[1]] = [ |
554 | game.players[1], | |
555 | game.players[0] | |
556 | ]; | |
f41ce580 | 557 | } |
7f3484bd | 558 | // NOTE: clocks in seconds, initime in milliseconds |
6808d7a1 | 559 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
e71161fb | 560 | const L = game.moves.length; |
6808d7a1 | 561 | if (game.score == "*") { |
e71161fb | 562 | // Set clocks + initime |
92240cf0 | 563 | game.clocks = [tc.mainTime, tc.mainTime]; |
b7cbbda1 | 564 | game.initime = [0, 0]; |
92240cf0 BA |
565 | if (L >= 1) { |
566 | const gameLastupdate = game.moves[L-1].played; | |
567 | game.initime[L % 2] = gameLastupdate; | |
c3d16e78 BA |
568 | if (L >= 2) { |
569 | game.clocks[L % 2] = | |
570 | tc.mainTime - (Date.now() - gameLastupdate) / 1000; | |
571 | } | |
5f131484 | 572 | } |
92a523d1 | 573 | } |
9ef63965 | 574 | // Sort chat messages from newest to oldest |
6808d7a1 BA |
575 | game.chats.sort((c1, c2) => { |
576 | return c2.added - c1.added; | |
577 | }); | |
0d329b05 | 578 | if (myIdx >= 0 && game.score == "*" && game.chats.length > 0) { |
8477e53d | 579 | // Did a chat message arrive after my last move? |
9ef63965 | 580 | let dtLastMove = 0; |
e71161fb BA |
581 | if (L == 1 && myIdx == 0) |
582 | dtLastMove = game.moves[0].played; | |
583 | else if (L >= 2) { | |
584 | if (L % 2 == 0) { | |
585 | // It's now white turn | |
586 | dtLastMove = game.moves[L-1-(1-myIdx)].played; | |
587 | } else { | |
588 | // Black turn: | |
589 | dtLastMove = game.moves[L-1-myIdx].played; | |
9ef63965 BA |
590 | } |
591 | } | |
592 | if (dtLastMove < game.chats[0].added) | |
593 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
594 | } | |
595 | // Now that we used idx and played, re-format moves as for live games | |
8477e53d | 596 | game.moves = game.moves.map(m => m.squares); |
c0b27606 | 597 | } |
6808d7a1 | 598 | if (gtype == "live" && game.clocks[0] < 0) { |
8477e53d | 599 | // Game is unstarted |
66d03f23 | 600 | game.clocks = [tc.mainTime, tc.mainTime]; |
6808d7a1 | 601 | if (game.score == "*") { |
b7cbbda1 | 602 | game.initime[0] = Date.now(); |
6808d7a1 | 603 | if (myIdx >= 0) { |
b7cbbda1 | 604 | // I play in this live game; corr games don't have clocks+initime |
6808d7a1 | 605 | GameStorage.update(game.id, { |
b7cbbda1 | 606 | clocks: game.clocks, |
6808d7a1 | 607 | initime: game.initime |
b7cbbda1 BA |
608 | }); |
609 | } | |
22efa391 | 610 | } |
66d03f23 | 611 | } |
6808d7a1 BA |
612 | if (game.drawOffer) { |
613 | if (game.drawOffer == "t") | |
8477e53d | 614 | // Three repetitions |
77c50966 | 615 | this.drawOffer = "threerep"; |
6808d7a1 | 616 | else { |
8477e53d | 617 | // Draw offered by any of the players: |
6808d7a1 | 618 | if (myIdx < 0) this.drawOffer = "received"; |
6808d7a1 | 619 | else { |
77c50966 | 620 | // I play in this game: |
6808d7a1 BA |
621 | if ( |
622 | (game.drawOffer == "w" && myIdx == 0) || | |
623 | (game.drawOffer == "b" && myIdx == 1) | |
624 | ) | |
77c50966 | 625 | this.drawOffer = "sent"; |
6808d7a1 | 626 | else this.drawOffer = "received"; |
77c50966 BA |
627 | } |
628 | } | |
629 | } | |
725da57f BA |
630 | this.repeat = {}; //reset: scan past moves' FEN: |
631 | let repIdx = 0; | |
725da57f | 632 | let vr_tmp = new V(game.fenStart); |
725da57f BA |
633 | let curTurn = "n"; |
634 | game.moves.forEach(m => { | |
e71161fb BA |
635 | playMove(m, vr_tmp); |
636 | const fenIdx = vr_tmp.getFen().replace(/ /g, "_"); | |
637 | this.repeat[fenIdx] = this.repeat[fenIdx] | |
638 | ? this.repeat[fenIdx] + 1 | |
725da57f BA |
639 | : 1; |
640 | }); | |
725da57f | 641 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; |
6808d7a1 | 642 | this.game = Object.assign( |
cf742aaf | 643 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 644 | { |
c0b27606 | 645 | type: gtype, |
66d03f23 | 646 | increment: tc.increment, |
9ef63965 | 647 | mycolor: mycolor, |
5f131484 BA |
648 | // opponent sid not strictly required (or available), but easier |
649 | // at least oppsid or oppid is available anyway: | |
6808d7a1 | 650 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, |
725da57f | 651 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].uid, |
e71161fb BA |
652 | movesCount: game.moves.length |
653 | }, | |
654 | game, | |
4b0384fa | 655 | ); |
f9c36b2d BA |
656 | if (this.fullGameRequested) |
657 | // Second (or more) time the full game is asked: | |
658 | this.fullGameRequested = false; | |
9ef63965 | 659 | this.re_setClocks(); |
a0c41e7e BA |
660 | this.$nextTick(() => { |
661 | this.game.rendered = true; | |
662 | // Did lastate arrive before game was rendered? | |
6808d7a1 | 663 | if (this.lastate) this.processLastate(); |
a0c41e7e | 664 | }); |
6808d7a1 | 665 | if (callback) callback(); |
967a2686 | 666 | }; |
f9c36b2d | 667 | if (!!game) { |
6808d7a1 BA |
668 | afterRetrieval(game); |
669 | return; | |
967a2686 | 670 | } |
6808d7a1 BA |
671 | if (this.gameRef.rid) { |
672 | // Remote live game: forgetting about callback func... (TODO: design) | |
673 | this.send("askfullgame", { target: this.gameRef.rid }); | |
674 | } else { | |
f41ce580 | 675 | // Local or corr game |
8477e53d | 676 | // NOTE: afterRetrieval() is never called if game not found |
11667c79 | 677 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 678 | } |
a6088c90 | 679 | }, |
9ef63965 | 680 | re_setClocks: function() { |
725da57f | 681 | if (this.game.movesCount < 2 || this.game.score != "*") { |
9ef63965 BA |
682 | // 1st move not completed yet, or game over: freeze time |
683 | this.virtualClocks = this.game.clocks.map(s => ppt(s)); | |
684 | return; | |
685 | } | |
686 | const currentTurn = this.vr.turn; | |
8477e53d | 687 | const currentMovesCount = this.game.moves.length; |
6808d7a1 BA |
688 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
689 | let countdown = | |
690 | this.game.clocks[colorIdx] - | |
691 | (Date.now() - this.game.initime[colorIdx]) / 1000; | |
692 | this.virtualClocks = [0, 1].map(i => { | |
693 | const removeTime = | |
694 | i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0; | |
9ef63965 BA |
695 | return ppt(this.game.clocks[i] - removeTime); |
696 | }); | |
697 | let clockUpdate = setInterval(() => { | |
6808d7a1 BA |
698 | if ( |
699 | countdown < 0 || | |
8477e53d | 700 | this.game.moves.length > currentMovesCount || |
6808d7a1 BA |
701 | this.game.score != "*" |
702 | ) { | |
9ef63965 BA |
703 | clearInterval(clockUpdate); |
704 | if (countdown < 0) | |
6808d7a1 | 705 | this.gameOver( |
8477e53d | 706 | currentTurn == "w" ? "0-1" : "1-0", |
00c07ba3 | 707 | "Time" |
6808d7a1 BA |
708 | ); |
709 | } else | |
710 | this.$set( | |
711 | this.virtualClocks, | |
712 | colorIdx, | |
713 | ppt(Math.max(0, --countdown)) | |
714 | ); | |
9ef63965 BA |
715 | }, 1000); |
716 | }, | |
8477e53d | 717 | // Post-process a (potentially partial) move (which was just played in BaseGame) |
e71161fb | 718 | processMove: function(move, data) { |
e5c1d0fb | 719 | if (!data) data = {}; |
e71161fb BA |
720 | const moveCol = this.vr.turn; |
721 | const doProcessMove = () => { | |
722 | const colorIdx = ["w", "b"].indexOf(moveCol); | |
723 | const nextIdx = 1 - colorIdx; | |
f9c36b2d | 724 | if (!!this.game.mycolor && !data.receiveMyMove) { |
e71161fb BA |
725 | // NOTE: 'var' to see that variable outside this block |
726 | var filtered_move = getFilteredMove(move); | |
9ef63965 | 727 | } |
e71161fb | 728 | // Send move ("newmove" event) to people in the room (if our turn) |
e5c1d0fb | 729 | let addTime = (this.game.type == "live") ? data.addTime : 0; |
f9c36b2d | 730 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
e71161fb BA |
731 | if (this.drawOffer == "received") |
732 | // I refuse draw | |
733 | this.drawOffer = ""; | |
92240cf0 BA |
734 | // 'addTime' is irrelevant for corr games: |
735 | if (this.game.type == "live" && this.game.movesCount >= 2) { | |
e71161fb BA |
736 | const elapsed = Date.now() - this.game.initime[colorIdx]; |
737 | // elapsed time is measured in milliseconds | |
738 | addTime = this.game.increment - elapsed / 1000; | |
739 | } | |
740 | const sendMove = { | |
741 | move: filtered_move, | |
f9c36b2d | 742 | index: this.game.movesCount, |
92240cf0 | 743 | addTime: addTime, //undefined for corr games |
f9c36b2d | 744 | cancelDrawOffer: this.drawOffer == "" |
e71161fb | 745 | }; |
f9c36b2d | 746 | this.opponentGotMove = false; |
e5c1d0fb BA |
747 | this.send("newmove", {data: sendMove}); |
748 | // If the opponent doesn't reply gotmove soon enough, re-send move: | |
749 | let retrySendmove = setInterval( () => { | |
750 | if (this.opponentGotMove) { | |
751 | clearInterval(retrySendmove); | |
752 | return; | |
753 | } | |
754 | let oppsid = this.game.players[nextIdx].sid; | |
755 | if (!oppsid) { | |
756 | oppsid = Object.keys(this.people).find( | |
757 | sid => this.people[sid].id == this.game.players[nextIdx].uid | |
758 | ); | |
759 | } | |
760 | if (!oppsid || !this.people[oppsid]) | |
761 | // Opponent is disconnected: he'll ask last state | |
762 | clearInterval(retrySendmove); | |
763 | else this.send("newmove", {data: sendMove, target: oppsid}); | |
764 | }, 750); | |
dce792f6 | 765 | } |
e71161fb BA |
766 | // Update current game object (no need for moves stack): |
767 | playMove(move, this.vr); | |
725da57f | 768 | this.game.movesCount++; |
f9c36b2d | 769 | // TODO: notifyTurn: "changeturn" message |
e71161fb | 770 | // (add)Time indication: useful in case of lastate infos requested |
92240cf0 BA |
771 | this.game.moves.push(this.game.type == "live" |
772 | ? {move:move, addTime:addTime} | |
773 | : move); | |
e71161fb | 774 | this.game.fen = this.vr.getFen(); |
92240cf0 BA |
775 | if (this.game.type == "live") this.game.clocks[colorIdx] += addTime; |
776 | // In corr games, just reset clock to mainTime: | |
777 | else this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime; | |
e71161fb | 778 | // data.initime is set only when I receive a "lastate" move from opponent |
e5c1d0fb | 779 | this.game.initime[nextIdx] = data.initime || Date.now(); |
e71161fb BA |
780 | this.re_setClocks(); |
781 | // If repetition detected, consider that a draw offer was received: | |
f9c36b2d BA |
782 | const fenObj = this.vr.getFenForRepeat(); |
783 | this.repeat[fenObj] = this.repeat[fenObj] ? this.repeat[fenObj] + 1 : 1; | |
784 | if (this.repeat[fenObj] >= 3) this.drawOffer = "threerep"; | |
e71161fb BA |
785 | else if (this.drawOffer == "threerep") this.drawOffer = ""; |
786 | // Since corr games are stored at only one location, update should be | |
787 | // done only by one player for each move: | |
788 | if ( | |
f9c36b2d BA |
789 | !!this.game.mycolor && |
790 | !data.receiveMyMove && | |
e71161fb BA |
791 | (this.game.type == "live" || moveCol == this.game.mycolor) |
792 | ) { | |
793 | let drawCode = ""; | |
794 | switch (this.drawOffer) { | |
795 | case "threerep": | |
796 | drawCode = "t"; | |
797 | break; | |
798 | case "sent": | |
799 | drawCode = this.game.mycolor; | |
800 | break; | |
801 | case "received": | |
802 | drawCode = V.GetOppCol(this.game.mycolor); | |
803 | break; | |
804 | } | |
805 | if (this.game.type == "corr") { | |
806 | GameStorage.update(this.gameRef.id, { | |
807 | fen: this.game.fen, | |
808 | move: { | |
809 | squares: filtered_move, | |
810 | played: Date.now(), | |
811 | idx: this.game.moves.length - 1 | |
812 | }, | |
813 | // Code "n" for "None" to force reset (otherwise it's ignored) | |
814 | drawOffer: drawCode || "n" | |
815 | }); | |
816 | } | |
817 | else { | |
818 | // Live game: | |
819 | GameStorage.update(this.gameRef.id, { | |
820 | fen: this.game.fen, | |
821 | move: filtered_move, | |
822 | clocks: this.game.clocks, | |
823 | initime: this.game.initime, | |
824 | drawOffer: drawCode | |
825 | }); | |
826 | } | |
e69f159d | 827 | } |
e71161fb | 828 | }; |
f9c36b2d BA |
829 | if ( |
830 | this.game.type == "corr" && | |
831 | moveCol == this.game.mycolor && | |
832 | !data.receiveMyMove | |
833 | ) { | |
e71161fb | 834 | setTimeout(() => { |
f9c36b2d BA |
835 | // TODO: remplacer cette confirm box par qqch de plus discret |
836 | // (et de même pour challenge accepté / refusé) | |
e71161fb BA |
837 | if ( |
838 | !confirm( | |
839 | this.st.tr["Move played:"] + | |
840 | " " + | |
841 | getFullNotation(move) + | |
842 | "\n" + | |
843 | this.st.tr["Are you sure?"] | |
844 | ) | |
845 | ) { | |
846 | this.$refs["basegame"].cancelLastMove(); | |
847 | return; | |
848 | } | |
849 | doProcessMove(); | |
850 | // Let small time to finish drawing current move attempt: | |
851 | }, 500); | |
6d68309a | 852 | } |
e71161fb | 853 | else doProcessMove(); |
b4fb1612 | 854 | }, |
430a2038 | 855 | gameOver: function(score, scoreMsg) { |
430a2038 | 856 | this.game.score = score; |
8477e53d | 857 | this.$set(this.game, "scoreMsg", scoreMsg || getScoreMessage(score)); |
ab6f48ea BA |
858 | const myIdx = this.game.players.findIndex(p => { |
859 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
860 | }); | |
6808d7a1 | 861 | if (myIdx >= 0) { |
8477e53d | 862 | // OK, I play in this game |
6808d7a1 BA |
863 | GameStorage.update(this.gameRef.id, { |
864 | score: score, | |
865 | scoreMsg: scoreMsg | |
866 | }); | |
48ab808f | 867 | // Notify the score to main Hall. TODO: only one player (currently double send) |
6808d7a1 | 868 | this.send("result", { gid: this.game.id, score: score }); |
dcd68c41 | 869 | } |
6808d7a1 BA |
870 | } |
871 | } | |
a6088c90 BA |
872 | }; |
873 | </script> | |
7e1a1fe9 | 874 | |
41c80bb6 | 875 | <style lang="sass" scoped> |
72ccbd67 | 876 | .connected |
050ae3b5 | 877 | background-color: lightgreen |
72ccbd67 | 878 | |
ed06d9e9 BA |
879 | #participants |
880 | margin-left: 5px | |
881 | ||
882 | .anonymous | |
883 | color: grey | |
884 | font-style: italic | |
885 | ||
ec905cbc BA |
886 | #playersInfo > p |
887 | margin: 0 | |
888 | ||
430a2038 BA |
889 | @media screen and (min-width: 768px) |
890 | #actions | |
891 | width: 300px | |
892 | @media screen and (max-width: 767px) | |
893 | .game | |
894 | width: 100% | |
72ccbd67 | 895 | |
430a2038 | 896 | #actions |
cf94b843 | 897 | display: inline-block |
1d6d7b1d | 898 | margin: 0 |
430a2038 BA |
899 | button |
900 | display: inline-block | |
430a2038 | 901 | margin: 0 |
a1c48034 | 902 | |
050ae3b5 BA |
903 | @media screen and (max-width: 767px) |
904 | #aboveBoard | |
905 | text-align: center | |
885d93a7 BA |
906 | @media screen and (min-width: 768px) |
907 | #aboveBoard | |
908 | margin-left: 30% | |
050ae3b5 | 909 | |
2f258c37 BA |
910 | .variant-cadence |
911 | padding-right: 10px | |
912 | ||
913 | .variant-name | |
8c5f5390 | 914 | font-weight: bold |
77c50966 | 915 | padding-right: 10px |
77c50966 | 916 | |
050ae3b5 BA |
917 | .name |
918 | font-size: 1.5rem | |
919 | padding: 1px | |
920 | ||
921 | .time | |
922 | font-size: 2rem | |
923 | display: inline-block | |
924 | margin-left: 10px | |
925 | ||
926 | .split-names | |
927 | display: inline-block | |
928 | margin: 0 15px | |
929 | ||
430a2038 | 930 | #chat |
a1c48034 | 931 | padding-top: 20px |
a154d45e | 932 | max-width: 767px |
430a2038 | 933 | border: none; |
cf94b843 BA |
934 | |
935 | #chatBtn | |
936 | margin: 0 10px 0 0 | |
dcd68c41 BA |
937 | |
938 | .draw-sent, .draw-sent:hover | |
939 | background-color: lightyellow | |
940 | ||
941 | .draw-received, .draw-received:hover | |
942 | background-color: lightgreen | |
943 | ||
944 | .draw-threerep, .draw-threerep:hover | |
945 | background-color: #e4d1fc | |
2f258c37 BA |
946 | |
947 | .somethingnew | |
948 | background-color: #c5fefe | |
7e1a1fe9 | 949 | </style> |