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