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