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