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