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