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