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