| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalInfo.modal(type="checkbox") |
| 4 | div#infoDiv( |
| 5 | role="dialog" |
| 6 | data-checkbox="modalInfo" |
| 7 | ) |
| 8 | .card.text-center |
| 9 | label.modal-close(for="modalInfo") |
| 10 | p(v-html="infoMessage") |
| 11 | input#modalAccept.modal(type="checkbox") |
| 12 | div#acceptDiv(role="dialog") |
| 13 | .card.text-center |
| 14 | p |
| 15 | span.variantName {{ curChallToAccept.vname }} |
| 16 | span {{ curChallToAccept.cadence }} |
| 17 | span {{ st.tr["with"] + " " + curChallToAccept.from.name }} |
| 18 | .diagram(v-html="tchallDiag") |
| 19 | .button-group#buttonsTchall |
| 20 | button.acceptBtn(@click="decisionChallenge(true)") {{ st.tr["Accept challenge?"] }} |
| 21 | button.refuseBtn(@click="decisionChallenge(false)") {{ st.tr["Refuse"] }} |
| 22 | input#modalNewgame.modal(type="checkbox") |
| 23 | div#newgameDiv( |
| 24 | role="dialog" |
| 25 | data-checkbox="modalNewgame" |
| 26 | ) |
| 27 | .card |
| 28 | label#closeNewgame.modal-close(for="modalNewgame") |
| 29 | div(@keyup.enter="newChallenge()") |
| 30 | fieldset |
| 31 | label(for="selectVariant") {{ st.tr["Variant"] }} * |
| 32 | select#selectVariant( |
| 33 | @change="loadNewchallVariant(trySetNewchallDiag)" |
| 34 | v-model="newchallenge.vid" |
| 35 | ) |
| 36 | option( |
| 37 | v-for="v in st.variants" |
| 38 | :value="v.id" |
| 39 | :selected="newchallenge.vid==v.id" |
| 40 | ) |
| 41 | | {{ v.name }} |
| 42 | fieldset |
| 43 | label(for="cadence") {{ st.tr["Cadence"] }} * |
| 44 | div#predefinedCadences |
| 45 | button(type="button") 3+2 |
| 46 | button(type="button") 5+3 |
| 47 | button(type="button") 15+5 |
| 48 | input#cadence( |
| 49 | type="text" |
| 50 | v-model="newchallenge.cadence" |
| 51 | placeholder="5+0, 1h+30s, 7d+1d ..." |
| 52 | ) |
| 53 | fieldset(v-if="st.user.id > 0") |
| 54 | label(for="selectPlayers") {{ st.tr["Play with?"] }} |
| 55 | input#selectPlayers( |
| 56 | type="text" |
| 57 | v-model="newchallenge.to" |
| 58 | ) |
| 59 | fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0") |
| 60 | input#inputFen( |
| 61 | placeholder="FEN" |
| 62 | @input="trySetNewchallDiag()" |
| 63 | type="text" |
| 64 | v-model="newchallenge.fen" |
| 65 | ) |
| 66 | .diagram(v-html="newchallenge.diag") |
| 67 | button(@click="newChallenge()") {{ st.tr["Send challenge"] }} |
| 68 | input#modalPeople.modal( |
| 69 | type="checkbox" |
| 70 | @click="resetChatColor()" |
| 71 | ) |
| 72 | div#peopleWrap( |
| 73 | role="dialog" |
| 74 | data-checkbox="modalPeople" |
| 75 | ) |
| 76 | .card |
| 77 | label.modal-close(for="modalPeople") |
| 78 | #people |
| 79 | #players |
| 80 | p( |
| 81 | v-for="sid in Object.keys(people)" |
| 82 | v-if="!!people[sid].name" |
| 83 | ) |
| 84 | span {{ people[sid].name }} |
| 85 | button.player-action( |
| 86 | v-if="isGamer(sid) || (st.user.id > 0 && sid!=st.user.sid)" |
| 87 | @click="challOrWatch(sid)" |
| 88 | ) |
| 89 | | {{ getActionLabel(sid) }} |
| 90 | p.anonymous @nonymous ({{ anonymousCount }}) |
| 91 | #chat |
| 92 | Chat( |
| 93 | :newChat="newChat" |
| 94 | @mychat="processChat" |
| 95 | :pastChats="[]" |
| 96 | ) |
| 97 | .clearer |
| 98 | .row |
| 99 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 100 | .button-group |
| 101 | button#peopleBtn(onClick="window.doClick('modalPeople')") |
| 102 | | {{ st.tr["Who's there?"] }} |
| 103 | button(onClick="window.doClick('modalNewgame')") |
| 104 | | {{ st.tr["New game"] }} |
| 105 | .row |
| 106 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 107 | div#div2 |
| 108 | .button-group |
| 109 | button.tabbtn#btnClive(@click="setDisplay('c','live',$event)") |
| 110 | | {{ st.tr["Live challenges"] }} |
| 111 | button.tabbtn#btnCcorr(@click="setDisplay('c','corr',$event)") |
| 112 | | {{ st.tr["Correspondance challenges"] }} |
| 113 | ChallengeList( |
| 114 | v-show="cdisplay=='live'" |
| 115 | :challenges="filterChallenges('live')" |
| 116 | @click-challenge="clickChallenge" |
| 117 | ) |
| 118 | ChallengeList( |
| 119 | v-show="cdisplay=='corr'" |
| 120 | :challenges="filterChallenges('corr')" |
| 121 | @click-challenge="clickChallenge" |
| 122 | ) |
| 123 | div#div3 |
| 124 | .button-group |
| 125 | button.tabbtn#btnGlive(@click="setDisplay('g','live',$event)") |
| 126 | | {{ st.tr["Live games"] }} |
| 127 | button.tabbtn#btnGcorr(@click="setDisplay('g','corr',$event)") |
| 128 | | {{ st.tr["Correspondance games"] }} |
| 129 | GameList( |
| 130 | v-show="gdisplay=='live'" |
| 131 | :games="filterGames('live')" |
| 132 | :showBoth="true" |
| 133 | @show-game="showGame" |
| 134 | ) |
| 135 | GameList( |
| 136 | v-show="gdisplay=='corr'" |
| 137 | :games="filterGames('corr')" |
| 138 | :showBoth="true" |
| 139 | @show-game="showGame" |
| 140 | ) |
| 141 | </template> |
| 142 | |
| 143 | <script> |
| 144 | import { store } from "@/store"; |
| 145 | import { checkChallenge } from "@/data/challengeCheck"; |
| 146 | import { ArrayFun } from "@/utils/array"; |
| 147 | import { ajax } from "@/utils/ajax"; |
| 148 | import params from "@/parameters"; |
| 149 | import { getRandString, shuffle } from "@/utils/alea"; |
| 150 | import { getDiagram } from "@/utils/printDiagram"; |
| 151 | import Chat from "@/components/Chat.vue"; |
| 152 | import GameList from "@/components/GameList.vue"; |
| 153 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 154 | import { GameStorage } from "@/utils/gameStorage"; |
| 155 | import { processModalClick } from "@/utils/modalClick"; |
| 156 | export default { |
| 157 | name: "my-hall", |
| 158 | components: { |
| 159 | Chat, |
| 160 | GameList, |
| 161 | ChallengeList |
| 162 | }, |
| 163 | data: function() { |
| 164 | return { |
| 165 | st: store.state, |
| 166 | cdisplay: "live", //or corr |
| 167 | gdisplay: "live", |
| 168 | games: [], |
| 169 | challenges: [], |
| 170 | people: {}, |
| 171 | infoMessage: "", |
| 172 | newchallenge: { |
| 173 | fen: "", |
| 174 | vid: parseInt(localStorage.getItem("vid")) || 0, |
| 175 | to: "", //name of challenged player (if any) |
| 176 | cadence: localStorage.getItem("cadence") || "", |
| 177 | // VariantRules object, stored to not interfere with |
| 178 | // diagrams of targetted challenges: |
| 179 | V: null, |
| 180 | vname: "", |
| 181 | diag: "" //visualizing FEN |
| 182 | }, |
| 183 | tchallDiag: "", |
| 184 | curChallToAccept: {from: {}}, |
| 185 | newChat: "", |
| 186 | conn: null, |
| 187 | connexionString: "", |
| 188 | // Related to (killing of) self multi-connects: |
| 189 | newConnect: {}, |
| 190 | killed: {} |
| 191 | }; |
| 192 | }, |
| 193 | watch: { |
| 194 | // st.variants changes only once, at loading from [] to [...] |
| 195 | "st.variants": function() { |
| 196 | // Set potential challenges and games variant names: |
| 197 | this.challenges.concat(this.games).forEach(o => { |
| 198 | if (o.vname == "") o.vname = this.getVname(o.vid); |
| 199 | }); |
| 200 | if (!this.newchallenge.V && this.newchallenge.vid > 0) |
| 201 | this.loadNewchallVariant(); |
| 202 | } |
| 203 | }, |
| 204 | computed: { |
| 205 | anonymousCount: function() { |
| 206 | let count = 0; |
| 207 | Object.values(this.people).forEach(p => { |
| 208 | count += !p.name ? 1 : 0; |
| 209 | }); |
| 210 | return count; |
| 211 | } |
| 212 | }, |
| 213 | created: function() { |
| 214 | if (this.st.variants.length > 0 && this.newchallenge.vid > 0) |
| 215 | this.loadNewchallVariant(); |
| 216 | const my = this.st.user; |
| 217 | this.$set(this.people, my.sid, { id: my.id, name: my.name, pages: ["/"] }); |
| 218 | // Ask server for current corr games (all but mines) |
| 219 | ajax( |
| 220 | "/games", |
| 221 | "GET", |
| 222 | { uid: this.st.user.id, excluded: true }, |
| 223 | response => { |
| 224 | this.games = this.games.concat( |
| 225 | response.games.map(g => { |
| 226 | const type = this.classifyObject(g); |
| 227 | const vname = this.getVname(g.vid); |
| 228 | return Object.assign({}, g, { type: type, vname: vname }); |
| 229 | }) |
| 230 | ); |
| 231 | } |
| 232 | ); |
| 233 | // Also ask for corr challenges (open + sent by/to me) |
| 234 | ajax("/challenges", "GET", { uid: this.st.user.id }, response => { |
| 235 | // Gather all senders names, and then retrieve full identity: |
| 236 | // (TODO [perf]: some might be online...) |
| 237 | let names = {}; |
| 238 | response.challenges.forEach(c => { |
| 239 | if (c.uid != this.st.user.id) names[c.uid] = ""; |
| 240 | else if (!!c.target && c.target != this.st.user.id) |
| 241 | names[c.target] = ""; |
| 242 | }); |
| 243 | const addChallenges = () => { |
| 244 | names[this.st.user.id] = this.st.user.name; //in case of |
| 245 | this.challenges = this.challenges.concat( |
| 246 | response.challenges.map(c => { |
| 247 | const from = { name: names[c.uid], id: c.uid }; //or just name |
| 248 | const type = this.classifyObject(c); |
| 249 | const vname = this.getVname(c.vid); |
| 250 | return Object.assign( |
| 251 | {}, |
| 252 | { |
| 253 | type: type, |
| 254 | vname: vname, |
| 255 | from: from, |
| 256 | to: c.target ? names[c.target] : "" |
| 257 | }, |
| 258 | c |
| 259 | ); |
| 260 | }) |
| 261 | ); |
| 262 | }; |
| 263 | if (Object.keys(names).length > 0) { |
| 264 | ajax( |
| 265 | "/users", |
| 266 | "GET", |
| 267 | { ids: Object.keys(names).join(",") }, |
| 268 | response2 => { |
| 269 | response2.users.forEach(u => { |
| 270 | names[u.id] = u.name; |
| 271 | }); |
| 272 | addChallenges(); |
| 273 | } |
| 274 | ); |
| 275 | } else addChallenges(); |
| 276 | }); |
| 277 | const connectAndPoll = () => { |
| 278 | this.send("connect"); |
| 279 | this.send("pollclientsandgamers"); |
| 280 | }; |
| 281 | // Initialize connection |
| 282 | this.connexionString = |
| 283 | params.socketUrl + |
| 284 | "/?sid=" + |
| 285 | this.st.user.sid + |
| 286 | "&tmpId=" + |
| 287 | getRandString() + |
| 288 | "&page=" + |
| 289 | encodeURIComponent(this.$route.path); |
| 290 | this.conn = new WebSocket(this.connexionString); |
| 291 | this.conn.onopen = connectAndPoll; |
| 292 | this.conn.onmessage = this.socketMessageListener; |
| 293 | this.conn.onclose = this.socketCloseListener; |
| 294 | }, |
| 295 | mounted: function() { |
| 296 | ["peopleWrap", "infoDiv", "newgameDiv"].forEach(eltName => { |
| 297 | let elt = document.getElementById(eltName); |
| 298 | elt.addEventListener("click", processModalClick); |
| 299 | }); |
| 300 | document.querySelectorAll("#predefinedCadences > button").forEach(b => { |
| 301 | b.addEventListener("click", () => { |
| 302 | this.newchallenge.cadence = b.innerHTML; |
| 303 | }); |
| 304 | }); |
| 305 | const dispCorr = this.$route.query["disp"]; |
| 306 | const showCtype = |
| 307 | dispCorr || localStorage.getItem("type-challenges") || "live"; |
| 308 | const showGtype = |
| 309 | dispCorr || localStorage.getItem("type-games") || "live"; |
| 310 | this.setDisplay("c", showCtype); |
| 311 | this.setDisplay("g", showGtype); |
| 312 | }, |
| 313 | beforeDestroy: function() { |
| 314 | this.send("disconnect"); |
| 315 | }, |
| 316 | methods: { |
| 317 | // Helpers: |
| 318 | send: function(code, obj) { |
| 319 | if (this.conn) { |
| 320 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
| 321 | } |
| 322 | }, |
| 323 | getVname: function(vid) { |
| 324 | const variant = this.st.variants.find(v => v.id == vid); |
| 325 | // this.st.variants might be uninitialized (variant == null) |
| 326 | return variant ? variant.name : ""; |
| 327 | }, |
| 328 | filterChallenges: function(type) { |
| 329 | return this.challenges.filter(c => c.type == type); |
| 330 | }, |
| 331 | filterGames: function(type) { |
| 332 | return this.games.filter(g => g.type == type); |
| 333 | }, |
| 334 | classifyObject: function(o) { |
| 335 | //challenge or game |
| 336 | return o.cadence.indexOf("d") === -1 ? "live" : "corr"; |
| 337 | }, |
| 338 | setDisplay: function(letter, type, e) { |
| 339 | this[letter + "display"] = type; |
| 340 | localStorage.setItem( |
| 341 | "type-" + (letter == "c" ? "challenges" : "games"), |
| 342 | type |
| 343 | ); |
| 344 | let elt = e |
| 345 | ? e.target |
| 346 | : document.getElementById("btn" + letter.toUpperCase() + type); |
| 347 | elt.classList.add("active"); |
| 348 | elt.classList.remove("somethingnew"); //in case of |
| 349 | if (elt.previousElementSibling) |
| 350 | elt.previousElementSibling.classList.remove("active"); |
| 351 | else elt.nextElementSibling.classList.remove("active"); |
| 352 | }, |
| 353 | isGamer: function(sid) { |
| 354 | return this.people[sid].pages.some(p => p.indexOf("/game/") >= 0); |
| 355 | }, |
| 356 | getActionLabel: function(sid) { |
| 357 | return this.people[sid].pages.some(p => p == "/") |
| 358 | ? "Challenge" |
| 359 | : "Observe"; |
| 360 | }, |
| 361 | challOrWatch: function(sid) { |
| 362 | if (this.people[sid].pages.some(p => p == "/")) { |
| 363 | // Available, in Hall |
| 364 | this.newchallenge.to = this.people[sid].name; |
| 365 | document.getElementById("modalPeople").checked = false; |
| 366 | window.doClick("modalNewgame"); |
| 367 | } else { |
| 368 | // In some game, maybe playing maybe not: show a random one |
| 369 | let gids = []; |
| 370 | this.people[sid].pages.forEach(p => { |
| 371 | const matchGid = p.match(/[a-zA-Z0-9]+$/); |
| 372 | if (matchGid) gids.push(matchGid[0]); |
| 373 | }); |
| 374 | const gid = gids[Math.floor(Math.random() * gids.length)]; |
| 375 | const game = this.games.find(g => g.id == gid); |
| 376 | if (game) this.showGame(game); |
| 377 | else this.$router.push("/game/" + gid); //game vs. me |
| 378 | } |
| 379 | }, |
| 380 | showGame: function(g) { |
| 381 | // NOTE: we are an observer, since only games I don't play are shown here |
| 382 | // ==> Moves sent by connected remote player(s) if live game |
| 383 | let url = "/game/" + g.id; |
| 384 | if (g.type == "live") |
| 385 | url += "?rid=" + g.rids[Math.floor(Math.random() * g.rids.length)]; |
| 386 | this.$router.push(url); |
| 387 | }, |
| 388 | resetChatColor: function() { |
| 389 | // TODO: this is called twice, once on opening an once on closing |
| 390 | document.getElementById("peopleBtn").classList.remove("somethingnew"); |
| 391 | }, |
| 392 | processChat: function(chat) { |
| 393 | this.send("newchat", { data: chat }); |
| 394 | }, |
| 395 | // Messaging center: |
| 396 | socketMessageListener: function(msg) { |
| 397 | if (!this.conn) return; |
| 398 | const data = JSON.parse(msg.data); |
| 399 | switch (data.code) { |
| 400 | case "pollclientsandgamers": { |
| 401 | // Since people can be both in Hall and Game, |
| 402 | // need to track "askIdentity" requests: |
| 403 | let identityAsked = {}; |
| 404 | data.sockIds.forEach(s => { |
| 405 | const page = s.page || "/"; |
| 406 | if (s.sid != this.st.user.sid && !identityAsked[s.sid]) { |
| 407 | identityAsked[s.sid] = true; |
| 408 | this.send("askidentity", { target: s.sid, page: page }); |
| 409 | } |
| 410 | if (!this.people[s.sid]) |
| 411 | this.$set(this.people, s.sid, { id: 0, name: "", pages: [page] }); |
| 412 | else if (this.people[s.sid].pages.indexOf(page) < 0) |
| 413 | this.people[s.sid].pages.push(page); |
| 414 | if (!s.page) |
| 415 | //peer is in Hall |
| 416 | this.send("askchallenge", { target: s.sid }); |
| 417 | //peer is in Game |
| 418 | else this.send("askgame", { target: s.sid, page: page }); |
| 419 | }); |
| 420 | break; |
| 421 | } |
| 422 | case "connect": |
| 423 | case "gconnect": { |
| 424 | const page = data.page || "/"; |
| 425 | // NOTE: player could have been polled earlier, but might have logged in then |
| 426 | // So it's a good idea to ask identity if he was anonymous. |
| 427 | // But only ask game / challenge if currently disconnected. |
| 428 | if (!this.people[data.from]) { |
| 429 | this.$set(this.people, data.from, { |
| 430 | name: "", |
| 431 | id: 0, |
| 432 | pages: [page] |
| 433 | }); |
| 434 | if (data.code == "connect") |
| 435 | this.send("askchallenge", { target: data.from }); |
| 436 | else this.send("askgame", { target: data.from, page: page }); |
| 437 | } else { |
| 438 | // append page if not already in list |
| 439 | if (this.people[data.from].pages.indexOf(page) < 0) |
| 440 | this.people[data.from].pages.push(page); |
| 441 | } |
| 442 | if (this.people[data.from].id == 0) { |
| 443 | this.newConnect[data.from] = true; //for self multi-connects tests |
| 444 | this.send("askidentity", { target: data.from, page: page }); |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | case "disconnect": |
| 449 | case "gdisconnect": { |
| 450 | // If the user reloads the page twice very quickly (experienced with Firefox), |
| 451 | // the first reload won't have time to connect but will trigger a "close" event anyway. |
| 452 | // ==> Next check is required. |
| 453 | if (!this.people[data.from]) return; |
| 454 | // Disconnect means no more tmpIds: |
| 455 | if (data.code == "disconnect") { |
| 456 | // Remove the live challenge sent by this player: |
| 457 | ArrayFun.remove(this.challenges, c => c.from.sid == data.from); |
| 458 | } else { |
| 459 | // Remove the matching live game if now unreachable |
| 460 | const gid = data.page.match(/[a-zA-Z0-9]+$/)[0]; |
| 461 | const gidx = this.games.findIndex(g => g.id == gid); |
| 462 | if (gidx >= 0) { |
| 463 | const game = this.games[gidx]; |
| 464 | if ( |
| 465 | game.type == "live" && |
| 466 | game.rids.length == 1 && |
| 467 | game.rids[0] == data.from |
| 468 | ) { |
| 469 | this.games.splice(gidx, 1); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | const page = data.page || "/"; |
| 474 | ArrayFun.remove(this.people[data.from].pages, p => p == page); |
| 475 | if (this.people[data.from].pages.length == 0) |
| 476 | this.$delete(this.people, data.from); |
| 477 | break; |
| 478 | } |
| 479 | case "killed": |
| 480 | // I logged in elsewhere: |
| 481 | alert(this.st.tr["New connexion detected: tab now offline"]); |
| 482 | // TODO: this fails. See https://github.com/websockets/ws/issues/489 |
| 483 | //this.conn.removeEventListener("message", this.socketMessageListener); |
| 484 | //this.conn.removeEventListener("close", this.socketCloseListener); |
| 485 | //this.conn.close(); |
| 486 | this.conn = null; |
| 487 | break; |
| 488 | case "askidentity": { |
| 489 | // Request for identification (TODO: anonymous shouldn't need to reply) |
| 490 | const me = { |
| 491 | // Decompose to avoid revealing email |
| 492 | name: this.st.user.name, |
| 493 | sid: this.st.user.sid, |
| 494 | id: this.st.user.id |
| 495 | }; |
| 496 | this.send("identity", { data: me, target: data.from }); |
| 497 | break; |
| 498 | } |
| 499 | case "identity": { |
| 500 | const user = data.data; |
| 501 | if (user.name) { |
| 502 | // If I multi-connect, kill current connexion if no mark (I'm older) |
| 503 | if ( |
| 504 | this.newConnect[user.sid] && |
| 505 | user.id > 0 && |
| 506 | user.id == this.st.user.id && |
| 507 | user.sid != this.st.user.sid |
| 508 | ) { |
| 509 | if (!this.killed[this.st.user.sid]) { |
| 510 | this.send("killme", { sid: this.st.user.sid }); |
| 511 | this.killed[this.st.user.sid] = true; |
| 512 | } |
| 513 | } |
| 514 | if (user.sid != this.st.user.sid) { |
| 515 | //I already know my identity... |
| 516 | this.$set(this.people, user.sid, { |
| 517 | id: user.id, |
| 518 | name: user.name, |
| 519 | pages: this.people[user.sid].pages |
| 520 | }); |
| 521 | } |
| 522 | } |
| 523 | delete this.newConnect[user.sid]; |
| 524 | break; |
| 525 | } |
| 526 | case "askchallenge": { |
| 527 | // Send my current live challenge (if any) |
| 528 | const cIdx = this.challenges.findIndex( |
| 529 | c => c.from.sid == this.st.user.sid && c.type == "live" |
| 530 | ); |
| 531 | if (cIdx >= 0) { |
| 532 | const c = this.challenges[cIdx]; |
| 533 | // NOTE: in principle, should only send targeted challenge to the target. |
| 534 | // But we may not know yet the identity of the target (just name), |
| 535 | // so cannot decide if data.from is the target or not. |
| 536 | const myChallenge = { |
| 537 | id: c.id, |
| 538 | from: this.st.user.sid, |
| 539 | to: c.to, |
| 540 | fen: c.fen, |
| 541 | vid: c.vid, |
| 542 | cadence: c.cadence, |
| 543 | added: c.added |
| 544 | }; |
| 545 | this.send("challenge", { data: myChallenge, target: data.from }); |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | case "challenge": //after "askchallenge" |
| 550 | case "newchallenge": { |
| 551 | // NOTE about next condition: see "askchallenge" case. |
| 552 | const chall = data.data; |
| 553 | if ( |
| 554 | !chall.to || |
| 555 | (this.people[chall.from].id > 0 && |
| 556 | (chall.from == this.st.user.sid || chall.to == this.st.user.name)) |
| 557 | ) { |
| 558 | let newChall = Object.assign({}, chall); |
| 559 | newChall.type = this.classifyObject(chall); |
| 560 | newChall.added = Date.now(); |
| 561 | let fromValues = Object.assign({}, this.people[chall.from]); |
| 562 | delete fromValues["pages"]; //irrelevant in this context |
| 563 | newChall.from = Object.assign({ sid: chall.from }, fromValues); |
| 564 | newChall.vname = this.getVname(newChall.vid); |
| 565 | this.challenges.push(newChall); |
| 566 | if ( |
| 567 | (newChall.type == "live" && this.cdisplay == "corr") || |
| 568 | (newChall.type == "corr" && this.cdisplay == "live") |
| 569 | ) { |
| 570 | document |
| 571 | .getElementById("btnC" + newChall.type) |
| 572 | .classList.add("somethingnew"); |
| 573 | } |
| 574 | } |
| 575 | break; |
| 576 | } |
| 577 | case "refusechallenge": { |
| 578 | const cid = data.data; |
| 579 | ArrayFun.remove(this.challenges, c => c.id == cid); |
| 580 | alert(this.st.tr["Challenge declined"]); |
| 581 | break; |
| 582 | } |
| 583 | case "deletechallenge": { |
| 584 | // NOTE: the challenge may be already removed |
| 585 | const cid = data.data; |
| 586 | ArrayFun.remove(this.challenges, c => c.id == cid); |
| 587 | break; |
| 588 | } |
| 589 | case "game": //individual request |
| 590 | case "newgame": { |
| 591 | // NOTE: it may be live or correspondance |
| 592 | const game = data.data; |
| 593 | // Ignore games where I play (corr games) |
| 594 | if (game.players.every(p => p.id != this.st.user.id)) |
| 595 | { |
| 596 | let locGame = this.games.find(g => g.id == game.id); |
| 597 | if (!locGame) { |
| 598 | let newGame = game; |
| 599 | newGame.type = this.classifyObject(game); |
| 600 | newGame.vname = this.getVname(game.vid); |
| 601 | if (!game.score) |
| 602 | //if new game from Hall |
| 603 | newGame.score = "*"; |
| 604 | newGame.rids = [game.rid]; |
| 605 | delete newGame["rid"]; |
| 606 | this.games.push(newGame); |
| 607 | if ( |
| 608 | (newGame.type == "live" && this.gdisplay == "corr") || |
| 609 | (newGame.type == "corr" && this.gdisplay == "live") |
| 610 | ) { |
| 611 | document |
| 612 | .getElementById("btnG" + newGame.type) |
| 613 | .classList.add("somethingnew"); |
| 614 | } |
| 615 | } else { |
| 616 | // Append rid (if not already in list) |
| 617 | if (!locGame.rids.includes(game.rid)) locGame.rids.push(game.rid); |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | } |
| 622 | case "result": { |
| 623 | let g = this.games.find(g => g.id == data.gid); |
| 624 | if (g) g.score = data.score; |
| 625 | break; |
| 626 | } |
| 627 | case "startgame": { |
| 628 | // New game just started: data contain all information |
| 629 | const gameInfo = data.data; |
| 630 | if (this.classifyObject(gameInfo) == "live") |
| 631 | this.startNewGame(gameInfo); |
| 632 | else { |
| 633 | this.infoMessage = |
| 634 | this.st.tr["New correspondance game:"] + |
| 635 | " <a href='#/game/" + |
| 636 | gameInfo.id + |
| 637 | "'>" + |
| 638 | "#/game/" + |
| 639 | gameInfo.id + |
| 640 | "</a>"; |
| 641 | let modalBox = document.getElementById("modalInfo"); |
| 642 | modalBox.checked = true; |
| 643 | } |
| 644 | break; |
| 645 | } |
| 646 | case "newchat": |
| 647 | this.newChat = data.data; |
| 648 | if (!document.getElementById("modalPeople").checked) |
| 649 | document.getElementById("peopleBtn").classList.add("somethingnew"); |
| 650 | break; |
| 651 | } |
| 652 | }, |
| 653 | socketCloseListener: function() { |
| 654 | if (!this.conn) return; |
| 655 | this.conn = new WebSocket(this.connexionString); |
| 656 | this.conn.addEventListener("message", this.socketMessageListener); |
| 657 | this.conn.addEventListener("close", this.socketCloseListener); |
| 658 | }, |
| 659 | // Challenge lifecycle: |
| 660 | loadNewchallVariant: async function(cb) { |
| 661 | const vname = this.getVname(this.newchallenge.vid); |
| 662 | const vModule = await import("@/variants/" + vname + ".js"); |
| 663 | this.newchallenge.V = vModule.VariantRules; |
| 664 | this.newchallenge.vname = vname; |
| 665 | if (cb) |
| 666 | cb(); |
| 667 | }, |
| 668 | trySetNewchallDiag: function() { |
| 669 | if (!this.newchallenge.fen) { |
| 670 | this.newchallenge.diag = ""; |
| 671 | return; |
| 672 | } |
| 673 | // If vid > 0 then the variant is loaded (function above): |
| 674 | window.V = this.newchallenge.V; |
| 675 | if ( |
| 676 | this.newchallenge.vid > 0 && |
| 677 | this.newchallenge.fen && |
| 678 | V.IsGoodFen(this.newchallenge.fen) |
| 679 | ) { |
| 680 | const parsedFen = V.ParseFen(this.newchallenge.fen); |
| 681 | this.newchallenge.diag = getDiagram({ |
| 682 | position: parsedFen.position, |
| 683 | orientation: V.GetOppCol(parsedFen.turn) |
| 684 | }); |
| 685 | } |
| 686 | }, |
| 687 | newChallenge: async function() { |
| 688 | if (this.newchallenge.cadence.match(/^[0-9]+$/)) |
| 689 | this.newchallenge.cadence += "+0"; //assume minutes, no increment |
| 690 | const ctype = this.classifyObject(this.newchallenge); |
| 691 | // TODO: cadence still unchecked so ctype could be wrong... |
| 692 | let error = ""; |
| 693 | if (!this.newchallenge.vid) |
| 694 | error = this.st.tr["Please select a variant"]; |
| 695 | else if (ctype == "corr" && this.st.user.id <= 0) |
| 696 | error = this.st.tr["Please log in to play correspondance games"]; |
| 697 | else if (this.newchallenge.to) { |
| 698 | if (this.newchallenge.to == this.st.user.name) |
| 699 | error = this.st.tr["Self-challenge is forbidden"]; |
| 700 | else if ( |
| 701 | ctype == "live" && |
| 702 | Object.values(this.people).every(p => p.name != this.newchallenge.to) |
| 703 | ) |
| 704 | error = this.newchallenge.to + " " + this.st.tr["is not online"]; |
| 705 | } |
| 706 | if (error) { |
| 707 | alert(error); |
| 708 | return; |
| 709 | } |
| 710 | window.V = this.newchallenge.V; |
| 711 | error = checkChallenge(this.newchallenge); |
| 712 | if (error) { |
| 713 | alert(error); |
| 714 | return; |
| 715 | } |
| 716 | // NOTE: "from" information is not required here |
| 717 | let chall = Object.assign({}, this.newchallenge); |
| 718 | const finishAddChallenge = cid => { |
| 719 | chall.id = cid || "c" + getRandString(); |
| 720 | // Remove old challenge if any (only one at a time of a given type): |
| 721 | const cIdx = this.challenges.findIndex( |
| 722 | c => |
| 723 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && |
| 724 | c.type == ctype |
| 725 | ); |
| 726 | if (cIdx >= 0) { |
| 727 | // Delete current challenge (will be replaced now) |
| 728 | this.send("deletechallenge", { data: this.challenges[cIdx].id }); |
| 729 | if (ctype == "corr") { |
| 730 | ajax("/challenges", "DELETE", { id: this.challenges[cIdx].id }); |
| 731 | } |
| 732 | this.challenges.splice(cIdx, 1); |
| 733 | } |
| 734 | this.send("newchallenge", { |
| 735 | data: Object.assign({ from: this.st.user.sid }, chall) |
| 736 | }); |
| 737 | // Add new challenge: |
| 738 | chall.from = { |
| 739 | // Decompose to avoid revealing email |
| 740 | sid: this.st.user.sid, |
| 741 | id: this.st.user.id, |
| 742 | name: this.st.user.name |
| 743 | }; |
| 744 | chall.added = Date.now(); |
| 745 | // NOTE: vname and type are redundant (can be deduced from cadence + vid) |
| 746 | chall.type = ctype; |
| 747 | chall.vname = this.newchallenge.vname; |
| 748 | this.challenges.push(chall); |
| 749 | // Remember cadence + vid for quicker further challenges: |
| 750 | localStorage.setItem("cadence", chall.cadence); |
| 751 | localStorage.setItem("vid", chall.vid); |
| 752 | document.getElementById("modalNewgame").checked = false; |
| 753 | }; |
| 754 | if (ctype == "live") { |
| 755 | // Live challenges have a random ID |
| 756 | finishAddChallenge(null); |
| 757 | } else { |
| 758 | // Correspondance game: send challenge to server |
| 759 | ajax("/challenges", "POST", { chall: chall }, response => { |
| 760 | finishAddChallenge(response.cid); |
| 761 | }); |
| 762 | } |
| 763 | }, |
| 764 | // Callback function after a diagram was showed to accept |
| 765 | // or refuse targetted challenge: |
| 766 | decisionChallenge: function(accepted) { |
| 767 | this.curChallToAccept.accepted = accepted; |
| 768 | this.finishProcessingChallenge(this.curChallToAccept); |
| 769 | document.getElementById("modalAccept").checked = false; |
| 770 | }, |
| 771 | finishProcessingChallenge: function(c) { |
| 772 | if (c.accepted) { |
| 773 | c.seat = { |
| 774 | // Again, avoid c.seat = st.user to not reveal email |
| 775 | sid: this.st.user.sid, |
| 776 | id: this.st.user.id, |
| 777 | name: this.st.user.name |
| 778 | }; |
| 779 | this.launchGame(c); |
| 780 | } else { |
| 781 | this.send("refusechallenge", { data: c.id, target: c.from.sid }); |
| 782 | } |
| 783 | this.send("deletechallenge", { data: c.id }); |
| 784 | }, |
| 785 | clickChallenge: async function(c) { |
| 786 | const myChallenge = |
| 787 | c.from.sid == this.st.user.sid || //live |
| 788 | (this.st.user.id > 0 && c.from.id == this.st.user.id); //corr |
| 789 | if (!myChallenge) { |
| 790 | if (c.type == "corr" && this.st.user.id <= 0) { |
| 791 | alert(this.st.tr["Please log in to accept corr challenges"]); |
| 792 | return; |
| 793 | } |
| 794 | c.accepted = true; |
| 795 | const vModule = await import("@/variants/" + c.vname + ".js"); |
| 796 | window.V = vModule.VariantRules; |
| 797 | if (c.to) { |
| 798 | // c.to == this.st.user.name (connected) |
| 799 | if (c.fen) { |
| 800 | const parsedFen = V.ParseFen(c.fen); |
| 801 | c.mycolor = V.GetOppCol(parsedFen.turn); |
| 802 | this.tchallDiag = getDiagram({ |
| 803 | position: parsedFen.position, |
| 804 | orientation: c.mycolor |
| 805 | }); |
| 806 | this.curChallToAccept = c; |
| 807 | document.getElementById("modalAccept").checked = true; |
| 808 | } |
| 809 | else { |
| 810 | if (!confirm(this.st.tr["Accept challenge?"])) |
| 811 | c.accepted = false; |
| 812 | this.finishProcessingChallenge(c); |
| 813 | } |
| 814 | } |
| 815 | else |
| 816 | this.finishProcessingChallenge(c); |
| 817 | } |
| 818 | else { |
| 819 | // My challenge |
| 820 | if (c.type == "corr") { |
| 821 | ajax("/challenges", "DELETE", { id: c.id }); |
| 822 | } |
| 823 | this.send("deletechallenge", { data: c.id }); |
| 824 | } |
| 825 | // In all cases, the challenge is consumed: |
| 826 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 827 | }, |
| 828 | // NOTE: when launching game, the challenge is already being deleted |
| 829 | launchGame: function(c) { |
| 830 | // These game informations will be shared |
| 831 | let gameInfo = { |
| 832 | id: getRandString(), |
| 833 | fen: c.fen || V.GenRandInitFen(), |
| 834 | // White player index 0, black player index 1: |
| 835 | players: c.mycolor |
| 836 | ? (c.mycolor == "w" ? [c.seat, c.from] : [c.from, c.seat]) |
| 837 | : shuffle([c.from, c.seat]), |
| 838 | vid: c.vid, |
| 839 | cadence: c.cadence |
| 840 | }; |
| 841 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
| 842 | if (!oppsid) { |
| 843 | oppsid = Object.keys(this.people).find( |
| 844 | sid => this.people[sid].id == c.from.id |
| 845 | ); |
| 846 | } |
| 847 | const notifyNewgame = () => { |
| 848 | if (oppsid) |
| 849 | //opponent is online |
| 850 | this.send("startgame", { data: gameInfo, target: oppsid }); |
| 851 | // Send game info (only if live) to everyone except me in this tab |
| 852 | this.send("newgame", { data: gameInfo }); |
| 853 | }; |
| 854 | if (c.type == "live") { |
| 855 | notifyNewgame(); |
| 856 | this.startNewGame(gameInfo); |
| 857 | } //corr: game only on server |
| 858 | else { |
| 859 | ajax( |
| 860 | "/games", |
| 861 | "POST", |
| 862 | { gameInfo: gameInfo, cid: c.id }, //cid useful to delete challenge |
| 863 | response => { |
| 864 | gameInfo.id = response.gameId; |
| 865 | notifyNewgame(); |
| 866 | this.$router.push("/game/" + response.gameId); |
| 867 | } |
| 868 | ); |
| 869 | } |
| 870 | }, |
| 871 | // NOTE: for live games only (corr games start on the server) |
| 872 | startNewGame: function(gameInfo) { |
| 873 | const game = Object.assign({}, gameInfo, { |
| 874 | // (other) Game infos: constant |
| 875 | fenStart: gameInfo.fen, |
| 876 | vname: this.getVname(gameInfo.vid), |
| 877 | created: Date.now(), |
| 878 | // Game state (including FEN): will be updated |
| 879 | moves: [], |
| 880 | clocks: [-1, -1], //-1 = unstarted |
| 881 | initime: [0, 0], //initialized later |
| 882 | score: "*" |
| 883 | }); |
| 884 | GameStorage.add(game, (err) => { |
| 885 | // If an error occurred, game is not added: abort |
| 886 | if (!err) { |
| 887 | if (this.st.settings.sound >= 1) |
| 888 | new Audio("/sounds/newgame.mp3").play().catch(() => {}); |
| 889 | this.$router.push("/game/" + gameInfo.id); |
| 890 | } |
| 891 | }); |
| 892 | } |
| 893 | } |
| 894 | }; |
| 895 | </script> |
| 896 | |
| 897 | <style lang="sass" scoped> |
| 898 | .active |
| 899 | color: #42a983 |
| 900 | |
| 901 | #infoDiv > .card |
| 902 | padding: 15px 0 |
| 903 | max-width: 430px |
| 904 | |
| 905 | #newgameDiv > .card, #acceptDiv > .card |
| 906 | max-width: 767px |
| 907 | max-height: 100% |
| 908 | |
| 909 | div#peopleWrap > .card |
| 910 | max-height: 100% |
| 911 | |
| 912 | @media screen and (min-width: 1281px) |
| 913 | div#peopleWrap > .card |
| 914 | max-width: 66.67% |
| 915 | |
| 916 | @media screen and (max-width: 1280px) |
| 917 | div#peopleWrap > .card |
| 918 | max-width: 83.33% |
| 919 | |
| 920 | @media screen and (max-width: 767px) |
| 921 | div#peopleWrap > .card |
| 922 | max-width: 100% |
| 923 | |
| 924 | #players |
| 925 | width: 50% |
| 926 | position: relative |
| 927 | float: left |
| 928 | |
| 929 | #chat |
| 930 | width: 50% |
| 931 | float: left |
| 932 | position: relative |
| 933 | |
| 934 | @media screen and (max-width: 767px) |
| 935 | #players, #chats |
| 936 | width: 100% |
| 937 | |
| 938 | #chat > .card |
| 939 | max-width: 100% |
| 940 | margin: 0; |
| 941 | border: none; |
| 942 | |
| 943 | #players > p |
| 944 | margin-left: 5px |
| 945 | |
| 946 | .anonymous |
| 947 | font-style: italic |
| 948 | |
| 949 | button.player-action |
| 950 | margin-left: 32px |
| 951 | |
| 952 | .somethingnew |
| 953 | background-color: #c5fefe !important |
| 954 | |
| 955 | .tabbtn |
| 956 | background-color: #f9faee |
| 957 | |
| 958 | button.acceptBtn |
| 959 | background-color: lightgreen |
| 960 | button.refuseBtn |
| 961 | background-color: red |
| 962 | |
| 963 | #buttonsTchall |
| 964 | margin-top: 10px |
| 965 | |
| 966 | .variantName |
| 967 | font-weight: bold |
| 968 | |
| 969 | .diagram |
| 970 | margin: 0 auto |
| 971 | max-width: 400px |
| 972 | |
| 973 | #inputFen |
| 974 | width: 100% |
| 975 | |
| 976 | #div2, #div3 |
| 977 | margin-top: 15px |
| 978 | @media screen and (max-width: 767px) |
| 979 | #div2, #div3 |
| 980 | margin-top: 0 |
| 981 | </style> |