| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalRules.modal(type="checkbox") |
| 4 | div#rulesDiv( |
| 5 | role="dialog" |
| 6 | data-checkbox="modalRules" |
| 7 | ) |
| 8 | .card |
| 9 | label.modal-close(for="modalRules") |
| 10 | a#variantNameInGame(:href="'/#/variants/'+game.vname") {{ game.vname }} |
| 11 | div(v-html="rulesContent") |
| 12 | input#modalScore.modal(type="checkbox") |
| 13 | div#scoreDiv( |
| 14 | role="dialog" |
| 15 | data-checkbox="modalScore" |
| 16 | ) |
| 17 | .card.text-center |
| 18 | label.modal-close(for="modalScore") |
| 19 | p.score-section |
| 20 | span.score {{ game.score }} |
| 21 | | : |
| 22 | span.score-msg {{ st.tr[game.scoreMsg] }} |
| 23 | input#modalRematch.modal(type="checkbox") |
| 24 | div#rematchDiv( |
| 25 | role="dialog" |
| 26 | data-checkbox="modalRematch" |
| 27 | ) |
| 28 | .card.text-center |
| 29 | label.modal-close(for="modalRematch") |
| 30 | a( |
| 31 | :href="'#/game/' + rematchId" |
| 32 | onClick="document.getElementById('modalRematch').checked=false" |
| 33 | ) |
| 34 | | {{ st.tr["Rematch in progress"] }} |
| 35 | input#modalChat.modal( |
| 36 | type="checkbox" |
| 37 | @click="toggleChat()" |
| 38 | ) |
| 39 | div#chatWrap( |
| 40 | role="dialog" |
| 41 | data-checkbox="modalChat" |
| 42 | ) |
| 43 | .card |
| 44 | label.modal-close(for="modalChat") |
| 45 | #participants |
| 46 | span {{ st.tr["Participant(s):"] }} |
| 47 | span( |
| 48 | v-for="p in Object.values(people)" |
| 49 | v-if="participateInChat(p)" |
| 50 | ) |
| 51 | | {{ p.name }} |
| 52 | span.anonymous(v-if="someAnonymousPresent()") + @nonymous |
| 53 | Chat( |
| 54 | ref="chatcomp" |
| 55 | :players="game.players" |
| 56 | :pastChats="game.chats" |
| 57 | @mychat="processChat" |
| 58 | @chatcleared="clearChat" |
| 59 | ) |
| 60 | input#modalConfirm.modal(type="checkbox") |
| 61 | div#confirmDiv(role="dialog") |
| 62 | .card |
| 63 | .diagram( |
| 64 | v-if="!!vr && ['all','byrow'].includes(vr.showMoves)" |
| 65 | v-html="curDiag" |
| 66 | ) |
| 67 | p.text-center(v-else) |
| 68 | span {{ st.tr["Move played:"] + " " }} |
| 69 | span.bold {{ moveNotation }} |
| 70 | br |
| 71 | span {{ st.tr["Are you sure?"] }} |
| 72 | .button-group#buttonsConfirm |
| 73 | // onClick for acceptBtn: set dynamically |
| 74 | button.acceptBtn |
| 75 | span {{ st.tr["Validate"] }} |
| 76 | button.refuseBtn(@click="cancelMove()") |
| 77 | span {{ st.tr["Cancel"] }} |
| 78 | .row |
| 79 | #aboveBoard.col-sm-12 |
| 80 | span.variant-cadence(v-if="game.type!='import'") {{ game.cadence }} |
| 81 | span.variant-name {{ game.vname }} |
| 82 | span#nextGame( |
| 83 | v-if="nextIds.length > 0" |
| 84 | @click="showNextGame()" |
| 85 | ) |
| 86 | | {{ st.tr["Next_g"] }} |
| 87 | button#chatBtn( |
| 88 | :class="btnTooltipClass()" |
| 89 | onClick="window.doClick('modalChat')" |
| 90 | aria-label="Chat" |
| 91 | ) |
| 92 | img(src="/images/icons/chat.svg") |
| 93 | #actions(v-if="game.score=='*'") |
| 94 | button( |
| 95 | @click="clickDraw()" |
| 96 | :class="btnTooltipClass('draw')" |
| 97 | :aria-label="st.tr['Draw']" |
| 98 | ) |
| 99 | img(src="/images/icons/draw.svg") |
| 100 | button( |
| 101 | v-if="!!game.mycolor" |
| 102 | :class="btnTooltipClass()" |
| 103 | @click="abortGame()" |
| 104 | :aria-label="st.tr['Abort']" |
| 105 | ) |
| 106 | img(src="/images/icons/abort.svg") |
| 107 | button( |
| 108 | v-if="!!game.mycolor" |
| 109 | :class="btnTooltipClass()" |
| 110 | @click="resign()" |
| 111 | :aria-label="st.tr['Resign']" |
| 112 | ) |
| 113 | img(src="/images/icons/resign.svg") |
| 114 | button( |
| 115 | v-else |
| 116 | :class="btnTooltipClass('rematch')" |
| 117 | @click="clickRematch()" |
| 118 | :aria-label="st.tr['Rematch']" |
| 119 | ) |
| 120 | img(src="/images/icons/rematch.svg") |
| 121 | #playersInfo |
| 122 | div(v-if="isLargeScreen()") |
| 123 | span.name(:class="{connected: isConnected(0)}") |
| 124 | | {{ game.players[0].name || "@nonymous" }} |
| 125 | span.time( |
| 126 | v-if="game.score=='*'" |
| 127 | :class="{yourturn: !!vr && vr.turn == 'w'}" |
| 128 | ) |
| 129 | span.time-left {{ virtualClocks[0][0] }} |
| 130 | span.time-separator(v-if="!!virtualClocks[0][1]") : |
| 131 | span.time-right(v-if="!!virtualClocks[0][1]") |
| 132 | | {{ virtualClocks[0][1] }} |
| 133 | span.split-names - |
| 134 | span.name(:class="{connected: isConnected(1)}") |
| 135 | | {{ game.players[1].name || "@nonymous" }} |
| 136 | span.time( |
| 137 | v-if="game.score=='*'" |
| 138 | :class="{yourturn: !!vr && vr.turn == 'b'}" |
| 139 | ) |
| 140 | span.time-left {{ virtualClocks[1][0] }} |
| 141 | span.time-separator(v-if="!!virtualClocks[1][1]") : |
| 142 | span.time-right(v-if="!!virtualClocks[1][1]") |
| 143 | | {{ virtualClocks[1][1] }} |
| 144 | div(v-else) |
| 145 | span.name(:class="{connected: isConnected(0)}") |
| 146 | | {{ game.players[0].name || "@nonymous" }} |
| 147 | span.split-names - |
| 148 | span.name(:class="{connected: isConnected(1)}") |
| 149 | | {{ game.players[1].name || "@nonymous" }} |
| 150 | div(v-if="game.score=='*'") |
| 151 | span.time(:class="{yourturn: !!vr && vr.turn == 'w'}") |
| 152 | span.time-left {{ virtualClocks[0][0] }} |
| 153 | span.time-separator(v-if="!!virtualClocks[0][1]") : |
| 154 | span.time-right(v-if="!!virtualClocks[0][1]") |
| 155 | | {{ virtualClocks[0][1] }} |
| 156 | span.separator |
| 157 | span.time(:class="{yourturn: !!vr && vr.turn == 'b'}") |
| 158 | span.time-left {{ virtualClocks[1][0] }} |
| 159 | span.time-separator(v-if="!!virtualClocks[1][1]") : |
| 160 | span.time-right(v-if="!!virtualClocks[1][1]") |
| 161 | | {{ virtualClocks[1][1] }} |
| 162 | BaseGame( |
| 163 | ref="basegame" |
| 164 | :game="game" |
| 165 | @newmove="processMove" |
| 166 | ) |
| 167 | </template> |
| 168 | |
| 169 | <script> |
| 170 | import BaseGame from "@/components/BaseGame.vue"; |
| 171 | import Chat from "@/components/Chat.vue"; |
| 172 | import { store } from "@/store"; |
| 173 | import { GameStorage } from "@/utils/gameStorage"; |
| 174 | import { ImportgameStorage } from "@/utils/importgameStorage"; |
| 175 | import { ppt } from "@/utils/datetime"; |
| 176 | import { notify } from "@/utils/notifications"; |
| 177 | import { ajax } from "@/utils/ajax"; |
| 178 | import { extractTime } from "@/utils/timeControl"; |
| 179 | import { getRandString } from "@/utils/alea"; |
| 180 | import { getScoreMessage } from "@/utils/scoring"; |
| 181 | import { getFullNotation } from "@/utils/notation"; |
| 182 | import { getDiagram, replaceByDiag } from "@/utils/printDiagram"; |
| 183 | import { processModalClick } from "@/utils/modalClick"; |
| 184 | import { playMove, getFilteredMove } from "@/utils/playUndo"; |
| 185 | import { ArrayFun } from "@/utils/array"; |
| 186 | import params from "@/parameters"; |
| 187 | export default { |
| 188 | name: "my-game", |
| 189 | components: { |
| 190 | BaseGame, |
| 191 | Chat |
| 192 | }, |
| 193 | data: function() { |
| 194 | return { |
| 195 | st: store.state, |
| 196 | // gameRef can point to a corr game, local game or remote live game |
| 197 | gameRef: "", |
| 198 | nextIds: [], |
| 199 | game: {}, //passed to BaseGame |
| 200 | focus: !document.hidden, //will not always work... TODO |
| 201 | // virtualClocks will be initialized from true game.clocks |
| 202 | virtualClocks: [], |
| 203 | vr: null, //"variant rules" object initialized from FEN |
| 204 | rulesContent: "", |
| 205 | drawOffer: "", |
| 206 | rematchId: "", |
| 207 | rematchOffer: "", |
| 208 | lastateAsked: false, |
| 209 | people: {}, //players + observers |
| 210 | lastate: undefined, //used if opponent send lastate before game is ready |
| 211 | repeat: {}, //detect position repetition |
| 212 | curDiag: "", //for corr moves confirmation |
| 213 | conn: null, |
| 214 | roomInitialized: false, |
| 215 | // If newmove has wrong index: ask fullgame again: |
| 216 | askGameTime: 0, |
| 217 | gameIsLoading: false, |
| 218 | // If asklastate got no reply, ask again: |
| 219 | gotLastate: false, |
| 220 | gotMoveIdx: -1, //last move index received |
| 221 | // If newmove got no pingback, send again: |
| 222 | opponentGotMove: false, |
| 223 | connexionString: "", |
| 224 | socketCloseListener: 0, |
| 225 | // Incomplete info games: show move played |
| 226 | moveNotation: "", |
| 227 | // Intervals from setInterval(): |
| 228 | askLastate: null, |
| 229 | retrySendmove: null, |
| 230 | clockUpdate: null, |
| 231 | // Related to (killing of) self multi-connects: |
| 232 | newConnect: {} |
| 233 | }; |
| 234 | }, |
| 235 | watch: { |
| 236 | $route: function(to, from) { |
| 237 | if (to.path.length < 6 || to.path.substr(0, 6) != "/game/") |
| 238 | // Page change |
| 239 | this.cleanBeforeDestroy(); |
| 240 | else if (from.params["id"] != to.params["id"]) { |
| 241 | // Change everything: |
| 242 | this.cleanBeforeDestroy(); |
| 243 | let boardDiv = document.querySelector(".game"); |
| 244 | if (!!boardDiv) |
| 245 | // In case of incomplete information variant: |
| 246 | boardDiv.style.visibility = "hidden"; |
| 247 | this.atCreation(); |
| 248 | } else |
| 249 | // Same game ID |
| 250 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); |
| 251 | } |
| 252 | }, |
| 253 | // NOTE: some redundant code with Hall.vue (mostly related to people array) |
| 254 | created: function() { |
| 255 | this.atCreation(); |
| 256 | }, |
| 257 | mounted: function() { |
| 258 | document.getElementById("chatWrap") |
| 259 | .addEventListener("click", (e) => { |
| 260 | processModalClick(e, () => { |
| 261 | this.toggleChat("close") |
| 262 | }); |
| 263 | }); |
| 264 | ["rulesDiv", "rematchDiv", "scoreDiv"].forEach( |
| 265 | (eltName) => { |
| 266 | document.getElementById(eltName) |
| 267 | .addEventListener("click", processModalClick); |
| 268 | } |
| 269 | ); |
| 270 | }, |
| 271 | beforeDestroy: function() { |
| 272 | this.cleanBeforeDestroy(); |
| 273 | }, |
| 274 | methods: { |
| 275 | cleanBeforeDestroy: function() { |
| 276 | clearInterval(this.socketCloseListener); |
| 277 | document.removeEventListener('visibilitychange', this.visibilityChange); |
| 278 | window.removeEventListener('focus', this.onFocus); |
| 279 | window.removeEventListener('blur', this.onBlur); |
| 280 | if (!!this.askLastate) clearInterval(this.askLastate); |
| 281 | if (!!this.retrySendmove) clearInterval(this.retrySendmove); |
| 282 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); |
| 283 | this.conn.removeEventListener("message", this.socketMessageListener); |
| 284 | this.send("disconnect"); |
| 285 | this.conn = null; |
| 286 | }, |
| 287 | visibilityChange: function() { |
| 288 | // TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27 |
| 289 | this.focus = (document.visibilityState == "visible"); |
| 290 | this.send(this.focus ? "getfocus" : "losefocus"); |
| 291 | }, |
| 292 | onFocus: function() { |
| 293 | this.focus = true; |
| 294 | this.send("getfocus"); |
| 295 | }, |
| 296 | onBlur: function() { |
| 297 | this.focus = false; |
| 298 | this.send("losefocus"); |
| 299 | }, |
| 300 | isLargeScreen: function() { |
| 301 | return window.innerWidth >= 768; |
| 302 | }, |
| 303 | btnTooltipClass: function(thing) { |
| 304 | let append = {}; |
| 305 | if (!!thing) append = { [thing + "-" + this[thing + "Offer"]]: true }; |
| 306 | return ( |
| 307 | Object.assign( |
| 308 | { tooltip: !("ontouchstart" in window) }, |
| 309 | append |
| 310 | ) |
| 311 | ); |
| 312 | }, |
| 313 | participateInChat: function(p) { |
| 314 | return Object.keys(p.tmpIds).some(x => p.tmpIds[x].focus) && !!p.name; |
| 315 | }, |
| 316 | someAnonymousPresent: function() { |
| 317 | return ( |
| 318 | Object.values(this.people).some(p => |
| 319 | !p.name && Object.keys(p.tmpIds).some(x => p.tmpIds[x].focus) |
| 320 | ) |
| 321 | ); |
| 322 | }, |
| 323 | atCreation: function() { |
| 324 | document.addEventListener('visibilitychange', this.visibilityChange); |
| 325 | window.addEventListener('focus', this.onFocus); |
| 326 | window.addEventListener('blur', this.onBlur); |
| 327 | // 0] (Re)Set variables |
| 328 | this.gameRef = this.$route.params["id"]; |
| 329 | // next = next corr games IDs to navigate faster (if applicable) |
| 330 | this.nextIds = JSON.parse(this.$route.query["next"] || "[]"); |
| 331 | // Always add myself to players' list |
| 332 | const my = this.st.user; |
| 333 | const tmpId = getRandString(); |
| 334 | this.$set( |
| 335 | this.people, |
| 336 | my.sid, |
| 337 | { |
| 338 | id: my.id, |
| 339 | name: my.name, |
| 340 | tmpIds: { |
| 341 | tmpId: { focus: true } |
| 342 | } |
| 343 | } |
| 344 | ); |
| 345 | this.game = { |
| 346 | players: [{ name: "" }, { name: "" }], |
| 347 | chats: [], |
| 348 | rendered: false |
| 349 | }; |
| 350 | let chatComp = this.$refs["chatcomp"]; |
| 351 | if (!!chatComp) chatComp.chats = []; |
| 352 | this.virtualClocks = [[0,0], [0,0]]; |
| 353 | this.vr = null; |
| 354 | this.rulesContent = ""; |
| 355 | this.drawOffer = ""; |
| 356 | this.lastateAsked = false; |
| 357 | this.rematchOffer = ""; |
| 358 | this.lastate = undefined; |
| 359 | this.roomInitialized = false; |
| 360 | this.askGameTime = 0; |
| 361 | this.gameIsLoading = false; |
| 362 | this.gotLastate = false; |
| 363 | this.gotMoveIdx = -1; |
| 364 | this.opponentGotMove = false; |
| 365 | this.askLastate = null; |
| 366 | this.retrySendmove = null; |
| 367 | this.clockUpdate = null; |
| 368 | this.newConnect = {}; |
| 369 | // 1] Initialize connection |
| 370 | this.connexionString = |
| 371 | params.socketUrl + |
| 372 | "/?sid=" + this.st.user.sid + |
| 373 | "&id=" + this.st.user.id + |
| 374 | "&tmpId=" + tmpId + |
| 375 | "&page=" + |
| 376 | // Discard potential "/?next=[...]" for page indication: |
| 377 | encodeURIComponent(this.$route.path.match(/\/game\/[a-zA-Z0-9]+/)[0]); |
| 378 | this.conn = new WebSocket(this.connexionString); |
| 379 | this.conn.addEventListener("message", this.socketMessageListener); |
| 380 | this.socketCloseListener = setInterval( |
| 381 | () => { |
| 382 | if (this.conn.readyState == 3) { |
| 383 | this.conn.removeEventListener( |
| 384 | "message", this.socketMessageListener); |
| 385 | this.conn = new WebSocket(this.connexionString); |
| 386 | this.conn.addEventListener("message", this.socketMessageListener); |
| 387 | } |
| 388 | }, |
| 389 | 1000 |
| 390 | ); |
| 391 | // Socket init required before loading remote game: |
| 392 | const socketInit = callback => { |
| 393 | if (this.conn.readyState == 1) |
| 394 | // 1 == OPEN state |
| 395 | callback(); |
| 396 | else |
| 397 | // Socket not ready yet (initial loading) |
| 398 | // NOTE: first arg is Websocket object, unused here: |
| 399 | this.conn.onopen = () => callback(); |
| 400 | }; |
| 401 | this.fetchGame((game) => { |
| 402 | if (!!game) |
| 403 | this.loadVariantThenGame(game, () => socketInit(this.roomInit)); |
| 404 | else |
| 405 | // Live game stored remotely: need socket to retrieve it |
| 406 | // NOTE: the callback "roomInit" will be lost, so it's not provided. |
| 407 | // --> It will be given when receiving "fullgame" socket event. |
| 408 | socketInit(() => { this.send("askfullgame"); }); |
| 409 | }); |
| 410 | }, |
| 411 | roomInit: function() { |
| 412 | if (!this.roomInitialized) { |
| 413 | // Notify the room only now that I connected, because |
| 414 | // messages might be lost otherwise (if game loading is slow) |
| 415 | this.send("connect"); |
| 416 | this.send("pollclients"); |
| 417 | // We may ask fullgame several times if some moves are lost, |
| 418 | // but room should be init only once: |
| 419 | this.roomInitialized = true; |
| 420 | } |
| 421 | }, |
| 422 | send: function(code, obj) { |
| 423 | if (!!this.conn && this.conn.readyState == 1) |
| 424 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
| 425 | }, |
| 426 | isConnected: function(index) { |
| 427 | const player = this.game.players[index]; |
| 428 | // Is it me ? In this case no need to bother with focus |
| 429 | if ( |
| 430 | this.st.user.sid == player.sid || |
| 431 | (!!player.name && this.st.user.id == player.id) |
| 432 | ) { |
| 433 | // Still have to check for name (because of potential multi-accounts |
| 434 | // on same browser, although this should be rare...) |
| 435 | return (!this.st.user.name || this.st.user.name == player.name); |
| 436 | } |
| 437 | // Try to find a match in people: |
| 438 | return ( |
| 439 | ( |
| 440 | !!player.sid && |
| 441 | Object.keys(this.people).some(sid => { |
| 442 | return ( |
| 443 | sid == player.sid && |
| 444 | Object.values(this.people[sid].tmpIds).some(v => v.focus) |
| 445 | ); |
| 446 | }) |
| 447 | ) |
| 448 | || |
| 449 | ( |
| 450 | player.id > 0 && |
| 451 | Object.values(this.people).some(p => { |
| 452 | return ( |
| 453 | p.id == player.id && |
| 454 | Object.values(p.tmpIds).some(v => v.focus) |
| 455 | ); |
| 456 | }) |
| 457 | ) |
| 458 | ); |
| 459 | }, |
| 460 | getOppsid: function() { |
| 461 | let oppsid = this.game.oppsid; |
| 462 | if (!oppsid) { |
| 463 | oppsid = Object.keys(this.people).find( |
| 464 | sid => this.people[sid].id == this.game.oppid |
| 465 | ); |
| 466 | } |
| 467 | // oppsid is useful only if opponent is online: |
| 468 | if (!!oppsid && !!this.people[oppsid]) return oppsid; |
| 469 | return null; |
| 470 | }, |
| 471 | // NOTE: action if provided is always a closing action |
| 472 | toggleChat: function(action) { |
| 473 | if (!action && document.getElementById("modalChat").checked) |
| 474 | // Entering chat |
| 475 | document.getElementById("inputChat").focus(); |
| 476 | else { |
| 477 | document.getElementById("chatBtn").classList.remove("somethingnew"); |
| 478 | if (!!this.game.mycolor) { |
| 479 | // Update "chatRead" variable either on server or locally |
| 480 | if (this.game.type == "corr") |
| 481 | this.updateCorrGame({ chatRead: this.game.mycolor }); |
| 482 | else if (this.game.type == "live") |
| 483 | GameStorage.update(this.gameRef, { chatRead: true }); |
| 484 | } |
| 485 | } |
| 486 | }, |
| 487 | processChat: function(chat) { |
| 488 | this.send("newchat", { data: chat }); |
| 489 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) |
| 490 | if (!!this.game.mycolor) { |
| 491 | if (this.game.type == "corr") |
| 492 | this.updateCorrGame({ chat: chat }); |
| 493 | else { |
| 494 | // Live game |
| 495 | chat.added = Date.now(); |
| 496 | GameStorage.update(this.gameRef, { chat: chat }); |
| 497 | } |
| 498 | } |
| 499 | }, |
| 500 | clearChat: function() { |
| 501 | if (!!this.game.mycolor) { |
| 502 | if (this.game.type == "corr") { |
| 503 | ajax( |
| 504 | "/chats", |
| 505 | "DELETE", |
| 506 | { data: { gid: this.game.id } } |
| 507 | ); |
| 508 | } else { |
| 509 | // Live game |
| 510 | GameStorage.update(this.gameRef, { delchat: true }); |
| 511 | } |
| 512 | this.$set(this.game, "chats", []); |
| 513 | } |
| 514 | }, |
| 515 | getGameType: function(game) { |
| 516 | if (!!game.id.toString().match(/^i/)) return "import"; |
| 517 | return game.cadence.indexOf("d") >= 0 ? "corr" : "live"; |
| 518 | }, |
| 519 | // Notify something after a new move (to opponent and me on MyGames page) |
| 520 | notifyMyGames: function(thing, data) { |
| 521 | this.send( |
| 522 | "notify" + thing, |
| 523 | { |
| 524 | data: data, |
| 525 | targets: this.game.players.map(p => { |
| 526 | return { sid: p.sid, id: p.id }; |
| 527 | }) |
| 528 | } |
| 529 | ); |
| 530 | }, |
| 531 | showNextGame: function() { |
| 532 | // Did I play in current game? If not, add it to nextIds list |
| 533 | if (this.game.score == "*" && this.vr.turn == this.game.mycolor) |
| 534 | this.nextIds.unshift(this.game.id); |
| 535 | const nextGid = this.nextIds.pop(); |
| 536 | this.$router.push( |
| 537 | "/game/" + nextGid + "/?next=" + JSON.stringify(this.nextIds)); |
| 538 | }, |
| 539 | askGameAgain: function() { |
| 540 | this.gameIsLoading = true; |
| 541 | const currentUrl = document.location.href; |
| 542 | const doAskGame = () => { |
| 543 | if (document.location.href != currentUrl) return; //page change |
| 544 | this.fetchGame((game) => { |
| 545 | if (!!game) |
| 546 | // This is my game: just reload. |
| 547 | this.loadGame(game); |
| 548 | else |
| 549 | // Just ask fullgame again (once!), this is much simpler. |
| 550 | // If this fails, the user could just reload page :/ |
| 551 | this.send("askfullgame"); |
| 552 | }); |
| 553 | }; |
| 554 | // Delay of at least 2s between two game requests |
| 555 | const now = Date.now(); |
| 556 | const delay = Math.max(2000 - (now - this.askGameTime), 0); |
| 557 | this.askGameTime = now; |
| 558 | setTimeout(doAskGame, delay); |
| 559 | }, |
| 560 | socketMessageListener: function(msg) { |
| 561 | if (!this.conn) return; |
| 562 | const data = JSON.parse(msg.data); |
| 563 | switch (data.code) { |
| 564 | case "pollclients": |
| 565 | // TODO: shuffling and random filtering on server, |
| 566 | // if the room is really crowded. |
| 567 | Object.keys(data.sockIds).forEach(sid => { |
| 568 | if (sid != this.st.user.sid) { |
| 569 | this.send("askidentity", { target: sid }); |
| 570 | this.people[sid] = { tmpIds: data.sockIds[sid] }; |
| 571 | } |
| 572 | else { |
| 573 | // Complete my tmpIds: |
| 574 | Object.assign(this.people[sid].tmpIds, data.sockIds[sid]); |
| 575 | } |
| 576 | }); |
| 577 | break; |
| 578 | case "connect": |
| 579 | if (!this.people[data.from[0]]) { |
| 580 | // focus depends on the tmpId (e.g. tab) |
| 581 | this.$set( |
| 582 | this.people, |
| 583 | data.from[0], |
| 584 | { |
| 585 | tmpIds: { |
| 586 | [data.from[1]]: { focus: true } |
| 587 | } |
| 588 | } |
| 589 | ); |
| 590 | // For self multi-connects tests: |
| 591 | this.newConnect[data.from[0]] = true; |
| 592 | this.send("askidentity", { target: data.from[0] }); |
| 593 | } else { |
| 594 | this.people[data.from[0]].tmpIds[data.from[1]] = { focus: true }; |
| 595 | this.$forceUpdate(); //TODO: shouldn't be required |
| 596 | } |
| 597 | break; |
| 598 | case "disconnect": |
| 599 | if (!this.people[data.from[0]]) return; |
| 600 | delete this.people[data.from[0]].tmpIds[data.from[1]]; |
| 601 | if (Object.keys(this.people[data.from[0]].tmpIds).length == 0) |
| 602 | this.$delete(this.people, data.from[0]); |
| 603 | else this.$forceUpdate(); //TODO: shouldn't be required |
| 604 | break; |
| 605 | case "getfocus": { |
| 606 | let player = this.people[data.from[0]]; |
| 607 | if (!!player) { |
| 608 | player.tmpIds[data.from[1]].focus = true; |
| 609 | this.$forceUpdate(); //TODO: shouldn't be required |
| 610 | } |
| 611 | break; |
| 612 | } |
| 613 | case "losefocus": { |
| 614 | let player = this.people[data.from[0]]; |
| 615 | if (!!player) { |
| 616 | player.tmpIds[data.from[1]].focus = false; |
| 617 | this.$forceUpdate(); //TODO: shouldn't be required |
| 618 | } |
| 619 | break; |
| 620 | } |
| 621 | case "askidentity": { |
| 622 | // Request for identification |
| 623 | const me = { |
| 624 | // Decompose to avoid revealing email |
| 625 | name: this.st.user.name, |
| 626 | sid: this.st.user.sid, |
| 627 | id: this.st.user.id |
| 628 | }; |
| 629 | this.send("identity", { data: me, target: data.from }); |
| 630 | break; |
| 631 | } |
| 632 | case "identity": { |
| 633 | const user = data.data; |
| 634 | let player = this.people[user.sid]; |
| 635 | // player.tmpIds is already set |
| 636 | player.name = user.name; |
| 637 | player.id = user.id; |
| 638 | if (this.game.type == "live") { |
| 639 | const myGidx = |
| 640 | this.game.players.findIndex(p => p.sid == this.st.user.sid); |
| 641 | // Sometimes a player name isn't stored yet (TODO: why?) |
| 642 | if ( |
| 643 | myGidx >= 0 && |
| 644 | !this.game.players[1 - myGidx].name && |
| 645 | this.game.players[1 - myGidx].sid == user.sid && |
| 646 | !!user.name |
| 647 | ) { |
| 648 | this.game.players[1-myGidx].name = user.name; |
| 649 | GameStorage.update( |
| 650 | this.gameRef, |
| 651 | { playerName: { idx: 1 - myGidx, name: user.name } } |
| 652 | ); |
| 653 | } |
| 654 | } |
| 655 | this.$forceUpdate(); //TODO: shouldn't be required |
| 656 | // If I multi-connect, kill current connexion if no mark (I'm older) |
| 657 | if (this.newConnect[user.sid]) { |
| 658 | delete this.newConnect[user.sid]; |
| 659 | if ( |
| 660 | user.id > 0 && |
| 661 | user.id == this.st.user.id && |
| 662 | user.sid != this.st.user.sid |
| 663 | ) { |
| 664 | this.cleanBeforeDestroy(); |
| 665 | alert(this.st.tr["New connexion detected: tab now offline"]); |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | // Ask potentially missed last state, if opponent and I play |
| 670 | if ( |
| 671 | !this.gotLastate && |
| 672 | !!this.game.mycolor && |
| 673 | this.game.type == "live" && |
| 674 | this.game.players.some(p => p.sid == user.sid) |
| 675 | ) { |
| 676 | this.send("asklastate", { target: user.sid }); |
| 677 | let counter = 1; |
| 678 | this.askLastate = setInterval( |
| 679 | () => { |
| 680 | // Ask at most 3 times: |
| 681 | // if no reply after that there should be a network issue. |
| 682 | if ( |
| 683 | counter < 3 && |
| 684 | !this.gotLastate && |
| 685 | !!this.people[user.sid] |
| 686 | ) { |
| 687 | this.send("asklastate", { target: user.sid }); |
| 688 | counter++; |
| 689 | } else { |
| 690 | clearInterval(this.askLastate); |
| 691 | } |
| 692 | }, |
| 693 | 1500 |
| 694 | ); |
| 695 | } |
| 696 | break; |
| 697 | } |
| 698 | case "askgame": |
| 699 | // Send current (live or import) game, |
| 700 | // if not asked by any of the players |
| 701 | if ( |
| 702 | this.game.type != "corr" && |
| 703 | this.game.players.every(p => p.sid != data.from[0]) |
| 704 | ) { |
| 705 | const myGame = { |
| 706 | id: this.game.id, |
| 707 | // FEN is current position, unused for now |
| 708 | fen: this.game.fen, |
| 709 | players: this.game.players, |
| 710 | vid: this.game.vid, |
| 711 | cadence: this.game.cadence, |
| 712 | score: this.game.score |
| 713 | }; |
| 714 | this.send("game", { data: myGame, target: data.from }); |
| 715 | } |
| 716 | break; |
| 717 | case "askfullgame": |
| 718 | const gameToSend = Object.keys(this.game) |
| 719 | .filter(k => |
| 720 | [ |
| 721 | "id","fen","players","vid","cadence","fenStart","vname", |
| 722 | "moves","clocks","score","drawOffer","rematchOffer" |
| 723 | ].includes(k)) |
| 724 | .reduce( |
| 725 | (obj, k) => { |
| 726 | obj[k] = this.game[k]; |
| 727 | return obj; |
| 728 | }, |
| 729 | {} |
| 730 | ); |
| 731 | this.send("fullgame", { data: gameToSend, target: data.from }); |
| 732 | break; |
| 733 | case "fullgame": |
| 734 | if (!!data.data.empty) { |
| 735 | alert(this.st.tr["The game should be in another tab"]); |
| 736 | this.$router.go(-1); |
| 737 | } |
| 738 | else |
| 739 | // Callback "roomInit" to poll clients only after game is loaded |
| 740 | this.loadVariantThenGame(data.data, this.roomInit); |
| 741 | break; |
| 742 | case "asklastate": |
| 743 | // Sending informative last state if I played a move or score != "*" |
| 744 | // If the game or moves aren't loaded yet, delay the sending: |
| 745 | // TODO: socket init after game load, so the game is supposedly ready |
| 746 | if (!this.game || !this.game.moves) this.lastateAsked = true; |
| 747 | else this.sendLastate(data.from); |
| 748 | break; |
| 749 | // TODO: possible bad scenario: reload page while oppponent sends a |
| 750 | // move => get both lastate and newmove, process both, add move twice. |
| 751 | // Confirm scenario? Fix? |
| 752 | case "lastate": { |
| 753 | // Got opponent infos about last move |
| 754 | this.gotLastate = true; |
| 755 | this.lastate = data.data; |
| 756 | if (this.lastate.movesCount - 1 > this.gotMoveIdx) |
| 757 | this.gotMoveIdx = this.lastate.movesCount - 1; |
| 758 | if (this.game.rendered) |
| 759 | // Game is rendered (Board component) |
| 760 | this.processLastate(); |
| 761 | // Else: will be processed when game is ready |
| 762 | break; |
| 763 | } |
| 764 | case "newmove": { |
| 765 | |
| 766 | // DEBUG: |
| 767 | console.log("Receive move"); |
| 768 | console.log(data.data); |
| 769 | //moveslist not updated when receiving a move? (see in baseGame) |
| 770 | |
| 771 | const movePlus = data.data; |
| 772 | const movesCount = this.game.moves.length; |
| 773 | if (movePlus.index > movesCount) { |
| 774 | // This can only happen if I'm an observer and missed a move. |
| 775 | if (this.gotMoveIdx < movePlus.index) |
| 776 | this.gotMoveIdx = movePlus.index; |
| 777 | if (!this.gameIsLoading) this.askGameAgain(); |
| 778 | } |
| 779 | else { |
| 780 | if ( |
| 781 | movePlus.index < movesCount || |
| 782 | this.gotMoveIdx >= movePlus.index |
| 783 | ) { |
| 784 | // Opponent re-send but we already have the move: |
| 785 | // (maybe he didn't receive our pingback...) |
| 786 | this.send("gotmove", {data: movePlus.index, target: data.from}); |
| 787 | } else { |
| 788 | this.gotMoveIdx = movePlus.index; |
| 789 | const receiveMyMove = (movePlus.color == this.game.mycolor); |
| 790 | const moveColIdx = ["w", "b"].indexOf(movePlus.color); |
| 791 | if (!receiveMyMove && !!this.game.mycolor) { |
| 792 | // Notify opponent that I got the move: |
| 793 | this.send( |
| 794 | "gotmove", |
| 795 | { data: movePlus.index, target: data.from } |
| 796 | ); |
| 797 | // And myself if I'm elsewhere: |
| 798 | if (!this.focus) { |
| 799 | notify( |
| 800 | "New move", |
| 801 | { |
| 802 | body: |
| 803 | (this.game.players[moveColIdx].name || "@nonymous") + |
| 804 | " just played." |
| 805 | } |
| 806 | ); |
| 807 | } |
| 808 | } |
| 809 | if (movePlus.cancelDrawOffer) { |
| 810 | // Opponent refuses draw |
| 811 | this.drawOffer = ""; |
| 812 | // NOTE for corr games: drawOffer reset by player in turn |
| 813 | if ( |
| 814 | this.game.type == "live" && |
| 815 | !!this.game.mycolor && |
| 816 | !receiveMyMove |
| 817 | ) { |
| 818 | GameStorage.update(this.gameRef, { drawOffer: "" }); |
| 819 | } |
| 820 | } |
| 821 | this.$refs["basegame"].play(movePlus.move, "received"); |
| 822 | // Freeze time while the move is being play |
| 823 | // (TODO: a callback would be cleaner here) |
| 824 | clearInterval(this.clockUpdate); |
| 825 | this.clockUpdate = null; |
| 826 | const freezeDuration = ["all", "highlight"].includes(V.ShowMoves) |
| 827 | // 250 = length of animation, 500 = delay between sub-moves |
| 828 | ? 250 + 750 * |
| 829 | (Array.isArray(movePlus.move) ? movePlus.move.length - 1 : 0) |
| 830 | // Incomplete information: no move animation |
| 831 | : 0; |
| 832 | setTimeout( |
| 833 | () => { |
| 834 | this.game.clocks[moveColIdx] = movePlus.clock; |
| 835 | this.processMove( |
| 836 | movePlus.move, |
| 837 | { receiveMyMove: receiveMyMove } |
| 838 | ); |
| 839 | }, |
| 840 | freezeDuration |
| 841 | ); |
| 842 | } |
| 843 | } |
| 844 | break; |
| 845 | } |
| 846 | case "gotmove": { |
| 847 | this.opponentGotMove = true; |
| 848 | // Now his clock starts running on my side: |
| 849 | const oppIdx = ['w','b'].indexOf(this.vr.turn); |
| 850 | // NOTE: next line to avoid multi-resetClocks when several tabs |
| 851 | // on same game, resulting in a faster countdown. |
| 852 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); |
| 853 | this.re_setClocks(); |
| 854 | break; |
| 855 | } |
| 856 | case "resign": |
| 857 | const score = (data.data == "b" ? "1-0" : "0-1"); |
| 858 | const side = (data.data == "w" ? "White" : "Black"); |
| 859 | this.gameOver(score, side + " surrender"); |
| 860 | break; |
| 861 | case "abort": |
| 862 | this.gameOver("?", "Stop"); |
| 863 | break; |
| 864 | case "draw": |
| 865 | this.gameOver("1/2", data.data); |
| 866 | break; |
| 867 | case "drawoffer": |
| 868 | // NOTE: observers don't know who offered draw |
| 869 | this.drawOffer = "received"; |
| 870 | if (!!this.game.mycolor && this.game.type == "live") { |
| 871 | GameStorage.update( |
| 872 | this.gameRef, |
| 873 | { drawOffer: V.GetOppCol(this.game.mycolor) } |
| 874 | ); |
| 875 | } |
| 876 | break; |
| 877 | case "rematchoffer": |
| 878 | // NOTE: observers don't know who offered rematch |
| 879 | this.rematchOffer = data.data ? "received" : ""; |
| 880 | if (!!this.game.mycolor && this.game.type == "live") { |
| 881 | GameStorage.update( |
| 882 | this.gameRef, |
| 883 | { rematchOffer: data.data ? V.GetOppCol(this.game.mycolor) : "" } |
| 884 | ); |
| 885 | } |
| 886 | break; |
| 887 | case "newgame": { |
| 888 | // A game started, redirect if I'm playing in |
| 889 | const gameInfo = data.data; |
| 890 | const gameType = this.getGameType(gameInfo); |
| 891 | if ( |
| 892 | gameType == "live" && |
| 893 | gameInfo.players.some(p => p.sid == this.st.user.sid) |
| 894 | ) { |
| 895 | this.addAndGotoLiveGame(gameInfo); |
| 896 | } else if ( |
| 897 | gameType == "corr" && |
| 898 | this.st.user.id > 0 && |
| 899 | gameInfo.players.some(p => p.id == this.st.user.id) |
| 900 | ) { |
| 901 | this.$router.push("/game/" + gameInfo.id); |
| 902 | } else { |
| 903 | this.rematchId = gameInfo.id; |
| 904 | document.getElementById("modalRules").checked = false; |
| 905 | document.getElementById("modalScore").checked = false; |
| 906 | document.getElementById("modalRematch").checked = true; |
| 907 | } |
| 908 | break; |
| 909 | } |
| 910 | case "newchat": { |
| 911 | let chat = data.data; |
| 912 | this.$refs["chatcomp"].newChat(chat); |
| 913 | if (this.game.type == "live") { |
| 914 | chat.added = Date.now(); |
| 915 | if (!!this.game.mycolor) |
| 916 | GameStorage.update(this.gameRef, { chat: chat }); |
| 917 | } |
| 918 | if (!document.getElementById("modalChat").checked) |
| 919 | document.getElementById("chatBtn").classList.add("somethingnew"); |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | }, |
| 924 | updateCorrGame: function(obj, callback) { |
| 925 | ajax( |
| 926 | "/games", |
| 927 | "PUT", |
| 928 | { |
| 929 | data: { |
| 930 | gid: this.gameRef, |
| 931 | newObj: obj |
| 932 | }, |
| 933 | success: () => { |
| 934 | if (!!callback) callback(); |
| 935 | } |
| 936 | } |
| 937 | ); |
| 938 | }, |
| 939 | sendLastate: function(target) { |
| 940 | // Send our "last state" informations to opponent |
| 941 | const L = this.game.moves.length; |
| 942 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); |
| 943 | const myLastate = { |
| 944 | lastMove: |
| 945 | (L > 0 && this.vr.turn != this.game.mycolor) |
| 946 | ? this.game.moves[L - 1] |
| 947 | : undefined, |
| 948 | clock: this.game.clocks[myIdx], |
| 949 | // Since we played a move (or abort or resign), |
| 950 | // only drawOffer=="sent" is possible |
| 951 | drawSent: this.drawOffer == "sent" ? true : undefined, |
| 952 | rematchSent: this.rematchOffer == "sent" ? true : undefined, |
| 953 | score: this.game.score != "*" ? this.game.score : undefined, |
| 954 | scoreMsg: this.game.score != "*" ? this.game.scoreMsg : undefined, |
| 955 | movesCount: L |
| 956 | }; |
| 957 | this.send("lastate", { data: myLastate, target: target }); |
| 958 | }, |
| 959 | // lastate was received, but maybe game wasn't ready yet: |
| 960 | processLastate: function() { |
| 961 | const data = this.lastate; |
| 962 | this.lastate = undefined; //security... |
| 963 | if (!!data.score) { |
| 964 | const oppCol = V.GetOppCol(this.game.mycolor); |
| 965 | if (!!data.rematchSent) { |
| 966 | if (this.game.rematchOffer != oppCol) { |
| 967 | // Opponent sended rematch offer while we were offline: |
| 968 | this.rematchOffer = "received"; |
| 969 | GameStorage.update( |
| 970 | this.gameRef, |
| 971 | { rematchOffer: oppCol } |
| 972 | ); |
| 973 | } |
| 974 | } |
| 975 | else { |
| 976 | if (this.game.rematchOffer == oppCol) { |
| 977 | // Opponent cancelled rematch offer while we were offline: |
| 978 | this.rematchOffer = ""; |
| 979 | GameStorage.update( |
| 980 | this.gameRef, |
| 981 | { rematchOffer: "" } |
| 982 | ); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | else { |
| 987 | const L = this.game.moves.length; |
| 988 | const oppIdx = 1 - ["w", "b"].indexOf(this.game.mycolor); |
| 989 | this.game.clocks[oppIdx] = data.clock; |
| 990 | if (data.movesCount > L) { |
| 991 | // Just got last move from him |
| 992 | this.$refs["basegame"].play(data.lastMove, "received"); |
| 993 | this.processMove(data.lastMove); |
| 994 | } else { |
| 995 | if (!!this.clockUpdate) clearInterval(this.clockUpdate); |
| 996 | this.re_setClocks(); |
| 997 | } |
| 998 | if (!!data.drawSent) this.drawOffer = "received"; |
| 999 | if (!!data.score) { |
| 1000 | this.drawOffer = ""; |
| 1001 | if (this.game.score == "*") |
| 1002 | this.gameOver(data.score, data.scoreMsg); |
| 1003 | } |
| 1004 | } |
| 1005 | }, |
| 1006 | clickDraw: function() { |
| 1007 | if (!this.game.mycolor || this.game.type == "import") return; |
| 1008 | if (["received", "threerep"].includes(this.drawOffer)) { |
| 1009 | if (!confirm(this.st.tr["Accept draw?"])) return; |
| 1010 | const message = |
| 1011 | this.drawOffer == "received" |
| 1012 | ? "Mutual agreement" |
| 1013 | : "Three repetitions"; |
| 1014 | this.send("draw", { data: message }); |
| 1015 | this.gameOver("1/2", message); |
| 1016 | } else if (this.drawOffer == "") { |
| 1017 | // No effect if drawOffer == "sent" |
| 1018 | if (this.game.mycolor != this.vr.turn) { |
| 1019 | alert(this.st.tr["Draw offer only in your turn"]); |
| 1020 | return; |
| 1021 | } |
| 1022 | if (!confirm(this.st.tr["Offer draw?"])) return; |
| 1023 | this.drawOffer = "sent"; |
| 1024 | this.send("drawoffer"); |
| 1025 | if (this.game.type == "live") { |
| 1026 | GameStorage.update( |
| 1027 | this.gameRef, |
| 1028 | { drawOffer: this.game.mycolor } |
| 1029 | ); |
| 1030 | } else this.updateCorrGame({ drawOffer: this.game.mycolor }); |
| 1031 | } |
| 1032 | }, |
| 1033 | addAndGotoLiveGame: function(gameInfo, callback) { |
| 1034 | const game = Object.assign( |
| 1035 | {}, |
| 1036 | gameInfo, |
| 1037 | { |
| 1038 | // (other) Game infos: constant |
| 1039 | fenStart: gameInfo.fen, |
| 1040 | vname: this.game.vname, |
| 1041 | created: Date.now(), |
| 1042 | // Game state (including FEN): will be updated |
| 1043 | moves: [], |
| 1044 | clocks: [-1, -1], //-1 = unstarted |
| 1045 | score: "*" |
| 1046 | } |
| 1047 | ); |
| 1048 | GameStorage.add(game, (err) => { |
| 1049 | // No error expected. |
| 1050 | if (!err) { |
| 1051 | if (this.st.settings.sound) |
| 1052 | new Audio("/sounds/newgame.flac").play().catch(() => {}); |
| 1053 | if (!!callback) callback(); |
| 1054 | this.$router.push("/game/" + gameInfo.id); |
| 1055 | } |
| 1056 | }); |
| 1057 | }, |
| 1058 | clickRematch: function() { |
| 1059 | if (!this.game.mycolor || this.game.type == "import") return; |
| 1060 | if (this.rematchOffer == "received") { |
| 1061 | // Start a new game! |
| 1062 | let gameInfo = { |
| 1063 | id: getRandString(), //ignored if corr |
| 1064 | fen: V.GenRandInitFen(this.game.randomness), |
| 1065 | players: [this.game.players[1], this.game.players[0]], |
| 1066 | vid: this.game.vid, |
| 1067 | cadence: this.game.cadence |
| 1068 | }; |
| 1069 | const notifyNewGame = () => { |
| 1070 | const oppsid = this.getOppsid(); //may be null |
| 1071 | this.send("rnewgame", { data: gameInfo, oppsid: oppsid }); |
| 1072 | // To main Hall if corr game: |
| 1073 | if (this.game.type == "corr") |
| 1074 | this.send("newgame", { data: gameInfo, page: "/" }); |
| 1075 | // Also to MyGames page: |
| 1076 | this.notifyMyGames("newgame", gameInfo); |
| 1077 | }; |
| 1078 | if (this.game.type == "live") |
| 1079 | this.addAndGotoLiveGame(gameInfo, notifyNewGame); |
| 1080 | else { |
| 1081 | // corr game |
| 1082 | ajax( |
| 1083 | "/games", |
| 1084 | "POST", |
| 1085 | { |
| 1086 | // cid is useful to delete the challenge: |
| 1087 | data: { gameInfo: gameInfo }, |
| 1088 | success: (response) => { |
| 1089 | gameInfo.id = response.gameId; |
| 1090 | notifyNewGame(); |
| 1091 | this.$router.push("/game/" + response.gameId); |
| 1092 | } |
| 1093 | } |
| 1094 | ); |
| 1095 | } |
| 1096 | } else if (this.rematchOffer == "") { |
| 1097 | this.rematchOffer = "sent"; |
| 1098 | this.send("rematchoffer", { data: true }); |
| 1099 | if (this.game.type == "live") { |
| 1100 | GameStorage.update( |
| 1101 | this.gameRef, |
| 1102 | { rematchOffer: this.game.mycolor } |
| 1103 | ); |
| 1104 | } else this.updateCorrGame({ rematchOffer: this.game.mycolor }); |
| 1105 | } else if (this.rematchOffer == "sent") { |
| 1106 | // Toggle rematch offer (on --> off) |
| 1107 | this.rematchOffer = ""; |
| 1108 | this.send("rematchoffer", { data: false }); |
| 1109 | if (this.game.type == "live") { |
| 1110 | GameStorage.update( |
| 1111 | this.gameRef, |
| 1112 | { rematchOffer: '' } |
| 1113 | ); |
| 1114 | } else this.updateCorrGame({ rematchOffer: 'n' }); |
| 1115 | } |
| 1116 | }, |
| 1117 | abortGame: function() { |
| 1118 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) |
| 1119 | return; |
| 1120 | this.gameOver("?", "Stop"); |
| 1121 | this.send("abort"); |
| 1122 | }, |
| 1123 | resign: function() { |
| 1124 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
| 1125 | return; |
| 1126 | this.send("resign", { data: this.game.mycolor }); |
| 1127 | const score = (this.game.mycolor == "w" ? "0-1" : "1-0"); |
| 1128 | const side = (this.game.mycolor == "w" ? "White" : "Black"); |
| 1129 | this.gameOver(score, side + " surrender"); |
| 1130 | }, |
| 1131 | loadGame: function(game, callback) { |
| 1132 | const gtype = game.type || this.getGameType(game); |
| 1133 | const tc = extractTime(game.cadence); |
| 1134 | const myIdx = game.players.findIndex(p => { |
| 1135 | return ( |
| 1136 | p.sid == this.st.user.sid || |
| 1137 | (!!p.name && p.id == this.st.user.id) |
| 1138 | ); |
| 1139 | }); |
| 1140 | // Sometimes the name isn't stored yet (TODO: why?) |
| 1141 | if ( |
| 1142 | myIdx >= 0 && |
| 1143 | gtype == "live" && |
| 1144 | !game.players[myIdx].name && |
| 1145 | !!this.st.user.name |
| 1146 | ) { |
| 1147 | game.players[myIdx].name = this.st.user.name; |
| 1148 | GameStorage.update( |
| 1149 | game.id, |
| 1150 | { playerName: { idx: myIdx, name: this.st.user.name } } |
| 1151 | ); |
| 1152 | } |
| 1153 | // "mycolor" is undefined for observers |
| 1154 | const mycolor = [undefined, "w", "b"][myIdx + 1]; |
| 1155 | if (gtype == "corr") { |
| 1156 | if (mycolor == 'w') game.chatRead = game.chatReadWhite; |
| 1157 | else if (mycolor == 'b') game.chatRead = game.chatReadBlack; |
| 1158 | // NOTE: clocks in seconds |
| 1159 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
| 1160 | game.clocks = [tc.mainTime, tc.mainTime]; |
| 1161 | const L = game.moves.length; |
| 1162 | if (game.score == "*") { |
| 1163 | // Adjust clocks |
| 1164 | if (L >= 2) { |
| 1165 | game.clocks[L % 2] -= |
| 1166 | (Date.now() - game.moves[L-1].played) / 1000; |
| 1167 | } |
| 1168 | } |
| 1169 | // Now that we used idx and played, re-format moves as for live games |
| 1170 | game.moves = game.moves.map(m => m.squares); |
| 1171 | } |
| 1172 | else if (gtype == "live") { |
| 1173 | if (game.clocks[0] < 0) { |
| 1174 | // Game is unstarted. clock is ignored until move 2 |
| 1175 | game.clocks = [tc.mainTime, tc.mainTime]; |
| 1176 | if (myIdx >= 0) { |
| 1177 | // I play in this live game |
| 1178 | GameStorage.update( |
| 1179 | game.id, |
| 1180 | { clocks: game.clocks } |
| 1181 | ); |
| 1182 | } |
| 1183 | } else { |
| 1184 | if (!!game.initime) |
| 1185 | // It's my turn: clocks not updated yet |
| 1186 | game.clocks[myIdx] -= (Date.now() - game.initime) / 1000; |
| 1187 | } |
| 1188 | } |
| 1189 | else |
| 1190 | // gtype == "import" |
| 1191 | game.clocks = [tc.mainTime, tc.mainTime]; |
| 1192 | // Live games before 26/03/2020 don't have chat history: |
| 1193 | if (!game.chats) game.chats = []; //TODO: remove line |
| 1194 | // Sort chat messages from newest to oldest |
| 1195 | game.chats.sort((c1, c2) => c2.added - c1.added); |
| 1196 | if ( |
| 1197 | myIdx >= 0 && |
| 1198 | game.chats.length > 0 && |
| 1199 | (!game.chatRead || game.chatRead < game.chats[0].added) |
| 1200 | ) { |
| 1201 | // A chat message arrived since my last reading: |
| 1202 | document.getElementById("chatBtn").classList.add("somethingnew"); |
| 1203 | } |
| 1204 | // TODO: merge next 2 "if" conditions |
| 1205 | if (!!game.drawOffer) { |
| 1206 | if (game.drawOffer == "t") |
| 1207 | // Three repetitions |
| 1208 | this.drawOffer = "threerep"; |
| 1209 | else { |
| 1210 | // Draw offered by any of the players: |
| 1211 | if (myIdx < 0) this.drawOffer = "received"; |
| 1212 | else { |
| 1213 | // I play in this game: |
| 1214 | if ( |
| 1215 | (game.drawOffer == "w" && myIdx == 0) || |
| 1216 | (game.drawOffer == "b" && myIdx == 1) |
| 1217 | ) |
| 1218 | this.drawOffer = "sent"; |
| 1219 | else this.drawOffer = "received"; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | if (!!game.rematchOffer) { |
| 1224 | if (myIdx < 0) this.rematchOffer = "received"; |
| 1225 | else { |
| 1226 | // I play in this game: |
| 1227 | if ( |
| 1228 | (game.rematchOffer == "w" && myIdx == 0) || |
| 1229 | (game.rematchOffer == "b" && myIdx == 1) |
| 1230 | ) { |
| 1231 | this.rematchOffer = "sent"; |
| 1232 | } |
| 1233 | else this.rematchOffer = "received"; |
| 1234 | } |
| 1235 | } |
| 1236 | this.repeat = {}; //reset: scan past moves' FEN: |
| 1237 | let repIdx = 0; |
| 1238 | this.vr = new V(game.fenStart); |
| 1239 | let curTurn = "n"; |
| 1240 | game.moves.forEach(m => { |
| 1241 | playMove(m, this.vr); |
| 1242 | const fenIdx = this.vr.getFenForRepeat(); |
| 1243 | this.repeat[fenIdx] = this.repeat[fenIdx] |
| 1244 | ? this.repeat[fenIdx] + 1 |
| 1245 | : 1; |
| 1246 | }); |
| 1247 | // Imported games don't have current FEN |
| 1248 | if (!game.fen) game.fen = this.vr.getFen(); |
| 1249 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; |
| 1250 | this.game = Object.assign( |
| 1251 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
| 1252 | { |
| 1253 | type: gtype, |
| 1254 | increment: tc.increment, |
| 1255 | mycolor: mycolor, |
| 1256 | // opponent sid not strictly required (or available), but easier |
| 1257 | // at least oppsid or oppid is available anyway: |
| 1258 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, |
| 1259 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].id |
| 1260 | }, |
| 1261 | game |
| 1262 | ); |
| 1263 | this.$refs["basegame"].re_setVariables(this.game); |
| 1264 | if (!this.gameIsLoading) { |
| 1265 | // Initial loading: |
| 1266 | this.gotMoveIdx = game.moves.length - 1; |
| 1267 | // If we arrive here after 'nextGame' action, the board might be hidden |
| 1268 | let boardDiv = document.querySelector(".game"); |
| 1269 | if (!!boardDiv && boardDiv.style.visibility == "hidden") |
| 1270 | boardDiv.style.visibility = "visible"; |
| 1271 | } |
| 1272 | this.re_setClocks(); |
| 1273 | this.$nextTick(() => { |
| 1274 | this.game.rendered = true; |
| 1275 | // Did lastate arrive before game was rendered? |
| 1276 | if (!!this.lastate) this.processLastate(); |
| 1277 | }); |
| 1278 | if (this.lastateAsked) { |
| 1279 | this.lastateAsked = false; |
| 1280 | this.sendLastate(game.oppsid); |
| 1281 | } |
| 1282 | if (this.gameIsLoading) { |
| 1283 | this.gameIsLoading = false; |
| 1284 | if (this.gotMoveIdx >= game.moves.length) |
| 1285 | // Some moves arrived meanwhile... |
| 1286 | this.askGameAgain(); |
| 1287 | } |
| 1288 | if (!!callback) callback(); |
| 1289 | }, |
| 1290 | loadVariantThenGame: async function(game, callback) { |
| 1291 | await import("@/variants/" + game.vname + ".js") |
| 1292 | .then((vModule) => { |
| 1293 | window.V = vModule[game.vname + "Rules"]; |
| 1294 | this.loadGame(game, callback); |
| 1295 | }); |
| 1296 | // (AJAX) Request to get rules content (plain text, HTML) |
| 1297 | this.rulesContent = |
| 1298 | require( |
| 1299 | "raw-loader!@/translations/rules/" + |
| 1300 | game.vname + "/" + |
| 1301 | this.st.lang + ".pug" |
| 1302 | ) |
| 1303 | // Next two lines fix a weird issue after last update (2019-11) |
| 1304 | .replace(/\\n/g, " ") |
| 1305 | .replace(/\\"/g, '"') |
| 1306 | .replace('module.exports = "', "") |
| 1307 | .replace(/"$/, "") |
| 1308 | .replace(/(fen:)([^:]*):/g, replaceByDiag); |
| 1309 | }, |
| 1310 | // 3 cases for loading a game: |
| 1311 | // - from indexedDB (running or completed live game I play) |
| 1312 | // - from server (one correspondance game I play[ed] or not) |
| 1313 | // - from remote peer (one live game I don't play, finished or not) |
| 1314 | fetchGame: function(callback) { |
| 1315 | if (Number.isInteger(this.gameRef) || !isNaN(parseInt(this.gameRef))) { |
| 1316 | // corr games identifiers are integers |
| 1317 | ajax( |
| 1318 | "/games", |
| 1319 | "GET", |
| 1320 | { |
| 1321 | data: { gid: this.gameRef }, |
| 1322 | success: (res) => { |
| 1323 | res.game.moves.forEach(m => { |
| 1324 | m.squares = JSON.parse(m.squares); |
| 1325 | }); |
| 1326 | callback(res.game); |
| 1327 | } |
| 1328 | } |
| 1329 | ); |
| 1330 | } |
| 1331 | else if (!!this.gameRef.match(/^i/)) |
| 1332 | // Game import (maybe remote) |
| 1333 | ImportgameStorage.get(this.gameRef, callback); |
| 1334 | else |
| 1335 | // Local live game (or remote) |
| 1336 | GameStorage.get(this.gameRef, callback); |
| 1337 | }, |
| 1338 | re_setClocks: function() { |
| 1339 | this.virtualClocks = this.game.clocks.map(s => ppt(s).split(':')); |
| 1340 | if (this.game.moves.length < 2 || this.game.score != "*") { |
| 1341 | // 1st move not completed yet, or game over: freeze time |
| 1342 | return; |
| 1343 | } |
| 1344 | const currentTurn = this.vr.turn; |
| 1345 | const currentMovesCount = this.game.moves.length; |
| 1346 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
| 1347 | this.clockUpdate = setInterval( |
| 1348 | () => { |
| 1349 | if ( |
| 1350 | this.game.clocks[colorIdx] < 0 || |
| 1351 | this.game.moves.length > currentMovesCount || |
| 1352 | this.game.score != "*" |
| 1353 | ) { |
| 1354 | clearInterval(this.clockUpdate); |
| 1355 | this.clockUpdate = null; |
| 1356 | if (this.game.clocks[colorIdx] < 0) |
| 1357 | this.gameOver( |
| 1358 | currentTurn == "w" ? "0-1" : "1-0", |
| 1359 | "Time" |
| 1360 | ); |
| 1361 | } else { |
| 1362 | this.$set( |
| 1363 | this.virtualClocks, |
| 1364 | colorIdx, |
| 1365 | ppt(Math.max(0, --this.game.clocks[colorIdx])).split(':') |
| 1366 | ); |
| 1367 | } |
| 1368 | }, |
| 1369 | 1000 |
| 1370 | ); |
| 1371 | }, |
| 1372 | // Update variables and storage after a move: |
| 1373 | processMove: function(move, data) { |
| 1374 | if (this.game.type == "import") |
| 1375 | // Shouldn't receive any messages in this mode: |
| 1376 | return; |
| 1377 | if (!data) data = {}; |
| 1378 | const moveCol = this.vr.turn; |
| 1379 | const colorIdx = ["w", "b"].indexOf(moveCol); |
| 1380 | const nextIdx = 1 - colorIdx; |
| 1381 | const doProcessMove = () => { |
| 1382 | const origMovescount = this.game.moves.length; |
| 1383 | // The move is (about to be) played: stop clock |
| 1384 | clearInterval(this.clockUpdate); |
| 1385 | this.clockUpdate = null; |
| 1386 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
| 1387 | if (this.drawOffer == "received") |
| 1388 | // I refuse draw |
| 1389 | this.drawOffer = ""; |
| 1390 | if (this.game.type == "live" && origMovescount >= 2) { |
| 1391 | this.game.clocks[colorIdx] += this.game.increment; |
| 1392 | // For a correct display in casqe of disconnected opponent: |
| 1393 | this.$set( |
| 1394 | this.virtualClocks, |
| 1395 | colorIdx, |
| 1396 | ppt(this.game.clocks[colorIdx]).split(':') |
| 1397 | ); |
| 1398 | GameStorage.update(this.gameRef, { |
| 1399 | // It's not my turn anymore: |
| 1400 | initime: null |
| 1401 | }); |
| 1402 | } |
| 1403 | } |
| 1404 | // Update current game object: |
| 1405 | playMove(move, this.vr); |
| 1406 | if (!data.score) |
| 1407 | // Received move, score is computed in BaseGame, but maybe not yet. |
| 1408 | // ==> Compute it here, although this is redundant (TODO) |
| 1409 | data.score = this.vr.getCurrentScore(); |
| 1410 | if (data.score != "*") this.gameOver(data.score); |
| 1411 | this.game.moves.push(move); |
| 1412 | this.game.fen = this.vr.getFen(); |
| 1413 | if (this.game.type == "corr") { |
| 1414 | // In corr games, just reset clock to mainTime: |
| 1415 | this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime; |
| 1416 | } |
| 1417 | // If repetition detected, consider that a draw offer was received: |
| 1418 | const fenObj = this.vr.getFenForRepeat(); |
| 1419 | this.repeat[fenObj] = |
| 1420 | !!this.repeat[fenObj] |
| 1421 | ? this.repeat[fenObj] + 1 |
| 1422 | : 1; |
| 1423 | if (this.repeat[fenObj] >= 3) this.drawOffer = "threerep"; |
| 1424 | else if (this.drawOffer == "threerep") this.drawOffer = ""; |
| 1425 | if (!!this.game.mycolor && !data.receiveMyMove) { |
| 1426 | // NOTE: 'var' to see that variable outside this block |
| 1427 | var filtered_move = getFilteredMove(move); |
| 1428 | } |
| 1429 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
| 1430 | // Notify turn on MyGames page: |
| 1431 | this.notifyMyGames( |
| 1432 | "turn", |
| 1433 | { |
| 1434 | gid: this.gameRef, |
| 1435 | turn: this.vr.turn |
| 1436 | } |
| 1437 | ); |
| 1438 | } |
| 1439 | // Since corr games are stored at only one location, update should be |
| 1440 | // done only by one player for each move: |
| 1441 | if ( |
| 1442 | this.game.type == "live" && |
| 1443 | !!this.game.mycolor && |
| 1444 | moveCol != this.game.mycolor && |
| 1445 | this.game.moves.length >= 2 |
| 1446 | ) { |
| 1447 | // Receive a move: update initime |
| 1448 | this.game.initime = Date.now(); |
| 1449 | GameStorage.update(this.gameRef, { |
| 1450 | // It's my turn now! |
| 1451 | initime: this.game.initime |
| 1452 | }); |
| 1453 | } |
| 1454 | if ( |
| 1455 | !!this.game.mycolor && |
| 1456 | !data.receiveMyMove && |
| 1457 | (this.game.type == "live" || moveCol == this.game.mycolor) |
| 1458 | ) { |
| 1459 | let drawCode = ""; |
| 1460 | switch (this.drawOffer) { |
| 1461 | case "threerep": |
| 1462 | drawCode = "t"; |
| 1463 | break; |
| 1464 | case "sent": |
| 1465 | drawCode = this.game.mycolor; |
| 1466 | break; |
| 1467 | case "received": |
| 1468 | drawCode = V.GetOppCol(this.game.mycolor); |
| 1469 | break; |
| 1470 | } |
| 1471 | if (this.game.type == "corr") { |
| 1472 | // corr: only move, fen and score |
| 1473 | this.updateCorrGame({ |
| 1474 | fen: this.game.fen, |
| 1475 | move: { |
| 1476 | squares: filtered_move, |
| 1477 | idx: origMovescount |
| 1478 | }, |
| 1479 | // Code "n" for "None" to force reset (otherwise it's ignored) |
| 1480 | drawOffer: drawCode || "n" |
| 1481 | }); |
| 1482 | } |
| 1483 | else { |
| 1484 | const updateStorage = () => { |
| 1485 | GameStorage.update(this.gameRef, { |
| 1486 | fen: this.game.fen, |
| 1487 | move: filtered_move, |
| 1488 | moveIdx: origMovescount, |
| 1489 | clocks: this.game.clocks, |
| 1490 | drawOffer: drawCode |
| 1491 | }); |
| 1492 | }; |
| 1493 | // The active tab can update storage immediately |
| 1494 | if (this.focus) updateStorage(); |
| 1495 | // Small random delay otherwise |
| 1496 | else setTimeout(updateStorage, 500 + 1000 * Math.random()); |
| 1497 | } |
| 1498 | } |
| 1499 | // Send move ("newmove" event) to people in the room (if our turn) |
| 1500 | if (moveCol == this.game.mycolor && !data.receiveMyMove) { |
| 1501 | let sendMove = { |
| 1502 | move: filtered_move, |
| 1503 | index: origMovescount, |
| 1504 | // color is required to check if this is my move |
| 1505 | // (if several tabs opened) |
| 1506 | color: moveCol, |
| 1507 | cancelDrawOffer: this.drawOffer == "" |
| 1508 | }; |
| 1509 | if (this.game.type == "live") |
| 1510 | sendMove["clock"] = this.game.clocks[colorIdx]; |
| 1511 | // (Live) Clocks will re-start when the opponent pingback arrive |
| 1512 | this.opponentGotMove = false; |
| 1513 | this.send("newmove", {data: sendMove}); |
| 1514 | // If the opponent doesn't reply gotmove soon enough, re-send move: |
| 1515 | // Do this at most 2 times, because mpore would mean network issues, |
| 1516 | // opponent would then be expected to disconnect/reconnect. |
| 1517 | let counter = 1; |
| 1518 | const currentUrl = document.location.href; |
| 1519 | this.retrySendmove = setInterval( |
| 1520 | () => { |
| 1521 | if ( |
| 1522 | counter >= 3 || |
| 1523 | this.opponentGotMove || |
| 1524 | document.location.href != currentUrl //page change |
| 1525 | ) { |
| 1526 | clearInterval(this.retrySendmove); |
| 1527 | return; |
| 1528 | } |
| 1529 | const oppsid = this.getOppsid(); |
| 1530 | if (!oppsid) |
| 1531 | // Opponent is disconnected: he'll ask last state |
| 1532 | clearInterval(this.retrySendmove); |
| 1533 | else { |
| 1534 | this.send("newmove", { data: sendMove, target: oppsid }); |
| 1535 | counter++; |
| 1536 | } |
| 1537 | }, |
| 1538 | 1500 |
| 1539 | ); |
| 1540 | } |
| 1541 | else |
| 1542 | // Not my move or I'm an observer: just start other player's clock |
| 1543 | this.re_setClocks(); |
| 1544 | }; |
| 1545 | if ( |
| 1546 | this.game.type == "corr" && |
| 1547 | moveCol == this.game.mycolor && |
| 1548 | !data.receiveMyMove |
| 1549 | ) { |
| 1550 | let boardDiv = document.querySelector(".game"); |
| 1551 | const afterSetScore = () => { |
| 1552 | doProcessMove(); |
| 1553 | if (this.st.settings.gotonext && this.nextIds.length > 0) |
| 1554 | this.showNextGame(); |
| 1555 | else { |
| 1556 | // The board might have been hidden: |
| 1557 | if (boardDiv.style.visibility == "hidden") |
| 1558 | boardDiv.style.visibility = "visible"; |
| 1559 | if (data.score == "*") this.re_setClocks(); |
| 1560 | } |
| 1561 | }; |
| 1562 | let el = document.querySelector("#buttonsConfirm > .acceptBtn"); |
| 1563 | // We may play several moves in a row: in case of, remove listener: |
| 1564 | let elClone = el.cloneNode(true); |
| 1565 | el.parentNode.replaceChild(elClone, el); |
| 1566 | elClone.addEventListener( |
| 1567 | "click", |
| 1568 | () => { |
| 1569 | document.getElementById("modalConfirm").checked = false; |
| 1570 | if (!!data.score && data.score != "*") |
| 1571 | // Set score first |
| 1572 | this.gameOver(data.score, null, afterSetScore); |
| 1573 | else afterSetScore(); |
| 1574 | } |
| 1575 | ); |
| 1576 | // PlayOnBoard is enough, and more appropriate for Synchrone Chess |
| 1577 | const arMove = (Array.isArray(move) ? move : [move]); |
| 1578 | for (let i = 0; i < arMove.length; i++) |
| 1579 | V.PlayOnBoard(this.vr.board, arMove[i]); |
| 1580 | const position = this.vr.getBaseFen(); |
| 1581 | for (let i = arMove.length - 1; i >= 0; i--) |
| 1582 | V.UndoOnBoard(this.vr.board, arMove[i]); |
| 1583 | if (["all","byrow"].includes(V.ShowMoves)) { |
| 1584 | this.curDiag = getDiagram({ |
| 1585 | position: position, |
| 1586 | orientation: V.CanFlip ? this.game.mycolor : "w" |
| 1587 | }); |
| 1588 | document.querySelector("#confirmDiv > .card").style.width = |
| 1589 | boardDiv.offsetWidth + "px"; |
| 1590 | } else { |
| 1591 | // Incomplete information: just ask confirmation |
| 1592 | // Hide the board, because otherwise it could reveal infos |
| 1593 | boardDiv.style.visibility = "hidden"; |
| 1594 | this.moveNotation = getFullNotation(move); |
| 1595 | } |
| 1596 | document.getElementById("modalConfirm").checked = true; |
| 1597 | } |
| 1598 | else { |
| 1599 | // Normal situation |
| 1600 | if (!!data.score && data.score != "*") |
| 1601 | this.gameOver(data.score, null, doProcessMove); |
| 1602 | else doProcessMove(); |
| 1603 | } |
| 1604 | }, |
| 1605 | cancelMove: function() { |
| 1606 | let boardDiv = document.querySelector(".game"); |
| 1607 | if (boardDiv.style.visibility == "hidden") |
| 1608 | boardDiv.style.visibility = "visible"; |
| 1609 | document.getElementById("modalConfirm").checked = false; |
| 1610 | this.$refs["basegame"].cancelLastMove(); |
| 1611 | }, |
| 1612 | // In corr games, callback to change page only after score is set: |
| 1613 | gameOver: function(score, scoreMsg, callback) { |
| 1614 | this.game.score = score; |
| 1615 | if (!scoreMsg) scoreMsg = getScoreMessage(score); |
| 1616 | this.game.scoreMsg = scoreMsg; |
| 1617 | document.getElementById("modalRules").checked = false; |
| 1618 | // Display result in a un-missable way: |
| 1619 | document.getElementById("modalScore").checked = true; |
| 1620 | this.$set(this.game, "scoreMsg", scoreMsg); |
| 1621 | const myIdx = this.game.players.findIndex(p => { |
| 1622 | return ( |
| 1623 | p.sid == this.st.user.sid || |
| 1624 | (!!p.name && p.id == this.st.user.id) |
| 1625 | ); |
| 1626 | }); |
| 1627 | if (myIdx >= 0) { |
| 1628 | // OK, I play in this game |
| 1629 | const scoreObj = { |
| 1630 | score: score, |
| 1631 | scoreMsg: scoreMsg |
| 1632 | }; |
| 1633 | if (this.game.type == "live") { |
| 1634 | GameStorage.update(this.gameRef, scoreObj); |
| 1635 | // Notify myself locally if I'm elsewhere: |
| 1636 | if (!this.focus) { |
| 1637 | notify( |
| 1638 | "Game over", |
| 1639 | { body: score + " : " + scoreMsg } |
| 1640 | ); |
| 1641 | } |
| 1642 | if (!!callback) callback(); |
| 1643 | } |
| 1644 | else this.updateCorrGame(scoreObj, callback); |
| 1645 | // Notify the score to main Hall. |
| 1646 | // TODO: only one player (currently double send) |
| 1647 | this.send("result", { gid: this.game.id, score: score }); |
| 1648 | // Also to MyGames page (TODO: doubled as well...) |
| 1649 | this.notifyMyGames( |
| 1650 | "score", |
| 1651 | { |
| 1652 | gid: this.gameRef, |
| 1653 | score: score |
| 1654 | } |
| 1655 | ); |
| 1656 | } |
| 1657 | else if (!!callback) callback(); |
| 1658 | } |
| 1659 | } |
| 1660 | }; |
| 1661 | </script> |
| 1662 | |
| 1663 | <style lang="sass" scoped> |
| 1664 | #scoreDiv > .card, #rematchDiv > .card |
| 1665 | padding: 10px 0 |
| 1666 | max-width: 430px |
| 1667 | |
| 1668 | #rulesDiv > .card |
| 1669 | padding: 5px 0 |
| 1670 | max-width: 50% |
| 1671 | max-height: 100% |
| 1672 | @media screen and (max-width: 1500px) |
| 1673 | max-width: 67% |
| 1674 | @media screen and (max-width: 1024px) |
| 1675 | max-width: 85% |
| 1676 | @media screen and (max-width: 767px) |
| 1677 | max-width: 100% |
| 1678 | |
| 1679 | p.score-section |
| 1680 | margin: 0 |
| 1681 | font-size: 1.3em |
| 1682 | span.score |
| 1683 | font-weight: bold |
| 1684 | |
| 1685 | .connected |
| 1686 | background-color: lightgreen |
| 1687 | |
| 1688 | #participants |
| 1689 | margin-left: 5px |
| 1690 | |
| 1691 | .anonymous |
| 1692 | color: grey |
| 1693 | font-style: italic |
| 1694 | |
| 1695 | #playersInfo > p |
| 1696 | margin: 0 |
| 1697 | |
| 1698 | @media screen and (max-width: 767px) |
| 1699 | .game |
| 1700 | width: 100% |
| 1701 | |
| 1702 | #actions |
| 1703 | display: inline-block |
| 1704 | margin: 0 |
| 1705 | |
| 1706 | button |
| 1707 | display: inline-block |
| 1708 | margin: 0 |
| 1709 | display: inline-flex |
| 1710 | img |
| 1711 | height: 22px |
| 1712 | display: flex |
| 1713 | @media screen and (max-width: 767px) |
| 1714 | height: 18px |
| 1715 | |
| 1716 | #aboveBoard |
| 1717 | text-align: center |
| 1718 | |
| 1719 | .variant-cadence |
| 1720 | padding-right: 10px |
| 1721 | |
| 1722 | .variant-name |
| 1723 | font-weight: bold |
| 1724 | padding-right: 10px |
| 1725 | |
| 1726 | span#nextGame |
| 1727 | background-color: #edda99 |
| 1728 | cursor: pointer |
| 1729 | display: inline-block |
| 1730 | margin-right: 10px |
| 1731 | |
| 1732 | span.separator |
| 1733 | display: inline-block |
| 1734 | margin: 0 |
| 1735 | padding: 0 |
| 1736 | width: 10px |
| 1737 | |
| 1738 | span.name |
| 1739 | font-size: 1.5rem |
| 1740 | @media screen and (max-width: 767px) |
| 1741 | font-size: 1.2rem |
| 1742 | padding: 0 3px |
| 1743 | |
| 1744 | span.time |
| 1745 | font-size: 2rem |
| 1746 | @media screen and (max-width: 767px) |
| 1747 | font-size: 1.5rem |
| 1748 | display: inline-block |
| 1749 | .time-left |
| 1750 | margin-left: 10px |
| 1751 | .time-right |
| 1752 | margin-left: 5px |
| 1753 | .time-separator |
| 1754 | margin-left: 5px |
| 1755 | position: relative |
| 1756 | top: -1px |
| 1757 | |
| 1758 | span.yourturn |
| 1759 | color: #831B1B |
| 1760 | .time-separator |
| 1761 | animation: blink-animation 2s steps(3, start) infinite |
| 1762 | @keyframes blink-animation |
| 1763 | to |
| 1764 | visibility: hidden |
| 1765 | |
| 1766 | .split-names |
| 1767 | display: inline-block |
| 1768 | margin: 0 15px |
| 1769 | |
| 1770 | #chatWrap > .card |
| 1771 | padding-top: 20px |
| 1772 | max-width: 767px |
| 1773 | border: none |
| 1774 | |
| 1775 | #confirmDiv > .card |
| 1776 | max-width: 767px |
| 1777 | max-height: 100% |
| 1778 | |
| 1779 | .draw-sent, .draw-sent:hover |
| 1780 | background-color: lightyellow |
| 1781 | |
| 1782 | .draw-received, .draw-received:hover |
| 1783 | background-color: #73C6B6 |
| 1784 | |
| 1785 | .draw-threerep, .draw-threerep:hover |
| 1786 | background-color: #D2B4DE |
| 1787 | |
| 1788 | .rematch-sent, .rematch-sent:hover |
| 1789 | background-color: lightyellow |
| 1790 | |
| 1791 | .rematch-received, .rematch-received:hover |
| 1792 | background-color: #48C9B0 |
| 1793 | |
| 1794 | .somethingnew |
| 1795 | background-color: #D2B4DE |
| 1796 | |
| 1797 | .diagram |
| 1798 | margin: 0 auto |
| 1799 | width: 100% |
| 1800 | |
| 1801 | #buttonsConfirm |
| 1802 | margin: 0 |
| 1803 | & > button > span |
| 1804 | width: 100% |
| 1805 | text-align: center |
| 1806 | |
| 1807 | button.acceptBtn |
| 1808 | background-color: lightgreen |
| 1809 | button.refuseBtn |
| 1810 | background-color: red |
| 1811 | |
| 1812 | a#variantNameInGame |
| 1813 | color: var(--card-fore-color) |
| 1814 | text-align: center |
| 1815 | font-weight: bold |
| 1816 | font-size: calc(1rem * var(--heading-ratio)) |
| 1817 | line-height: 1.2 |
| 1818 | margin: calc(1.5 * var(--universal-margin)) |
| 1819 | </style> |
| 1820 | |
| 1821 | <style lang="sass"> |
| 1822 | @import "@/styles/_rules.sass" |
| 1823 | @import "@/styles/_board_squares_img.sass" |
| 1824 | </style> |