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