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