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") | |
13 | .card.text-center | |
14 | p | |
15 | span.variantName {{ curChallToAccept.vname }} | |
16 | span {{ curChallToAccept.cadence }} | |
17 | span {{ st.tr["with"] + " " + curChallToAccept.from.name }} | |
18 | .diagram(v-html="tchallDiag") | |
19 | .button-group#buttonsTchall | |
20 | button.acceptBtn(@click="decisionChallenge(true)") {{ st.tr["Accept challenge?"] }} | |
21 | button.refuseBtn(@click="decisionChallenge(false)") {{ st.tr["Refuse"] }} | |
09d37571 BA |
22 | input#modalNewgame.modal( |
23 | type="checkbox" | |
24 | @change="cadenceFocusIfOpened($event)" | |
25 | ) | |
910d631b BA |
26 | div#newgameDiv( |
27 | role="dialog" | |
28 | data-checkbox="modalNewgame" | |
29 | ) | |
89021f18 | 30 | .card |
5b020e73 | 31 | label#closeNewgame.modal-close(for="modalNewgame") |
725da57f | 32 | div(@keyup.enter="newChallenge()") |
89021f18 BA |
33 | fieldset |
34 | label(for="selectVariant") {{ st.tr["Variant"] }} * | |
725da57f BA |
35 | select#selectVariant( |
36 | @change="loadNewchallVariant(trySetNewchallDiag)" | |
37 | v-model="newchallenge.vid" | |
38 | ) | |
910d631b BA |
39 | option( |
40 | v-for="v in st.variants" | |
41 | :value="v.id" | |
42 | :selected="newchallenge.vid==v.id" | |
43 | ) | |
89021f18 BA |
44 | | {{ v.name }} |
45 | fieldset | |
46 | label(for="cadence") {{ st.tr["Cadence"] }} * | |
47 | div#predefinedCadences | |
8477e53d | 48 | button(type="button") 15+5 |
8aa418f6 | 49 | button(type="button") 45+30 |
92240cf0 BA |
50 | button(type="button") 3d |
51 | button(type="button") 7d | |
910d631b BA |
52 | input#cadence( |
53 | type="text" | |
54 | v-model="newchallenge.cadence" | |
92240cf0 | 55 | placeholder="5+0, 1h+30s, 5d ..." |
910d631b | 56 | ) |
89021f18 BA |
57 | fieldset(v-if="st.user.id > 0") |
58 | label(for="selectPlayers") {{ st.tr["Play with?"] }} | |
910d631b BA |
59 | input#selectPlayers( |
60 | type="text" | |
61 | v-model="newchallenge.to" | |
62 | ) | |
89021f18 | 63 | fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0") |
910d631b | 64 | input#inputFen( |
725da57f BA |
65 | placeholder="FEN" |
66 | @input="trySetNewchallDiag()" | |
910d631b BA |
67 | type="text" |
68 | v-model="newchallenge.fen" | |
69 | ) | |
725da57f | 70 | .diagram(v-html="newchallenge.diag") |
9ddaf8da | 71 | button(@click="newChallenge()") {{ st.tr["Send challenge"] }} |
910d631b BA |
72 | input#modalPeople.modal( |
73 | type="checkbox" | |
74 | @click="resetChatColor()" | |
75 | ) | |
76 | div#peopleWrap( | |
77 | role="dialog" | |
78 | data-checkbox="modalPeople" | |
79 | ) | |
bd76b456 BA |
80 | .card |
81 | label.modal-close(for="modalPeople") | |
82 | #people | |
83 | #players | |
910d631b BA |
84 | p( |
85 | v-for="sid in Object.keys(people)" | |
86 | v-if="!!people[sid].name" | |
87 | ) | |
bd76b456 | 88 | span {{ people[sid].name }} |
910d631b | 89 | button.player-action( |
21771bb9 | 90 | v-if="isGamer(sid) || (st.user.id > 0 && sid!=st.user.sid)" |
910d631b BA |
91 | @click="challOrWatch(sid)" |
92 | ) | |
bd76b456 BA |
93 | | {{ getActionLabel(sid) }} |
94 | p.anonymous @nonymous ({{ anonymousCount }}) | |
95 | #chat | |
910d631b BA |
96 | Chat( |
97 | :newChat="newChat" | |
98 | @mychat="processChat" | |
99 | :pastChats="[]" | |
100 | ) | |
bd76b456 | 101 | .clearer |
9d58ef95 | 102 | .row |
bd76b456 BA |
103 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
104 | .button-group | |
910d631b | 105 | button#peopleBtn(onClick="window.doClick('modalPeople')") |
8a0f881d | 106 | | {{ st.tr["Who's there?"] }} |
910d631b BA |
107 | button(onClick="window.doClick('modalNewgame')") |
108 | | {{ st.tr["New game"] }} | |
9d58ef95 | 109 | .row |
9ca1e26b | 110 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
2f258c37 | 111 | div#div2 |
ed06d9e9 | 112 | .button-group |
2f258c37 | 113 | button.tabbtn#btnClive(@click="setDisplay('c','live',$event)") |
602d6bef | 114 | | {{ st.tr["Live challenges"] }} |
2f258c37 | 115 | button.tabbtn#btnCcorr(@click="setDisplay('c','corr',$event)") |
602d6bef | 116 | | {{ st.tr["Correspondance challenges"] }} |
910d631b BA |
117 | ChallengeList( |
118 | v-show="cdisplay=='live'" | |
119 | :challenges="filterChallenges('live')" | |
120 | @click-challenge="clickChallenge" | |
121 | ) | |
122 | ChallengeList( | |
123 | v-show="cdisplay=='corr'" | |
124 | :challenges="filterChallenges('corr')" | |
125 | @click-challenge="clickChallenge" | |
126 | ) | |
2f258c37 | 127 | div#div3 |
ed06d9e9 | 128 | .button-group |
2f258c37 | 129 | button.tabbtn#btnGlive(@click="setDisplay('g','live',$event)") |
602d6bef | 130 | | {{ st.tr["Live games"] }} |
2f258c37 | 131 | button.tabbtn#btnGcorr(@click="setDisplay('g','corr',$event)") |
602d6bef | 132 | | {{ st.tr["Correspondance games"] }} |
910d631b BA |
133 | GameList( |
134 | v-show="gdisplay=='live'" | |
135 | :games="filterGames('live')" | |
136 | :showBoth="true" | |
137 | @show-game="showGame" | |
138 | ) | |
139 | GameList( | |
140 | v-show="gdisplay=='corr'" | |
141 | :games="filterGames('corr')" | |
142 | :showBoth="true" | |
143 | @show-game="showGame" | |
144 | ) | |
625022fd BA |
145 | </template> |
146 | ||
147 | <script> | |
5b020e73 | 148 | import { store } from "@/store"; |
9d58ef95 BA |
149 | import { checkChallenge } from "@/data/challengeCheck"; |
150 | import { ArrayFun } from "@/utils/array"; | |
03608482 | 151 | import { ajax } from "@/utils/ajax"; |
8418f0d7 | 152 | import params from "@/parameters"; |
4b0384fa | 153 | import { getRandString, shuffle } from "@/utils/alea"; |
725da57f | 154 | import { getDiagram } from "@/utils/printDiagram"; |
603b8a8b | 155 | import Chat from "@/components/Chat.vue"; |
5b020e73 BA |
156 | import GameList from "@/components/GameList.vue"; |
157 | import ChallengeList from "@/components/ChallengeList.vue"; | |
967a2686 | 158 | import { GameStorage } from "@/utils/gameStorage"; |
602d6bef | 159 | import { processModalClick } from "@/utils/modalClick"; |
625022fd | 160 | export default { |
cf2343ce | 161 | name: "my-hall", |
5b020e73 | 162 | components: { |
603b8a8b | 163 | Chat, |
5b020e73 | 164 | GameList, |
6808d7a1 | 165 | ChallengeList |
5b020e73 | 166 | }, |
6808d7a1 | 167 | data: function() { |
fb54f098 | 168 | return { |
5b020e73 | 169 | st: store.state, |
6855163c | 170 | cdisplay: "live", //or corr |
fb54f098 | 171 | gdisplay: "live", |
6855163c | 172 | games: [], |
b4d619d1 | 173 | challenges: [], |
71468011 | 174 | people: {}, |
3d55deea | 175 | infoMessage: "", |
9d58ef95 | 176 | newchallenge: { |
fb54f098 | 177 | fen: "", |
725da57f | 178 | vid: parseInt(localStorage.getItem("vid")) || 0, |
6fba6e0c | 179 | to: "", //name of challenged player (if any) |
725da57f BA |
180 | cadence: localStorage.getItem("cadence") || "", |
181 | // VariantRules object, stored to not interfere with | |
182 | // diagrams of targetted challenges: | |
183 | V: null, | |
184 | vname: "", | |
185 | diag: "" //visualizing FEN | |
fb54f098 | 186 | }, |
725da57f BA |
187 | tchallDiag: "", |
188 | curChallToAccept: {from: {}}, | |
ac8f441c | 189 | newChat: "", |
8418f0d7 | 190 | conn: null, |
51d87b52 BA |
191 | connexionString: "", |
192 | // Related to (killing of) self multi-connects: | |
193 | newConnect: {}, | |
6808d7a1 | 194 | killed: {} |
fb54f098 BA |
195 | }; |
196 | }, | |
fd7aea36 BA |
197 | watch: { |
198 | // st.variants changes only once, at loading from [] to [...] | |
6808d7a1 | 199 | "st.variants": function() { |
fd7aea36 | 200 | // Set potential challenges and games variant names: |
71468011 | 201 | this.challenges.concat(this.games).forEach(o => { |
6808d7a1 | 202 | if (o.vname == "") o.vname = this.getVname(o.vid); |
fd7aea36 | 203 | }); |
725da57f BA |
204 | if (!this.newchallenge.V && this.newchallenge.vid > 0) |
205 | this.loadNewchallVariant(); | |
6808d7a1 | 206 | } |
fd7aea36 | 207 | }, |
b4d619d1 | 208 | computed: { |
ed06d9e9 BA |
209 | anonymousCount: function() { |
210 | let count = 0; | |
6808d7a1 BA |
211 | Object.values(this.people).forEach(p => { |
212 | count += !p.name ? 1 : 0; | |
213 | }); | |
ed06d9e9 | 214 | return count; |
6808d7a1 | 215 | } |
b4d619d1 | 216 | }, |
9d58ef95 | 217 | created: function() { |
725da57f BA |
218 | if (this.st.variants.length > 0 && this.newchallenge.vid > 0) |
219 | this.loadNewchallVariant(); | |
66d03f23 | 220 | const my = this.st.user; |
6808d7a1 | 221 | this.$set(this.people, my.sid, { id: my.id, name: my.name, pages: ["/"] }); |
3d55deea BA |
222 | // Ask server for current corr games (all but mines) |
223 | ajax( | |
224 | "/games", | |
225 | "GET", | |
6808d7a1 | 226 | { uid: this.st.user.id, excluded: true }, |
3d55deea | 227 | response => { |
6808d7a1 BA |
228 | this.games = this.games.concat( |
229 | response.games.map(g => { | |
230 | const type = this.classifyObject(g); | |
231 | const vname = this.getVname(g.vid); | |
232 | return Object.assign({}, g, { type: type, vname: vname }); | |
233 | }) | |
234 | ); | |
3d55deea BA |
235 | } |
236 | ); | |
fe4c7e67 | 237 | // Also ask for corr challenges (open + sent by/to me) |
6808d7a1 BA |
238 | ajax("/challenges", "GET", { uid: this.st.user.id }, response => { |
239 | // Gather all senders names, and then retrieve full identity: | |
240 | // (TODO [perf]: some might be online...) | |
241 | let names = {}; | |
242 | response.challenges.forEach(c => { | |
243 | if (c.uid != this.st.user.id) names[c.uid] = ""; | |
6808d7a1 BA |
244 | else if (!!c.target && c.target != this.st.user.id) |
245 | names[c.target] = ""; | |
246 | }); | |
247 | const addChallenges = () => { | |
248 | names[this.st.user.id] = this.st.user.name; //in case of | |
249 | this.challenges = this.challenges.concat( | |
250 | response.challenges.map(c => { | |
251 | const from = { name: names[c.uid], id: c.uid }; //or just name | |
252 | const type = this.classifyObject(c); | |
253 | const vname = this.getVname(c.vid); | |
254 | return Object.assign( | |
255 | {}, | |
256 | { | |
257 | type: type, | |
258 | vname: vname, | |
259 | from: from, | |
260 | to: c.target ? names[c.target] : "" | |
261 | }, | |
262 | c | |
263 | ); | |
264 | }) | |
265 | ); | |
266 | }; | |
267 | if (Object.keys(names).length > 0) { | |
268 | ajax( | |
269 | "/users", | |
270 | "GET", | |
271 | { ids: Object.keys(names).join(",") }, | |
272 | response2 => { | |
273 | response2.users.forEach(u => { | |
274 | names[u.id] = u.name; | |
275 | }); | |
276 | addChallenges(); | |
277 | } | |
278 | ); | |
279 | } else addChallenges(); | |
280 | }); | |
71468011 BA |
281 | const connectAndPoll = () => { |
282 | this.send("connect"); | |
283 | this.send("pollclientsandgamers"); | |
4d64881e | 284 | }; |
8418f0d7 | 285 | // Initialize connection |
6808d7a1 BA |
286 | this.connexionString = |
287 | params.socketUrl + | |
288 | "/?sid=" + | |
289 | this.st.user.sid + | |
290 | "&tmpId=" + | |
291 | getRandString() + | |
292 | "&page=" + | |
293 | encodeURIComponent(this.$route.path); | |
51d87b52 | 294 | this.conn = new WebSocket(this.connexionString); |
71468011 | 295 | this.conn.onopen = connectAndPoll; |
8418f0d7 | 296 | this.conn.onmessage = this.socketMessageListener; |
51d87b52 | 297 | this.conn.onclose = this.socketCloseListener; |
9d58ef95 | 298 | }, |
25d18342 | 299 | mounted: function() { |
6808d7a1 | 300 | ["peopleWrap", "infoDiv", "newgameDiv"].forEach(eltName => { |
bd76b456 BA |
301 | let elt = document.getElementById(eltName); |
302 | elt.addEventListener("click", processModalClick); | |
303 | }); | |
6808d7a1 BA |
304 | document.querySelectorAll("#predefinedCadences > button").forEach(b => { |
305 | b.addEventListener("click", () => { | |
306 | this.newchallenge.cadence = b.innerHTML; | |
307 | }); | |
308 | }); | |
8a0f881d BA |
309 | const dispCorr = this.$route.query["disp"]; |
310 | const showCtype = | |
311 | dispCorr || localStorage.getItem("type-challenges") || "live"; | |
312 | const showGtype = | |
313 | dispCorr || localStorage.getItem("type-games") || "live"; | |
6808d7a1 BA |
314 | this.setDisplay("c", showCtype); |
315 | this.setDisplay("g", showGtype); | |
25d18342 | 316 | }, |
8418f0d7 | 317 | beforeDestroy: function() { |
71468011 | 318 | this.send("disconnect"); |
8418f0d7 | 319 | }, |
fb54f098 | 320 | methods: { |
a6bddfc6 | 321 | // Helpers: |
09d37571 BA |
322 | cadenceFocusIfOpened: function() { |
323 | if (event.target.checked) | |
324 | document.getElementById("cadence").focus(); | |
325 | }, | |
71468011 | 326 | send: function(code, obj) { |
6808d7a1 BA |
327 | if (this.conn) { |
328 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); | |
51d87b52 | 329 | } |
71468011 BA |
330 | }, |
331 | getVname: function(vid) { | |
332 | const variant = this.st.variants.find(v => v.id == vid); | |
333 | // this.st.variants might be uninitialized (variant == null) | |
6808d7a1 | 334 | return variant ? variant.name : ""; |
71468011 | 335 | }, |
6855163c BA |
336 | filterChallenges: function(type) { |
337 | return this.challenges.filter(c => c.type == type); | |
338 | }, | |
339 | filterGames: function(type) { | |
a9b131f1 | 340 | return this.games.filter(g => g.type == type); |
6855163c | 341 | }, |
6808d7a1 BA |
342 | classifyObject: function(o) { |
343 | //challenge or game | |
344 | return o.cadence.indexOf("d") === -1 ? "live" : "corr"; | |
a6bddfc6 | 345 | }, |
5bcc9b31 BA |
346 | setDisplay: function(letter, type, e) { |
347 | this[letter + "display"] = type; | |
6808d7a1 BA |
348 | localStorage.setItem( |
349 | "type-" + (letter == "c" ? "challenges" : "games"), | |
350 | type | |
351 | ); | |
352 | let elt = e | |
7f36b53a BA |
353 | ? e.target |
354 | : document.getElementById("btn" + letter.toUpperCase() + type); | |
355 | elt.classList.add("active"); | |
2f258c37 | 356 | elt.classList.remove("somethingnew"); //in case of |
6808d7a1 | 357 | if (elt.previousElementSibling) |
7f36b53a | 358 | elt.previousElementSibling.classList.remove("active"); |
6808d7a1 | 359 | else elt.nextElementSibling.classList.remove("active"); |
7f36b53a BA |
360 | }, |
361 | isGamer: function(sid) { | |
362 | return this.people[sid].pages.some(p => p.indexOf("/game/") >= 0); | |
363 | }, | |
71468011 BA |
364 | getActionLabel: function(sid) { |
365 | return this.people[sid].pages.some(p => p == "/") | |
366 | ? "Challenge" | |
367 | : "Observe"; | |
ac8f441c | 368 | }, |
71468011 | 369 | challOrWatch: function(sid) { |
6808d7a1 | 370 | if (this.people[sid].pages.some(p => p == "/")) { |
71468011 BA |
371 | // Available, in Hall |
372 | this.newchallenge.to = this.people[sid].name; | |
3e4e742c | 373 | document.getElementById("modalPeople").checked = false; |
6808d7a1 BA |
374 | window.doClick("modalNewgame"); |
375 | } else { | |
7f36b53a BA |
376 | // In some game, maybe playing maybe not: show a random one |
377 | let gids = []; | |
378 | this.people[sid].pages.forEach(p => { | |
379 | const matchGid = p.match(/[a-zA-Z0-9]+$/); | |
6808d7a1 | 380 | if (matchGid) gids.push(matchGid[0]); |
7f36b53a BA |
381 | }); |
382 | const gid = gids[Math.floor(Math.random() * gids.length)]; | |
26f70eee BA |
383 | const game = this.games.find(g => g.id == gid); |
384 | if (game) this.showGame(game); | |
385 | else this.$router.push("/game/" + gid); //game vs. me | |
71468011 BA |
386 | } |
387 | }, | |
51d87b52 | 388 | showGame: function(g) { |
71468011 BA |
389 | // NOTE: we are an observer, since only games I don't play are shown here |
390 | // ==> Moves sent by connected remote player(s) if live game | |
391 | let url = "/game/" + g.id; | |
392 | if (g.type == "live") | |
51d87b52 | 393 | url += "?rid=" + g.rids[Math.floor(Math.random() * g.rids.length)]; |
71468011 BA |
394 | this.$router.push(url); |
395 | }, | |
bd76b456 BA |
396 | resetChatColor: function() { |
397 | // TODO: this is called twice, once on opening an once on closing | |
2f258c37 | 398 | document.getElementById("peopleBtn").classList.remove("somethingnew"); |
bd76b456 | 399 | }, |
71468011 | 400 | processChat: function(chat) { |
6808d7a1 | 401 | this.send("newchat", { data: chat }); |
a6bddfc6 BA |
402 | }, |
403 | // Messaging center: | |
9d58ef95 | 404 | socketMessageListener: function(msg) { |
6808d7a1 | 405 | if (!this.conn) return; |
9d58ef95 | 406 | const data = JSON.parse(msg.data); |
6808d7a1 BA |
407 | switch (data.code) { |
408 | case "pollclientsandgamers": { | |
51d87b52 BA |
409 | // Since people can be both in Hall and Game, |
410 | // need to track "askIdentity" requests: | |
71468011 BA |
411 | let identityAsked = {}; |
412 | data.sockIds.forEach(s => { | |
7f36b53a | 413 | const page = s.page || "/"; |
6808d7a1 | 414 | if (s.sid != this.st.user.sid && !identityAsked[s.sid]) { |
71468011 | 415 | identityAsked[s.sid] = true; |
6808d7a1 | 416 | this.send("askidentity", { target: s.sid, page: page }); |
71468011 BA |
417 | } |
418 | if (!this.people[s.sid]) | |
6808d7a1 | 419 | this.$set(this.people, s.sid, { id: 0, name: "", pages: [page] }); |
7f36b53a BA |
420 | else if (this.people[s.sid].pages.indexOf(page) < 0) |
421 | this.people[s.sid].pages.push(page); | |
6808d7a1 BA |
422 | if (!s.page) |
423 | //peer is in Hall | |
424 | this.send("askchallenge", { target: s.sid }); | |
425 | //peer is in Game | |
426 | else this.send("askgame", { target: s.sid, page: page }); | |
5a3da968 | 427 | }); |
ac8f441c | 428 | break; |
71468011 BA |
429 | } |
430 | case "connect": | |
6808d7a1 | 431 | case "gconnect": { |
7f36b53a | 432 | const page = data.page || "/"; |
71468011 BA |
433 | // NOTE: player could have been polled earlier, but might have logged in then |
434 | // So it's a good idea to ask identity if he was anonymous. | |
435 | // But only ask game / challenge if currently disconnected. | |
6808d7a1 BA |
436 | if (!this.people[data.from]) { |
437 | this.$set(this.people, data.from, { | |
438 | name: "", | |
439 | id: 0, | |
440 | pages: [page] | |
441 | }); | |
71468011 | 442 | if (data.code == "connect") |
6808d7a1 BA |
443 | this.send("askchallenge", { target: data.from }); |
444 | else this.send("askgame", { target: data.from, page: page }); | |
445 | } else { | |
71468011 | 446 | // append page if not already in list |
7f36b53a BA |
447 | if (this.people[data.from].pages.indexOf(page) < 0) |
448 | this.people[data.from].pages.push(page); | |
71468011 | 449 | } |
6808d7a1 | 450 | if (this.people[data.from].id == 0) { |
51d87b52 | 451 | this.newConnect[data.from] = true; //for self multi-connects tests |
6808d7a1 | 452 | this.send("askidentity", { target: data.from, page: page }); |
51d87b52 | 453 | } |
71468011 | 454 | break; |
7f36b53a | 455 | } |
71468011 | 456 | case "disconnect": |
6808d7a1 | 457 | case "gdisconnect": { |
092de306 BA |
458 | // If the user reloads the page twice very quickly (experienced with Firefox), |
459 | // the first reload won't have time to connect but will trigger a "close" event anyway. | |
460 | // ==> Next check is required. | |
6808d7a1 | 461 | if (!this.people[data.from]) return; |
71468011 | 462 | // Disconnect means no more tmpIds: |
6808d7a1 | 463 | if (data.code == "disconnect") { |
51d87b52 | 464 | // Remove the live challenge sent by this player: |
71468011 | 465 | ArrayFun.remove(this.challenges, c => c.from.sid == data.from); |
6808d7a1 | 466 | } else { |
51d87b52 BA |
467 | // Remove the matching live game if now unreachable |
468 | const gid = data.page.match(/[a-zA-Z0-9]+$/)[0]; | |
469 | const gidx = this.games.findIndex(g => g.id == gid); | |
6808d7a1 | 470 | if (gidx >= 0) { |
51d87b52 | 471 | const game = this.games[gidx]; |
6808d7a1 BA |
472 | if ( |
473 | game.type == "live" && | |
474 | game.rids.length == 1 && | |
475 | game.rids[0] == data.from | |
476 | ) { | |
51d87b52 BA |
477 | this.games.splice(gidx, 1); |
478 | } | |
71468011 BA |
479 | } |
480 | } | |
51d87b52 BA |
481 | const page = data.page || "/"; |
482 | ArrayFun.remove(this.people[data.from].pages, p => p == page); | |
483 | if (this.people[data.from].pages.length == 0) | |
484 | this.$delete(this.people, data.from); | |
485 | break; | |
6808d7a1 | 486 | } |
51d87b52 BA |
487 | case "killed": |
488 | // I logged in elsewhere: | |
51d87b52 | 489 | this.conn = null; |
09d37571 | 490 | alert(this.st.tr["New connexion detected: tab now offline"]); |
5a3da968 | 491 | break; |
6808d7a1 | 492 | case "askidentity": { |
51d87b52 BA |
493 | // Request for identification (TODO: anonymous shouldn't need to reply) |
494 | const me = { | |
495 | // Decompose to avoid revealing email | |
496 | name: this.st.user.name, | |
497 | sid: this.st.user.sid, | |
6808d7a1 | 498 | id: this.st.user.id |
51d87b52 | 499 | }; |
6808d7a1 | 500 | this.send("identity", { data: me, target: data.from }); |
5a3da968 | 501 | break; |
51d87b52 | 502 | } |
6808d7a1 | 503 | case "identity": { |
71468011 | 504 | const user = data.data; |
6808d7a1 | 505 | if (user.name) { |
51d87b52 | 506 | // If I multi-connect, kill current connexion if no mark (I'm older) |
6808d7a1 BA |
507 | if ( |
508 | this.newConnect[user.sid] && | |
509 | user.id > 0 && | |
510 | user.id == this.st.user.id && | |
511 | user.sid != this.st.user.sid | |
512 | ) { | |
513 | if (!this.killed[this.st.user.sid]) { | |
514 | this.send("killme", { sid: this.st.user.sid }); | |
51d87b52 BA |
515 | this.killed[this.st.user.sid] = true; |
516 | } | |
517 | } | |
6808d7a1 BA |
518 | if (user.sid != this.st.user.sid) { |
519 | //I already know my identity... | |
520 | this.$set(this.people, user.sid, { | |
521 | id: user.id, | |
522 | name: user.name, | |
523 | pages: this.people[user.sid].pages | |
524 | }); | |
51d87b52 BA |
525 | } |
526 | } | |
527 | delete this.newConnect[user.sid]; | |
dcd68c41 | 528 | break; |
71468011 | 529 | } |
6808d7a1 | 530 | case "askchallenge": { |
6855163c | 531 | // Send my current live challenge (if any) |
6808d7a1 BA |
532 | const cIdx = this.challenges.findIndex( |
533 | c => c.from.sid == this.st.user.sid && c.type == "live" | |
534 | ); | |
535 | if (cIdx >= 0) { | |
dd75774d | 536 | const c = this.challenges[cIdx]; |
71468011 BA |
537 | // NOTE: in principle, should only send targeted challenge to the target. |
538 | // But we may not know yet the identity of the target (just name), | |
539 | // so cannot decide if data.from is the target or not. | |
6808d7a1 | 540 | const myChallenge = { |
2ada153c | 541 | id: c.id, |
71468011 | 542 | from: this.st.user.sid, |
81d9ce72 BA |
543 | to: c.to, |
544 | fen: c.fen, | |
545 | vid: c.vid, | |
71468011 | 546 | cadence: c.cadence, |
6808d7a1 | 547 | added: c.added |
dd75774d | 548 | }; |
6808d7a1 | 549 | this.send("challenge", { data: myChallenge, target: data.from }); |
81d9ce72 BA |
550 | } |
551 | break; | |
1efe1d79 | 552 | } |
71468011 | 553 | case "challenge": //after "askchallenge" |
6808d7a1 | 554 | case "newchallenge": { |
a64d9122 | 555 | // NOTE about next condition: see "askchallenge" case. |
71468011 | 556 | const chall = data.data; |
6808d7a1 BA |
557 | if ( |
558 | !chall.to || | |
559 | (this.people[chall.from].id > 0 && | |
560 | (chall.from == this.st.user.sid || chall.to == this.st.user.name)) | |
561 | ) { | |
71468011 BA |
562 | let newChall = Object.assign({}, chall); |
563 | newChall.type = this.classifyObject(chall); | |
564 | newChall.added = Date.now(); | |
565 | let fromValues = Object.assign({}, this.people[chall.from]); | |
566 | delete fromValues["pages"]; //irrelevant in this context | |
6808d7a1 | 567 | newChall.from = Object.assign({ sid: chall.from }, fromValues); |
a64d9122 BA |
568 | newChall.vname = this.getVname(newChall.vid); |
569 | this.challenges.push(newChall); | |
6808d7a1 BA |
570 | if ( |
571 | (newChall.type == "live" && this.cdisplay == "corr") || | |
572 | (newChall.type == "corr" && this.cdisplay == "live") | |
573 | ) { | |
574 | document | |
575 | .getElementById("btnC" + newChall.type) | |
576 | .classList.add("somethingnew"); | |
2f258c37 | 577 | } |
a64d9122 | 578 | } |
81d9ce72 | 579 | break; |
71468011 | 580 | } |
6808d7a1 | 581 | case "refusechallenge": { |
71468011 BA |
582 | const cid = data.data; |
583 | ArrayFun.remove(this.challenges, c => c.id == cid); | |
584 | alert(this.st.tr["Challenge declined"]); | |
585 | break; | |
586 | } | |
6808d7a1 | 587 | case "deletechallenge": { |
71468011 BA |
588 | // NOTE: the challenge may be already removed |
589 | const cid = data.data; | |
590 | ArrayFun.remove(this.challenges, c => c.id == cid); | |
591 | break; | |
592 | } | |
593 | case "game": //individual request | |
6808d7a1 | 594 | case "newgame": { |
71468011 BA |
595 | // NOTE: it may be live or correspondance |
596 | const game = data.data; | |
866842c3 BA |
597 | // Ignore games where I play (corr games) |
598 | if (game.players.every(p => p.id != this.st.user.id)) | |
599 | { | |
600 | let locGame = this.games.find(g => g.id == game.id); | |
601 | if (!locGame) { | |
602 | let newGame = game; | |
603 | newGame.type = this.classifyObject(game); | |
604 | newGame.vname = this.getVname(game.vid); | |
605 | if (!game.score) | |
606 | //if new game from Hall | |
607 | newGame.score = "*"; | |
608 | newGame.rids = [game.rid]; | |
609 | delete newGame["rid"]; | |
610 | this.games.push(newGame); | |
611 | if ( | |
612 | (newGame.type == "live" && this.gdisplay == "corr") || | |
613 | (newGame.type == "corr" && this.gdisplay == "live") | |
614 | ) { | |
615 | document | |
616 | .getElementById("btnG" + newGame.type) | |
617 | .classList.add("somethingnew"); | |
618 | } | |
619 | } else { | |
620 | // Append rid (if not already in list) | |
621 | if (!locGame.rids.includes(game.rid)) locGame.rids.push(game.rid); | |
2f258c37 | 622 | } |
51d87b52 | 623 | } |
81d9ce72 | 624 | break; |
1efe1d79 | 625 | } |
6808d7a1 | 626 | case "result": { |
48ab808f | 627 | let g = this.games.find(g => g.id == data.gid); |
6808d7a1 | 628 | if (g) g.score = data.score; |
48ab808f BA |
629 | break; |
630 | } | |
6808d7a1 | 631 | case "startgame": { |
5d04793e | 632 | // New game just started: data contain all information |
71468011 BA |
633 | const gameInfo = data.data; |
634 | if (this.classifyObject(gameInfo) == "live") | |
635 | this.startNewGame(gameInfo); | |
6808d7a1 BA |
636 | else { |
637 | this.infoMessage = | |
638 | this.st.tr["New correspondance game:"] + | |
639 | " <a href='#/game/" + | |
640 | gameInfo.id + | |
641 | "'>" + | |
642 | "#/game/" + | |
643 | gameInfo.id + | |
644 | "</a>"; | |
dce792f6 BA |
645 | let modalBox = document.getElementById("modalInfo"); |
646 | modalBox.checked = true; | |
5d04793e | 647 | } |
9d58ef95 | 648 | break; |
71468011 | 649 | } |
ac8f441c | 650 | case "newchat": |
71468011 | 651 | this.newChat = data.data; |
bd76b456 | 652 | if (!document.getElementById("modalPeople").checked) |
2f258c37 | 653 | document.getElementById("peopleBtn").classList.add("somethingnew"); |
9d58ef95 BA |
654 | break; |
655 | } | |
656 | }, | |
51d87b52 | 657 | socketCloseListener: function() { |
6808d7a1 | 658 | if (!this.conn) return; |
51d87b52 BA |
659 | this.conn = new WebSocket(this.connexionString); |
660 | this.conn.addEventListener("message", this.socketMessageListener); | |
661 | this.conn.addEventListener("close", this.socketCloseListener); | |
662 | }, | |
a6bddfc6 | 663 | // Challenge lifecycle: |
725da57f BA |
664 | loadNewchallVariant: async function(cb) { |
665 | const vname = this.getVname(this.newchallenge.vid); | |
666 | const vModule = await import("@/variants/" + vname + ".js"); | |
667 | this.newchallenge.V = vModule.VariantRules; | |
668 | this.newchallenge.vname = vname; | |
669 | if (cb) | |
670 | cb(); | |
671 | }, | |
672 | trySetNewchallDiag: function() { | |
673 | if (!this.newchallenge.fen) { | |
674 | this.newchallenge.diag = ""; | |
675 | return; | |
676 | } | |
677 | // If vid > 0 then the variant is loaded (function above): | |
678 | window.V = this.newchallenge.V; | |
679 | if ( | |
680 | this.newchallenge.vid > 0 && | |
681 | this.newchallenge.fen && | |
682 | V.IsGoodFen(this.newchallenge.fen) | |
683 | ) { | |
684 | const parsedFen = V.ParseFen(this.newchallenge.fen); | |
685 | this.newchallenge.diag = getDiagram({ | |
686 | position: parsedFen.position, | |
687 | orientation: V.GetOppCol(parsedFen.turn) | |
688 | }); | |
689 | } | |
690 | }, | |
9d58ef95 | 691 | newChallenge: async function() { |
8477e53d BA |
692 | if (this.newchallenge.cadence.match(/^[0-9]+$/)) |
693 | this.newchallenge.cadence += "+0"; //assume minutes, no increment | |
694 | const ctype = this.classifyObject(this.newchallenge); | |
695 | // TODO: cadence still unchecked so ctype could be wrong... | |
6808d7a1 | 696 | let error = ""; |
8477e53d | 697 | if (!this.newchallenge.vid) |
6808d7a1 | 698 | error = this.st.tr["Please select a variant"]; |
8477e53d BA |
699 | else if (ctype == "corr" && this.st.user.id <= 0) |
700 | error = this.st.tr["Please log in to play correspondance games"]; | |
701 | else if (this.newchallenge.to) { | |
702 | if (this.newchallenge.to == this.st.user.name) | |
703 | error = this.st.tr["Self-challenge is forbidden"]; | |
704 | else if ( | |
705 | ctype == "live" && | |
706 | Object.values(this.people).every(p => p.name != this.newchallenge.to) | |
707 | ) | |
708 | error = this.newchallenge.to + " " + this.st.tr["is not online"]; | |
709 | } | |
6808d7a1 BA |
710 | if (error) { |
711 | alert(error); | |
712 | return; | |
713 | } | |
725da57f | 714 | window.V = this.newchallenge.V; |
6808d7a1 | 715 | error = checkChallenge(this.newchallenge); |
6808d7a1 BA |
716 | if (error) { |
717 | alert(error); | |
718 | return; | |
719 | } | |
bb7dd7db | 720 | // NOTE: "from" information is not required here |
a7808884 | 721 | let chall = Object.assign({}, this.newchallenge); |
6808d7a1 | 722 | const finishAddChallenge = cid => { |
1efe1d79 | 723 | chall.id = cid || "c" + getRandString(); |
fe4c7e67 | 724 | // Remove old challenge if any (only one at a time of a given type): |
6808d7a1 BA |
725 | const cIdx = this.challenges.findIndex( |
726 | c => | |
727 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && | |
728 | c.type == ctype | |
729 | ); | |
730 | if (cIdx >= 0) { | |
5ea8d113 | 731 | // Delete current challenge (will be replaced now) |
6808d7a1 BA |
732 | this.send("deletechallenge", { data: this.challenges[cIdx].id }); |
733 | if (ctype == "corr") { | |
734 | ajax("/challenges", "DELETE", { id: this.challenges[cIdx].id }); | |
5ea8d113 BA |
735 | } |
736 | this.challenges.splice(cIdx, 1); | |
737 | } | |
6808d7a1 BA |
738 | this.send("newchallenge", { |
739 | data: Object.assign({ from: this.st.user.sid }, chall) | |
740 | }); | |
5ea8d113 | 741 | // Add new challenge: |
6808d7a1 | 742 | chall.from = { |
725da57f | 743 | // Decompose to avoid revealing email |
dcd68c41 BA |
744 | sid: this.st.user.sid, |
745 | id: this.st.user.id, | |
6808d7a1 | 746 | name: this.st.user.name |
dcd68c41 | 747 | }; |
71468011 BA |
748 | chall.added = Date.now(); |
749 | // NOTE: vname and type are redundant (can be deduced from cadence + vid) | |
750 | chall.type = ctype; | |
725da57f | 751 | chall.vname = this.newchallenge.vname; |
1efe1d79 | 752 | this.challenges.push(chall); |
71468011 BA |
753 | // Remember cadence + vid for quicker further challenges: |
754 | localStorage.setItem("cadence", chall.cadence); | |
25d18342 | 755 | localStorage.setItem("vid", chall.vid); |
b4d619d1 | 756 | document.getElementById("modalNewgame").checked = false; |
db1f1f9a BA |
757 | // Show the challenge if not on current display |
758 | if ( | |
759 | (ctype == "live" && this.cdisplay == "corr") || | |
760 | (ctype == "corr" && this.cdisplay == "live") | |
761 | ) { | |
762 | this.setDisplay('c', ctype); | |
763 | } | |
b4d619d1 | 764 | }; |
6808d7a1 | 765 | if (ctype == "live") { |
1efe1d79 | 766 | // Live challenges have a random ID |
71468011 | 767 | finishAddChallenge(null); |
6808d7a1 | 768 | } else { |
b4d619d1 | 769 | // Correspondance game: send challenge to server |
6808d7a1 BA |
770 | ajax("/challenges", "POST", { chall: chall }, response => { |
771 | finishAddChallenge(response.cid); | |
772 | }); | |
9d58ef95 | 773 | } |
fb54f098 | 774 | }, |
725da57f BA |
775 | // Callback function after a diagram was showed to accept |
776 | // or refuse targetted challenge: | |
777 | decisionChallenge: function(accepted) { | |
778 | this.curChallToAccept.accepted = accepted; | |
779 | this.finishProcessingChallenge(this.curChallToAccept); | |
780 | document.getElementById("modalAccept").checked = false; | |
781 | }, | |
782 | finishProcessingChallenge: function(c) { | |
783 | if (c.accepted) { | |
784 | c.seat = { | |
785 | // Again, avoid c.seat = st.user to not reveal email | |
786 | sid: this.st.user.sid, | |
787 | id: this.st.user.id, | |
788 | name: this.st.user.name | |
789 | }; | |
790 | this.launchGame(c); | |
791 | } else { | |
792 | this.send("refusechallenge", { data: c.id, target: c.from.sid }); | |
793 | } | |
794 | this.send("deletechallenge", { data: c.id }); | |
795 | }, | |
796 | clickChallenge: async function(c) { | |
6808d7a1 BA |
797 | const myChallenge = |
798 | c.from.sid == this.st.user.sid || //live | |
799 | (this.st.user.id > 0 && c.from.id == this.st.user.id); //corr | |
800 | if (!myChallenge) { | |
801 | if (c.type == "corr" && this.st.user.id <= 0) { | |
802 | alert(this.st.tr["Please log in to accept corr challenges"]); | |
803 | return; | |
804 | } | |
a6bddfc6 | 805 | c.accepted = true; |
725da57f BA |
806 | const vModule = await import("@/variants/" + c.vname + ".js"); |
807 | window.V = vModule.VariantRules; | |
6808d7a1 | 808 | if (c.to) { |
725da57f BA |
809 | // c.to == this.st.user.name (connected) |
810 | if (c.fen) { | |
811 | const parsedFen = V.ParseFen(c.fen); | |
0cd02605 | 812 | c.mycolor = V.GetOppCol(parsedFen.turn); |
725da57f BA |
813 | this.tchallDiag = getDiagram({ |
814 | position: parsedFen.position, | |
0cd02605 | 815 | orientation: c.mycolor |
725da57f BA |
816 | }); |
817 | this.curChallToAccept = c; | |
818 | document.getElementById("modalAccept").checked = true; | |
819 | } | |
820 | else { | |
821 | if (!confirm(this.st.tr["Accept challenge?"])) | |
822 | c.accepted = false; | |
823 | this.finishProcessingChallenge(c); | |
824 | } | |
485fccd5 | 825 | } |
725da57f BA |
826 | else |
827 | this.finishProcessingChallenge(c); | |
828 | } | |
6808d7a1 | 829 | else { |
725da57f | 830 | // My challenge |
6808d7a1 BA |
831 | if (c.type == "corr") { |
832 | ajax("/challenges", "DELETE", { id: c.id }); | |
36093eba | 833 | } |
6808d7a1 | 834 | this.send("deletechallenge", { data: c.id }); |
485fccd5 | 835 | } |
5ea8d113 | 836 | // In all cases, the challenge is consumed: |
3d55deea | 837 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
a6bddfc6 | 838 | }, |
a64d9122 | 839 | // NOTE: when launching game, the challenge is already being deleted |
725da57f | 840 | launchGame: function(c) { |
71468011 | 841 | // These game informations will be shared |
6808d7a1 | 842 | let gameInfo = { |
11667c79 | 843 | id: getRandString(), |
a6bddfc6 | 844 | fen: c.fen || V.GenRandInitFen(), |
0cd02605 BA |
845 | // White player index 0, black player index 1: |
846 | players: c.mycolor | |
847 | ? (c.mycolor == "w" ? [c.seat, c.from] : [c.from, c.seat]) | |
848 | : shuffle([c.from, c.seat]), | |
a6bddfc6 | 849 | vid: c.vid, |
6808d7a1 | 850 | cadence: c.cadence |
a6bddfc6 | 851 | }; |
5ea8d113 | 852 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
6808d7a1 BA |
853 | if (!oppsid) { |
854 | oppsid = Object.keys(this.people).find( | |
855 | sid => this.people[sid].id == c.from.id | |
856 | ); | |
8c564f46 | 857 | } |
71468011 | 858 | const notifyNewgame = () => { |
6808d7a1 BA |
859 | if (oppsid) |
860 | //opponent is online | |
861 | this.send("startgame", { data: gameInfo, target: oppsid }); | |
71468011 | 862 | // Send game info (only if live) to everyone except me in this tab |
6808d7a1 | 863 | this.send("newgame", { data: gameInfo }); |
411d23cd | 864 | }; |
6808d7a1 | 865 | if (c.type == "live") { |
71468011 | 866 | notifyNewgame(); |
485fccd5 | 867 | this.startNewGame(gameInfo); |
6808d7a1 BA |
868 | } //corr: game only on server |
869 | else { | |
485fccd5 BA |
870 | ajax( |
871 | "/games", | |
872 | "POST", | |
6808d7a1 | 873 | { gameInfo: gameInfo, cid: c.id }, //cid useful to delete challenge |
411d23cd | 874 | response => { |
11667c79 | 875 | gameInfo.id = response.gameId; |
71468011 | 876 | notifyNewgame(); |
411d23cd BA |
877 | this.$router.push("/game/" + response.gameId); |
878 | } | |
485fccd5 BA |
879 | ); |
880 | } | |
fb54f098 | 881 | }, |
a9b131f1 | 882 | // NOTE: for live games only (corr games start on the server) |
42c15a75 | 883 | startNewGame: function(gameInfo) { |
25996aed BA |
884 | const game = Object.assign({}, gameInfo, { |
885 | // (other) Game infos: constant | |
6d01bb17 | 886 | fenStart: gameInfo.fen, |
71468011 BA |
887 | vname: this.getVname(gameInfo.vid), |
888 | created: Date.now(), | |
25996aed | 889 | // Game state (including FEN): will be updated |
967a2686 | 890 | moves: [], |
a9b131f1 | 891 | clocks: [-1, -1], //-1 = unstarted |
66d03f23 | 892 | initime: [0, 0], //initialized later |
6808d7a1 | 893 | score: "*" |
a7808884 | 894 | }); |
8477e53d BA |
895 | GameStorage.add(game, (err) => { |
896 | // If an error occurred, game is not added: abort | |
897 | if (!err) { | |
db1f1f9a BA |
898 | if (this.st.settings.sound) |
899 | new Audio("/sounds/newgame.wav").play().catch(() => {}); | |
8477e53d BA |
900 | this.$router.push("/game/" + gameInfo.id); |
901 | } | |
902 | }); | |
6808d7a1 BA |
903 | } |
904 | } | |
85e5b5c1 | 905 | }; |
ccd4a2b7 | 906 | </script> |
85e5b5c1 | 907 | |
41c80bb6 | 908 | <style lang="sass" scoped> |
5bcc9b31 BA |
909 | .active |
910 | color: #42a983 | |
a154d45e BA |
911 | |
912 | #infoDiv > .card | |
f854c94f BA |
913 | padding: 15px 0 |
914 | max-width: 430px | |
a154d45e | 915 | |
725da57f | 916 | #newgameDiv > .card, #acceptDiv > .card |
a154d45e BA |
917 | max-width: 767px |
918 | max-height: 100% | |
919 | ||
bd76b456 BA |
920 | div#peopleWrap > .card |
921 | max-height: 100% | |
922 | ||
923 | @media screen and (min-width: 1281px) | |
924 | div#peopleWrap > .card | |
925 | max-width: 66.67% | |
926 | ||
927 | @media screen and (max-width: 1280px) | |
928 | div#peopleWrap > .card | |
929 | max-width: 83.33% | |
930 | ||
931 | @media screen and (max-width: 767px) | |
932 | div#peopleWrap > .card | |
933 | max-width: 100% | |
934 | ||
ed06d9e9 BA |
935 | #players |
936 | width: 50% | |
937 | position: relative | |
938 | float: left | |
910d631b | 939 | |
ed06d9e9 BA |
940 | #chat |
941 | width: 50% | |
942 | float: left | |
943 | position: relative | |
910d631b | 944 | |
ed06d9e9 BA |
945 | @media screen and (max-width: 767px) |
946 | #players, #chats | |
947 | width: 100% | |
910d631b | 948 | |
72ccbd67 BA |
949 | #chat > .card |
950 | max-width: 100% | |
951 | margin: 0; | |
952 | border: none; | |
910d631b | 953 | |
41c80bb6 | 954 | #players > p |
ed06d9e9 | 955 | margin-left: 5px |
910d631b | 956 | |
dcd68c41 BA |
957 | .anonymous |
958 | font-style: italic | |
910d631b | 959 | |
dcd68c41 | 960 | button.player-action |
41c80bb6 | 961 | margin-left: 32px |
2f258c37 BA |
962 | |
963 | .somethingnew | |
964 | background-color: #c5fefe !important | |
965 | ||
966 | .tabbtn | |
5fe7e71c | 967 | background-color: #f9faee |
2f258c37 | 968 | |
725da57f BA |
969 | button.acceptBtn |
970 | background-color: lightgreen | |
971 | button.refuseBtn | |
972 | background-color: red | |
973 | ||
974 | #buttonsTchall | |
975 | margin-top: 10px | |
976 | ||
977 | .variantName | |
978 | font-weight: bold | |
979 | ||
980 | .diagram | |
981 | margin: 0 auto | |
982 | max-width: 400px | |
983 | ||
984 | #inputFen | |
985 | width: 100% | |
986 | ||
2f258c37 BA |
987 | #div2, #div3 |
988 | margin-top: 15px | |
989 | @media screen and (max-width: 767px) | |
990 | #div2, #div3 | |
991 | margin-top: 0 | |
85e5b5c1 | 992 | </style> |