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