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