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