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