| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalInfo.modal(type="checkbox") |
| 4 | div(role="dialog" aria-labelledby="infoMessage") |
| 5 | .card.smallpad.small-modal.text-center |
| 6 | label.modal-close(for="modalInfo") |
| 7 | h3#infoMessage.section |
| 8 | p(v-html="infoMessage") |
| 9 | input#modalNewgame.modal(type="checkbox") |
| 10 | div(role="dialog" aria-labelledby="titleFenedit") |
| 11 | .card.smallpad |
| 12 | label#closeNewgame.modal-close(for="modalNewgame") |
| 13 | fieldset |
| 14 | label(for="selectVariant") {{ st.tr["Variant"] }} |
| 15 | select#selectVariant(v-model="newchallenge.vid") |
| 16 | option(v-for="v in st.variants" :value="v.id") {{ v.name }} |
| 17 | fieldset |
| 18 | label(for="timeControl") {{ st.tr["Time control"] }} |
| 19 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
| 20 | placeholder="3m+2s, 1h+30s, 7d+1d ...") |
| 21 | fieldset(v-if="st.user.id > 0") |
| 22 | label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }} |
| 23 | input#selectPlayers(type="text" v-model="newchallenge.to") |
| 24 | fieldset(v-if="st.user.id > 0") |
| 25 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
| 26 | input#inputFen(type="text" v-model="newchallenge.fen") |
| 27 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
| 28 | .row |
| 29 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 30 | button(onClick="doClick('modalNewgame')") New game |
| 31 | .row |
| 32 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 33 | .collapse |
| 34 | input#challengeSection(type="radio" checked aria-hidden="true" name="accordion") |
| 35 | label(for="challengeSection" aria-hidden="true") Challenges |
| 36 | div |
| 37 | .button-group |
| 38 | button(@click="cdisplay='live'") Live Challenges |
| 39 | button(@click="cdisplay='corr'") Correspondance challenges |
| 40 | ChallengeList(v-show="cdisplay=='live'" |
| 41 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") |
| 42 | ChallengeList(v-show="cdisplay=='corr'" |
| 43 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") |
| 44 | input#peopleSection(type="radio" aria-hidden="true" name="accordion") |
| 45 | label(for="peopleSection" aria-hidden="true") People |
| 46 | div |
| 47 | .button-group |
| 48 | button(@click="pdisplay='players'") Players |
| 49 | button(@click="pdisplay='chat'") Chat |
| 50 | #players(v-show="pdisplay=='players'") |
| 51 | h3 Online players |
| 52 | .player(v-for="p in uniquePlayers" @click="tryChallenge(p)" |
| 53 | :class="{anonymous: !!p.count}" |
| 54 | ) |
| 55 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} |
| 56 | #chat(v-show="pdisplay=='chat'") |
| 57 | h3 Chat (TODO) |
| 58 | input#gameSection(type="radio" aria-hidden="true" name="accordion") |
| 59 | label(for="gameSection" aria-hidden="true") Games |
| 60 | div |
| 61 | .button-group |
| 62 | button(@click="gdisplay='live'") Live games |
| 63 | button(@click="gdisplay='corr'") Correspondance games |
| 64 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" |
| 65 | @show-game="showGame") |
| 66 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" |
| 67 | @show-game="showGame") |
| 68 | </template> |
| 69 | |
| 70 | <script> |
| 71 | import { store } from "@/store"; |
| 72 | import { checkChallenge } from "@/data/challengeCheck"; |
| 73 | import { ArrayFun } from "@/utils/array"; |
| 74 | import { ajax } from "@/utils/ajax"; |
| 75 | import { getRandString, shuffle } from "@/utils/alea"; |
| 76 | import GameList from "@/components/GameList.vue"; |
| 77 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 78 | import { GameStorage } from "@/utils/gameStorage"; |
| 79 | export default { |
| 80 | name: "my-hall", |
| 81 | components: { |
| 82 | GameList, |
| 83 | ChallengeList, |
| 84 | }, |
| 85 | data: function () { |
| 86 | return { |
| 87 | st: store.state, |
| 88 | cdisplay: "live", //or corr |
| 89 | pdisplay: "players", //or chat |
| 90 | gdisplay: "live", |
| 91 | games: [], |
| 92 | challenges: [], |
| 93 | people: [], //(all) online players |
| 94 | infoMessage: "", |
| 95 | newchallenge: { |
| 96 | fen: "", |
| 97 | vid: 0, |
| 98 | to: "", //name of challenged player (if any) |
| 99 | timeControl: "", //"2m+2s" ...etc |
| 100 | }, |
| 101 | }; |
| 102 | }, |
| 103 | watch: { |
| 104 | // st.variants changes only once, at loading from [] to [...] |
| 105 | "st.variants": function(variantArray) { |
| 106 | // Set potential challenges and games variant names: |
| 107 | this.challenges.forEach(c => { |
| 108 | if (c.vname == "") |
| 109 | c.vname = this.getVname(c.vid); |
| 110 | }); |
| 111 | this.games.forEach(g => { |
| 112 | if (g.vname == "") |
| 113 | g.vname = this.getVname(g.vid) |
| 114 | }); |
| 115 | }, |
| 116 | }, |
| 117 | computed: { |
| 118 | uniquePlayers: function() { |
| 119 | // Show e.g. "@nonymous (5)", and do nothing on click on anonymous |
| 120 | let anonymous = {id:0, name:"@nonymous", count:0}; |
| 121 | let playerList = []; |
| 122 | this.people.forEach(p => { |
| 123 | if (p.id > 0) |
| 124 | playerList.push(p); |
| 125 | else |
| 126 | anonymous.count++; |
| 127 | }); |
| 128 | if (anonymous.count > 0) |
| 129 | playerList.push(anonymous); |
| 130 | return playerList; |
| 131 | }, |
| 132 | }, |
| 133 | created: function() { |
| 134 | // Always add myself to players' list |
| 135 | const my = this.st.user; |
| 136 | this.people.push({sid:my.sid, id:my.id, name:my.name}); |
| 137 | // Retrieve live challenge (not older than 30 minute) if any: |
| 138 | const chall = JSON.parse(localStorage.getItem("challenge") || "false"); |
| 139 | if (!!chall) |
| 140 | { |
| 141 | if ((Date.now() - chall.added)/1000 <= 30*60) |
| 142 | this.challenges.push(chall); |
| 143 | else |
| 144 | localStorage.removeItem("challenge"); |
| 145 | } |
| 146 | // Ask server for current corr games (all but mines) |
| 147 | ajax( |
| 148 | "/games", |
| 149 | "GET", |
| 150 | {uid: this.st.user.id, excluded: true}, |
| 151 | response => { |
| 152 | this.games = this.games.concat(response.games.map(g => { |
| 153 | const type = this.classifyObject(g); |
| 154 | const vname = this.getVname(g.vid); |
| 155 | return Object.assign({}, g, {type: type, vname: vname}); |
| 156 | })); |
| 157 | } |
| 158 | ); |
| 159 | // Also ask for corr challenges (open + sent to me) |
| 160 | ajax( |
| 161 | "/challenges", |
| 162 | "GET", |
| 163 | {uid: this.st.user.id}, |
| 164 | response => { |
| 165 | // Gather all senders names, and then retrieve full identity: |
| 166 | // (TODO [perf]: some might be online...) |
| 167 | const uids = response.challenges.map(c => { return c.uid }); |
| 168 | ajax("/users", |
| 169 | "GET", |
| 170 | { ids: uids.join(",") }, |
| 171 | response2 => { |
| 172 | let names = {}; |
| 173 | response2.users.forEach(u => {names[u.id] = u.name}); |
| 174 | this.challenges = this.challenges.concat( |
| 175 | response.challenges.map(c => { |
| 176 | // (just players names in fact) |
| 177 | const from = {name: names[c.uid], id: c.uid}; |
| 178 | const type = this.classifyObject(c); |
| 179 | const vname = this.getVname(c.vid); |
| 180 | return Object.assign({}, c, {type: type, vname: vname, from: from}); |
| 181 | }) |
| 182 | ) |
| 183 | } |
| 184 | ); |
| 185 | } |
| 186 | ); |
| 187 | // 0.1] Ask server for room composition: |
| 188 | const funcPollClients = () => { |
| 189 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
| 190 | }; |
| 191 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
| 192 | funcPollClients(); |
| 193 | else //socket not ready yet (initial loading) |
| 194 | this.st.conn.onopen = funcPollClients; |
| 195 | this.st.conn.onmessage = this.socketMessageListener; |
| 196 | const socketCloseListener = () => { |
| 197 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
| 198 | this.st.conn.addEventListener('message', this.socketMessageListener); |
| 199 | this.st.conn.addEventListener('close', socketCloseListener); |
| 200 | }; |
| 201 | this.st.conn.onclose = socketCloseListener; |
| 202 | }, |
| 203 | methods: { |
| 204 | // Helpers: |
| 205 | filterChallenges: function(type) { |
| 206 | return this.challenges.filter(c => c.type == type); |
| 207 | }, |
| 208 | filterGames: function(type) { |
| 209 | return this.games.filter(g => g.type == type); |
| 210 | }, |
| 211 | classifyObject: function(o) { //challenge or game |
| 212 | // Heuristic: should work for most cases... (TODO) |
| 213 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
| 214 | }, |
| 215 | showGame: function(g) { |
| 216 | // NOTE: we are an observer, since only games I don't play are shown here |
| 217 | // ==> Moves sent by connected remote player(s) if live game |
| 218 | let url = "/game/" + g.id; |
| 219 | if (g.type == "live") |
| 220 | { |
| 221 | const remotes = g.players.filter(p => this.people.some(pl => pl.sid == p.sid)); |
| 222 | const rIdx = (remotes.length == 1 ? 0 : Math.floor(Math.random()*2)); |
| 223 | url += "?rid=" + remotes[rIdx].sid; |
| 224 | } |
| 225 | this.$router.push(url); |
| 226 | }, |
| 227 | // TODO: ...filter(...)[0].name, one-line, just remove this function |
| 228 | getVname: function(vid) { |
| 229 | const vIdx = this.st.variants.findIndex(v => v.id == vid); |
| 230 | return vIdx >= 0 ? this.st.variants[vIdx].name : ""; |
| 231 | }, |
| 232 | getSid: function(pname) { |
| 233 | const pIdx = this.people.findIndex(pl => pl.name == pname); |
| 234 | return (pIdx === -1 ? null : this.people[pIdx].sid); |
| 235 | }, |
| 236 | getPname: function(sid) { |
| 237 | const pIdx = this.people.findIndex(pl => pl.sid == sid); |
| 238 | return (pIdx === -1 ? null : this.people[pIdx].name); |
| 239 | }, |
| 240 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
| 241 | const doSend = (code, obj, sid) => { |
| 242 | this.st.conn.send(JSON.stringify(Object.assign( |
| 243 | {}, |
| 244 | {code: code}, |
| 245 | obj, |
| 246 | {target: sid} |
| 247 | ))); |
| 248 | }; |
| 249 | if (!!to) |
| 250 | { |
| 251 | // Challenge with targeted players |
| 252 | const targetSid = this.getSid(to); |
| 253 | if (!targetSid) |
| 254 | { |
| 255 | if (!!warnDisconnected) |
| 256 | alert("Warning: " + pname + " is not connected"); |
| 257 | } |
| 258 | else |
| 259 | doSend(code, obj, targetSid); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | // Open challenge: send to all connected players (except us) |
| 264 | this.people.forEach(p => { |
| 265 | if (p.sid != this.st.user.sid) //only sid is always set |
| 266 | doSend(code, obj, p.sid); |
| 267 | }); |
| 268 | } |
| 269 | }, |
| 270 | // Messaging center: |
| 271 | socketMessageListener: function(msg) { |
| 272 | const data = JSON.parse(msg.data); |
| 273 | switch (data.code) |
| 274 | { |
| 275 | // 0.2] Receive clients list (just socket IDs) |
| 276 | case "pollclients": |
| 277 | { |
| 278 | data.sockIds.forEach(sid => { |
| 279 | this.people.push({sid:sid, id:0, name:""}); |
| 280 | // Ask identity, challenges and game(s) |
| 281 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 282 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
| 283 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
| 284 | }); |
| 285 | break; |
| 286 | } |
| 287 | case "askidentity": |
| 288 | { |
| 289 | // Request for identification: reply if I'm not anonymous |
| 290 | if (this.st.user.id > 0) |
| 291 | { |
| 292 | this.st.conn.send(JSON.stringify( |
| 293 | // people[0] instead of st.user to avoid sending email |
| 294 | {code:"identity", user:this.people[0], target:data.from})); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case "askchallenge": |
| 299 | { |
| 300 | // Send my current live challenge (if any) |
| 301 | const cIdx = this.challenges |
| 302 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
| 303 | if (cIdx >= 0) |
| 304 | { |
| 305 | const c = this.challenges[cIdx]; |
| 306 | const myChallenge = |
| 307 | { |
| 308 | // Minimal challenge informations: (from not required) |
| 309 | id: c.id, |
| 310 | to: c.to, |
| 311 | fen: c.fen, |
| 312 | vid: c.vid, |
| 313 | timeControl: c.timeControl |
| 314 | }; |
| 315 | this.st.conn.send(JSON.stringify({code:"challenge", |
| 316 | chall:myChallenge, target:data.from})); |
| 317 | } |
| 318 | break; |
| 319 | } |
| 320 | case "askgame": |
| 321 | { |
| 322 | // Send my current live game (if any) |
| 323 | GameStorage.getCurrent((game) => { |
| 324 | if (!!game) |
| 325 | { |
| 326 | const myGame = |
| 327 | { |
| 328 | // Minimal game informations: |
| 329 | id: game.id, |
| 330 | players: game.players.map(p => p.name), |
| 331 | vid: game.vid, |
| 332 | timeControl: game.timeControl, |
| 333 | }; |
| 334 | this.st.conn.send(JSON.stringify({code:"game", |
| 335 | game:myGame, target:data.from})); |
| 336 | } |
| 337 | }); |
| 338 | break; |
| 339 | } |
| 340 | case "identity": |
| 341 | { |
| 342 | const pIdx = this.people.findIndex(p => p.sid == data.user.sid); |
| 343 | this.people[pIdx].id = data.user.id; |
| 344 | this.people[pIdx].name = data.user.name; |
| 345 | break; |
| 346 | } |
| 347 | case "challenge": |
| 348 | { |
| 349 | // Receive challenge from some player (+sid) |
| 350 | let newChall = data.chall; |
| 351 | newChall.type = this.classifyObject(data.chall); |
| 352 | const pIdx = this.people.findIndex(p => p.sid == data.from); |
| 353 | newChall.from = this.people[pIdx]; //may be anonymous |
| 354 | newChall.added = Date.now(); //TODO: this is reception timestamp, not creation |
| 355 | newChall.vname = this.getVname(newChall.vid); |
| 356 | this.challenges.push(newChall); |
| 357 | break; |
| 358 | } |
| 359 | case "game": |
| 360 | { |
| 361 | // Receive game from some player (+sid) |
| 362 | // NOTE: it may be correspondance (if newgame while we are connected) |
| 363 | if (!this.games.some(g => g.id == data.game.id)) //ignore duplicates |
| 364 | { |
| 365 | let newGame = data.game; |
| 366 | newGame.type = this.classifyObject(data.game); |
| 367 | newGame.vname = this.getVname(data.game.vid); |
| 368 | newGame.rid = data.from; |
| 369 | newGame.score = "*"; |
| 370 | this.games.push(newGame); |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | case "newgame": |
| 375 | { |
| 376 | // TODO: next line required ?! |
| 377 | //ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 378 | // New game just started: data contain all information |
| 379 | if (this.classifyObject(data.gameInfo) == "live") |
| 380 | this.startNewGame(data.gameInfo); |
| 381 | else |
| 382 | { |
| 383 | this.infoMessage = "New game started: " + |
| 384 | "<a href='/game/" + data.gameInfo.gameId + "'>" + |
| 385 | "/game/" + data.gameInfo.gameId + "</a>"; |
| 386 | let modalBox = document.getElementById("modalInfo"); |
| 387 | modalBox.checked = true; |
| 388 | setTimeout(() => { modalBox.checked = false; }, 3000); |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | case "refusechallenge": |
| 393 | { |
| 394 | alert(this.getPname(data.from) + " declined your challenge"); |
| 395 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 396 | break; |
| 397 | } |
| 398 | case "deletechallenge": |
| 399 | { |
| 400 | // NOTE: the challenge may be already removed |
| 401 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 402 | localStorage.removeItem("challenge"); //in case of |
| 403 | break; |
| 404 | } |
| 405 | case "connect": |
| 406 | { |
| 407 | this.people.push({name:"", id:0, sid:data.sid}); |
| 408 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); |
| 409 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid})); |
| 410 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid})); |
| 411 | break; |
| 412 | } |
| 413 | case "disconnect": |
| 414 | { |
| 415 | ArrayFun.remove(this.people, p => p.sid == data.sid); |
| 416 | // Also remove all challenges sent by this player: |
| 417 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
| 418 | // And all live games where he plays and no other opponent is online |
| 419 | ArrayFun.remove(this.games, g => |
| 420 | g.type == "live" && (g.players.every(p => p.sid == data.sid |
| 421 | || !this.people.some(pl => pl.sid == p.sid))), "all"); |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | }, |
| 426 | // Challenge lifecycle: |
| 427 | tryChallenge: function(player) { |
| 428 | if (player.id == 0) |
| 429 | return; //anonymous players cannot be challenged |
| 430 | this.newchallenge.to = player.name; |
| 431 | doClick("modalNewgame"); |
| 432 | }, |
| 433 | newChallenge: async function() { |
| 434 | const vname = this.getVname(this.newchallenge.vid); |
| 435 | const vModule = await import("@/variants/" + vname + ".js"); |
| 436 | window.V = vModule.VariantRules; |
| 437 | const error = checkChallenge(this.newchallenge); |
| 438 | if (!!error) |
| 439 | return alert(error); |
| 440 | const ctype = this.classifyObject(this.newchallenge); |
| 441 | if (ctype == "corr" && this.st.user.id <= 0) |
| 442 | return alert("Please log in to play correspondance games"); |
| 443 | // NOTE: "from" information is not required here |
| 444 | let chall = Object.assign({}, this.newchallenge); |
| 445 | const finishAddChallenge = (cid,warnDisconnected) => { |
| 446 | chall.id = cid || "c" + getRandString(); |
| 447 | // Send challenge to peers (if connected) |
| 448 | this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected); |
| 449 | chall.added = Date.now(); |
| 450 | // NOTE: vname and type are redundant (can be deduced from timeControl + vid) |
| 451 | chall.type = ctype; |
| 452 | chall.vname = vname; |
| 453 | chall.from = this.people[0]; //avoid sending email |
| 454 | this.challenges.push(chall); |
| 455 | localStorage.setItem("challenge", JSON.stringify(chall)); |
| 456 | document.getElementById("modalNewgame").checked = false; |
| 457 | }; |
| 458 | const cIdx = this.challenges.findIndex( |
| 459 | c => c.from.sid == this.st.user.sid && c.type == ctype); |
| 460 | if (cIdx >= 0) |
| 461 | { |
| 462 | // Delete current challenge (will be replaced now) |
| 463 | this.sendSomethingTo(this.challenges[cIdx].to, |
| 464 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
| 465 | if (ctype == "corr") |
| 466 | { |
| 467 | ajax( |
| 468 | "/challenges", |
| 469 | "DELETE", |
| 470 | {id: this.challenges[cIdx].id} |
| 471 | ); |
| 472 | } |
| 473 | this.challenges.splice(cIdx, 1); |
| 474 | } |
| 475 | if (ctype == "live") |
| 476 | { |
| 477 | // Live challenges have a random ID |
| 478 | finishAddChallenge(null, "warnDisconnected"); |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | // Correspondance game: send challenge to server |
| 483 | ajax( |
| 484 | "/challenges", |
| 485 | "POST", |
| 486 | { chall: chall }, |
| 487 | response => { finishAddChallenge(response.cid); } |
| 488 | ); |
| 489 | } |
| 490 | }, |
| 491 | clickChallenge: function(c) { |
| 492 | const myChallenge = (c.from.sid == this.st.user.sid //live |
| 493 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr |
| 494 | if (!myChallenge) |
| 495 | { |
| 496 | if (c.type == "corr" && this.st.user.id <= 0) |
| 497 | return alert("Please log in to accept corr challenges"); |
| 498 | c.accepted = true; |
| 499 | if (!!c.to) //c.to == this.st.user.name (connected) |
| 500 | { |
| 501 | // TODO: if special FEN, show diagram after loading variant |
| 502 | c.accepted = confirm("Accept challenge?"); |
| 503 | } |
| 504 | if (c.accepted) |
| 505 | { |
| 506 | c.seat = this.people[0]; //== this.st.user, avoid revealing email |
| 507 | this.launchGame(c); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | this.st.conn.send(JSON.stringify({ |
| 512 | code: "refusechallenge", |
| 513 | cid: c.id, target: c.from.sid})); |
| 514 | } |
| 515 | } |
| 516 | else //my challenge |
| 517 | { |
| 518 | localStorage.removeItem("challenge"); |
| 519 | if (c.type == "corr") |
| 520 | { |
| 521 | ajax( |
| 522 | "/challenges", |
| 523 | "DELETE", |
| 524 | {id: c.id} |
| 525 | ); |
| 526 | } |
| 527 | } |
| 528 | // In (almost) all cases, the challenge is consumed: |
| 529 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 530 | // NOTE: deletechallenge event might be redundant (but it's easier this way) |
| 531 | this.sendSomethingTo((!!c.to ? c.from : null), "deletechallenge", {cid:c.id}); |
| 532 | }, |
| 533 | // NOTE: when launching game, the challenge is already deleted |
| 534 | launchGame: async function(c) { |
| 535 | const vModule = await import("@/variants/" + c.vname + ".js"); |
| 536 | window.V = vModule.VariantRules; |
| 537 | // These game informations will be sent to other players |
| 538 | const gameInfo = |
| 539 | { |
| 540 | gameId: getRandString(), |
| 541 | fen: c.fen || V.GenRandInitFen(), |
| 542 | players: shuffle([c.from, c.seat]), //white then black |
| 543 | vid: c.vid, |
| 544 | timeControl: c.timeControl, |
| 545 | }; |
| 546 | let target = c.from.sid; //may not be defined if corr + offline opp |
| 547 | if (!target) |
| 548 | { |
| 549 | const opponent = this.people.find(p => p.id == c.from.id); |
| 550 | if (!!opponent) |
| 551 | target = opponent.sid |
| 552 | } |
| 553 | if (!!target) //opponent is online |
| 554 | { |
| 555 | this.st.conn.send(JSON.stringify({code:"newgame", |
| 556 | gameInfo:gameInfo, target:target, cid:c.id})); |
| 557 | } |
| 558 | if (c.type == "live") |
| 559 | this.startNewGame(gameInfo); |
| 560 | else //corr: game only on server |
| 561 | { |
| 562 | ajax( |
| 563 | "/games", |
| 564 | "POST", |
| 565 | {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge |
| 566 | response => { this.$router.push("/game/" + response.gameId); } |
| 567 | ); |
| 568 | } |
| 569 | }, |
| 570 | // NOTE: for live games only (corr games start on the server) |
| 571 | startNewGame: function(gameInfo) { |
| 572 | const game = Object.assign({}, gameInfo, { |
| 573 | // (other) Game infos: constant |
| 574 | fenStart: gameInfo.fen, |
| 575 | created: Date.now(), |
| 576 | // Game state (including FEN): will be updated |
| 577 | moves: [], |
| 578 | clocks: [-1, -1], //-1 = unstarted |
| 579 | initime: [0, 0], //initialized later |
| 580 | score: "*", |
| 581 | }); |
| 582 | GameStorage.add(game); |
| 583 | if (this.st.settings.sound >= 1) |
| 584 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 585 | this.$router.push("/game/" + gameInfo.gameId); |
| 586 | }, |
| 587 | }, |
| 588 | }; |
| 589 | </script> |
| 590 | |
| 591 | <style lang="sass"> |
| 592 | // TODO |
| 593 | </style> |