| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalInfo.modal(type="checkbox") |
| 4 | div#infoDiv(role="dialog" data-checkbox="modalInfo" 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#newgameDiv(role="dialog" data-checkbox="modalNewgame" |
| 11 | aria-labelledby="titleFenedit") |
| 12 | .card.smallpad(@keyup.enter="newChallenge") |
| 13 | label#closeNewgame.modal-close(for="modalNewgame") |
| 14 | fieldset |
| 15 | label(for="selectVariant") {{ st.tr["Variant"] }} * |
| 16 | select#selectVariant(v-model="newchallenge.vid") |
| 17 | option(v-for="v in st.variants" :value="v.id" |
| 18 | :selected="newchallenge.vid==v.id") |
| 19 | | {{ v.name }} |
| 20 | fieldset |
| 21 | label(for="timeControl") {{ st.tr["Time control"] }} * |
| 22 | div#predefinedTimeControls |
| 23 | button 3+2 |
| 24 | button 5+3 |
| 25 | button 15+5 |
| 26 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
| 27 | placeholder="5+0, 1h+30s, 7d+1d ...") |
| 28 | fieldset(v-if="st.user.id > 0") |
| 29 | label(for="selectPlayers") {{ st.tr["Play with?"] }} |
| 30 | input#selectPlayers(type="text" v-model="newchallenge.to") |
| 31 | fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0") |
| 32 | label(for="inputFen") FEN |
| 33 | input#inputFen(type="text" v-model="newchallenge.fen") |
| 34 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
| 35 | .row |
| 36 | .col-sm-12 |
| 37 | button#newGame(onClick="doClick('modalNewgame')") {{ st.tr["New game"] }} |
| 38 | .row |
| 39 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 40 | div |
| 41 | .button-group |
| 42 | button(@click="(e) => setDisplay('c','live',e)" class="active") |
| 43 | | {{ st.tr["Live challenges"] }} |
| 44 | button(@click="(e) => setDisplay('c','corr',e)") |
| 45 | | {{ st.tr["Correspondance challenges"] }} |
| 46 | ChallengeList(v-show="cdisplay=='live'" |
| 47 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") |
| 48 | ChallengeList(v-show="cdisplay=='corr'" |
| 49 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") |
| 50 | #people |
| 51 | h3.text-center {{ st.tr["Who's there?"] }} |
| 52 | #players |
| 53 | p(v-for="sid in Object.keys(people)" v-if="!!people[sid].name") |
| 54 | span {{ people[sid].name }} |
| 55 | button.player-action( |
| 56 | v-if="people[sid].name != st.user.name" |
| 57 | @click="challOrWatch(sid, $event)" |
| 58 | ) |
| 59 | | {{ st.tr[!!people[sid].gamer ? 'Playing' : 'Available'] }} |
| 60 | p.anonymous @nonymous ({{ anonymousCount }}) |
| 61 | #chat |
| 62 | Chat(:newChat="newChat" @mychat="processChat") |
| 63 | .clearer |
| 64 | div |
| 65 | .button-group |
| 66 | button(@click="(e) => setDisplay('g','live',e)" class="active") |
| 67 | | {{ st.tr["Live games"] }} |
| 68 | button(@click="(e) => setDisplay('g','corr',e)") |
| 69 | | {{ st.tr["Correspondance games"] }} |
| 70 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" |
| 71 | @show-game="showGame") |
| 72 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" |
| 73 | @show-game="showGame") |
| 74 | </template> |
| 75 | |
| 76 | <script> |
| 77 | import { store } from "@/store"; |
| 78 | import { checkChallenge } from "@/data/challengeCheck"; |
| 79 | import { ArrayFun } from "@/utils/array"; |
| 80 | import { ajax } from "@/utils/ajax"; |
| 81 | import { getRandString, shuffle } from "@/utils/alea"; |
| 82 | import Chat from "@/components/Chat.vue"; |
| 83 | import GameList from "@/components/GameList.vue"; |
| 84 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 85 | import { GameStorage } from "@/utils/gameStorage"; |
| 86 | import { processModalClick } from "@/utils/modalClick"; |
| 87 | |
| 88 | export default { |
| 89 | name: "my-hall", |
| 90 | components: { |
| 91 | Chat, |
| 92 | GameList, |
| 93 | ChallengeList, |
| 94 | }, |
| 95 | data: function () { |
| 96 | return { |
| 97 | st: store.state, |
| 98 | cdisplay: "live", //or corr |
| 99 | pdisplay: "players", //or chat |
| 100 | gdisplay: "live", |
| 101 | games: [], |
| 102 | challenges: [], |
| 103 | people: {}, //people in main hall |
| 104 | infoMessage: "", |
| 105 | newchallenge: { |
| 106 | fen: "", |
| 107 | vid: localStorage.getItem("vid") || "", |
| 108 | to: "", //name of challenged player (if any) |
| 109 | timeControl: localStorage.getItem("timeControl") || "", |
| 110 | }, |
| 111 | newChat: "", |
| 112 | }; |
| 113 | }, |
| 114 | watch: { |
| 115 | // st.variants changes only once, at loading from [] to [...] |
| 116 | "st.variants": function(variantArray) { |
| 117 | // Set potential challenges and games variant names: |
| 118 | this.challenges.forEach(c => { |
| 119 | if (c.vname == "") |
| 120 | c.vname = this.getVname(c.vid); |
| 121 | }); |
| 122 | this.games.forEach(g => { |
| 123 | if (g.vname == "") |
| 124 | g.vname = this.getVname(g.vid); |
| 125 | }); |
| 126 | }, |
| 127 | }, |
| 128 | computed: { |
| 129 | anonymousCount: function() { |
| 130 | let count = 0; |
| 131 | Object.values(this.people).forEach(p => { count += (!p.name ? 1 : 0); }); |
| 132 | return count; |
| 133 | }, |
| 134 | }, |
| 135 | created: function() { |
| 136 | // Always add myself to players' list |
| 137 | const my = this.st.user; |
| 138 | this.$set(this.people, my.sid, {id:my.id, name:my.name}); |
| 139 | // Retrieve live challenge (not older than 30 minute) if any: |
| 140 | const chall = JSON.parse(localStorage.getItem("challenge") || "false"); |
| 141 | if (!!chall) |
| 142 | { |
| 143 | // NOTE: a challenge survives 3 minutes, for potential connection issues |
| 144 | if ((Date.now() - chall.added)/1000 <= 3*60) |
| 145 | { |
| 146 | chall.added = Date.now(); //update added time, for next disconnect... |
| 147 | this.challenges.push(chall); |
| 148 | localStorage.setItem("challenge", JSON.stringify(chall)); |
| 149 | } |
| 150 | else |
| 151 | localStorage.removeItem("challenge"); |
| 152 | } |
| 153 | // Ask server for current corr games (all but mines) |
| 154 | ajax( |
| 155 | "/games", |
| 156 | "GET", |
| 157 | {uid: this.st.user.id, excluded: true}, |
| 158 | response => { |
| 159 | this.games = this.games.concat(response.games.map(g => { |
| 160 | const type = this.classifyObject(g); |
| 161 | const vname = this.getVname(g.vid); |
| 162 | return Object.assign({}, g, {type: type, vname: vname}); |
| 163 | })); |
| 164 | } |
| 165 | ); |
| 166 | // Also ask for corr challenges (open + sent by/to me) |
| 167 | ajax( |
| 168 | "/challenges", |
| 169 | "GET", |
| 170 | {uid: this.st.user.id}, |
| 171 | response => { |
| 172 | // Gather all senders names, and then retrieve full identity: |
| 173 | // (TODO [perf]: some might be online...) |
| 174 | let names = {}; |
| 175 | response.challenges.forEach(c => { |
| 176 | if (c.uid != this.st.user.id) |
| 177 | names[c.uid] = ""; //unknwon for now |
| 178 | else if (!!c.target && c.target != this.st.user.id) |
| 179 | names[c.target] = ""; |
| 180 | }); |
| 181 | const addChallenges = (newChalls) => { |
| 182 | names[this.st.user.id] = this.st.user.name; //in case of |
| 183 | this.challenges = this.challenges.concat( |
| 184 | response.challenges.map(c => { |
| 185 | const from = {name: names[c.uid], id: c.uid}; //or just name |
| 186 | const type = this.classifyObject(c); |
| 187 | const vname = this.getVname(c.vid); |
| 188 | return Object.assign({}, |
| 189 | { |
| 190 | type: type, |
| 191 | vname: vname, |
| 192 | from: from, |
| 193 | to: (!!c.target ? names[c.target] : ""), |
| 194 | }, |
| 195 | c); |
| 196 | }) |
| 197 | ); |
| 198 | }; |
| 199 | if (names !== {}) |
| 200 | { |
| 201 | ajax("/users", |
| 202 | "GET", |
| 203 | { ids: Object.keys(names).join(",") }, |
| 204 | response2 => { |
| 205 | response2.users.forEach(u => {names[u.id] = u.name}); |
| 206 | addChallenges(); |
| 207 | } |
| 208 | ); |
| 209 | } |
| 210 | else |
| 211 | addChallenges(); |
| 212 | } |
| 213 | ); |
| 214 | // 0.1] Ask server for room composition: |
| 215 | const funcPollClients = () => { |
| 216 | // Same strategy as in Game.vue: send connection |
| 217 | // after we're sure WebSocket is initialized |
| 218 | this.st.conn.send(JSON.stringify({code:"connect"})); |
| 219 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
| 220 | this.st.conn.send(JSON.stringify({code:"pollgamers"})); |
| 221 | }; |
| 222 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
| 223 | funcPollClients(); |
| 224 | else //socket not ready yet (initial loading) |
| 225 | this.st.conn.onopen = funcPollClients; |
| 226 | this.st.conn.onmessage = this.socketMessageListener; |
| 227 | const socketCloseListener = () => { |
| 228 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
| 229 | this.st.conn.addEventListener('message', this.socketMessageListener); |
| 230 | this.st.conn.addEventListener('close', socketCloseListener); |
| 231 | }; |
| 232 | this.st.conn.onclose = socketCloseListener; |
| 233 | }, |
| 234 | mounted: function() { |
| 235 | [document.getElementById("infoDiv"),document.getElementById("newgameDiv")] |
| 236 | .forEach(elt => elt.addEventListener("click", processModalClick)); |
| 237 | document.querySelectorAll("#predefinedTimeControls > button").forEach( |
| 238 | (b) => { b.addEventListener("click", |
| 239 | () => { this.newchallenge.timeControl = b.innerHTML; } |
| 240 | )} |
| 241 | ); |
| 242 | }, |
| 243 | methods: { |
| 244 | // Helpers: |
| 245 | filterChallenges: function(type) { |
| 246 | return this.challenges.filter(c => c.type == type); |
| 247 | }, |
| 248 | filterGames: function(type) { |
| 249 | return this.games.filter(g => g.type == type); |
| 250 | }, |
| 251 | classifyObject: function(o) { //challenge or game |
| 252 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
| 253 | }, |
| 254 | showGame: function(g) { |
| 255 | // NOTE: we are an observer, since only games I don't play are shown here |
| 256 | // ==> Moves sent by connected remote player(s) if live game |
| 257 | let url = "/game/" + g.id; |
| 258 | if (g.type == "live") |
| 259 | url += "?rid=" + g.rid; |
| 260 | this.$router.push(url); |
| 261 | }, |
| 262 | setDisplay: function(letter, type, e) { |
| 263 | this[letter + "display"] = type; |
| 264 | e.target.classList.add("active"); |
| 265 | if (!!e.target.previousElementSibling) |
| 266 | e.target.previousElementSibling.classList.remove("active"); |
| 267 | else |
| 268 | e.target.nextElementSibling.classList.remove("active"); |
| 269 | }, |
| 270 | getVname: function(vid) { |
| 271 | const variant = this.st.variants.find(v => v.id == vid); |
| 272 | // this.st.variants might be uninitialized (variant == null) |
| 273 | return (!!variant ? variant.name : ""); |
| 274 | }, |
| 275 | processChat: function(chat) { |
| 276 | // When received on server, this will trigger a "notifyRoom" |
| 277 | this.st.conn.send(JSON.stringify({code:"newchat", chat: chat})); |
| 278 | }, |
| 279 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
| 280 | const doSend = (code, obj, sid) => { |
| 281 | this.st.conn.send(JSON.stringify(Object.assign( |
| 282 | {code: code}, |
| 283 | obj, |
| 284 | {target: sid} |
| 285 | ))); |
| 286 | }; |
| 287 | if (!to || (!to.sid && !to.name)) |
| 288 | { |
| 289 | // Open challenge: send to all connected players (me excepted) |
| 290 | Object.keys(this.people).forEach(sid => { |
| 291 | if (sid != this.st.user.sid) |
| 292 | doSend(code, obj, sid); |
| 293 | }); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | let targetSid = ""; |
| 298 | if (!!to.sid) |
| 299 | targetSid = to.sid; |
| 300 | else |
| 301 | { |
| 302 | if (to.name == this.st.user.name) |
| 303 | return alert(this.st.tr["Cannot challenge self"]); |
| 304 | // Challenge with targeted players |
| 305 | targetSid = |
| 306 | Object.keys(this.people).find(sid => this.people[sid].name == to.name); |
| 307 | if (!targetSid) |
| 308 | { |
| 309 | if (!!warnDisconnected) |
| 310 | alert(this.st.tr["Warning: target is not connected"]); |
| 311 | return false; |
| 312 | } |
| 313 | } |
| 314 | doSend(code, obj, targetSid); |
| 315 | } |
| 316 | return true; |
| 317 | }, |
| 318 | // Messaging center: |
| 319 | socketMessageListener: function(msg) { |
| 320 | const data = JSON.parse(msg.data); |
| 321 | switch (data.code) |
| 322 | { |
| 323 | case "duplicate": |
| 324 | alert(this.st.tr["Warning: multi-tabs not supported"]); |
| 325 | break; |
| 326 | // 0.2] Receive clients list (just socket IDs) |
| 327 | case "pollclients": |
| 328 | data.sockIds.forEach(sid => { |
| 329 | this.$set(this.people, sid, {id:0, name:""}); |
| 330 | // Ask identity and challenges |
| 331 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 332 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
| 333 | }); |
| 334 | break; |
| 335 | case "pollgamers": |
| 336 | // NOTE: we could make a difference between people in hall |
| 337 | // and gamers, but is it necessary? |
| 338 | data.sockIds.forEach(sid => { |
| 339 | this.$set(this.people, sid, {id:0, name:"", gamer:true}); |
| 340 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 341 | }); |
| 342 | // Also ask current games to all playing peers (TODO: some design issue) |
| 343 | this.st.conn.send(JSON.stringify({code:"askgames"})); |
| 344 | break; |
| 345 | case "askidentity": |
| 346 | { |
| 347 | // Request for identification: reply if I'm not anonymous |
| 348 | if (this.st.user.id > 0) |
| 349 | { |
| 350 | this.st.conn.send(JSON.stringify({code:"identity", |
| 351 | user: { |
| 352 | // NOTE: decompose to avoid revealing email |
| 353 | name: this.st.user.name, |
| 354 | sid: this.st.user.sid, |
| 355 | id: this.st.user.id, |
| 356 | }, |
| 357 | target:data.from})); |
| 358 | } |
| 359 | break; |
| 360 | } |
| 361 | case "identity": |
| 362 | { |
| 363 | this.$set(this.people, data.user.sid, |
| 364 | { |
| 365 | id: data.user.id, |
| 366 | name: data.user.name, |
| 367 | gamer: this.people[data.user.sid].gamer, |
| 368 | }); |
| 369 | break; |
| 370 | } |
| 371 | case "askchallenge": |
| 372 | { |
| 373 | // Send my current live challenge (if any) |
| 374 | const cIdx = this.challenges.findIndex(c => |
| 375 | c.from.sid == this.st.user.sid && c.type == "live"); |
| 376 | if (cIdx >= 0) |
| 377 | { |
| 378 | const c = this.challenges[cIdx]; |
| 379 | // TODO: code below requires "c.to" to have given his identity, |
| 380 | // but it can happen that the identity arrives later, which |
| 381 | // prevent him from receiving the challenge. |
| 382 | // ==> Filter later (when receiving challenge) |
| 383 | // if (!!c.to) |
| 384 | // { |
| 385 | // // Only share targeted challenges to the targets: |
| 386 | // const toSid = Object.keys(this.people).find(k => |
| 387 | // this.people[k].name == c.to); |
| 388 | // if (toSid != data.from) |
| 389 | // return; |
| 390 | // } |
| 391 | const myChallenge = |
| 392 | { |
| 393 | // Minimal challenge informations: (from not required) |
| 394 | id: c.id, |
| 395 | to: c.to, |
| 396 | fen: c.fen, |
| 397 | vid: c.vid, |
| 398 | timeControl: c.timeControl, |
| 399 | added: c.added, |
| 400 | }; |
| 401 | this.st.conn.send(JSON.stringify({code:"challenge", |
| 402 | chall:myChallenge, target:data.from})); |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | case "challenge": |
| 407 | { |
| 408 | // Receive challenge from some player (+sid) |
| 409 | // NOTE about next condition: see "askchallenge" case. |
| 410 | if (!data.chall.to || data.chall.to == this.st.user.name) |
| 411 | { |
| 412 | let newChall = data.chall; |
| 413 | newChall.type = this.classifyObject(data.chall); |
| 414 | newChall.from = |
| 415 | Object.assign({sid:data.from}, this.people[data.from]); |
| 416 | newChall.vname = this.getVname(newChall.vid); |
| 417 | this.challenges.push(newChall); |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | case "game": |
| 422 | { |
| 423 | // Receive game from some player (+sid) |
| 424 | // NOTE: it may be correspondance (if newgame while we are connected) |
| 425 | // If duplicate found: select rid (remote ID) at random |
| 426 | let game = this.games.find(g => g.id == data.game.id); |
| 427 | if (!!game) |
| 428 | { |
| 429 | if (Math.random() < 0.5) |
| 430 | game.rid = data.from; |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | let newGame = data.game; |
| 435 | newGame.type = this.classifyObject(data.game); |
| 436 | newGame.vname = this.getVname(data.game.vid); |
| 437 | newGame.rid = data.from; |
| 438 | if (!data.game.score) |
| 439 | newGame.score = "*"; |
| 440 | this.games.push(newGame); |
| 441 | } |
| 442 | break; |
| 443 | } |
| 444 | case "newgame": |
| 445 | { |
| 446 | // New game just started: data contain all information |
| 447 | if (this.classifyObject(data.gameInfo) == "live") |
| 448 | this.startNewGame(data.gameInfo); |
| 449 | else |
| 450 | { |
| 451 | this.infoMessage = "New game started: " + |
| 452 | "<a href='#/game/" + data.gameInfo.id + "'>" + |
| 453 | "#/game/" + data.gameInfo.id + "</a>"; |
| 454 | let modalBox = document.getElementById("modalInfo"); |
| 455 | modalBox.checked = true; |
| 456 | setTimeout(() => { modalBox.checked = false; }, 3000); |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | case "newchat": |
| 461 | this.newChat = data.chat; |
| 462 | break; |
| 463 | case "refusechallenge": |
| 464 | { |
| 465 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 466 | localStorage.removeItem("challenge"); |
| 467 | alert(this.st.tr["Challenge declined"]); |
| 468 | break; |
| 469 | } |
| 470 | case "deletechallenge": |
| 471 | { |
| 472 | // NOTE: the challenge may be already removed |
| 473 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 474 | localStorage.removeItem("challenge"); //in case of |
| 475 | break; |
| 476 | } |
| 477 | case "connect": |
| 478 | case "gconnect": |
| 479 | this.$set(this.people, data.from, {name:"", id:0, gamer:data.code[0]=='g'}); |
| 480 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); |
| 481 | if (data.code == "connect") |
| 482 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.from})); |
| 483 | else |
| 484 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.from})); |
| 485 | break; |
| 486 | case "disconnect": |
| 487 | case "gdisconnect": |
| 488 | this.$delete(this.people, data.from); |
| 489 | if (data.code == "disconnect") |
| 490 | { |
| 491 | // Also remove all challenges sent by this player: |
| 492 | ArrayFun.remove(this.challenges, c => c.from.sid == data.from); |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | // And all live games where he plays and no other opponent is online |
| 497 | ArrayFun.remove(this.games, g => |
| 498 | g.type == "live" && (g.players.every(p => p.sid == data.from |
| 499 | || !this.people[p.sid])), "all"); |
| 500 | } |
| 501 | break; |
| 502 | } |
| 503 | }, |
| 504 | // Challenge lifecycle: |
| 505 | tryChallenge: function(sid) { |
| 506 | if (this.people[sid].id == 0) |
| 507 | return; //anonymous players cannot be challenged |
| 508 | // TODO: SID is available, so we could use it instead of searching from name |
| 509 | this.newchallenge.to = this.people[sid].name; |
| 510 | doClick("modalNewgame"); |
| 511 | }, |
| 512 | challOrWatch: function(sid, e) { |
| 513 | switch (e.target.innerHTML) |
| 514 | { |
| 515 | case "Available": |
| 516 | this.tryChallenge(sid); |
| 517 | break; |
| 518 | case "Playing": |
| 519 | this.showGame(this.games.find( |
| 520 | g => g.players.some(pl => pl.sid == sid || pl.uid == this.people[sid].id))); |
| 521 | break; |
| 522 | }; |
| 523 | }, |
| 524 | newChallenge: async function() { |
| 525 | if (this.newchallenge.vid == "") |
| 526 | return alert(this.st.tr["Please select a variant"]); |
| 527 | if (!!this.newchallenge.to && this.newchallenge.to == this.st.user.name) |
| 528 | return alert(this.st.tr["Self-challenge is forbidden"]); |
| 529 | const vname = this.getVname(this.newchallenge.vid); |
| 530 | const vModule = await import("@/variants/" + vname + ".js"); |
| 531 | window.V = vModule.VariantRules; |
| 532 | if (!!this.newchallenge.timeControl.match(/^[0-9]+$/)) |
| 533 | this.newchallenge.timeControl += "+0"; //assume minutes, no increment |
| 534 | const error = checkChallenge(this.newchallenge); |
| 535 | if (!!error) |
| 536 | return alert(error); |
| 537 | const ctype = this.classifyObject(this.newchallenge); |
| 538 | if (ctype == "corr" && this.st.user.id <= 0) |
| 539 | return alert(this.st.tr["Please log in to play correspondance games"]); |
| 540 | // NOTE: "from" information is not required here |
| 541 | let chall = Object.assign({}, this.newchallenge); |
| 542 | const finishAddChallenge = (cid,warnDisconnected) => { |
| 543 | chall.id = cid || "c" + getRandString(); |
| 544 | // Send challenge to peers (if connected) |
| 545 | const isSent = this.sendSomethingTo({name:chall.to}, "challenge", |
| 546 | {chall:chall}, !!warnDisconnected); |
| 547 | if (!isSent) |
| 548 | return; |
| 549 | // Remove old challenge if any (only one at a time of a given type): |
| 550 | const cIdx = this.challenges.findIndex(c => |
| 551 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && c.type == ctype); |
| 552 | if (cIdx >= 0) |
| 553 | { |
| 554 | // Delete current challenge (will be replaced now) |
| 555 | this.sendSomethingTo({name:this.challenges[cIdx].to}, |
| 556 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
| 557 | if (ctype == "corr") |
| 558 | { |
| 559 | ajax( |
| 560 | "/challenges", |
| 561 | "DELETE", |
| 562 | {id: this.challenges[cIdx].id} |
| 563 | ); |
| 564 | } |
| 565 | this.challenges.splice(cIdx, 1); |
| 566 | } |
| 567 | // Add new challenge: |
| 568 | chall.added = Date.now(); |
| 569 | // NOTE: vname and type are redundant (can be deduced from timeControl + vid) |
| 570 | chall.type = ctype; |
| 571 | chall.vname = vname; |
| 572 | chall.from = { //decompose to avoid revealing email |
| 573 | sid: this.st.user.sid, |
| 574 | id: this.st.user.id, |
| 575 | name: this.st.user.name, |
| 576 | }; |
| 577 | this.challenges.push(chall); |
| 578 | if (ctype == "live") |
| 579 | localStorage.setItem("challenge", JSON.stringify(chall)); |
| 580 | // Also remember timeControl + vid for quicker further challenges: |
| 581 | localStorage.setItem("timeControl", chall.timeControl); |
| 582 | localStorage.setItem("vid", chall.vid); |
| 583 | document.getElementById("modalNewgame").checked = false; |
| 584 | }; |
| 585 | if (ctype == "live") |
| 586 | { |
| 587 | // Live challenges have a random ID |
| 588 | finishAddChallenge(null, "warnDisconnected"); |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | // Correspondance game: send challenge to server |
| 593 | ajax( |
| 594 | "/challenges", |
| 595 | "POST", |
| 596 | { chall: chall }, |
| 597 | response => { finishAddChallenge(response.cid); } |
| 598 | ); |
| 599 | } |
| 600 | }, |
| 601 | clickChallenge: function(c) { |
| 602 | const myChallenge = (c.from.sid == this.st.user.sid //live |
| 603 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr |
| 604 | if (!myChallenge) |
| 605 | { |
| 606 | if (c.type == "corr" && this.st.user.id <= 0) |
| 607 | return alert(this.st.tr["Please log in to accept corr challenges"]); |
| 608 | c.accepted = true; |
| 609 | if (!!c.to) //c.to == this.st.user.name (connected) |
| 610 | { |
| 611 | // TODO: if special FEN, show diagram after loading variant |
| 612 | c.accepted = confirm("Accept challenge?"); |
| 613 | } |
| 614 | if (c.accepted) |
| 615 | { |
| 616 | c.seat = { //again, avoid c.seat = st.user to not reveal email |
| 617 | sid: this.st.user.sid, |
| 618 | id: this.st.user.id, |
| 619 | name: this.st.user.name, |
| 620 | }; |
| 621 | this.launchGame(c); |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | this.st.conn.send(JSON.stringify({ |
| 626 | code: "refusechallenge", |
| 627 | cid: c.id, target: c.from.sid})); |
| 628 | } |
| 629 | this.sendSomethingTo(!!c.to ? {sid:c.from.sid} : null, "deletechallenge", {cid:c.id}); |
| 630 | } |
| 631 | else //my challenge |
| 632 | { |
| 633 | if (c.type == "corr") |
| 634 | { |
| 635 | ajax( |
| 636 | "/challenges", |
| 637 | "DELETE", |
| 638 | {id: c.id} |
| 639 | ); |
| 640 | } |
| 641 | else //live |
| 642 | localStorage.removeItem("challenge"); |
| 643 | this.sendSomethingTo({name:c.to}, "deletechallenge", {cid:c.id}); |
| 644 | } |
| 645 | // In all cases, the challenge is consumed: |
| 646 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 647 | }, |
| 648 | // NOTE: when launching game, the challenge is already being deleted |
| 649 | launchGame: async function(c) { |
| 650 | const vModule = await import("@/variants/" + c.vname + ".js"); |
| 651 | window.V = vModule.VariantRules; |
| 652 | // These game informations will be sent to other players |
| 653 | const gameInfo = |
| 654 | { |
| 655 | id: getRandString(), |
| 656 | fen: c.fen || V.GenRandInitFen(), |
| 657 | players: shuffle([c.from, c.seat]), //white then black |
| 658 | vid: c.vid, |
| 659 | vname: c.vname, //theoretically vid is enough, but much easier with vname |
| 660 | timeControl: c.timeControl, |
| 661 | }; |
| 662 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
| 663 | if (!oppsid) |
| 664 | { |
| 665 | oppsid = Object.keys(this.people).find(sid => |
| 666 | this.people[sid].id == c.from.id); |
| 667 | } |
| 668 | const tryNotifyOpponent = () => { |
| 669 | if (!!oppsid) //opponent is online |
| 670 | { |
| 671 | this.st.conn.send(JSON.stringify({code:"newgame", |
| 672 | gameInfo:gameInfo, target:oppsid, cid:c.id})); |
| 673 | } |
| 674 | }; |
| 675 | if (c.type == "live") |
| 676 | { |
| 677 | // NOTE: in this case we are sure opponent is online |
| 678 | tryNotifyOpponent(); |
| 679 | this.startNewGame(gameInfo); |
| 680 | } |
| 681 | else //corr: game only on server |
| 682 | { |
| 683 | ajax( |
| 684 | "/games", |
| 685 | "POST", |
| 686 | {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge |
| 687 | response => { |
| 688 | gameInfo.id = response.gameId; |
| 689 | tryNotifyOpponent(); |
| 690 | this.$router.push("/game/" + response.gameId); |
| 691 | } |
| 692 | ); |
| 693 | } |
| 694 | // Send game info to everyone except opponent (and me) |
| 695 | Object.keys(this.people).forEach(sid => { |
| 696 | if (![this.st.user.sid,oppsid].includes(sid)) |
| 697 | { |
| 698 | this.st.conn.send(JSON.stringify({code:"game", |
| 699 | game: { //minimal game info: |
| 700 | id: gameInfo.id, |
| 701 | players: gameInfo.players, |
| 702 | vid: gameInfo.vid, |
| 703 | timeControl: gameInfo.timeControl, |
| 704 | }, |
| 705 | target: sid})); |
| 706 | } |
| 707 | }); |
| 708 | }, |
| 709 | // NOTE: for live games only (corr games start on the server) |
| 710 | startNewGame: function(gameInfo) { |
| 711 | const game = Object.assign({}, gameInfo, { |
| 712 | // (other) Game infos: constant |
| 713 | fenStart: gameInfo.fen, |
| 714 | added: Date.now(), |
| 715 | // Game state (including FEN): will be updated |
| 716 | moves: [], |
| 717 | clocks: [-1, -1], //-1 = unstarted |
| 718 | initime: [0, 0], //initialized later |
| 719 | score: "*", |
| 720 | }); |
| 721 | GameStorage.add(game); |
| 722 | if (this.st.settings.sound >= 1) |
| 723 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 724 | this.$router.push("/game/" + gameInfo.id); |
| 725 | }, |
| 726 | }, |
| 727 | }; |
| 728 | </script> |
| 729 | |
| 730 | <style lang="sass" scoped> |
| 731 | .active |
| 732 | color: #42a983 |
| 733 | #newGame |
| 734 | display: block |
| 735 | margin: 10px auto 5px auto |
| 736 | #people |
| 737 | width: 100% |
| 738 | #players |
| 739 | width: 50% |
| 740 | position: relative |
| 741 | float: left |
| 742 | #chat |
| 743 | width: 50% |
| 744 | float: left |
| 745 | position: relative |
| 746 | @media screen and (max-width: 767px) |
| 747 | #players, #chats |
| 748 | width: 100% |
| 749 | #chat > .card |
| 750 | max-width: 100% |
| 751 | margin: 0; |
| 752 | border: none; |
| 753 | #players > p |
| 754 | margin-left: 5px |
| 755 | .anonymous |
| 756 | font-style: italic |
| 757 | button.player-action |
| 758 | margin-left: 32px |
| 759 | </style> |