Commit | Line | Data |
---|---|---|
ccd4a2b7 | 1 | <template lang="pug"> |
9d58ef95 | 2 | main |
dce792f6 BA |
3 | input#modalInfo.modal(type="checkbox") |
4 | div(role="dialog" aria-labelledby="infoMessage") | |
5 | .card.smallpad.small-modal.text-center | |
6 | label.modal-close(for="modalInfo") | |
7 | h3#infoMessage.section | |
8 | p New game started: #[a(href="/game/" + {{ newGameId }})] | |
5b020e73 BA |
9 | input#modalNewgame.modal(type="checkbox") |
10 | div(role="dialog" aria-labelledby="titleFenedit") | |
11 | .card.smallpad | |
12 | label#closeNewgame.modal-close(for="modalNewgame") | |
13 | fieldset | |
14 | label(for="selectVariant") {{ st.tr["Variant"] }} | |
9d58ef95 | 15 | select#selectVariant(v-model="newchallenge.vid") |
85e5b5c1 | 16 | option(v-for="v in st.variants" :value="v.id") {{ v.name }} |
5b020e73 | 17 | fieldset |
b4d619d1 | 18 | label(for="timeControl") {{ st.tr["Time control"] }} |
9d58ef95 | 19 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
b4d619d1 BA |
20 | placeholder="3m+2s, 1h+30s, 7d+1d ...") |
21 | fieldset(v-if="st.user.id > 0") | |
9d58ef95 | 22 | label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }} |
6fba6e0c | 23 | input#selectPlayers(type="text" v-model="newchallenge.to") |
b4d619d1 | 24 | fieldset(v-if="st.user.id > 0") |
9d58ef95 BA |
25 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
26 | input#inputFen(type="text" v-model="newchallenge.fen") | |
b4d619d1 | 27 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
9d58ef95 BA |
28 | .row |
29 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
30 | button(onClick="doClick('modalNewgame')") New game | |
31 | .row | |
1efe1d79 | 32 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
6855163c BA |
33 | .collapse |
34 | input#challengeSection(type="radio" checked aria-hidden="true" name="accordion") | |
35 | label(for="challengeSection" aria-hidden="true") Challenges | |
36 | div | |
37 | .button-group | |
38 | button(@click="cdisplay='live'") Live Challenges | |
39 | button(@click="cdisplay='corr'") Correspondance challenges | |
40 | ChallengeList(v-show="cdisplay=='live'" | |
41 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") | |
42 | ChallengeList(v-show="cdisplay=='corr'" | |
43 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") | |
1efe1d79 | 44 | input#peopleSection(type="radio" aria-hidden="true" name="accordion") |
6855163c BA |
45 | label(for="peopleSection" aria-hidden="true") People |
46 | div | |
1efe1d79 BA |
47 | .button-group |
48 | button(@click="pdisplay='players'") Players | |
49 | button(@click="pdisplay='chat'") Chat | |
6855163c BA |
50 | #players(v-show="pdisplay=='players'") |
51 | h3 Online players | |
52 | .player(v-for="p in uniquePlayers" @click="tryChallenge(p)" | |
53 | :class="{anonymous: !!p.count}" | |
54 | ) | |
55 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} | |
56 | #chat(v-show="pdisplay=='chat'") | |
57 | h3 Chat (TODO) | |
1efe1d79 | 58 | input#gameSection(type="radio" aria-hidden="true" name="accordion") |
6855163c BA |
59 | label(for="gameSection" aria-hidden="true") Games |
60 | div | |
61 | .button-group | |
62 | button(@click="gdisplay='live'") Live games | |
63 | button(@click="gdisplay='corr'") Correspondance games | |
64 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" | |
65 | @show-game="showGame") | |
66 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" | |
67 | @show-game="showGame") | |
625022fd BA |
68 | </template> |
69 | ||
70 | <script> | |
5b020e73 | 71 | import { store } from "@/store"; |
9d58ef95 BA |
72 | import { checkChallenge } from "@/data/challengeCheck"; |
73 | import { ArrayFun } from "@/utils/array"; | |
03608482 | 74 | import { ajax } from "@/utils/ajax"; |
4b0384fa | 75 | import { getRandString, shuffle } from "@/utils/alea"; |
5b020e73 BA |
76 | import GameList from "@/components/GameList.vue"; |
77 | import ChallengeList from "@/components/ChallengeList.vue"; | |
967a2686 | 78 | import { GameStorage } from "@/utils/gameStorage"; |
625022fd | 79 | export default { |
cf2343ce | 80 | name: "my-hall", |
5b020e73 BA |
81 | components: { |
82 | GameList, | |
83 | ChallengeList, | |
84 | }, | |
fb54f098 BA |
85 | data: function () { |
86 | return { | |
5b020e73 | 87 | st: store.state, |
6855163c BA |
88 | cdisplay: "live", //or corr |
89 | pdisplay: "players", //or chat | |
fb54f098 | 90 | gdisplay: "live", |
6855163c | 91 | games: [], |
b4d619d1 | 92 | challenges: [], |
6fba6e0c | 93 | people: [], //(all) online players |
dce792f6 | 94 | newGameId: 0, |
9d58ef95 | 95 | newchallenge: { |
fb54f098 BA |
96 | fen: "", |
97 | vid: 0, | |
6fba6e0c | 98 | to: "", //name of challenged player (if any) |
6faa92f2 | 99 | timeControl: "", //"2m+2s" ...etc |
fb54f098 BA |
100 | }, |
101 | }; | |
102 | }, | |
fd7aea36 BA |
103 | watch: { |
104 | // st.variants changes only once, at loading from [] to [...] | |
105 | "st.variants": function(variantArray) { | |
106 | // Set potential challenges and games variant names: | |
107 | this.challenges.forEach(c => { | |
108 | if (c.vname == "") | |
109 | c.vname = this.getVname(c.vid); | |
110 | }); | |
111 | this.games.forEach(g => { | |
112 | if (g.vname == "") | |
113 | g.vname = this.getVname(g.vid) | |
114 | }); | |
115 | }, | |
116 | }, | |
b4d619d1 BA |
117 | computed: { |
118 | uniquePlayers: function() { | |
6855163c | 119 | // Show e.g. "@nonymous (5)", and do nothing on click on anonymous |
4d64881e BA |
120 | let anonymous = {id:0, name:"@nonymous", count:0}; |
121 | let playerList = []; | |
6fba6e0c | 122 | this.people.forEach(p => { |
b4d619d1 BA |
123 | if (p.id > 0) |
124 | playerList.push(p); | |
125 | else | |
4d64881e | 126 | anonymous.count++; |
b4d619d1 | 127 | }); |
4d64881e BA |
128 | if (anonymous.count > 0) |
129 | playerList.push(anonymous); | |
b4d619d1 BA |
130 | return playerList; |
131 | }, | |
132 | }, | |
9d58ef95 | 133 | created: function() { |
4d64881e | 134 | // Always add myself to players' list |
66d03f23 BA |
135 | const my = this.st.user; |
136 | this.people.push({sid:my.sid, id:my.id, name:my.name}); | |
f6f2bef1 BA |
137 | // Retrieve live challenge (not older than 30 minute) if any: |
138 | const chall = JSON.parse(localStorage.getItem("challenge") || "false"); | |
139 | if (!!chall) | |
140 | { | |
141 | if ((Date.now() - chall.added)/1000 <= 30*60) | |
142 | this.challenges.push(chall); | |
143 | else | |
144 | localStorage.removeItem("challenge"); | |
145 | } | |
98f48579 BA |
146 | if (this.st.user.id > 0) |
147 | { | |
5d04793e BA |
148 | // Ask server for current corr games (all but mines) |
149 | ajax( | |
150 | "/games", | |
151 | "GET", | |
152 | {uid: this.st.user.id, excluded: true}, | |
153 | response => { | |
25996aed | 154 | this.games = this.games.concat(response.games.map(g => { |
a9b131f1 BA |
155 | const type = this.classifyObject(g); |
156 | const vname = this.getVname(g.vid); | |
157 | return Object.assign({}, g, {type: type, vname: vname}); | |
158 | })); | |
5d04793e BA |
159 | } |
160 | ); | |
161 | // Also ask for corr challenges (open + sent to me) | |
98f48579 BA |
162 | ajax( |
163 | "/challenges", | |
164 | "GET", | |
165 | {uid: this.st.user.id}, | |
166 | response => { | |
bebcc8d4 BA |
167 | // Gather all senders names, and then retrieve full identity: |
168 | // (TODO [perf]: some might be online...) | |
169 | const uids = response.challenges.map(c => { return c.uid }); | |
170 | ajax("/users", | |
171 | "GET", | |
ed9c9c37 BA |
172 | { ids: uids.join(",") }, |
173 | response2 => { | |
174 | let names = {}; | |
175 | response2.users.forEach(u => {names[u.id] = u.name}); | |
bebcc8d4 BA |
176 | this.challenges = this.challenges.concat( |
177 | response.challenges.map(c => { | |
178 | // (just players names in fact) | |
179 | const from = {name: names[c.uid], id: c.uid}; | |
180 | const type = this.classifyObject(c); | |
181 | const vname = this.getVname(c.vid); | |
182 | return Object.assign({}, c, {type: type, vname: vname, from: from}); | |
183 | }) | |
184 | ) | |
185 | } | |
186 | ); | |
98f48579 BA |
187 | } |
188 | ); | |
189 | } | |
2ada153c | 190 | // 0.1] Ask server for room composition: |
7b01e447 | 191 | const funcPollClients = () => { |
81d9ce72 | 192 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
4d64881e | 193 | }; |
7b01e447 BA |
194 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
195 | funcPollClients(); | |
196 | else //socket not ready yet (initial loading) | |
3cb412e9 | 197 | this.st.conn.onopen = funcPollClients; |
4d64881e BA |
198 | this.st.conn.onmessage = this.socketMessageListener; |
199 | const socketCloseListener = () => { | |
cdb34c93 | 200 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
4d64881e BA |
201 | this.st.conn.addEventListener('message', this.socketMessageListener); |
202 | this.st.conn.addEventListener('close', socketCloseListener); | |
203 | }; | |
204 | this.st.conn.onclose = socketCloseListener; | |
9d58ef95 | 205 | }, |
fb54f098 | 206 | methods: { |
a6bddfc6 | 207 | // Helpers: |
6855163c BA |
208 | filterChallenges: function(type) { |
209 | return this.challenges.filter(c => c.type == type); | |
210 | }, | |
211 | filterGames: function(type) { | |
a9b131f1 | 212 | return this.games.filter(g => g.type == type); |
6855163c | 213 | }, |
2ada153c | 214 | classifyObject: function(o) { //challenge or game |
6855163c | 215 | // Heuristic: should work for most cases... (TODO) |
2ada153c | 216 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
6855163c | 217 | }, |
2ada153c | 218 | showGame: function(g) { |
5bd05dba | 219 | // NOTE: we are an observer, since only games I don't play are shown here |
2ada153c | 220 | // ==> Moves sent by connected remote player(s) if live game |
d9b86b16 | 221 | let url = "/game/" + g.id; |
2ada153c BA |
222 | if (g.type == "live") |
223 | { | |
d9b86b16 BA |
224 | const remotes = g.players.filter(p => this.people.some(pl => pl.sid == p.sid)); |
225 | const rIdx = (remotes.length == 1 ? 0 : Math.floor(Math.random()*2)); | |
226 | url += "?rid=" + remotes[rIdx].sid; | |
2ada153c BA |
227 | } |
228 | this.$router.push(url); | |
a6bddfc6 | 229 | }, |
a7808884 | 230 | // TODO: ...filter(...)[0].name, one-line, just remove this function |
a6bddfc6 BA |
231 | getVname: function(vid) { |
232 | const vIdx = this.st.variants.findIndex(v => v.id == vid); | |
ed9c9c37 | 233 | return vIdx >= 0 ? this.st.variants[vIdx].name : ""; |
a6bddfc6 BA |
234 | }, |
235 | getSid: function(pname) { | |
6fba6e0c BA |
236 | const pIdx = this.people.findIndex(pl => pl.name == pname); |
237 | return (pIdx === -1 ? null : this.people[pIdx].sid); | |
a6bddfc6 | 238 | }, |
5bd05dba | 239 | getPname: function(sid) { |
6fba6e0c BA |
240 | const pIdx = this.people.findIndex(pl => pl.sid == sid); |
241 | return (pIdx === -1 ? null : this.people[pIdx].name); | |
5bd05dba | 242 | }, |
a6bddfc6 BA |
243 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
244 | const doSend = (code, obj, sid) => { | |
245 | this.st.conn.send(JSON.stringify(Object.assign( | |
246 | {}, | |
247 | {code: code}, | |
248 | obj, | |
249 | {target: sid} | |
250 | ))); | |
251 | }; | |
c9695cb1 | 252 | if (!!to) |
a6bddfc6 | 253 | { |
c9695cb1 BA |
254 | // Challenge with targeted players |
255 | const targetSid = this.getSid(to); | |
256 | if (!targetSid) | |
257 | { | |
258 | if (!!warnDisconnected) | |
259 | alert("Warning: " + pname + " is not connected"); | |
260 | } | |
261 | else | |
262 | doSend(code, obj, targetSid); | |
a6bddfc6 BA |
263 | } |
264 | else | |
265 | { | |
266 | // Open challenge: send to all connected players (except us) | |
6fba6e0c | 267 | this.people.forEach(p => { |
a6bddfc6 BA |
268 | if (p.sid != this.st.user.sid) //only sid is always set |
269 | doSend(code, obj, p.sid); | |
270 | }); | |
271 | } | |
272 | }, | |
273 | // Messaging center: | |
9d58ef95 BA |
274 | socketMessageListener: function(msg) { |
275 | const data = JSON.parse(msg.data); | |
276 | switch (data.code) | |
277 | { | |
f4f4c03c | 278 | // 0.2] Receive clients list (just socket IDs) |
81d9ce72 | 279 | case "pollclients": |
1efe1d79 | 280 | { |
5a3da968 | 281 | data.sockIds.forEach(sid => { |
6fba6e0c | 282 | this.people.push({sid:sid, id:0, name:""}); |
81d9ce72 | 283 | // Ask identity, challenges and game(s) |
5a3da968 | 284 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
2ada153c | 285 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
81d9ce72 | 286 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
5a3da968 BA |
287 | }); |
288 | break; | |
1efe1d79 | 289 | } |
81d9ce72 | 290 | case "askidentity": |
1efe1d79 | 291 | { |
6855163c BA |
292 | // Request for identification: reply if I'm not anonymous |
293 | if (this.st.user.id > 0) | |
294 | { | |
295 | this.st.conn.send(JSON.stringify( | |
66d03f23 BA |
296 | // people[0] instead of st.user to avoid sending email |
297 | {code:"identity", user:this.people[0], target:data.from})); | |
6855163c | 298 | } |
5a3da968 | 299 | break; |
1efe1d79 | 300 | } |
dd75774d | 301 | case "askchallenge": |
1efe1d79 | 302 | { |
6855163c | 303 | // Send my current live challenge (if any) |
dd75774d | 304 | const cIdx = this.challenges |
6855163c | 305 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
dd75774d BA |
306 | if (cIdx >= 0) |
307 | { | |
308 | const c = this.challenges[cIdx]; | |
309 | const myChallenge = | |
310 | { | |
81d9ce72 | 311 | // Minimal challenge informations: (from not required) |
2ada153c | 312 | id: c.id, |
81d9ce72 BA |
313 | to: c.to, |
314 | fen: c.fen, | |
315 | vid: c.vid, | |
316 | timeControl: c.timeControl | |
dd75774d BA |
317 | }; |
318 | this.st.conn.send(JSON.stringify({code:"challenge", | |
42c15a75 | 319 | chall:myChallenge, target:data.from})); |
81d9ce72 BA |
320 | } |
321 | break; | |
1efe1d79 | 322 | } |
81d9ce72 | 323 | case "askgame": |
1efe1d79 | 324 | { |
42c15a75 BA |
325 | // Send my current live game (if any) |
326 | GameStorage.getCurrent((game) => { | |
327 | if (!!game) | |
328 | { | |
329 | const myGame = | |
330 | { | |
331 | // Minimal game informations: | |
332 | id: game.id, | |
333 | players: game.players.map(p => p.name), | |
a9b131f1 | 334 | vid: game.vid, |
42c15a75 BA |
335 | timeControl: game.timeControl, |
336 | }; | |
337 | this.st.conn.send(JSON.stringify({code:"game", | |
338 | game:myGame, target:data.from})); | |
339 | } | |
340 | }); | |
81d9ce72 | 341 | break; |
1efe1d79 | 342 | } |
5a3da968 | 343 | case "identity": |
1efe1d79 | 344 | { |
6fba6e0c BA |
345 | const pIdx = this.people.findIndex(p => p.sid == data.user.sid); |
346 | this.people[pIdx].id = data.user.id; | |
347 | this.people[pIdx].name = data.user.name; | |
5a3da968 | 348 | break; |
1efe1d79 | 349 | } |
dd75774d | 350 | case "challenge": |
1efe1d79 | 351 | { |
dd75774d | 352 | // Receive challenge from some player (+sid) |
6855163c | 353 | let newChall = data.chall; |
2ada153c | 354 | newChall.type = this.classifyObject(data.chall); |
6fba6e0c BA |
355 | const pIdx = this.people.findIndex(p => p.sid == data.from); |
356 | newChall.from = this.people[pIdx]; //may be anonymous | |
42c15a75 | 357 | newChall.added = Date.now(); //TODO: this is reception timestamp, not creation |
25996aed | 358 | newChall.vname = this.getVname(newChall.vid); |
6855163c | 359 | this.challenges.push(newChall); |
81d9ce72 | 360 | break; |
1efe1d79 | 361 | } |
dd75774d | 362 | case "game": |
1efe1d79 | 363 | { |
6855163c | 364 | // Receive game from some player (+sid) |
6855163c | 365 | // NOTE: it may be correspondance (if newgame while we are connected) |
d9b86b16 BA |
366 | if (!this.games.some(g => g.id == data.game.id)) //ignore duplicates |
367 | { | |
368 | let newGame = data.game; | |
369 | newGame.type = this.classifyObject(data.game); | |
a9b131f1 | 370 | newGame.vname = this.getVname(data.game.vid); |
d9b86b16 BA |
371 | newGame.rid = data.from; |
372 | newGame.score = "*"; | |
373 | this.games.push(newGame); | |
374 | } | |
81d9ce72 | 375 | break; |
1efe1d79 | 376 | } |
9d58ef95 | 377 | case "newgame": |
1efe1d79 | 378 | { |
66d03f23 BA |
379 | // TODO: next line required ?! |
380 | //ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
5d04793e | 381 | // New game just started: data contain all information |
66d03f23 | 382 | if (this.classifyObject(data.gameInfo) == "live") |
5d04793e | 383 | this.startNewGame(data.gameInfo); |
5d04793e BA |
384 | else |
385 | { | |
dce792f6 BA |
386 | this.newGameId = data.gameInfo.gameId; |
387 | let modalBox = document.getElementById("modalInfo"); | |
388 | modalBox.checked = true; | |
389 | setTimeout(() => { modalBox.checked = false; }, 2500); | |
5d04793e | 390 | } |
9d58ef95 | 391 | break; |
1efe1d79 | 392 | } |
bb7dd7db BA |
393 | case "refusechallenge": |
394 | { | |
485fccd5 | 395 | alert(this.getPname(data.from) + " declined your challenge"); |
5bd05dba | 396 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
bb7dd7db BA |
397 | break; |
398 | } | |
1efe1d79 BA |
399 | case "deletechallenge": |
400 | { | |
1ba761c8 | 401 | // NOTE: the challenge may be already removed |
9d58ef95 | 402 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
66d03f23 | 403 | localStorage.removeItem("challenge"); //in case of |
9d58ef95 | 404 | break; |
1efe1d79 | 405 | } |
b4d619d1 | 406 | case "connect": |
1efe1d79 | 407 | { |
6fba6e0c | 408 | this.people.push({name:"", id:0, sid:data.sid}); |
5a3da968 | 409 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); |
f05815d7 BA |
410 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid})); |
411 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid})); | |
9d58ef95 | 412 | break; |
1efe1d79 | 413 | } |
b4d619d1 | 414 | case "disconnect": |
1efe1d79 | 415 | { |
6fba6e0c | 416 | ArrayFun.remove(this.people, p => p.sid == data.sid); |
a6bddfc6 | 417 | // Also remove all challenges sent by this player: |
2ada153c BA |
418 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
419 | // And all live games where he plays and no other opponent is online | |
420 | ArrayFun.remove(this.games, g => | |
421 | g.type == "live" && (g.players.every(p => p.sid == data.sid | |
6fba6e0c | 422 | || !this.people.some(pl => pl.sid == p.sid))), "all"); |
9d58ef95 | 423 | break; |
1efe1d79 | 424 | } |
9d58ef95 BA |
425 | } |
426 | }, | |
a6bddfc6 | 427 | // Challenge lifecycle: |
b4d619d1 BA |
428 | tryChallenge: function(player) { |
429 | if (player.id == 0) | |
430 | return; //anonymous players cannot be challenged | |
a7808884 | 431 | this.newchallenge.to = player.name; |
b4d619d1 | 432 | doClick("modalNewgame"); |
fb54f098 | 433 | }, |
9d58ef95 | 434 | newChallenge: async function() { |
bb7dd7db | 435 | const vname = this.getVname(this.newchallenge.vid); |
1efe1d79 BA |
436 | const vModule = await import("@/variants/" + vname + ".js"); |
437 | window.V = vModule.VariantRules; | |
9d58ef95 BA |
438 | const error = checkChallenge(this.newchallenge); |
439 | if (!!error) | |
440 | return alert(error); | |
2ada153c | 441 | const ctype = this.classifyObject(this.newchallenge); |
098cd7f1 BA |
442 | if (ctype == "corr" && this.st.user.id <= 0) |
443 | return alert("Please log in to play correspondance games"); | |
bb7dd7db | 444 | // NOTE: "from" information is not required here |
a7808884 | 445 | let chall = Object.assign({}, this.newchallenge); |
2ada153c | 446 | const finishAddChallenge = (cid,warnDisconnected) => { |
1efe1d79 | 447 | chall.id = cid || "c" + getRandString(); |
2ada153c | 448 | // Send challenge to peers (if connected) |
c9695cb1 | 449 | this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected); |
1efe1d79 | 450 | chall.added = Date.now(); |
a9b131f1 | 451 | // NOTE: vname and type are redundant (can be deduced from timeControl + vid) |
bb7dd7db BA |
452 | chall.type = ctype; |
453 | chall.vname = vname; | |
66d03f23 | 454 | chall.from = this.people[0]; //avoid sending email |
1efe1d79 | 455 | this.challenges.push(chall); |
f6f2bef1 | 456 | localStorage.setItem("challenge", JSON.stringify(chall)); |
b4d619d1 BA |
457 | document.getElementById("modalNewgame").checked = false; |
458 | }; | |
1efe1d79 BA |
459 | const cIdx = this.challenges.findIndex( |
460 | c => c.from.sid == this.st.user.sid && c.type == ctype); | |
461 | if (cIdx >= 0) | |
b4d619d1 | 462 | { |
1efe1d79 | 463 | // Delete current challenge (will be replaced now) |
bb7dd7db | 464 | this.sendSomethingTo(this.challenges[cIdx].to, |
1efe1d79 BA |
465 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
466 | if (ctype == "corr") | |
467 | { | |
468 | ajax( | |
469 | "/challenges", | |
470 | "DELETE", | |
471 | {id: this.challenges[cIdx].id} | |
472 | ); | |
473 | } | |
474 | this.challenges.splice(cIdx, 1); | |
475 | } | |
476 | if (ctype == "live") | |
477 | { | |
478 | // Live challenges have a random ID | |
2ada153c | 479 | finishAddChallenge(null, "warnDisconnected"); |
03608482 | 480 | } |
b4d619d1 | 481 | else |
03608482 | 482 | { |
b4d619d1 | 483 | // Correspondance game: send challenge to server |
03608482 | 484 | ajax( |
1efe1d79 | 485 | "/challenges", |
03608482 | 486 | "POST", |
bebcc8d4 | 487 | { chall: chall }, |
1efe1d79 | 488 | response => { finishAddChallenge(response.cid); } |
03608482 | 489 | ); |
9d58ef95 | 490 | } |
fb54f098 | 491 | }, |
a6bddfc6 | 492 | clickChallenge: function(c) { |
66d03f23 BA |
493 | // In all cases, the challenge is consumed: |
494 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
495 | // NOTE: deletechallenge event might be redundant (but it's easier this way) | |
496 | this.sendSomethingTo((!!c.to ? c.from : null), "deletechallenge", {cid:c.id}); | |
485fccd5 BA |
497 | const myChallenge = (c.from.sid == this.st.user.sid //live |
498 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr | |
499 | if (!myChallenge) | |
a6bddfc6 BA |
500 | { |
501 | c.accepted = true; | |
485fccd5 | 502 | if (!!c.to) //c.to == this.st.user.name (connected) |
a6bddfc6 BA |
503 | { |
504 | // TODO: if special FEN, show diagram after loading variant | |
505 | c.accepted = confirm("Accept challenge?"); | |
506 | } | |
485fccd5 | 507 | if (c.accepted) |
36093eba | 508 | { |
8c564f46 | 509 | c.seat = this.people[0]; //== this.st.user, avoid revealing email |
485fccd5 BA |
510 | this.launchGame(c); |
511 | } | |
512 | else | |
513 | { | |
514 | this.st.conn.send(JSON.stringify({ | |
515 | code: "refusechallenge", | |
516 | cid: c.id, target: c.from.sid})); | |
36093eba | 517 | } |
a6bddfc6 | 518 | } |
2be5d614 | 519 | else //my challenge |
485fccd5 | 520 | { |
2be5d614 BA |
521 | localStorage.removeItem("challenge"); |
522 | if (c.type == "corr") | |
523 | { | |
524 | ajax( | |
525 | "/challenges", | |
526 | "DELETE", | |
527 | {id: c.id} | |
528 | ); | |
529 | } | |
485fccd5 | 530 | } |
a6bddfc6 | 531 | }, |
485fccd5 | 532 | // NOTE: when launching game, the challenge is already deleted |
36093eba | 533 | launchGame: async function(c) { |
a9b131f1 | 534 | const vModule = await import("@/variants/" + c.vname + ".js"); |
a6bddfc6 | 535 | window.V = vModule.VariantRules; |
4b0384fa BA |
536 | // These game informations will be sent to other players |
537 | const gameInfo = | |
a6bddfc6 | 538 | { |
4b0384fa | 539 | gameId: getRandString(), |
a6bddfc6 | 540 | fen: c.fen || V.GenRandInitFen(), |
5d04793e | 541 | players: shuffle([c.from, c.seat]), //white then black |
a6bddfc6 | 542 | vid: c.vid, |
a9b131f1 | 543 | timeControl: c.timeControl, |
a6bddfc6 | 544 | }; |
8c564f46 BA |
545 | let target = c.from.sid; //may not be defined if corr + offline opp |
546 | if (!target) | |
547 | { | |
548 | const opponent = this.people.find(p => p.id == c.from.id); | |
549 | if (!!opponent) | |
550 | target = opponent.sid | |
551 | } | |
552 | if (!!target) //opponent is online | |
553 | { | |
554 | this.st.conn.send(JSON.stringify({code:"newgame", | |
555 | gameInfo:gameInfo, target:target, cid:c.id})); | |
556 | } | |
485fccd5 BA |
557 | if (c.type == "live") |
558 | this.startNewGame(gameInfo); | |
559 | else //corr: game only on server | |
560 | { | |
561 | ajax( | |
562 | "/games", | |
563 | "POST", | |
2be5d614 BA |
564 | {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge |
565 | response => { this.$router.push("/game/" + response.gameId); } | |
485fccd5 BA |
566 | ); |
567 | } | |
fb54f098 | 568 | }, |
a9b131f1 | 569 | // NOTE: for live games only (corr games start on the server) |
42c15a75 | 570 | startNewGame: function(gameInfo) { |
25996aed BA |
571 | const game = Object.assign({}, gameInfo, { |
572 | // (other) Game infos: constant | |
6d01bb17 | 573 | fenStart: gameInfo.fen, |
c0b27606 | 574 | created: Date.now(), |
25996aed | 575 | // Game state (including FEN): will be updated |
967a2686 | 576 | moves: [], |
a9b131f1 | 577 | clocks: [-1, -1], //-1 = unstarted |
66d03f23 | 578 | initime: [0, 0], //initialized later |
967a2686 | 579 | score: "*", |
a7808884 | 580 | }); |
967a2686 | 581 | GameStorage.add(game); |
7b626bdd BA |
582 | if (this.st.settings.sound >= 1) |
583 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
66d03f23 | 584 | this.$router.push("/game/" + gameInfo.gameId); |
1efe1d79 | 585 | }, |
fb54f098 | 586 | }, |
85e5b5c1 | 587 | }; |
ccd4a2b7 | 588 | </script> |
85e5b5c1 BA |
589 | |
590 | <style lang="sass"> | |
591 | // TODO | |
592 | </style> |