| 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 |
| 14 | p.text-center |
| 15 | span.variantName {{ curChallToAccept.vname }} |
| 16 | span {{ curChallToAccept.cadence }} |
| 17 | span {{ st.tr["with"] + " " + curChallToAccept.from.name }} |
| 18 | .diagram( |
| 19 | v-if="!!curChallToAccept.fen" |
| 20 | v-html="tchallDiag" |
| 21 | ) |
| 22 | .button-group#buttonsTchall(:style="tchallButtonsMargin()") |
| 23 | button.acceptBtn(@click="decisionChallenge(true)") |
| 24 | span {{ st.tr["Accept challenge?"] }} |
| 25 | button.refuseBtn(@click="decisionChallenge(false)") |
| 26 | span {{ st.tr["Refuse"] }} |
| 27 | input#modalNewgame.modal( |
| 28 | type="checkbox" |
| 29 | @change="cadenceFocusIfOpened($event)" |
| 30 | ) |
| 31 | div#newgameDiv( |
| 32 | role="dialog" |
| 33 | data-checkbox="modalNewgame" |
| 34 | ) |
| 35 | .card |
| 36 | label#closeNewgame.modal-close(for="modalNewgame") |
| 37 | div(@keyup.enter="issueNewChallenge()") |
| 38 | fieldset |
| 39 | label(for="selectVariant") {{ st.tr["Variant"] }} * |
| 40 | select#selectVariant( |
| 41 | @change="loadNewchallVariant(trySetNewchallDiag)" |
| 42 | v-model="newchallenge.vid" |
| 43 | ) |
| 44 | option( |
| 45 | v-for="v in st.variants" |
| 46 | :value="v.id" |
| 47 | :selected="newchallenge.vid==v.id" |
| 48 | ) |
| 49 | | {{ v.name }} |
| 50 | fieldset |
| 51 | label(for="cadence") {{ st.tr["Cadence"] }} * |
| 52 | div#predefinedCadences |
| 53 | button(type="button") 15+5 |
| 54 | button(type="button") 45+30 |
| 55 | button(type="button") 3d |
| 56 | button(type="button") 7d |
| 57 | input#cadence( |
| 58 | type="text" |
| 59 | v-model="newchallenge.cadence" |
| 60 | placeholder="5+0, 1h+30s, 5d ..." |
| 61 | ) |
| 62 | fieldset |
| 63 | label(for="selectRandomLevel") {{ st.tr["Randomness"] }} * |
| 64 | select#selectRandomLevel(v-model="newchallenge.randomness") |
| 65 | option(value="0") {{ st.tr["Deterministic"] }} |
| 66 | option(value="1") {{ st.tr["Symmetric random"] }} |
| 67 | option(value="2") {{ st.tr["Asymmetric random"] }} |
| 68 | fieldset |
| 69 | label(for="memorizeChall") {{ st.tr["Memorize"] }} |
| 70 | input#memorizeChall( |
| 71 | type="checkbox" |
| 72 | v-model="newchallenge.memorize" |
| 73 | ) |
| 74 | fieldset(v-if="st.user.id > 0") |
| 75 | label(for="selectPlayers") {{ st.tr["Play with"] }} |
| 76 | select#selectPlayersInList( |
| 77 | v-model="newchallenge.to" |
| 78 | @change="changeChallTarget()" |
| 79 | ) |
| 80 | option(value="") |
| 81 | option( |
| 82 | v-for="p in Object.values(people)" |
| 83 | v-if="!!p.name" |
| 84 | :value="p.name" |
| 85 | ) |
| 86 | | {{ p.name }} |
| 87 | input#selectPlayers( |
| 88 | type="text" |
| 89 | v-model="newchallenge.to" |
| 90 | ) |
| 91 | fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0") |
| 92 | input#inputFen( |
| 93 | placeholder="FEN" |
| 94 | @input="trySetNewchallDiag()" |
| 95 | type="text" |
| 96 | v-model="newchallenge.fen" |
| 97 | ) |
| 98 | .diagram(v-html="newchallenge.diag") |
| 99 | button(@click="issueNewChallenge()") {{ st.tr["Send challenge"] }} |
| 100 | input#modalPeople.modal( |
| 101 | type="checkbox" |
| 102 | @click="resetSocialColor()" |
| 103 | ) |
| 104 | div#peopleWrap( |
| 105 | role="dialog" |
| 106 | data-checkbox="modalPeople" |
| 107 | ) |
| 108 | .card |
| 109 | label.modal-close(for="modalPeople") |
| 110 | #people |
| 111 | #players |
| 112 | p( |
| 113 | v-for="sid in Object.keys(people)" |
| 114 | v-if="!!people[sid].name" |
| 115 | ) |
| 116 | span {{ people[sid].name }} |
| 117 | button.player-action( |
| 118 | v-if="isGamer(sid)" |
| 119 | @click="watchGame(sid)" |
| 120 | ) |
| 121 | | {{ st.tr["Observe"] }} |
| 122 | button.player-action( |
| 123 | v-else-if="isFocusedOnHall(sid)" |
| 124 | @click="challenge(sid)" |
| 125 | ) |
| 126 | | {{ st.tr["Challenge"] }} |
| 127 | p.anonymous @nonymous ({{ anonymousCount() }}) |
| 128 | #chat |
| 129 | Chat( |
| 130 | ref="chatcomp" |
| 131 | @mychat="processChat" |
| 132 | :pastChats="[]" |
| 133 | ) |
| 134 | .clearer |
| 135 | .row |
| 136 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 137 | .button-group |
| 138 | button#peopleBtn(@click="openModalPeople()") |
| 139 | | {{ st.tr["Who's there?"] }} |
| 140 | button(@click="showNewchallengeForm()") |
| 141 | | {{ st.tr["New game"] }} |
| 142 | .row(v-if="presetChalls.length > 0") |
| 143 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 144 | h4.text-center {{ st.tr["Preset challenges"] }} |
| 145 | table |
| 146 | thead |
| 147 | tr |
| 148 | th {{ st.tr["Variant"] }} |
| 149 | th {{ st.tr["Cadence"] }} |
| 150 | th {{ st.tr["Random?"] }} |
| 151 | th |
| 152 | tbody |
| 153 | tr( |
| 154 | v-for="pc in presetChalls" |
| 155 | @click="newChallFromPreset(pc)" |
| 156 | ) |
| 157 | td {{ pc.vname }} |
| 158 | td {{ pc.cadence }} |
| 159 | td(:class="getRandomnessClass(pc)") |
| 160 | td.remove-preset(@click="removePresetChall($event, pc)") |
| 161 | img(src="/images/icons/delete.svg") |
| 162 | .row |
| 163 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 164 | div#div2 |
| 165 | .button-group |
| 166 | button.tabbtn#btnClive(@click="setDisplay('c','live',$event)") |
| 167 | | {{ st.tr["Live challenges"] }} |
| 168 | button.tabbtn#btnCcorr(@click="setDisplay('c','corr',$event)") |
| 169 | | {{ st.tr["Correspondance challenges"] }} |
| 170 | ChallengeList( |
| 171 | v-show="cdisplay=='live'" |
| 172 | :challenges="filterChallenges('live')" |
| 173 | @click-challenge="clickChallenge" |
| 174 | ) |
| 175 | ChallengeList( |
| 176 | v-show="cdisplay=='corr'" |
| 177 | :challenges="filterChallenges('corr')" |
| 178 | @click-challenge="clickChallenge" |
| 179 | ) |
| 180 | div#div3 |
| 181 | .button-group |
| 182 | button.tabbtn#btnGlive(@click="setDisplay('g','live',$event)") |
| 183 | | {{ st.tr["Live games"] }} |
| 184 | button.tabbtn#btnGcorr(@click="setDisplay('g','corr',$event)") |
| 185 | | {{ st.tr["Correspondance games"] }} |
| 186 | GameList( |
| 187 | v-show="gdisplay=='live'" |
| 188 | :games="filterGames('live')" |
| 189 | :showBoth="true" |
| 190 | @show-game="showGame" |
| 191 | ) |
| 192 | div(v-show="gdisplay=='corr'") |
| 193 | GameList( |
| 194 | :games="filterGames('corr')" |
| 195 | :showBoth="true" |
| 196 | @show-game="showGame" |
| 197 | ) |
| 198 | button#loadMoreBtn( |
| 199 | v-if="hasMore" |
| 200 | @click="loadMoreCorr()" |
| 201 | ) |
| 202 | | {{ st.tr["Load more"] }} |
| 203 | </template> |
| 204 | |
| 205 | <script> |
| 206 | import { store } from "@/store"; |
| 207 | import { checkChallenge } from "@/data/challengeCheck"; |
| 208 | import { ArrayFun } from "@/utils/array"; |
| 209 | import { ajax } from "@/utils/ajax"; |
| 210 | import params from "@/parameters"; |
| 211 | import { getRandString, shuffle } from "@/utils/alea"; |
| 212 | import { getDiagram } from "@/utils/printDiagram"; |
| 213 | import Chat from "@/components/Chat.vue"; |
| 214 | import GameList from "@/components/GameList.vue"; |
| 215 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 216 | import { GameStorage } from "@/utils/gameStorage"; |
| 217 | import { processModalClick } from "@/utils/modalClick"; |
| 218 | export default { |
| 219 | name: "my-hall", |
| 220 | components: { |
| 221 | Chat, |
| 222 | GameList, |
| 223 | ChallengeList |
| 224 | }, |
| 225 | data: function() { |
| 226 | return { |
| 227 | st: store.state, |
| 228 | cdisplay: "live", //or corr |
| 229 | gdisplay: "live", |
| 230 | // timestamp of last showed (oldest) corr game: |
| 231 | cursor: Number.MAX_SAFE_INTEGER, |
| 232 | // hasMore == TRUE: a priori there could be more games to load |
| 233 | hasMore: true, |
| 234 | games: [], |
| 235 | challenges: [], |
| 236 | people: {}, |
| 237 | infoMessage: "", |
| 238 | newchallenge: { |
| 239 | fen: "", |
| 240 | vid: parseInt(localStorage.getItem("vid")) || 0, |
| 241 | to: "", //name of challenged player (if any) |
| 242 | cadence: localStorage.getItem("cadence") || "", |
| 243 | randomness: parseInt(localStorage.getItem("challRandomness")) || 2, |
| 244 | // VariantRules object, stored to not interfere with |
| 245 | // diagrams of targetted challenges: |
| 246 | V: null, |
| 247 | vname: "", |
| 248 | diag: "", //visualizing FEN |
| 249 | memorize: false //put settings in localStorage |
| 250 | }, |
| 251 | tchallDiag: "", |
| 252 | curChallToAccept: {from: {}}, |
| 253 | presetChalls: JSON.parse(localStorage.getItem("presetChalls") || "[]"), |
| 254 | conn: null, |
| 255 | connexionString: "", |
| 256 | // Related to (killing of) self multi-connects: |
| 257 | newConnect: {}, |
| 258 | killed: {} |
| 259 | }; |
| 260 | }, |
| 261 | watch: { |
| 262 | // st.variants changes only once, at loading from [] to [...] |
| 263 | "st.variants": function() { |
| 264 | // Set potential challenges and games variant names: |
| 265 | this.challenges.concat(this.games).forEach(o => { |
| 266 | if (!o.vname) o.vname = this.getVname(o.vid); |
| 267 | }); |
| 268 | if (!this.newchallenge.V && this.newchallenge.vid > 0) |
| 269 | this.loadNewchallVariant(); |
| 270 | }, |
| 271 | $route: function(to, from) { |
| 272 | if (to.path != "/") this.cleanBeforeDestroy(); |
| 273 | } |
| 274 | }, |
| 275 | created: function() { |
| 276 | document.addEventListener('visibilitychange', this.visibilityChange); |
| 277 | window.addEventListener("beforeunload", this.cleanBeforeDestroy); |
| 278 | if (this.st.variants.length > 0 && this.newchallenge.vid > 0) |
| 279 | this.loadNewchallVariant(); |
| 280 | const my = this.st.user; |
| 281 | this.$set( |
| 282 | this.people, |
| 283 | my.sid, |
| 284 | { |
| 285 | id: my.id, |
| 286 | name: my.name, |
| 287 | pages: [{ path: "/", focus: true }] |
| 288 | } |
| 289 | ); |
| 290 | const connectAndPoll = () => { |
| 291 | this.send("connect"); |
| 292 | this.send("pollclientsandgamers"); |
| 293 | }; |
| 294 | // Initialize connection |
| 295 | this.connexionString = |
| 296 | params.socketUrl + |
| 297 | "/?sid=" + |
| 298 | this.st.user.sid + |
| 299 | "&id=" + |
| 300 | this.st.user.id + |
| 301 | "&tmpId=" + |
| 302 | getRandString() + |
| 303 | "&page=" + |
| 304 | // Hall: path is "/" (could be hard-coded as well) |
| 305 | encodeURIComponent(this.$route.path); |
| 306 | this.conn = new WebSocket(this.connexionString); |
| 307 | this.conn.onopen = connectAndPoll; |
| 308 | this.conn.addEventListener("message", this.socketMessageListener); |
| 309 | this.conn.addEventListener("close", this.socketCloseListener); |
| 310 | }, |
| 311 | mounted: function() { |
| 312 | ["peopleWrap", "infoDiv", "newgameDiv"].forEach(eltName => { |
| 313 | document.getElementById(eltName) |
| 314 | .addEventListener("click", processModalClick); |
| 315 | }); |
| 316 | document.querySelectorAll("#predefinedCadences > button").forEach(b => { |
| 317 | b.addEventListener("click", () => { |
| 318 | this.newchallenge.cadence = b.innerHTML; |
| 319 | }); |
| 320 | }); |
| 321 | const dispCorr = this.$route.query["disp"]; |
| 322 | const showCtype = |
| 323 | dispCorr || localStorage.getItem("type-challenges") || "live"; |
| 324 | const showGtype = |
| 325 | dispCorr || localStorage.getItem("type-games") || "live"; |
| 326 | this.setDisplay('c', showCtype); |
| 327 | this.setDisplay('g', showGtype); |
| 328 | // Ask server for current corr games (all but mines) |
| 329 | this.loadMoreCorr(); |
| 330 | // Also ask for corr challenges (open + sent by/to me) |
| 331 | // List them all, because they are not supposed to be that many (TODO?) |
| 332 | ajax( |
| 333 | "/challenges", |
| 334 | "GET", |
| 335 | { |
| 336 | data: { uid: this.st.user.id }, |
| 337 | success: (response) => { |
| 338 | if ( |
| 339 | response.challenges.length > 0 && |
| 340 | this.challenges.length == 0 && |
| 341 | this.cdisplay == "live" |
| 342 | ) { |
| 343 | document |
| 344 | .getElementById("btnCcorr") |
| 345 | .classList.add("somethingnew"); |
| 346 | } |
| 347 | // Gather all senders names, and then retrieve full identity: |
| 348 | // (TODO [perf]: some might be online...) |
| 349 | let names = {}; |
| 350 | response.challenges.forEach(c => { |
| 351 | if (c.uid != this.st.user.id) names[c.uid] = ""; |
| 352 | else if (!!c.target && c.target != this.st.user.id) |
| 353 | names[c.target] = ""; |
| 354 | }); |
| 355 | const addChallenges = () => { |
| 356 | names[this.st.user.id] = this.st.user.name; //in case of |
| 357 | this.challenges = this.challenges.concat( |
| 358 | response.challenges.map(c => { |
| 359 | const from = { name: names[c.uid], id: c.uid }; //or just name |
| 360 | const type = this.classifyObject(c); |
| 361 | const vname = this.getVname(c.vid); |
| 362 | return Object.assign( |
| 363 | {}, |
| 364 | { |
| 365 | type: type, |
| 366 | vname: vname, |
| 367 | from: from, |
| 368 | to: c.target ? names[c.target] : "" |
| 369 | }, |
| 370 | c |
| 371 | ); |
| 372 | }) |
| 373 | ); |
| 374 | }; |
| 375 | if (Object.keys(names).length > 0) { |
| 376 | ajax( |
| 377 | "/users", |
| 378 | "GET", |
| 379 | { |
| 380 | data: { ids: Object.keys(names).join(",") }, |
| 381 | success: (response2) => { |
| 382 | response2.users.forEach(u => { |
| 383 | names[u.id] = u.name; |
| 384 | }); |
| 385 | addChallenges(); |
| 386 | } |
| 387 | } |
| 388 | ); |
| 389 | } else addChallenges(); |
| 390 | } |
| 391 | } |
| 392 | ); |
| 393 | }, |
| 394 | beforeDestroy: function() { |
| 395 | this.cleanBeforeDestroy(); |
| 396 | }, |
| 397 | methods: { |
| 398 | cleanBeforeDestroy: function() { |
| 399 | document.removeEventListener('visibilitychange', this.visibilityChange); |
| 400 | window.removeEventListener("beforeunload", this.cleanBeforeDestroy); |
| 401 | this.send("disconnect"); |
| 402 | }, |
| 403 | getRandomnessClass: function(pc) { |
| 404 | return { |
| 405 | ["random-" + pc.randomness]: true |
| 406 | }; |
| 407 | }, |
| 408 | openModalPeople: function() { |
| 409 | window.doClick("modalPeople"); |
| 410 | document.getElementById("inputChat").focus(); |
| 411 | }, |
| 412 | anonymousCount: function() { |
| 413 | let count = 0; |
| 414 | Object.values(this.people).forEach(p => { |
| 415 | // Do not cound people who did not send their identity yet: |
| 416 | count += (!p.name && p.id === 0) ? 1 : 0; |
| 417 | }); |
| 418 | return count; |
| 419 | }, |
| 420 | visibilityChange: function() { |
| 421 | // TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27 |
| 422 | this.send( |
| 423 | document.visibilityState == "visible" |
| 424 | ? "getfocus" |
| 425 | : "losefocus" |
| 426 | ); |
| 427 | }, |
| 428 | partialResetNewchallenge: function() { |
| 429 | // Reset potential target and custom FEN: |
| 430 | this.newchallenge.to = ""; |
| 431 | this.newchallenge.fen = ""; |
| 432 | this.newchallenge.diag = ""; |
| 433 | this.newchallenge.memorize = false; |
| 434 | }, |
| 435 | showNewchallengeForm: function() { |
| 436 | this.partialResetNewchallenge(); |
| 437 | window.doClick("modalNewgame"); |
| 438 | }, |
| 439 | addPresetChall: function(chall) { |
| 440 | // Add only if not already existing: |
| 441 | if (this.presetChalls.some(c => |
| 442 | c.vid == chall.vid && |
| 443 | c.cadence == chall.cadence && |
| 444 | c.randomness == chall.randomness |
| 445 | )) { |
| 446 | return; |
| 447 | } |
| 448 | const L = this.presetChalls.length; |
| 449 | this.presetChalls.push({ |
| 450 | index: L, |
| 451 | vid: chall.vid, |
| 452 | vname: chall.vname, //redundant, but easier |
| 453 | cadence: chall.cadence, |
| 454 | randomness: chall.randomness |
| 455 | }); |
| 456 | localStorage.setItem("presetChalls", JSON.stringify(this.presetChalls)); |
| 457 | }, |
| 458 | removePresetChall: function(e, pchall) { |
| 459 | e.stopPropagation(); |
| 460 | const pchallIdx = this.presetChalls.findIndex(pc => pc.index == pchall.index); |
| 461 | this.presetChalls.splice(pchallIdx, 1); |
| 462 | localStorage.setItem("presetChalls", JSON.stringify(this.presetChalls)); |
| 463 | }, |
| 464 | tchallButtonsMargin: function() { |
| 465 | if (!!this.curChallToAccept.fen) return { "margin-top": "10px" }; |
| 466 | return {}; |
| 467 | }, |
| 468 | changeChallTarget: function() { |
| 469 | if (!this.newchallenge.to) { |
| 470 | // Reset potential FEN + diagram |
| 471 | this.newchallenge.fen = ""; |
| 472 | this.newchallenge.diag = ""; |
| 473 | } |
| 474 | }, |
| 475 | cadenceFocusIfOpened: function() { |
| 476 | if (event.target.checked) |
| 477 | document.getElementById("cadence").focus(); |
| 478 | }, |
| 479 | send: function(code, obj) { |
| 480 | if (!!this.conn) { |
| 481 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); |
| 482 | } |
| 483 | }, |
| 484 | getVname: function(vid) { |
| 485 | const variant = this.st.variants.find(v => v.id == vid); |
| 486 | // this.st.variants might be uninitialized (variant == null) |
| 487 | return variant ? variant.name : ""; |
| 488 | }, |
| 489 | filterChallenges: function(type) { |
| 490 | return this.challenges.filter(c => c.type == type); |
| 491 | }, |
| 492 | filterGames: function(type) { |
| 493 | return this.games.filter(g => g.type == type); |
| 494 | }, |
| 495 | classifyObject: function(o) { |
| 496 | // o: challenge or game |
| 497 | return o.cadence.indexOf("d") === -1 ? "live" : "corr"; |
| 498 | }, |
| 499 | setDisplay: function(letter, type, e) { |
| 500 | this[letter + "display"] = type; |
| 501 | localStorage.setItem( |
| 502 | "type-" + (letter == "c" ? "challenges" : "games"), |
| 503 | type |
| 504 | ); |
| 505 | let elt = e |
| 506 | ? e.target |
| 507 | : document.getElementById("btn" + letter.toUpperCase() + type); |
| 508 | elt.classList.add("active"); |
| 509 | elt.classList.remove("somethingnew"); //in case of |
| 510 | if (!!elt.previousElementSibling) |
| 511 | elt.previousElementSibling.classList.remove("active"); |
| 512 | else elt.nextElementSibling.classList.remove("active"); |
| 513 | }, |
| 514 | isGamer: function(sid) { |
| 515 | return this.people[sid].pages |
| 516 | .some(p => p.focus && p.path.indexOf("/game/") >= 0); |
| 517 | }, |
| 518 | isFocusedOnHall: function(sid) { |
| 519 | return ( |
| 520 | // This is meant to challenge people, thus the next 2 conditions: |
| 521 | this.st.user.id > 0 && |
| 522 | sid != this.st.user.sid && |
| 523 | this.people[sid].pages.some(p => p.path == "/" && p.focus) |
| 524 | ); |
| 525 | }, |
| 526 | challenge: function(sid) { |
| 527 | this.partialResetNewchallenge(); |
| 528 | // Available, in Hall |
| 529 | this.newchallenge.to = this.people[sid].name; |
| 530 | // TODO: also store target sid to not re-search for it |
| 531 | document.getElementById("modalPeople").checked = false; |
| 532 | window.doClick("modalNewgame"); |
| 533 | }, |
| 534 | watchGame: function(sid) { |
| 535 | // In some game, maybe playing maybe not: show a random one |
| 536 | let gids = []; |
| 537 | this.people[sid].pages.forEach(p => { |
| 538 | if (p.focus) { |
| 539 | const matchGid = p.path.match(/[a-zA-Z0-9]+$/); |
| 540 | if (!!matchGid) gids.push(matchGid[0]); |
| 541 | } |
| 542 | }); |
| 543 | const gid = gids[Math.floor(Math.random() * gids.length)]; |
| 544 | const game = this.games.find(g => g.id == gid); |
| 545 | if (!!game) this.showGame(game); |
| 546 | else this.$router.push("/game/" + gid); //game vs. me |
| 547 | }, |
| 548 | showGame: function(g) { |
| 549 | // NOTE: we are an observer, since only games I don't play are shown here |
| 550 | // ==> Moves sent by connected remote player(s) if live game |
| 551 | this.$router.push("/game/" + g.id); |
| 552 | }, |
| 553 | resetSocialColor: function() { |
| 554 | // TODO: this is called twice, once on opening an once on closing |
| 555 | document.getElementById("peopleBtn").classList.remove("somethingnew"); |
| 556 | }, |
| 557 | processChat: function(chat) { |
| 558 | this.send("newchat", { data: chat }); |
| 559 | }, |
| 560 | getOppsid: function(c) { |
| 561 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
| 562 | if (!oppsid) { |
| 563 | oppsid = Object.keys(this.people).find( |
| 564 | sid => this.people[sid].id == c.from.id |
| 565 | ); |
| 566 | } |
| 567 | return oppsid; |
| 568 | }, |
| 569 | // Messaging center: |
| 570 | socketMessageListener: function(msg) { |
| 571 | if (!this.conn) return; |
| 572 | const data = JSON.parse(msg.data); |
| 573 | switch (data.code) { |
| 574 | case "pollclientsandgamers": { |
| 575 | // Since people can be both in Hall and Game, |
| 576 | // need to track "askIdentity" requests: |
| 577 | let identityAsked = {}; |
| 578 | // TODO: shuffling and random filtering on server, if |
| 579 | // the room is really crowded. |
| 580 | data.sockIds.forEach(s => { |
| 581 | const page = s.page || "/"; |
| 582 | if (s.sid != this.st.user.sid && !identityAsked[s.sid]) { |
| 583 | this.send("askidentity", { target: s.sid, page: page }); |
| 584 | identityAsked[s.sid] = true; |
| 585 | } |
| 586 | if (!this.people[s.sid]) { |
| 587 | // Do not set name or id: identity unknown yet |
| 588 | this.people[s.sid] = { pages: [{path: page, focus: true}] }; |
| 589 | } |
| 590 | else if (!(this.people[s.sid].pages.find(p => p.path == page))) |
| 591 | this.people[s.sid].pages.push({ path: page, focus: true }); |
| 592 | if (!s.page) |
| 593 | // Peer is in Hall |
| 594 | this.send("askchallenges", { target: s.sid }); |
| 595 | // Peer is in Game: ask only if live game |
| 596 | else if (!page.match(/\/[0-9]+$/)) |
| 597 | this.send("askgame", { target: s.sid, page: page }); |
| 598 | }); |
| 599 | break; |
| 600 | } |
| 601 | case "connect": |
| 602 | case "gconnect": { |
| 603 | const page = data.page || "/"; |
| 604 | if (data.code == "connect") { |
| 605 | // Ask challenges only on first connexion: |
| 606 | if (!this.people[data.from]) |
| 607 | this.send("askchallenges", { target: data.from }); |
| 608 | } |
| 609 | // Ask game only if live: |
| 610 | else if (!page.match(/\/[0-9]+$/)) |
| 611 | this.send("askgame", { target: data.from, page: page }); |
| 612 | if (!this.people[data.from]) |
| 613 | this.people[data.from] = { pages: [{ path: page, focus: true }] }; |
| 614 | else { |
| 615 | // Append page if not already in list |
| 616 | let ppage = this.people[data.from].pages.find(p => p.path == page); |
| 617 | if (!ppage) |
| 618 | this.people[data.from].pages.push({ path: page, focus: true }); |
| 619 | else ppage.focus = true; |
| 620 | this.$forceUpdate(); //TODO: shouldn't be required |
| 621 | } |
| 622 | if (!this.people[data.from].name && this.people[data.from].id !== 0) { |
| 623 | // Identity not known yet |
| 624 | this.newConnect[data.from] = true; //for self multi-connects tests |
| 625 | this.send("askidentity", { target: data.from, page: page }); |
| 626 | } |
| 627 | break; |
| 628 | } |
| 629 | case "disconnect": |
| 630 | case "gdisconnect": { |
| 631 | // If the user reloads the page twice very quickly (experienced with Firefox), |
| 632 | // the first reload won't have time to connect but will trigger a "close" event anyway. |
| 633 | // ==> Next check is required. |
| 634 | if (!this.people[data.from]) return; |
| 635 | const page = data.page || "/"; |
| 636 | ArrayFun.remove(this.people[data.from].pages, p => p.path == page); |
| 637 | if (this.people[data.from].pages.length == 0) |
| 638 | this.$delete(this.people, data.from); |
| 639 | else this.$forceUpdate(); //TODO: shouldn't be required |
| 640 | // Disconnect means no more tmpIds: |
| 641 | if (data.code == "disconnect") { |
| 642 | // Remove the live challenges sent by this player: |
| 643 | ArrayFun.remove( |
| 644 | this.challenges, |
| 645 | c => c.type == "live" && c.from.sid == data.from, |
| 646 | "all" |
| 647 | ); |
| 648 | } else { |
| 649 | // Remove the matching live game if now unreachable |
| 650 | const gid = page.match(/[a-zA-Z0-9]+$/)[0]; |
| 651 | // Corr games are always reachable: |
| 652 | if (!gid.match(/^[0-9]+$/)) { |
| 653 | // Live games are reachable as long as someone is on the game page |
| 654 | if (Object.values(this.people).every(p => |
| 655 | p.pages.every(pg => pg.path != page))) { |
| 656 | ArrayFun.remove(this.games, g => g.id == gid); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | break; |
| 661 | } |
| 662 | case "getfocus": |
| 663 | // If user reload a page, focus may arrive earlier than connect |
| 664 | if (!!this.people[data.from]) { |
| 665 | this.people[data.from].pages |
| 666 | .find(p => p.path == data.page).focus = true; |
| 667 | this.$forceUpdate(); //TODO: shouldn't be required |
| 668 | } |
| 669 | break; |
| 670 | case "losefocus": |
| 671 | if (!!this.people[data.from]) { |
| 672 | this.people[data.from].pages |
| 673 | .find(p => p.path == data.page).focus = false; |
| 674 | this.$forceUpdate(); //TODO: shouldn't be required |
| 675 | } |
| 676 | break; |
| 677 | case "killed": |
| 678 | // I logged in elsewhere: |
| 679 | this.conn.removeEventListener("message", this.socketMessageListener); |
| 680 | this.conn.removeEventListener("close", this.socketCloseListener); |
| 681 | this.conn = null; |
| 682 | alert(this.st.tr["New connexion detected: tab now offline"]); |
| 683 | break; |
| 684 | case "askidentity": { |
| 685 | // Request for identification |
| 686 | const me = { |
| 687 | // Decompose to avoid revealing email |
| 688 | name: this.st.user.name, |
| 689 | sid: this.st.user.sid, |
| 690 | id: this.st.user.id |
| 691 | }; |
| 692 | this.send("identity", { data: me, target: data.from }); |
| 693 | break; |
| 694 | } |
| 695 | case "identity": { |
| 696 | const user = data.data; |
| 697 | let player = this.people[user.sid]; |
| 698 | // player.pages is already set |
| 699 | player.id = user.id; |
| 700 | player.name = user.name; |
| 701 | // TODO: this.$set(people, ...) fails. So forceUpdate. |
| 702 | // But this shouldn't be like that! |
| 703 | this.$forceUpdate(); |
| 704 | // If I multi-connect, kill current connexion if no mark (I'm older) |
| 705 | if (this.newConnect[user.sid]) { |
| 706 | if ( |
| 707 | user.id > 0 && |
| 708 | user.id == this.st.user.id && |
| 709 | user.sid != this.st.user.sid && |
| 710 | !this.killed[this.st.user.sid] |
| 711 | ) { |
| 712 | this.send("killme", { sid: this.st.user.sid }); |
| 713 | this.killed[this.st.user.sid] = true; |
| 714 | } |
| 715 | delete this.newConnect[user.sid]; |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | case "askchallenges": { |
| 720 | // Send my current live challenges (if any) |
| 721 | const myChallenges = this.challenges |
| 722 | .filter(c => |
| 723 | c.from.sid == this.st.user.sid && c.type == "live" |
| 724 | ) |
| 725 | .map(c => { |
| 726 | // NOTE: in principle, should only send targeted challenge to the target. |
| 727 | // But we may not know yet the identity of the target (just name), |
| 728 | // so cannot decide if data.from is the target or not. |
| 729 | return { |
| 730 | id: c.id, |
| 731 | from: this.st.user.sid, |
| 732 | to: c.to, |
| 733 | randomness: c.randomness, |
| 734 | fen: c.fen, |
| 735 | vid: c.vid, |
| 736 | cadence: c.cadence, |
| 737 | added: c.added |
| 738 | }; |
| 739 | }); |
| 740 | if (myChallenges.length > 0) |
| 741 | this.send("challenges", { data: myChallenges, target: data.from }); |
| 742 | break; |
| 743 | } |
| 744 | case "challenges": //after "askchallenges" |
| 745 | data.data.forEach(this.addChallenge); |
| 746 | break; |
| 747 | case "newchallenge": |
| 748 | this.addChallenge(data.data); |
| 749 | break; |
| 750 | case "refusechallenge": { |
| 751 | const cid = data.data; |
| 752 | ArrayFun.remove(this.challenges, c => c.id == cid); |
| 753 | alert(this.st.tr["Challenge declined"]); |
| 754 | break; |
| 755 | } |
| 756 | case "deletechallenge_s": { |
| 757 | // NOTE: the challenge(s) may be already removed |
| 758 | const cref = data.data; |
| 759 | if (!!cref.cid) ArrayFun.remove(this.challenges, c => c.id == cref.cid); |
| 760 | else if (!!cref.sids) { |
| 761 | cref.sids.forEach(s => { |
| 762 | ArrayFun.remove( |
| 763 | this.challenges, |
| 764 | c => c.type == "live" && c.from.sid == s, |
| 765 | "all" |
| 766 | ); |
| 767 | }); |
| 768 | } |
| 769 | break; |
| 770 | } |
| 771 | case "game": // Individual request |
| 772 | case "newgame": { |
| 773 | const game = data.data; |
| 774 | // Ignore games where I play (will go in MyGames page), |
| 775 | // and also games that I already received. |
| 776 | if ( |
| 777 | game.players.every(p => |
| 778 | p.sid != this.st.user.sid && p.id != this.st.user.id) && |
| 779 | this.games.findIndex(g => g.id == game.id) == -1 |
| 780 | ) { |
| 781 | let newGame = game; |
| 782 | newGame.type = this.classifyObject(game); |
| 783 | newGame.vname = this.getVname(game.vid); |
| 784 | if (!game.score) |
| 785 | // New game from Hall |
| 786 | newGame.score = "*"; |
| 787 | this.games.push(newGame); |
| 788 | if ( |
| 789 | (newGame.type == "live" && this.gdisplay == "corr") || |
| 790 | (newGame.type == "corr" && this.gdisplay == "live") |
| 791 | ) { |
| 792 | document |
| 793 | .getElementById("btnG" + newGame.type) |
| 794 | .classList.add("somethingnew"); |
| 795 | } |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | case "result": { |
| 800 | let g = this.games.find(g => g.id == data.gid); |
| 801 | if (!!g) g.score = data.score; |
| 802 | break; |
| 803 | } |
| 804 | case "startgame": { |
| 805 | // New game just started, I'm involved |
| 806 | const gameInfo = data.data; |
| 807 | if (this.classifyObject(gameInfo) == "live") |
| 808 | this.startNewGame(gameInfo); |
| 809 | else { |
| 810 | this.infoMessage = |
| 811 | this.st.tr["New correspondance game:"] + |
| 812 | " <a href='#/game/" + |
| 813 | gameInfo.id + |
| 814 | "'>" + |
| 815 | "#/game/" + |
| 816 | gameInfo.id + |
| 817 | "</a>"; |
| 818 | document.getElementById("modalInfo").checked = true; |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | case "newchat": |
| 823 | this.$refs["chatcomp"].newChat(data.data); |
| 824 | if (!document.getElementById("modalPeople").checked) |
| 825 | document.getElementById("peopleBtn").classList.add("somethingnew"); |
| 826 | break; |
| 827 | } |
| 828 | }, |
| 829 | socketCloseListener: function() { |
| 830 | if (!this.conn) return; |
| 831 | this.conn = new WebSocket(this.connexionString); |
| 832 | this.conn.addEventListener("message", this.socketMessageListener); |
| 833 | this.conn.addEventListener("close", this.socketCloseListener); |
| 834 | }, |
| 835 | loadMoreCorr: function() { |
| 836 | ajax( |
| 837 | "/observedgames", |
| 838 | "GET", |
| 839 | { |
| 840 | data: { |
| 841 | uid: this.st.user.id, |
| 842 | cursor: this.cursor |
| 843 | }, |
| 844 | success: (res) => { |
| 845 | const L = res.games.length; |
| 846 | if (L > 0) { |
| 847 | if ( |
| 848 | this.cursor == Number.MAX_SAFE_INTEGER && |
| 849 | this.games.length == 0 && |
| 850 | this.gdisplay == "live" |
| 851 | ) { |
| 852 | // First loading: show indicators |
| 853 | document |
| 854 | .getElementById("btnGcorr") |
| 855 | .classList.add("somethingnew"); |
| 856 | } |
| 857 | this.cursor = res.games[L - 1].created; |
| 858 | let moreGames = res.games.map(g => { |
| 859 | const vname = this.getVname(g.vid); |
| 860 | return Object.assign( |
| 861 | {}, |
| 862 | g, |
| 863 | { |
| 864 | type: "corr", |
| 865 | vname: vname |
| 866 | } |
| 867 | ); |
| 868 | }); |
| 869 | this.games = this.games.concat(moreGames); |
| 870 | } else this.hasMore = false; |
| 871 | } |
| 872 | } |
| 873 | ); |
| 874 | }, |
| 875 | // Challenge lifecycle: |
| 876 | addChallenge: function(chall) { |
| 877 | // NOTE about next condition: see "askchallenges" case. |
| 878 | if ( |
| 879 | !chall.to || |
| 880 | (this.people[chall.from].id > 0 && |
| 881 | (chall.from == this.st.user.sid || chall.to == this.st.user.name)) |
| 882 | ) { |
| 883 | let newChall = Object.assign({}, chall); |
| 884 | newChall.type = this.classifyObject(chall); |
| 885 | newChall.randomness = chall.randomness; |
| 886 | newChall.added = Date.now(); |
| 887 | let fromValues = Object.assign({}, this.people[chall.from]); |
| 888 | delete fromValues["pages"]; //irrelevant in this context |
| 889 | newChall.from = Object.assign({ sid: chall.from }, fromValues); |
| 890 | newChall.vname = this.getVname(newChall.vid); |
| 891 | this.challenges.push(newChall); |
| 892 | if ( |
| 893 | (newChall.type == "live" && this.cdisplay == "corr") || |
| 894 | (newChall.type == "corr" && this.cdisplay == "live") |
| 895 | ) { |
| 896 | document |
| 897 | .getElementById("btnC" + newChall.type) |
| 898 | .classList.add("somethingnew"); |
| 899 | } |
| 900 | } |
| 901 | }, |
| 902 | loadNewchallVariant: async function(cb) { |
| 903 | const vname = this.getVname(this.newchallenge.vid); |
| 904 | await import("@/variants/" + vname + ".js") |
| 905 | .then((vModule) => { |
| 906 | window.V = vModule[vname + "Rules"]; |
| 907 | this.newchallenge.V = window.V; |
| 908 | this.newchallenge.vname = vname; |
| 909 | if (!!cb) cb(); |
| 910 | }); |
| 911 | }, |
| 912 | trySetNewchallDiag: function() { |
| 913 | if (!this.newchallenge.fen) { |
| 914 | this.newchallenge.diag = ""; |
| 915 | return; |
| 916 | } |
| 917 | // If vid > 0 then the variant is loaded (function above): |
| 918 | window.V = this.newchallenge.V; |
| 919 | if ( |
| 920 | this.newchallenge.vid > 0 && |
| 921 | !!this.newchallenge.fen && |
| 922 | V.IsGoodFen(this.newchallenge.fen) |
| 923 | ) { |
| 924 | const parsedFen = V.ParseFen(this.newchallenge.fen); |
| 925 | this.newchallenge.diag = getDiagram({ |
| 926 | position: parsedFen.position, |
| 927 | orientation: parsedFen.turn |
| 928 | }); |
| 929 | } else this.newchallenge.diag = ""; |
| 930 | }, |
| 931 | newChallFromPreset(pchall) { |
| 932 | this.partialResetNewchallenge(); |
| 933 | this.newchallenge.vid = pchall.vid; |
| 934 | this.newchallenge.cadence = pchall.cadence; |
| 935 | this.newchallenge.randomness = pchall.randomness; |
| 936 | this.loadNewchallVariant(this.issueNewChallenge); |
| 937 | }, |
| 938 | issueNewChallenge: async function() { |
| 939 | if (!!(this.newchallenge.cadence.match(/^[0-9]+$/))) |
| 940 | this.newchallenge.cadence += "+0"; //assume minutes, no increment |
| 941 | const ctype = this.classifyObject(this.newchallenge); |
| 942 | // TODO: cadence still unchecked so ctype could be wrong... |
| 943 | let error = ""; |
| 944 | if (!this.newchallenge.vid) |
| 945 | error = this.st.tr["Please select a variant"]; |
| 946 | else if (ctype == "corr" && this.st.user.id <= 0) |
| 947 | error = this.st.tr["Please log in to play correspondance games"]; |
| 948 | else if (!!this.newchallenge.to) { |
| 949 | if (this.newchallenge.to == this.st.user.name) |
| 950 | error = this.st.tr["Self-challenge is forbidden"]; |
| 951 | else if ( |
| 952 | ctype == "live" && |
| 953 | Object.values(this.people).every(p => p.name != this.newchallenge.to) |
| 954 | ) { |
| 955 | error = this.newchallenge.to + " " + this.st.tr["is not online"]; |
| 956 | } |
| 957 | } |
| 958 | if (error) { |
| 959 | alert(error); |
| 960 | return; |
| 961 | } |
| 962 | window.V = this.newchallenge.V; |
| 963 | error = checkChallenge(this.newchallenge); |
| 964 | if (error) { |
| 965 | alert(error); |
| 966 | return; |
| 967 | } |
| 968 | // NOTE: "from" information is not required here |
| 969 | let chall = Object.assign({}, this.newchallenge); |
| 970 | // Add only if not already issued (not counting target or FEN): |
| 971 | if (this.challenges.some(c => |
| 972 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && |
| 973 | c.vid == chall.vid && |
| 974 | c.cadence == chall.cadence && |
| 975 | c.randomness == chall.randomness |
| 976 | )) { |
| 977 | alert(this.st.tr["Challenge already exists"]); |
| 978 | return; |
| 979 | } |
| 980 | if (this.newchallenge.memorize) this.addPresetChall(this.newchallenge); |
| 981 | delete chall["V"]; |
| 982 | delete chall["diag"]; |
| 983 | const finishAddChallenge = cid => { |
| 984 | chall.id = cid || "c" + getRandString(); |
| 985 | const MAX_ALLOWED_CHALLS = 3; |
| 986 | // Remove oldest challenge if 3 found: only 3 at a time of a given type |
| 987 | let countMyChalls = 0; |
| 988 | let challToDelIdx = 0; |
| 989 | let oldestAdded = Number.MAX_SAFE_INTEGER; |
| 990 | for (let i=0; i<this.challenges.length; i++) { |
| 991 | const c = this.challenges[i]; |
| 992 | if ( |
| 993 | c.type == ctype && |
| 994 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) |
| 995 | ) { |
| 996 | countMyChalls++; |
| 997 | if (c.added < oldestAdded) { |
| 998 | challToDelIdx = i; |
| 999 | oldestAdded = c.added; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | if (countMyChalls >= MAX_ALLOWED_CHALLS) { |
| 1004 | this.send( |
| 1005 | "deletechallenge_s", |
| 1006 | { data: { cid: this.challenges[challToDelIdx].id } } |
| 1007 | ); |
| 1008 | if (ctype == "corr") { |
| 1009 | ajax( |
| 1010 | "/challenges", |
| 1011 | "DELETE", |
| 1012 | { data: { id: this.challenges[challToDelIdx].id } } |
| 1013 | ); |
| 1014 | } |
| 1015 | this.challenges.splice(challToDelIdx, 1); |
| 1016 | } |
| 1017 | this.send("newchallenge", { |
| 1018 | data: Object.assign({ from: this.st.user.sid }, chall) |
| 1019 | }); |
| 1020 | // Add new challenge: |
| 1021 | chall.from = { |
| 1022 | sid: this.st.user.sid, |
| 1023 | id: this.st.user.id, |
| 1024 | name: this.st.user.name |
| 1025 | }; |
| 1026 | chall.added = Date.now(); |
| 1027 | // NOTE: vname and type are redundant (can be deduced from cadence + vid) |
| 1028 | chall.type = ctype; |
| 1029 | chall.vname = this.newchallenge.vname; |
| 1030 | this.challenges.push(chall); |
| 1031 | // Remember cadence + vid for quicker further challenges: |
| 1032 | localStorage.setItem("cadence", chall.cadence); |
| 1033 | localStorage.setItem("vid", chall.vid); |
| 1034 | localStorage.setItem("challRandomness", chall.randomness); |
| 1035 | document.getElementById("modalNewgame").checked = false; |
| 1036 | // Show the challenge if not on current display |
| 1037 | if ( |
| 1038 | (ctype == "live" && this.cdisplay == "corr") || |
| 1039 | (ctype == "corr" && this.cdisplay == "live") |
| 1040 | ) { |
| 1041 | this.setDisplay('c', ctype); |
| 1042 | } |
| 1043 | }; |
| 1044 | if (ctype == "live") { |
| 1045 | // Live challenges have a random ID |
| 1046 | finishAddChallenge(null); |
| 1047 | } else { |
| 1048 | // Correspondance game: send challenge to server |
| 1049 | ajax( |
| 1050 | "/challenges", |
| 1051 | "POST", |
| 1052 | { |
| 1053 | data: { chall: chall }, |
| 1054 | success: (response) => { |
| 1055 | finishAddChallenge(response.id); |
| 1056 | } |
| 1057 | } |
| 1058 | ); |
| 1059 | } |
| 1060 | }, |
| 1061 | // Callback function after a diagram was showed to accept |
| 1062 | // or refuse targetted challenge: |
| 1063 | decisionChallenge: function(accepted) { |
| 1064 | this.curChallToAccept.accepted = accepted; |
| 1065 | this.finishProcessingChallenge(this.curChallToAccept); |
| 1066 | document.getElementById("modalAccept").checked = false; |
| 1067 | }, |
| 1068 | finishProcessingChallenge: function(c) { |
| 1069 | if (c.accepted) { |
| 1070 | c.seat = { |
| 1071 | sid: this.st.user.sid, |
| 1072 | id: this.st.user.id, |
| 1073 | name: this.st.user.name |
| 1074 | }; |
| 1075 | this.launchGame(c); |
| 1076 | if (c.type == "live") |
| 1077 | // Remove all live challenges of both players |
| 1078 | this.send("deletechallenge_s", { data: { sids: [c.from.sid, c.seat.sid] } }); |
| 1079 | else |
| 1080 | // Corr challenge: just remove the challenge |
| 1081 | this.send("deletechallenge_s", { data: { cid: c.id } }); |
| 1082 | } else { |
| 1083 | const oppsid = this.getOppsid(c); |
| 1084 | if (!!oppsid) |
| 1085 | this.send("refusechallenge", { data: c.id, target: oppsid }); |
| 1086 | if (c.type == "corr") { |
| 1087 | ajax( |
| 1088 | "/challenges", |
| 1089 | "DELETE", |
| 1090 | { data: { id: c.id } } |
| 1091 | ); |
| 1092 | } |
| 1093 | this.send("deletechallenge_s", { data: { cid: c.id } }); |
| 1094 | } |
| 1095 | }, |
| 1096 | // TODO: if several players click same challenge at the same time: problem |
| 1097 | clickChallenge: async function(c) { |
| 1098 | const myChallenge = |
| 1099 | c.from.sid == this.st.user.sid || //live |
| 1100 | (this.st.user.id > 0 && c.from.id == this.st.user.id); //corr |
| 1101 | if (!myChallenge) { |
| 1102 | if (c.type == "corr" && this.st.user.id <= 0) { |
| 1103 | alert(this.st.tr["Please log in to accept corr challenges"]); |
| 1104 | return; |
| 1105 | } |
| 1106 | c.accepted = true; |
| 1107 | await import("@/variants/" + c.vname + ".js") |
| 1108 | .then((vModule) => { |
| 1109 | window.V = vModule[c.vname + "Rules"]; |
| 1110 | if (!!c.to) { |
| 1111 | // c.to == this.st.user.name (connected) |
| 1112 | if (!!c.fen) { |
| 1113 | const parsedFen = V.ParseFen(c.fen); |
| 1114 | c.mycolor = V.GetOppCol(parsedFen.turn); |
| 1115 | this.tchallDiag = getDiagram({ |
| 1116 | position: parsedFen.position, |
| 1117 | orientation: c.mycolor |
| 1118 | }); |
| 1119 | } |
| 1120 | this.curChallToAccept = c; |
| 1121 | document.getElementById("modalAccept").checked = true; |
| 1122 | } |
| 1123 | else this.finishProcessingChallenge(c); |
| 1124 | }); |
| 1125 | } |
| 1126 | else { |
| 1127 | // My challenge |
| 1128 | if (c.type == "corr") { |
| 1129 | ajax( |
| 1130 | "/challenges", |
| 1131 | "DELETE", |
| 1132 | { data: { id: c.id } } |
| 1133 | ); |
| 1134 | } |
| 1135 | this.send("deletechallenge_s", { data: { cid: c.id } }); |
| 1136 | } |
| 1137 | // In all cases, the challenge is consumed: |
| 1138 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 1139 | }, |
| 1140 | // NOTE: when launching game, the challenge is already being deleted |
| 1141 | launchGame: function(c) { |
| 1142 | // White player index 0, black player index 1: |
| 1143 | const players = |
| 1144 | !!c.mycolor |
| 1145 | ? (c.mycolor == "w" ? [c.seat, c.from] : [c.from, c.seat]) |
| 1146 | : shuffle([c.from, c.seat]); |
| 1147 | // These game informations will be shared |
| 1148 | let gameInfo = { |
| 1149 | id: getRandString(), |
| 1150 | fen: c.fen || V.GenRandInitFen(c.randomness), |
| 1151 | randomness: c.randomness, //for rematch |
| 1152 | players: players, |
| 1153 | vid: c.vid, |
| 1154 | cadence: c.cadence |
| 1155 | }; |
| 1156 | const notifyNewgame = () => { |
| 1157 | const oppsid = this.getOppsid(c); |
| 1158 | if (!!oppsid) |
| 1159 | // Opponent is online |
| 1160 | this.send("startgame", { data: gameInfo, target: oppsid }); |
| 1161 | // If new corr game, notify Hall (except opponent and me) |
| 1162 | if (c.type == "corr") { |
| 1163 | this.send( |
| 1164 | "newgame", |
| 1165 | { |
| 1166 | data: gameInfo, |
| 1167 | excluded: [this.st.user.sid, oppsid] |
| 1168 | } |
| 1169 | ); |
| 1170 | } |
| 1171 | // Notify MyGames page: |
| 1172 | this.send( |
| 1173 | "notifynewgame", |
| 1174 | { |
| 1175 | data: gameInfo, |
| 1176 | targets: gameInfo.players |
| 1177 | } |
| 1178 | ); |
| 1179 | // NOTE: no need to send the game to the room, since I'll connect |
| 1180 | // on game just after, the main Hall will be notified. |
| 1181 | }; |
| 1182 | if (c.type == "live") { |
| 1183 | notifyNewgame(); |
| 1184 | this.startNewGame(gameInfo); |
| 1185 | } else { |
| 1186 | // corr: game only on server |
| 1187 | ajax( |
| 1188 | "/games", |
| 1189 | "POST", |
| 1190 | { |
| 1191 | // cid is useful to delete the challenge: |
| 1192 | data: { |
| 1193 | gameInfo: gameInfo, |
| 1194 | cid: c.id |
| 1195 | }, |
| 1196 | success: (response) => { |
| 1197 | gameInfo.id = response.id; |
| 1198 | notifyNewgame(); |
| 1199 | this.$router.push("/game/" + response.id); |
| 1200 | } |
| 1201 | } |
| 1202 | ); |
| 1203 | } |
| 1204 | }, |
| 1205 | // NOTE: for live games only (corr games start on the server) |
| 1206 | startNewGame: function(gameInfo) { |
| 1207 | const game = Object.assign( |
| 1208 | {}, |
| 1209 | gameInfo, |
| 1210 | { |
| 1211 | // (other) Game infos: constant |
| 1212 | fenStart: gameInfo.fen, |
| 1213 | vname: this.getVname(gameInfo.vid), |
| 1214 | created: Date.now(), |
| 1215 | // Game state (including FEN): will be updated |
| 1216 | moves: [], |
| 1217 | clocks: [-1, -1], //-1 = unstarted |
| 1218 | score: "*" |
| 1219 | } |
| 1220 | ); |
| 1221 | setTimeout( |
| 1222 | () => { |
| 1223 | GameStorage.add(game, (err) => { |
| 1224 | // If an error occurred, game is not added: a tab already |
| 1225 | // added the game. Maybe a focused one, maybe not. |
| 1226 | // We know for sure that it emitted the gong start sound. |
| 1227 | // ==> Do not play it again. |
| 1228 | if (!err && this.st.settings.sound) |
| 1229 | new Audio("/sounds/newgame.flac").play().catch(() => {}); |
| 1230 | this.$router.push("/game/" + gameInfo.id); |
| 1231 | }); |
| 1232 | }, |
| 1233 | document.hidden ? 500 + 1000 * Math.random() : 0 |
| 1234 | ); |
| 1235 | } |
| 1236 | } |
| 1237 | }; |
| 1238 | </script> |
| 1239 | |
| 1240 | <style lang="sass" scoped> |
| 1241 | .active |
| 1242 | color: #42a983 |
| 1243 | |
| 1244 | #infoDiv > .card |
| 1245 | padding: 15px 0 |
| 1246 | max-width: 430px |
| 1247 | |
| 1248 | #newgameDiv > .card, #acceptDiv > .card |
| 1249 | max-width: 767px |
| 1250 | max-height: 100% |
| 1251 | |
| 1252 | div#peopleWrap > .card |
| 1253 | max-height: 100% |
| 1254 | |
| 1255 | @media screen and (min-width: 1281px) |
| 1256 | div#peopleWrap > .card |
| 1257 | max-width: 66.67% |
| 1258 | |
| 1259 | @media screen and (max-width: 1280px) |
| 1260 | div#peopleWrap > .card |
| 1261 | max-width: 83.33% |
| 1262 | |
| 1263 | @media screen and (max-width: 767px) |
| 1264 | div#peopleWrap > .card |
| 1265 | max-width: 100% |
| 1266 | |
| 1267 | #players |
| 1268 | width: 50% |
| 1269 | position: relative |
| 1270 | float: left |
| 1271 | |
| 1272 | #chat |
| 1273 | width: 50% |
| 1274 | float: left |
| 1275 | position: relative |
| 1276 | |
| 1277 | @media screen and (max-width: 767px) |
| 1278 | #players, #chats |
| 1279 | width: 100% |
| 1280 | |
| 1281 | #chat > .card |
| 1282 | max-width: 100% |
| 1283 | margin: 0; |
| 1284 | border: none; |
| 1285 | |
| 1286 | #players > p |
| 1287 | margin-left: 5px |
| 1288 | |
| 1289 | .anonymous |
| 1290 | font-style: italic |
| 1291 | |
| 1292 | button.player-action |
| 1293 | margin-left: 32px |
| 1294 | |
| 1295 | .somethingnew |
| 1296 | background-color: #c5fefe !important |
| 1297 | |
| 1298 | .tabbtn |
| 1299 | background-color: #f9faee |
| 1300 | |
| 1301 | button.acceptBtn |
| 1302 | background-color: lightgreen |
| 1303 | button.refuseBtn |
| 1304 | background-color: red |
| 1305 | |
| 1306 | #buttonsTchall |
| 1307 | // margin-top set dynamically (depends if diagram showed or not) |
| 1308 | & > button > span |
| 1309 | width: 100% |
| 1310 | text-align: center |
| 1311 | |
| 1312 | .variantName |
| 1313 | font-weight: bold |
| 1314 | |
| 1315 | .diagram |
| 1316 | margin: 0 auto |
| 1317 | max-width: 400px |
| 1318 | // width: 100% required for Firefox |
| 1319 | width: 100% |
| 1320 | |
| 1321 | #inputFen |
| 1322 | width: 100% |
| 1323 | |
| 1324 | #div2, #div3 |
| 1325 | margin-top: 15px |
| 1326 | @media screen and (max-width: 767px) |
| 1327 | #div2, #div3 |
| 1328 | margin-top: 0 |
| 1329 | |
| 1330 | tr > td |
| 1331 | &.random-0 |
| 1332 | background-color: #FF5733 |
| 1333 | &.random-1 |
| 1334 | background-color: #2B63B4 |
| 1335 | &.random-2 |
| 1336 | background-color: #33B42B |
| 1337 | |
| 1338 | @media screen and (max-width: 767px) |
| 1339 | h4 |
| 1340 | margin: 5px 0 |
| 1341 | |
| 1342 | button#loadMoreBtn |
| 1343 | display: block |
| 1344 | margin: 0 auto |
| 1345 | |
| 1346 | td.remove-preset |
| 1347 | background-color: lightgrey |
| 1348 | text-align: center |
| 1349 | & > img |
| 1350 | height: 1em |
| 1351 | </style> |