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