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