| 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 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 | const uids = response.challenges.map(c => { return c.uid }); |
| 175 | ajax("/users", |
| 176 | "GET", |
| 177 | { ids: uids.join(",") }, |
| 178 | response2 => { |
| 179 | let names = {}; |
| 180 | response2.users.forEach(u => {names[u.id] = u.name}); |
| 181 | this.challenges = this.challenges.concat( |
| 182 | response.challenges.map(c => { |
| 183 | // (just players names in fact) |
| 184 | const from = {name: names[c.uid], id: c.uid}; |
| 185 | const type = this.classifyObject(c); |
| 186 | const vname = this.getVname(c.vid); |
| 187 | return Object.assign({}, c, {type: type, vname: vname, from: from}); |
| 188 | }) |
| 189 | ) |
| 190 | } |
| 191 | ); |
| 192 | } |
| 193 | ); |
| 194 | // 0.1] Ask server for room composition: |
| 195 | const funcPollClients = () => { |
| 196 | // Same strategy as in Game.vue: send connection |
| 197 | // after we're sure WebSocket is initialized |
| 198 | this.st.conn.send(JSON.stringify({code:"connect"})); |
| 199 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
| 200 | this.st.conn.send(JSON.stringify({code:"pollgamers"})); |
| 201 | }; |
| 202 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
| 203 | funcPollClients(); |
| 204 | else //socket not ready yet (initial loading) |
| 205 | this.st.conn.onopen = funcPollClients; |
| 206 | this.st.conn.onmessage = this.socketMessageListener; |
| 207 | const socketCloseListener = () => { |
| 208 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
| 209 | this.st.conn.addEventListener('message', this.socketMessageListener); |
| 210 | this.st.conn.addEventListener('close', socketCloseListener); |
| 211 | }; |
| 212 | this.st.conn.onclose = socketCloseListener; |
| 213 | }, |
| 214 | mounted: function() { |
| 215 | [document.getElementById("infoDiv"),document.getElementById("newgameDiv")] |
| 216 | .forEach(elt => elt.addEventListener("click", processModalClick)); |
| 217 | document.querySelectorAll("#predefinedTimeControls > button").forEach( |
| 218 | (b) => { b.addEventListener("click", |
| 219 | () => { this.newchallenge.timeControl = b.innerHTML; } |
| 220 | )} |
| 221 | ); |
| 222 | }, |
| 223 | methods: { |
| 224 | // Helpers: |
| 225 | filterChallenges: function(type) { |
| 226 | return this.challenges.filter(c => c.type == type); |
| 227 | }, |
| 228 | filterGames: function(type) { |
| 229 | return this.games.filter(g => g.type == type); |
| 230 | }, |
| 231 | classifyObject: function(o) { //challenge or game |
| 232 | // Heuristic: should work for most cases... (TODO) |
| 233 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
| 234 | }, |
| 235 | showGame: function(g) { |
| 236 | // NOTE: we are an observer, since only games I don't play are shown here |
| 237 | // ==> Moves sent by connected remote player(s) if live game |
| 238 | let url = "/game/" + g.id; |
| 239 | if (g.type == "live") |
| 240 | url += "?rid=" + g.rid; |
| 241 | this.$router.push(url); |
| 242 | }, |
| 243 | setDisplay: function(letter, type, e) { |
| 244 | this[letter + "display"] = type; |
| 245 | e.target.classList.add("active"); |
| 246 | if (!!e.target.previousElementSibling) |
| 247 | e.target.previousElementSibling.classList.remove("active"); |
| 248 | else |
| 249 | e.target.nextElementSibling.classList.remove("active"); |
| 250 | }, |
| 251 | getVname: function(vid) { |
| 252 | const variant = this.st.variants.find(v => v.id == vid); |
| 253 | // this.st.variants might be uninitialized (variant == null) |
| 254 | return (!!variant ? variant.name : ""); |
| 255 | }, |
| 256 | processChat: function(chat) { |
| 257 | // When received on server, this will trigger a "notifyRoom" |
| 258 | this.st.conn.send(JSON.stringify({code:"newchat", chat: chat})); |
| 259 | }, |
| 260 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
| 261 | const doSend = (code, obj, sid) => { |
| 262 | this.st.conn.send(JSON.stringify(Object.assign( |
| 263 | {code: code}, |
| 264 | obj, |
| 265 | {target: sid} |
| 266 | ))); |
| 267 | }; |
| 268 | if (!to || (!to.sid && !to.name)) |
| 269 | { |
| 270 | // Open challenge: send to all connected players (me excepted) |
| 271 | Object.keys(this.people).forEach(sid => { |
| 272 | if (sid != this.st.user.sid) |
| 273 | doSend(code, obj, sid); |
| 274 | }); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | let targetSid = ""; |
| 279 | if (!!to.sid) |
| 280 | targetSid = to.sid; |
| 281 | else |
| 282 | { |
| 283 | if (to.name == this.st.user.name) |
| 284 | return alert(this.st.tr["Cannot challenge self"]); |
| 285 | // Challenge with targeted players |
| 286 | targetSid = |
| 287 | Object.keys(this.people).find(sid => this.people[sid].name == to.name); |
| 288 | if (!targetSid) |
| 289 | { |
| 290 | if (!!warnDisconnected) |
| 291 | alert(this.st.tr["Warning: target is not connected"]); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | doSend(code, obj, targetSid); |
| 296 | } |
| 297 | return true; |
| 298 | }, |
| 299 | // Messaging center: |
| 300 | socketMessageListener: function(msg) { |
| 301 | const data = JSON.parse(msg.data); |
| 302 | switch (data.code) |
| 303 | { |
| 304 | case "duplicate": |
| 305 | alert(this.st.tr["Warning: multi-tabs not supported"]); |
| 306 | break; |
| 307 | // 0.2] Receive clients list (just socket IDs) |
| 308 | case "pollclients": |
| 309 | data.sockIds.forEach(sid => { |
| 310 | this.$set(this.people, sid, {id:0, name:""}); |
| 311 | // Ask identity and challenges |
| 312 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 313 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
| 314 | }); |
| 315 | break; |
| 316 | case "pollgamers": |
| 317 | // NOTE: we could make a difference between people in hall |
| 318 | // and gamers, but is it necessary? |
| 319 | data.sockIds.forEach(sid => { |
| 320 | this.$set(this.people, sid, {id:0, name:"", gamer:true}); |
| 321 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 322 | }); |
| 323 | // Also ask current games to all playing peers (TODO: some design issue) |
| 324 | this.st.conn.send(JSON.stringify({code:"askgames"})); |
| 325 | break; |
| 326 | case "askidentity": |
| 327 | { |
| 328 | // Request for identification: reply if I'm not anonymous |
| 329 | if (this.st.user.id > 0) |
| 330 | { |
| 331 | this.st.conn.send(JSON.stringify({code:"identity", |
| 332 | user: { |
| 333 | // NOTE: decompose to avoid revealing email |
| 334 | name: this.st.user.name, |
| 335 | sid: this.st.user.sid, |
| 336 | id: this.st.user.id, |
| 337 | }, |
| 338 | target:data.from})); |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | case "identity": |
| 343 | { |
| 344 | this.$set(this.people, data.user.sid, |
| 345 | { |
| 346 | id: data.user.id, |
| 347 | name: data.user.name, |
| 348 | gamer: this.people[data.user.sid].gamer, |
| 349 | }); |
| 350 | break; |
| 351 | } |
| 352 | case "askchallenge": |
| 353 | { |
| 354 | // Send my current live challenge (if any) |
| 355 | const cIdx = this.challenges.findIndex(c => |
| 356 | c.from.sid == this.st.user.sid && c.type == "live"); |
| 357 | if (cIdx >= 0) |
| 358 | { |
| 359 | const c = this.challenges[cIdx]; |
| 360 | if (!!c.to) |
| 361 | { |
| 362 | // Only share targeted challenges to the targets: |
| 363 | const toSid = Object.keys(this.people).find(k => |
| 364 | this.people[k].name == c.to); |
| 365 | if (toSid != data.from) |
| 366 | return; |
| 367 | } |
| 368 | const myChallenge = |
| 369 | { |
| 370 | // Minimal challenge informations: (from not required) |
| 371 | id: c.id, |
| 372 | to: c.to, |
| 373 | fen: c.fen, |
| 374 | vid: c.vid, |
| 375 | timeControl: c.timeControl, |
| 376 | }; |
| 377 | this.st.conn.send(JSON.stringify({code:"challenge", |
| 378 | chall:myChallenge, target:data.from})); |
| 379 | } |
| 380 | break; |
| 381 | } |
| 382 | case "challenge": |
| 383 | { |
| 384 | // Receive challenge from some player (+sid) |
| 385 | let newChall = data.chall; |
| 386 | newChall.type = this.classifyObject(data.chall); |
| 387 | newChall.from = |
| 388 | Object.assign({sid:data.from}, this.people[data.from]); |
| 389 | newChall.added = Date.now(); //TODO: this is reception timestamp, not creation |
| 390 | newChall.vname = this.getVname(newChall.vid); |
| 391 | this.challenges.push(newChall); |
| 392 | break; |
| 393 | } |
| 394 | case "game": |
| 395 | { |
| 396 | // Receive game from some player (+sid) |
| 397 | // NOTE: it may be correspondance (if newgame while we are connected) |
| 398 | // If duplicate found: select rid (remote ID) at random |
| 399 | let game = this.games.find(g => g.id == data.game.id); |
| 400 | if (!!game && Math.random() < 0.5) |
| 401 | game.rid = data.from; |
| 402 | else |
| 403 | { |
| 404 | let newGame = data.game; |
| 405 | newGame.type = this.classifyObject(data.game); |
| 406 | newGame.vname = this.getVname(data.game.vid); |
| 407 | newGame.rid = data.from; |
| 408 | this.games.push(newGame); |
| 409 | } |
| 410 | break; |
| 411 | } |
| 412 | case "newgame": |
| 413 | { |
| 414 | // TODO: next line required ?! |
| 415 | //ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 416 | // New game just started: data contain all information |
| 417 | if (this.classifyObject(data.gameInfo) == "live") |
| 418 | this.startNewGame(data.gameInfo); |
| 419 | else |
| 420 | { |
| 421 | this.infoMessage = "New game started: " + |
| 422 | "<a href='#/game/" + data.gameInfo.id + "'>" + |
| 423 | "#/game/" + data.gameInfo.id + "</a>"; |
| 424 | let modalBox = document.getElementById("modalInfo"); |
| 425 | modalBox.checked = true; |
| 426 | setTimeout(() => { modalBox.checked = false; }, 3000); |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case "newchat": |
| 431 | this.newChat = data.chat; |
| 432 | break; |
| 433 | case "refusechallenge": |
| 434 | { |
| 435 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 436 | localStorage.removeItem("challenge"); |
| 437 | alert(this.st.tr["Challenge declined"]); |
| 438 | break; |
| 439 | } |
| 440 | case "deletechallenge": |
| 441 | { |
| 442 | // NOTE: the challenge may be already removed |
| 443 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 444 | localStorage.removeItem("challenge"); //in case of |
| 445 | break; |
| 446 | } |
| 447 | case "connect": |
| 448 | case "gconnect": |
| 449 | this.$set(this.people, data.from, {name:"", id:0, gamer:data.code[0]=='g'}); |
| 450 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); |
| 451 | if (data.code == "connect") |
| 452 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.from})); |
| 453 | else |
| 454 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.from})); |
| 455 | break; |
| 456 | case "disconnect": |
| 457 | case "gdisconnect": |
| 458 | this.$delete(this.people, data.from); |
| 459 | if (data.code == "disconnect") |
| 460 | { |
| 461 | // Also remove all challenges sent by this player: |
| 462 | ArrayFun.remove(this.challenges, c => c.from.sid == data.from); |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | // And all live games where he plays and no other opponent is online |
| 467 | ArrayFun.remove(this.games, g => |
| 468 | g.type == "live" && (g.players.every(p => p.sid == data.from |
| 469 | || !this.people[p.sid])), "all"); |
| 470 | } |
| 471 | break; |
| 472 | } |
| 473 | }, |
| 474 | // Challenge lifecycle: |
| 475 | tryChallenge: function(sid) { |
| 476 | if (this.people[sid].id == 0) |
| 477 | return; //anonymous players cannot be challenged |
| 478 | // TODO: SID is available, so we could use it instead of searching from name |
| 479 | this.newchallenge.to = this.people[sid].name; |
| 480 | doClick("modalNewgame"); |
| 481 | }, |
| 482 | challOrWatch: function(sid, e) { |
| 483 | switch (e.target.innerHTML) |
| 484 | { |
| 485 | case "Available": |
| 486 | this.tryChallenge(sid); |
| 487 | break; |
| 488 | case "Playing": |
| 489 | this.showGame(this.games.find( |
| 490 | g => g.players.some(pl => pl.sid == sid || pl.uid == this.people[sid].id))); |
| 491 | break; |
| 492 | }; |
| 493 | }, |
| 494 | newChallenge: async function() { |
| 495 | if (this.newchallenge.vid == "") |
| 496 | return alert(this.st.tr["Please select a variant"]); |
| 497 | const vname = this.getVname(this.newchallenge.vid); |
| 498 | const vModule = await import("@/variants/" + vname + ".js"); |
| 499 | window.V = vModule.VariantRules; |
| 500 | if (!!this.newchallenge.timeControl.match(/^[0-9]+$/)) |
| 501 | this.newchallenge.timeControl += "+0"; //assume minutes, no increment |
| 502 | const error = checkChallenge(this.newchallenge); |
| 503 | if (!!error) |
| 504 | return alert(error); |
| 505 | const ctype = this.classifyObject(this.newchallenge); |
| 506 | if (ctype == "corr" && this.st.user.id <= 0) |
| 507 | return alert(this.st.tr["Please log in to play correspondance games"]); |
| 508 | // NOTE: "from" information is not required here |
| 509 | let chall = Object.assign({}, this.newchallenge); |
| 510 | const finishAddChallenge = (cid,warnDisconnected) => { |
| 511 | chall.id = cid || "c" + getRandString(); |
| 512 | // Send challenge to peers (if connected) |
| 513 | const isSent = this.sendSomethingTo({name:chall.to}, "challenge", |
| 514 | {chall:chall}, !!warnDisconnected); |
| 515 | if (!isSent) |
| 516 | return; |
| 517 | // Remove old challenge if any (only one at a time): |
| 518 | const cIdx = this.challenges.findIndex(c => |
| 519 | c.from.sid == this.st.user.sid && c.type == ctype); |
| 520 | if (cIdx >= 0) |
| 521 | { |
| 522 | // Delete current challenge (will be replaced now) |
| 523 | this.sendSomethingTo({name:this.challenges[cIdx].to}, |
| 524 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
| 525 | if (ctype == "corr") |
| 526 | { |
| 527 | ajax( |
| 528 | "/challenges", |
| 529 | "DELETE", |
| 530 | {id: this.challenges[cIdx].id} |
| 531 | ); |
| 532 | } |
| 533 | this.challenges.splice(cIdx, 1); |
| 534 | } |
| 535 | // Add new challenge: |
| 536 | chall.added = Date.now(); |
| 537 | // NOTE: vname and type are redundant (can be deduced from timeControl + vid) |
| 538 | chall.type = ctype; |
| 539 | chall.vname = vname; |
| 540 | chall.from = { //decompose to avoid revealing email |
| 541 | sid: this.st.user.sid, |
| 542 | id: this.st.user.id, |
| 543 | name: this.st.user.name, |
| 544 | }; |
| 545 | this.challenges.push(chall); |
| 546 | if (ctype == "live") |
| 547 | localStorage.setItem("challenge", JSON.stringify(chall)); |
| 548 | // Also remember timeControl + vid for quicker further challenges: |
| 549 | localStorage.setItem("timeControl", chall.timeControl); |
| 550 | localStorage.setItem("vid", chall.vid); |
| 551 | document.getElementById("modalNewgame").checked = false; |
| 552 | }; |
| 553 | if (ctype == "live") |
| 554 | { |
| 555 | // Live challenges have a random ID |
| 556 | finishAddChallenge(null, "warnDisconnected"); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | // Correspondance game: send challenge to server |
| 561 | ajax( |
| 562 | "/challenges", |
| 563 | "POST", |
| 564 | { chall: chall }, |
| 565 | response => { finishAddChallenge(response.cid); } |
| 566 | ); |
| 567 | } |
| 568 | }, |
| 569 | clickChallenge: function(c) { |
| 570 | const myChallenge = (c.from.sid == this.st.user.sid //live |
| 571 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr |
| 572 | if (!myChallenge) |
| 573 | { |
| 574 | if (c.type == "corr" && this.st.user.id <= 0) |
| 575 | return alert(this.st.tr["Please log in to accept corr challenges"]); |
| 576 | c.accepted = true; |
| 577 | if (!!c.to) //c.to == this.st.user.name (connected) |
| 578 | { |
| 579 | // TODO: if special FEN, show diagram after loading variant |
| 580 | c.accepted = confirm("Accept challenge?"); |
| 581 | } |
| 582 | if (c.accepted) |
| 583 | { |
| 584 | c.seat = { //again, avoid c.seat = st.user to not reveal email |
| 585 | sid: this.st.user.sid, |
| 586 | id: this.st.user.id, |
| 587 | name: this.st.user.name, |
| 588 | }; |
| 589 | this.launchGame(c); |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | this.st.conn.send(JSON.stringify({ |
| 594 | code: "refusechallenge", |
| 595 | cid: c.id, target: c.from.sid})); |
| 596 | } |
| 597 | this.sendSomethingTo(!!c.to ? {sid:c.from.sid} : null, "deletechallenge", {cid:c.id}); |
| 598 | } |
| 599 | else //my challenge |
| 600 | { |
| 601 | if (c.type == "corr") |
| 602 | { |
| 603 | ajax( |
| 604 | "/challenges", |
| 605 | "DELETE", |
| 606 | {id: c.id} |
| 607 | ); |
| 608 | } |
| 609 | else //live |
| 610 | localStorage.removeItem("challenge"); |
| 611 | this.sendSomethingTo({name:c.to}, "deletechallenge", {cid:c.id}); |
| 612 | } |
| 613 | // In all cases, the challenge is consumed: |
| 614 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 615 | }, |
| 616 | // NOTE: when launching game, the challenge is already deleted |
| 617 | launchGame: async function(c) { |
| 618 | const vModule = await import("@/variants/" + c.vname + ".js"); |
| 619 | window.V = vModule.VariantRules; |
| 620 | // These game informations will be sent to other players |
| 621 | const gameInfo = |
| 622 | { |
| 623 | id: getRandString(), |
| 624 | fen: c.fen || V.GenRandInitFen(), |
| 625 | players: shuffle([c.from, c.seat]), //white then black |
| 626 | vid: c.vid, |
| 627 | vname: c.vname, //theoretically vid is enough, but much easier with vname |
| 628 | timeControl: c.timeControl, |
| 629 | }; |
| 630 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
| 631 | if (!oppsid) |
| 632 | { |
| 633 | oppsid = Object.keys(this.people).find(sid => |
| 634 | this.people[sid].id == c.from.id); |
| 635 | } |
| 636 | const tryNotifyOpponent = () => { |
| 637 | if (!!oppsid) //opponent is online |
| 638 | { |
| 639 | this.st.conn.send(JSON.stringify({code:"newgame", |
| 640 | gameInfo:gameInfo, target:oppsid, cid:c.id})); |
| 641 | } |
| 642 | }; |
| 643 | if (c.type == "live") |
| 644 | { |
| 645 | // NOTE: in this case we are sure opponent is online |
| 646 | tryNotifyOpponent(); |
| 647 | this.startNewGame(gameInfo); |
| 648 | } |
| 649 | else //corr: game only on server |
| 650 | { |
| 651 | ajax( |
| 652 | "/games", |
| 653 | "POST", |
| 654 | {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge |
| 655 | response => { |
| 656 | gameInfo.id = response.gameId; |
| 657 | tryNotifyOpponent(); |
| 658 | this.$router.push("/game/" + response.gameId); |
| 659 | } |
| 660 | ); |
| 661 | } |
| 662 | // Send game info to everyone except opponent (and me) |
| 663 | Object.keys(this.people).forEach(sid => { |
| 664 | if (![this.st.user.sid,oppsid].includes(sid)) |
| 665 | { |
| 666 | this.st.conn.send(JSON.stringify({code:"game", |
| 667 | game: { //minimal game info: |
| 668 | id: gameInfo.id, |
| 669 | players: gameInfo.players, |
| 670 | vid: gameInfo.vid, |
| 671 | timeControl: gameInfo.timeControl, |
| 672 | }, |
| 673 | target: sid})); |
| 674 | } |
| 675 | }); |
| 676 | }, |
| 677 | // NOTE: for live games only (corr games start on the server) |
| 678 | startNewGame: function(gameInfo) { |
| 679 | const game = Object.assign({}, gameInfo, { |
| 680 | // (other) Game infos: constant |
| 681 | fenStart: gameInfo.fen, |
| 682 | added: Date.now(), |
| 683 | // Game state (including FEN): will be updated |
| 684 | moves: [], |
| 685 | clocks: [-1, -1], //-1 = unstarted |
| 686 | initime: [0, 0], //initialized later |
| 687 | score: "*", |
| 688 | }); |
| 689 | GameStorage.add(game); |
| 690 | if (this.st.settings.sound >= 1) |
| 691 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 692 | this.$router.push("/game/" + gameInfo.id); |
| 693 | }, |
| 694 | }, |
| 695 | }; |
| 696 | </script> |
| 697 | |
| 698 | <style lang="sass" scoped> |
| 699 | .active |
| 700 | color: #42a983 |
| 701 | #newGame |
| 702 | display: block |
| 703 | margin: 10px auto 5px auto |
| 704 | #people |
| 705 | width: 100% |
| 706 | #players |
| 707 | width: 50% |
| 708 | position: relative |
| 709 | float: left |
| 710 | #chat |
| 711 | width: 50% |
| 712 | float: left |
| 713 | position: relative |
| 714 | @media screen and (max-width: 767px) |
| 715 | #players, #chats |
| 716 | width: 100% |
| 717 | #chat > .card |
| 718 | max-width: 100% |
| 719 | margin: 0; |
| 720 | border: none; |
| 721 | #players > p |
| 722 | margin-left: 5px |
| 723 | .anonymous |
| 724 | font-style: italic |
| 725 | button.player-action |
| 726 | margin-left: 32px |
| 727 | </style> |