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 BA |
11 | fieldset |
12 | label(for="selectNbPlayers") {{ st.tr["Number of players"] }} | |
9d58ef95 | 13 | select#selectNbPlayers(v-model="newchallenge.nbPlayers") |
81d9ce72 | 14 | option(v-show="possibleNbplayers(2)" value="2" selected) 2 |
5b020e73 BA |
15 | option(v-show="possibleNbplayers(3)" value="3") 3 |
16 | option(v-show="possibleNbplayers(4)" value="4") 4 | |
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)"] }} |
5b020e73 | 23 | #selectPlayers |
81d9ce72 | 24 | input(type="text" v-model="newchallenge.to[0]") |
9d58ef95 | 25 | input(v-show="newchallenge.nbPlayers>=3" type="text" |
81d9ce72 | 26 | v-model="newchallenge.to[1]") |
9d58ef95 | 27 | input(v-show="newchallenge.nbPlayers==4" type="text" |
81d9ce72 | 28 | v-model="newchallenge.to[2]") |
b4d619d1 | 29 | fieldset(v-if="st.user.id > 0") |
9d58ef95 BA |
30 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
31 | input#inputFen(type="text" v-model="newchallenge.fen") | |
b4d619d1 | 32 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
9d58ef95 BA |
33 | .row |
34 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
35 | button(onClick="doClick('modalNewgame')") New game | |
36 | .row | |
1efe1d79 | 37 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
6855163c BA |
38 | .collapse |
39 | input#challengeSection(type="radio" checked aria-hidden="true" name="accordion") | |
40 | label(for="challengeSection" aria-hidden="true") Challenges | |
41 | div | |
42 | .button-group | |
43 | button(@click="cdisplay='live'") Live Challenges | |
44 | button(@click="cdisplay='corr'") Correspondance challenges | |
45 | ChallengeList(v-show="cdisplay=='live'" | |
46 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") | |
47 | ChallengeList(v-show="cdisplay=='corr'" | |
48 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") | |
1efe1d79 | 49 | input#peopleSection(type="radio" aria-hidden="true" name="accordion") |
6855163c BA |
50 | label(for="peopleSection" aria-hidden="true") People |
51 | div | |
1efe1d79 BA |
52 | .button-group |
53 | button(@click="pdisplay='players'") Players | |
54 | button(@click="pdisplay='chat'") Chat | |
6855163c BA |
55 | #players(v-show="pdisplay=='players'") |
56 | h3 Online players | |
57 | .player(v-for="p in uniquePlayers" @click="tryChallenge(p)" | |
58 | :class="{anonymous: !!p.count}" | |
59 | ) | |
60 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} | |
61 | #chat(v-show="pdisplay=='chat'") | |
62 | h3 Chat (TODO) | |
1efe1d79 | 63 | input#gameSection(type="radio" aria-hidden="true" name="accordion") |
6855163c BA |
64 | label(for="gameSection" aria-hidden="true") Games |
65 | div | |
66 | .button-group | |
67 | button(@click="gdisplay='live'") Live games | |
68 | button(@click="gdisplay='corr'") Correspondance games | |
69 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" | |
70 | @show-game="showGame") | |
71 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" | |
72 | @show-game="showGame") | |
625022fd BA |
73 | </template> |
74 | ||
75 | <script> | |
5b020e73 | 76 | import { store } from "@/store"; |
85e5b5c1 | 77 | import { NbPlayers } from "@/data/nbPlayers"; |
9d58ef95 BA |
78 | import { checkChallenge } from "@/data/challengeCheck"; |
79 | import { ArrayFun } from "@/utils/array"; | |
03608482 | 80 | import { ajax } from "@/utils/ajax"; |
a6bddfc6 | 81 | import { getRandString, shuffle } from "@/utils/alea"; |
5bd05dba | 82 | import { extractTime } from "@/utils/timeControl"; |
5b020e73 BA |
83 | import GameList from "@/components/GameList.vue"; |
84 | import ChallengeList from "@/components/ChallengeList.vue"; | |
625022fd | 85 | export default { |
cf2343ce | 86 | name: "my-hall", |
5b020e73 BA |
87 | components: { |
88 | GameList, | |
89 | ChallengeList, | |
90 | }, | |
fb54f098 BA |
91 | data: function () { |
92 | return { | |
5b020e73 | 93 | st: store.state, |
6855163c BA |
94 | cdisplay: "live", //or corr |
95 | pdisplay: "players", //or chat | |
fb54f098 | 96 | gdisplay: "live", |
6855163c | 97 | games: [], |
b4d619d1 | 98 | challenges: [], |
1efe1d79 | 99 | players: [], //online players (rename into "people" ?) |
9d58ef95 | 100 | newchallenge: { |
fb54f098 BA |
101 | fen: "", |
102 | vid: 0, | |
103 | nbPlayers: 0, | |
6faa92f2 BA |
104 | to: ["", "", ""], //name of challenged players |
105 | timeControl: "", //"2m+2s" ...etc | |
fb54f098 BA |
106 | }, |
107 | }; | |
108 | }, | |
b4d619d1 BA |
109 | computed: { |
110 | uniquePlayers: function() { | |
6855163c | 111 | // Show e.g. "@nonymous (5)", and do nothing on click on anonymous |
4d64881e BA |
112 | let anonymous = {id:0, name:"@nonymous", count:0}; |
113 | let playerList = []; | |
b4d619d1 BA |
114 | this.players.forEach(p => { |
115 | if (p.id > 0) | |
116 | playerList.push(p); | |
117 | else | |
4d64881e | 118 | anonymous.count++; |
b4d619d1 | 119 | }); |
4d64881e BA |
120 | if (anonymous.count > 0) |
121 | playerList.push(anonymous); | |
b4d619d1 BA |
122 | return playerList; |
123 | }, | |
124 | }, | |
9d58ef95 | 125 | created: function() { |
4d64881e BA |
126 | // Always add myself to players' list |
127 | this.players.push(this.st.user); | |
f4f4c03c | 128 | // Ask server for current corr games (all but mines) |
052d17ea BA |
129 | // ajax( |
130 | // "", | |
131 | // "GET", | |
132 | // response => { | |
133 | // | |
134 | // } | |
135 | // ); | |
2ada153c | 136 | // // Also ask for corr challenges (all) --> + accepted status if I play |
052d17ea BA |
137 | // ajax( |
138 | // "", | |
139 | // "GET", | |
140 | // response => { | |
141 | // | |
142 | // } | |
143 | // ); | |
2ada153c | 144 | // 0.1] Ask server for room composition: |
4d64881e | 145 | const socketOpenListener = () => { |
81d9ce72 | 146 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
4d64881e BA |
147 | }; |
148 | this.st.conn.onopen = socketOpenListener; | |
81d9ce72 BA |
149 | // TODO: is this required here? |
150 | this.oldOnmessage = this.st.conn.onmessage || Function.prototype; | |
4d64881e | 151 | this.st.conn.onmessage = this.socketMessageListener; |
052d17ea | 152 | const oldOnclose = this.st.conn.onclose; |
4d64881e | 153 | const socketCloseListener = () => { |
052d17ea | 154 | oldOnclose(); //reinitialize connexion (in store.js) |
4d64881e BA |
155 | this.st.conn.addEventListener('message', this.socketMessageListener); |
156 | this.st.conn.addEventListener('close', socketCloseListener); | |
157 | }; | |
158 | this.st.conn.onclose = socketCloseListener; | |
9d58ef95 | 159 | }, |
fb54f098 | 160 | methods: { |
a6bddfc6 | 161 | // Helpers: |
6855163c BA |
162 | filterChallenges: function(type) { |
163 | return this.challenges.filter(c => c.type == type); | |
164 | }, | |
165 | filterGames: function(type) { | |
166 | return this.games.filter(c => c.type == type); | |
167 | }, | |
2ada153c | 168 | classifyObject: function(o) { //challenge or game |
6855163c | 169 | // Heuristic: should work for most cases... (TODO) |
2ada153c | 170 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
6855163c | 171 | }, |
a6bddfc6 BA |
172 | possibleNbplayers: function(nbp) { |
173 | if (this.newchallenge.vid == 0) | |
174 | return false; | |
175 | const idxInVariants = | |
176 | this.st.variants.findIndex(v => v.id == this.newchallenge.vid); | |
177 | return NbPlayers[this.st.variants[idxInVariants].name].includes(nbp); | |
178 | }, | |
2ada153c | 179 | showGame: function(g) { |
5bd05dba | 180 | // NOTE: we are an observer, since only games I don't play are shown here |
2ada153c BA |
181 | // ==> Moves sent by connected remote player(s) if live game |
182 | let url = "/" + g.id; | |
183 | if (g.type == "live") | |
184 | { | |
185 | const sids = g.players.map(p => p.sid).join(","); | |
186 | url += "?sids=" + sids; | |
187 | } | |
188 | this.$router.push(url); | |
a6bddfc6 BA |
189 | }, |
190 | getVname: function(vid) { | |
191 | const vIdx = this.st.variants.findIndex(v => v.id == vid); | |
192 | return this.st.variants[vIdx].name; | |
193 | }, | |
194 | getSid: function(pname) { | |
195 | const pIdx = this.players.findIndex(pl => pl.name == pname); | |
196 | return (pIdx === -1 ? null : this.players[pIdx].sid); | |
197 | }, | |
5bd05dba BA |
198 | getPname: function(sid) { |
199 | const pIdx = this.players.findIndex(pl => pl.sid == sid); | |
200 | return (pIdx === -1 ? null : this.players[pIdx].name); | |
201 | }, | |
a6bddfc6 BA |
202 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
203 | const doSend = (code, obj, sid) => { | |
204 | this.st.conn.send(JSON.stringify(Object.assign( | |
205 | {}, | |
206 | {code: code}, | |
207 | obj, | |
208 | {target: sid} | |
209 | ))); | |
210 | }; | |
211 | else if (!!to[0]) | |
212 | { | |
213 | to.forEach(pname => { | |
214 | // Challenge with targeted players | |
215 | const targetSid = this.getSid(pname); | |
216 | if (!targetSid) | |
217 | { | |
218 | if (!!warnDisconnected) | |
219 | alert("Warning: " + pname + " is not connected"); | |
220 | } | |
221 | else | |
222 | doSend(code, obj, targetSid); | |
223 | }); | |
224 | } | |
225 | else | |
226 | { | |
227 | // Open challenge: send to all connected players (except us) | |
228 | this.players.forEach(p => { | |
229 | if (p.sid != this.st.user.sid) //only sid is always set | |
230 | doSend(code, obj, p.sid); | |
231 | }); | |
232 | } | |
233 | }, | |
234 | // Messaging center: | |
9d58ef95 | 235 | socketMessageListener: function(msg) { |
052d17ea BA |
236 | // Save and call current st.conn.onmessage if one was already defined |
237 | // --> also needed in future Game.vue (also in Chat.vue component) | |
052d17ea | 238 | this.oldOnmessage(msg); |
9d58ef95 BA |
239 | const data = JSON.parse(msg.data); |
240 | switch (data.code) | |
241 | { | |
f4f4c03c | 242 | // 0.2] Receive clients list (just socket IDs) |
81d9ce72 | 243 | case "pollclients": |
1efe1d79 | 244 | { |
5a3da968 BA |
245 | data.sockIds.forEach(sid => { |
246 | this.players.push({sid:sid, id:0, name:""}); | |
81d9ce72 | 247 | // Ask identity, challenges and game(s) |
5a3da968 | 248 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
2ada153c | 249 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
81d9ce72 | 250 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
5a3da968 BA |
251 | }); |
252 | break; | |
1efe1d79 | 253 | } |
81d9ce72 | 254 | case "askidentity": |
1efe1d79 | 255 | { |
6855163c BA |
256 | // Request for identification: reply if I'm not anonymous |
257 | if (this.st.user.id > 0) | |
258 | { | |
259 | this.st.conn.send(JSON.stringify( | |
260 | {code:"identity", user:this.st.user, target:data.from})); | |
261 | } | |
5a3da968 | 262 | break; |
1efe1d79 | 263 | } |
dd75774d | 264 | case "askchallenge": |
1efe1d79 | 265 | { |
6855163c | 266 | // Send my current live challenge (if any) |
dd75774d | 267 | const cIdx = this.challenges |
6855163c | 268 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
dd75774d BA |
269 | if (cIdx >= 0) |
270 | { | |
271 | const c = this.challenges[cIdx]; | |
272 | const myChallenge = | |
273 | { | |
81d9ce72 | 274 | // Minimal challenge informations: (from not required) |
2ada153c | 275 | id: c.id, |
81d9ce72 BA |
276 | to: c.to, |
277 | fen: c.fen, | |
278 | vid: c.vid, | |
279 | timeControl: c.timeControl | |
dd75774d BA |
280 | }; |
281 | this.st.conn.send(JSON.stringify({code:"challenge", | |
1efe1d79 | 282 | challenge:myChallenge, target:data.from})); |
81d9ce72 BA |
283 | } |
284 | break; | |
1efe1d79 | 285 | } |
81d9ce72 | 286 | case "askgame": |
1efe1d79 | 287 | { |
2ada153c BA |
288 | // Send my current live game (if any) |
289 | if (!!localStorage["gid"]) | |
290 | { | |
291 | const myGame = | |
292 | { | |
293 | // Minimal game informations: (fen+clock not required) | |
294 | id: localStorage["gid"], | |
295 | players: JSON.parse(localStorage["players"]), //array sid+name | |
296 | vid: localStorage["vid"], | |
297 | timeControl: localStorage["timeControl"], | |
298 | }; | |
299 | this.st.conn.send(JSON.stringify({code:"game", | |
300 | game:myGame, target:data.from})); | |
301 | } | |
81d9ce72 | 302 | break; |
1efe1d79 | 303 | } |
5a3da968 | 304 | case "identity": |
1efe1d79 | 305 | { |
6855163c BA |
306 | const pIdx = this.players.findIndex(p => p.sid == data.user.sid); |
307 | this.players[pIdx].id = data.user.id; | |
308 | this.players[pIdx].name = data.user.name; | |
5a3da968 | 309 | break; |
1efe1d79 | 310 | } |
dd75774d | 311 | case "challenge": |
1efe1d79 | 312 | { |
dd75774d | 313 | // Receive challenge from some player (+sid) |
6855163c | 314 | let newChall = data.chall; |
2ada153c | 315 | newChall.type = this.classifyObject(data.chall); |
bb7dd7db | 316 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
6855163c | 317 | newChall.from = this.players[pIdx]; //may be anonymous |
1efe1d79 | 318 | newChall.added = Date.now(); |
bb7dd7db | 319 | newChall.vname = this.getVname(newChall.vid); |
6855163c | 320 | this.challenges.push(newChall); |
81d9ce72 | 321 | break; |
1efe1d79 | 322 | } |
dd75774d | 323 | case "game": |
1efe1d79 | 324 | { |
6855163c | 325 | // Receive game from some player (+sid) |
6855163c | 326 | // NOTE: it may be correspondance (if newgame while we are connected) |
2ada153c BA |
327 | let newGame = data.game; |
328 | newGame.type = this.classifyObject(data.game); | |
329 | newGame.vname = this.getVname(newGame.vid); | |
330 | this.games.push(newGame); | |
81d9ce72 | 331 | break; |
1efe1d79 | 332 | } |
b4d619d1 BA |
333 | // * - receive "new game": if live, store locally + redirect to game |
334 | // * If corr: notify "new game has started", give link, but do not redirect | |
9d58ef95 | 335 | case "newgame": |
1efe1d79 | 336 | { |
5bd05dba BA |
337 | // Delete corresponding challenge: |
338 | ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
339 | // New game just started: data contain all informations | |
340 | this.newGame(data.gameInfo); | |
b4d619d1 | 341 | break; |
1efe1d79 | 342 | } |
b4d619d1 | 343 | // * - receive "accept/withdraw/cancel challenge": apply action to challenges list |
a6bddfc6 | 344 | // NOTE: challenge "socket" actions accept+withdraw only for live challenges |
9d58ef95 | 345 | case "acceptchallenge": |
1efe1d79 | 346 | { |
bb7dd7db | 347 | // Someone accept an open (or targeted) challenge |
1efe1d79 | 348 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
a6bddfc6 BA |
349 | let c = this.challenges[cIdx]; |
350 | if (!c.seats) | |
351 | c.seats = [...Array(c.to.length)]; | |
bb7dd7db | 352 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
a6bddfc6 BA |
353 | // Put this player in the first empty seat we find: |
354 | let sIdx = 0; | |
355 | for (; sIdx<c.seats.length; sIdx++) | |
1efe1d79 | 356 | { |
a6bddfc6 | 357 | if (!c.seats[sIdx]) |
1efe1d79 | 358 | { |
a6bddfc6 | 359 | c.seats[sIdx] = this.players[pIdx]; |
1efe1d79 BA |
360 | break; |
361 | } | |
362 | } | |
a6bddfc6 BA |
363 | if (sIdx == c.seats.length - 1) |
364 | { | |
365 | // All seats are taken: game can start | |
366 | this.launchGame(c); | |
367 | } | |
9d58ef95 | 368 | break; |
1efe1d79 | 369 | } |
9d58ef95 | 370 | case "withdrawchallenge": |
1efe1d79 | 371 | { |
9d58ef95 | 372 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
a6bddfc6 BA |
373 | let seats = this.challenges[cIdx].seats; |
374 | const sIdx = seats.findIndex(s => s.sid == data.sid); | |
375 | seats[sIdx] = undefined; | |
9d58ef95 | 376 | break; |
1efe1d79 | 377 | } |
bb7dd7db BA |
378 | case "refusechallenge": |
379 | { | |
5bd05dba BA |
380 | alert(this.getPname(data.from) + " refused your challenge"); |
381 | ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
bb7dd7db BA |
382 | break; |
383 | } | |
1efe1d79 BA |
384 | case "deletechallenge": |
385 | { | |
9d58ef95 BA |
386 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
387 | break; | |
1efe1d79 | 388 | } |
b4d619d1 | 389 | case "connect": |
1efe1d79 | 390 | { |
5a3da968 BA |
391 | this.players.push({name:"", id:0, sid:data.sid}); |
392 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); | |
2ada153c BA |
393 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
394 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); | |
9d58ef95 | 395 | break; |
1efe1d79 | 396 | } |
b4d619d1 | 397 | case "disconnect": |
1efe1d79 | 398 | { |
5a3da968 | 399 | ArrayFun.remove(this.players, p => p.sid == data.sid); |
a6bddfc6 | 400 | // Also remove all challenges sent by this player: |
2ada153c BA |
401 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
402 | // And all live games where he plays and no other opponent is online | |
403 | ArrayFun.remove(this.games, g => | |
404 | g.type == "live" && (g.players.every(p => p.sid == data.sid | |
405 | || !this.players.some(pl => pl.sid == p.sid))), "all"); | |
9d58ef95 | 406 | break; |
1efe1d79 | 407 | } |
9d58ef95 BA |
408 | } |
409 | }, | |
a6bddfc6 | 410 | // Challenge lifecycle: |
b4d619d1 BA |
411 | tryChallenge: function(player) { |
412 | if (player.id == 0) | |
413 | return; //anonymous players cannot be challenged | |
81d9ce72 | 414 | this.newchallenge.to[0] = player.name; |
b4d619d1 | 415 | doClick("modalNewgame"); |
fb54f098 | 416 | }, |
9d58ef95 | 417 | newChallenge: async function() { |
bb7dd7db | 418 | const vname = this.getVname(this.newchallenge.vid); |
1efe1d79 BA |
419 | const vModule = await import("@/variants/" + vname + ".js"); |
420 | window.V = vModule.VariantRules; | |
9d58ef95 BA |
421 | const error = checkChallenge(this.newchallenge); |
422 | if (!!error) | |
423 | return alert(error); | |
2ada153c | 424 | const ctype = this.classifyObject(this.newchallenge); |
1efe1d79 | 425 | const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers); |
bb7dd7db | 426 | // NOTE: "from" information is not required here |
1efe1d79 | 427 | let chall = |
dd75774d | 428 | { |
a6bddfc6 | 429 | fen: this.newchallenge.fen, |
1efe1d79 | 430 | to: cto, |
5578a7bf | 431 | timeControl: this.newchallenge.timeControl, |
1efe1d79 | 432 | vid: this.newchallenge.vid, |
5578a7bf | 433 | }; |
2ada153c | 434 | const finishAddChallenge = (cid,warnDisconnected) => { |
1efe1d79 | 435 | chall.id = cid || "c" + getRandString(); |
2ada153c BA |
436 | // Send challenge to peers (if connected) |
437 | this.sendSomethingTo(cto, "challenge", {chall:chall}, !!warnDisconnected); | |
1efe1d79 | 438 | chall.added = Date.now(); |
bb7dd7db BA |
439 | chall.type = ctype; |
440 | chall.vname = vname; | |
441 | chall.from = this.st.user; | |
1efe1d79 | 442 | this.challenges.push(chall); |
b4d619d1 BA |
443 | document.getElementById("modalNewgame").checked = false; |
444 | }; | |
1efe1d79 BA |
445 | const cIdx = this.challenges.findIndex( |
446 | c => c.from.sid == this.st.user.sid && c.type == ctype); | |
447 | if (cIdx >= 0) | |
b4d619d1 | 448 | { |
1efe1d79 | 449 | // Delete current challenge (will be replaced now) |
bb7dd7db | 450 | this.sendSomethingTo(this.challenges[cIdx].to, |
1efe1d79 BA |
451 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
452 | if (ctype == "corr") | |
453 | { | |
454 | ajax( | |
455 | "/challenges", | |
456 | "DELETE", | |
457 | {id: this.challenges[cIdx].id} | |
458 | ); | |
459 | } | |
460 | this.challenges.splice(cIdx, 1); | |
461 | } | |
462 | if (ctype == "live") | |
463 | { | |
464 | // Live challenges have a random ID | |
2ada153c | 465 | finishAddChallenge(null, "warnDisconnected"); |
03608482 | 466 | } |
b4d619d1 | 467 | else |
03608482 | 468 | { |
b4d619d1 | 469 | // Correspondance game: send challenge to server |
03608482 | 470 | ajax( |
1efe1d79 | 471 | "/challenges", |
03608482 | 472 | "POST", |
052d17ea | 473 | chall, |
1efe1d79 | 474 | response => { finishAddChallenge(response.cid); } |
03608482 | 475 | ); |
9d58ef95 | 476 | } |
fb54f098 | 477 | }, |
a6bddfc6 BA |
478 | // * - accept challenge (corr or live) --> send info to challenge creator |
479 | // * - cancel challenge (click on sent challenge) --> send info to all concerned players | |
480 | // * - withdraw from challenge (if >= 3 players and previously accepted) | |
481 | // * --> send info to challenge creator | |
482 | // * - refuse challenge: send "refuse" to challenge sender, and "delete" to others | |
483 | // * - prepare and start new game (if challenge is full after acceptation) | |
484 | // * --> include challenge ID (so that opponents can delete the challenge too) | |
485 | clickChallenge: function(c) { | |
2ada153c BA |
486 | switch (c.type) |
487 | { | |
488 | case "live": | |
489 | if (!!c.accepted) | |
490 | { | |
491 | this.st.conn.send(JSON.stringify({code: "withdrawchallenge", | |
492 | cid: c.id, target: c.from.sid})); | |
493 | ||
494 | break; | |
495 | case "corr": | |
496 | ajax( | |
497 | "/challenges", | |
498 | "PUT", | |
499 | {action:"withdraw", id: this.challenges[cIdx].id} | |
500 | ); | |
501 | break; | |
502 | } | |
503 | c.accepted = false; | |
a6bddfc6 | 504 | { |
a6bddfc6 BA |
505 | } |
506 | else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it | |
507 | { | |
508 | this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); | |
509 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
510 | } | |
511 | else //accept (or refuse) a challenge | |
512 | { | |
513 | c.accepted = true; | |
514 | if (!!c.to[0]) | |
515 | { | |
516 | // TODO: if special FEN, show diagram after loading variant | |
517 | c.accepted = confirm("Accept challenge?"); | |
518 | } | |
519 | this.st.conn.send(JSON.stringify({ | |
520 | code: (c.accepted ? "accept" : "refuse") + "challenge", | |
521 | cid: c.id, target: c.from.sid})); | |
522 | if (!c.accepted) | |
523 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
524 | } | |
525 | }, | |
5bd05dba BA |
526 | // c.type == corr alors use id...sinon sid (figés) |
527 | // NOTE: only for live games ? | |
a6bddfc6 BA |
528 | launchGame: function(c) { |
529 | // Just assign colors and pass the message | |
530 | const vname = this.getVname(c.vid); | |
531 | const vModule = await import("@/variants/" + vname + ".js"); | |
532 | window.V = vModule.VariantRules; | |
533 | let players = [c.from]; | |
534 | Array.prototype.push.apply(players, c.seats); | |
a6bddfc6 BA |
535 | let gameInfo = |
536 | { | |
a6bddfc6 BA |
537 | fen: c.fen || V.GenRandInitFen(), |
538 | // Shuffle players order (white then black then other colors). | |
5bd05dba BA |
539 | // Players' names may be required if game start when a player is offline |
540 | players: shuffle(players).map(p => {name:p.name, sid:p.sid}, | |
a6bddfc6 BA |
541 | vid: c.vid, |
542 | timeControl: c.timeControl, | |
543 | }; | |
544 | c.seats.forEach(s => { | |
5bd05dba | 545 | // NOTE: cid required to remove challenge |
a6bddfc6 | 546 | this.st.conn.send(JSON.stringify({code:"newgame", |
5bd05dba | 547 | gameInfo:gameInfo, cid:c.id, target:s.sid})); |
a6bddfc6 | 548 | }); |
5bd05dba BA |
549 | // Delete corresponding challenge: |
550 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
a6bddfc6 | 551 | this.newGame(gameInfo); //also! |
fb54f098 | 552 | }, |
5bd05dba | 553 | // NOTE: for live games only (corr games are laucnhed on server) |
a6bddfc6 | 554 | newGame: function(gameInfo) { |
5bd05dba BA |
555 | // Extract times (in [milli]seconds), set clocks, store in localStorage |
556 | const tc = extractTime(gameInfo.timeControl); | |
557 | dddddddd | |
558 | // TODO: [in game] send move + elapsed time (in milliseconds); in case of "lastate" message too | |
559 | // //setStorage(game); //TODO | |
a6bddfc6 BA |
560 | // if (this.settings.sound >= 1) |
561 | // new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
1efe1d79 | 562 | }, |
fb54f098 | 563 | }, |
85e5b5c1 | 564 | }; |
ccd4a2b7 | 565 | </script> |
85e5b5c1 BA |
566 | |
567 | <style lang="sass"> | |
568 | // TODO | |
569 | </style> |