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