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