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