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