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