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