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 | }, | |
0ee35787 BA |
125 | watch: { |
126 | // Watch event "user infos retrieved" (through /whoami) | |
127 | "st.user.id": function(newId) { | |
128 | if (newId > 0) //should always be the case | |
129 | { | |
130 | // Ask server for current corr games (all but mines) | |
131 | // ajax( | |
132 | // "/games", | |
133 | // "GET", | |
134 | // {excluded: this.st.user.id}, | |
135 | // response => { | |
136 | // this.games = this.games.concat(response.games); | |
137 | // } | |
138 | // ); | |
139 | // Also ask for corr challenges (open + sent to me) | |
140 | ajax( | |
141 | "/challenges", | |
142 | "GET", | |
143 | {uid: this.st.user.id}, | |
144 | response => { | |
145 | console.log(response.challenges); | |
146 | // TODO: post-treatment on challenges ? | |
147 | this.challenges = this.challenges.concat(response.challenges); | |
148 | } | |
149 | ); | |
150 | } | |
151 | }, | |
152 | }, | |
9d58ef95 | 153 | created: function() { |
4d64881e BA |
154 | // Always add myself to players' list |
155 | this.players.push(this.st.user); | |
2ada153c | 156 | // 0.1] Ask server for room composition: |
4d64881e | 157 | const socketOpenListener = () => { |
81d9ce72 | 158 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
4d64881e BA |
159 | }; |
160 | this.st.conn.onopen = socketOpenListener; | |
81d9ce72 BA |
161 | // TODO: is this required here? |
162 | this.oldOnmessage = this.st.conn.onmessage || Function.prototype; | |
4d64881e | 163 | this.st.conn.onmessage = this.socketMessageListener; |
052d17ea | 164 | const oldOnclose = this.st.conn.onclose; |
4d64881e | 165 | const socketCloseListener = () => { |
052d17ea | 166 | oldOnclose(); //reinitialize connexion (in store.js) |
4d64881e BA |
167 | this.st.conn.addEventListener('message', this.socketMessageListener); |
168 | this.st.conn.addEventListener('close', socketCloseListener); | |
169 | }; | |
170 | this.st.conn.onclose = socketCloseListener; | |
9d58ef95 | 171 | }, |
fb54f098 | 172 | methods: { |
a6bddfc6 | 173 | // Helpers: |
6855163c BA |
174 | filterChallenges: function(type) { |
175 | return this.challenges.filter(c => c.type == type); | |
176 | }, | |
177 | filterGames: function(type) { | |
178 | return this.games.filter(c => c.type == type); | |
179 | }, | |
2ada153c | 180 | classifyObject: function(o) { //challenge or game |
6855163c | 181 | // Heuristic: should work for most cases... (TODO) |
2ada153c | 182 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
6855163c | 183 | }, |
a6bddfc6 BA |
184 | possibleNbplayers: function(nbp) { |
185 | if (this.newchallenge.vid == 0) | |
186 | return false; | |
187 | const idxInVariants = | |
188 | this.st.variants.findIndex(v => v.id == this.newchallenge.vid); | |
189 | return NbPlayers[this.st.variants[idxInVariants].name].includes(nbp); | |
190 | }, | |
2ada153c | 191 | showGame: function(g) { |
5bd05dba | 192 | // NOTE: we are an observer, since only games I don't play are shown here |
2ada153c BA |
193 | // ==> Moves sent by connected remote player(s) if live game |
194 | let url = "/" + g.id; | |
195 | if (g.type == "live") | |
196 | { | |
197 | const sids = g.players.map(p => p.sid).join(","); | |
198 | url += "?sids=" + sids; | |
199 | } | |
200 | this.$router.push(url); | |
a6bddfc6 BA |
201 | }, |
202 | getVname: function(vid) { | |
203 | const vIdx = this.st.variants.findIndex(v => v.id == vid); | |
204 | return this.st.variants[vIdx].name; | |
205 | }, | |
206 | getSid: function(pname) { | |
207 | const pIdx = this.players.findIndex(pl => pl.name == pname); | |
208 | return (pIdx === -1 ? null : this.players[pIdx].sid); | |
209 | }, | |
5bd05dba BA |
210 | getPname: function(sid) { |
211 | const pIdx = this.players.findIndex(pl => pl.sid == sid); | |
212 | return (pIdx === -1 ? null : this.players[pIdx].name); | |
213 | }, | |
a6bddfc6 BA |
214 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
215 | const doSend = (code, obj, sid) => { | |
216 | this.st.conn.send(JSON.stringify(Object.assign( | |
217 | {}, | |
218 | {code: code}, | |
219 | obj, | |
220 | {target: sid} | |
221 | ))); | |
222 | }; | |
36093eba | 223 | if (!!to[0]) |
a6bddfc6 BA |
224 | { |
225 | to.forEach(pname => { | |
226 | // Challenge with targeted players | |
227 | const targetSid = this.getSid(pname); | |
228 | if (!targetSid) | |
229 | { | |
230 | if (!!warnDisconnected) | |
231 | alert("Warning: " + pname + " is not connected"); | |
232 | } | |
233 | else | |
234 | doSend(code, obj, targetSid); | |
235 | }); | |
236 | } | |
237 | else | |
238 | { | |
239 | // Open challenge: send to all connected players (except us) | |
240 | this.players.forEach(p => { | |
241 | if (p.sid != this.st.user.sid) //only sid is always set | |
242 | doSend(code, obj, p.sid); | |
243 | }); | |
244 | } | |
245 | }, | |
246 | // Messaging center: | |
9d58ef95 | 247 | socketMessageListener: function(msg) { |
052d17ea BA |
248 | // Save and call current st.conn.onmessage if one was already defined |
249 | // --> also needed in future Game.vue (also in Chat.vue component) | |
052d17ea | 250 | this.oldOnmessage(msg); |
9d58ef95 BA |
251 | const data = JSON.parse(msg.data); |
252 | switch (data.code) | |
253 | { | |
f4f4c03c | 254 | // 0.2] Receive clients list (just socket IDs) |
81d9ce72 | 255 | case "pollclients": |
1efe1d79 | 256 | { |
5a3da968 BA |
257 | data.sockIds.forEach(sid => { |
258 | this.players.push({sid:sid, id:0, name:""}); | |
81d9ce72 | 259 | // Ask identity, challenges and game(s) |
5a3da968 | 260 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
2ada153c | 261 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
81d9ce72 | 262 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
5a3da968 BA |
263 | }); |
264 | break; | |
1efe1d79 | 265 | } |
81d9ce72 | 266 | case "askidentity": |
1efe1d79 | 267 | { |
6855163c BA |
268 | // Request for identification: reply if I'm not anonymous |
269 | if (this.st.user.id > 0) | |
270 | { | |
271 | this.st.conn.send(JSON.stringify( | |
272 | {code:"identity", user:this.st.user, target:data.from})); | |
273 | } | |
5a3da968 | 274 | break; |
1efe1d79 | 275 | } |
dd75774d | 276 | case "askchallenge": |
1efe1d79 | 277 | { |
6855163c | 278 | // Send my current live challenge (if any) |
dd75774d | 279 | const cIdx = this.challenges |
6855163c | 280 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
dd75774d BA |
281 | if (cIdx >= 0) |
282 | { | |
283 | const c = this.challenges[cIdx]; | |
284 | const myChallenge = | |
285 | { | |
81d9ce72 | 286 | // Minimal challenge informations: (from not required) |
2ada153c | 287 | id: c.id, |
81d9ce72 BA |
288 | to: c.to, |
289 | fen: c.fen, | |
290 | vid: c.vid, | |
291 | timeControl: c.timeControl | |
dd75774d BA |
292 | }; |
293 | this.st.conn.send(JSON.stringify({code:"challenge", | |
1efe1d79 | 294 | challenge:myChallenge, target:data.from})); |
81d9ce72 BA |
295 | } |
296 | break; | |
1efe1d79 | 297 | } |
81d9ce72 | 298 | case "askgame": |
1efe1d79 | 299 | { |
2ada153c BA |
300 | // Send my current live game (if any) |
301 | if (!!localStorage["gid"]) | |
302 | { | |
303 | const myGame = | |
304 | { | |
305 | // Minimal game informations: (fen+clock not required) | |
306 | id: localStorage["gid"], | |
7b626bdd BA |
307 | players: JSON.parse(localStorage["players"]), //array sid+id+name |
308 | vname: localStorage["vname"], | |
2ada153c BA |
309 | timeControl: localStorage["timeControl"], |
310 | }; | |
311 | this.st.conn.send(JSON.stringify({code:"game", | |
312 | game:myGame, target:data.from})); | |
313 | } | |
81d9ce72 | 314 | break; |
1efe1d79 | 315 | } |
5a3da968 | 316 | case "identity": |
1efe1d79 | 317 | { |
6855163c BA |
318 | const pIdx = this.players.findIndex(p => p.sid == data.user.sid); |
319 | this.players[pIdx].id = data.user.id; | |
320 | this.players[pIdx].name = data.user.name; | |
5a3da968 | 321 | break; |
1efe1d79 | 322 | } |
dd75774d | 323 | case "challenge": |
1efe1d79 | 324 | { |
dd75774d | 325 | // Receive challenge from some player (+sid) |
6855163c | 326 | let newChall = data.chall; |
2ada153c | 327 | newChall.type = this.classifyObject(data.chall); |
bb7dd7db | 328 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
6855163c | 329 | newChall.from = this.players[pIdx]; //may be anonymous |
1efe1d79 | 330 | newChall.added = Date.now(); |
bb7dd7db | 331 | newChall.vname = this.getVname(newChall.vid); |
6855163c | 332 | this.challenges.push(newChall); |
81d9ce72 | 333 | break; |
1efe1d79 | 334 | } |
dd75774d | 335 | case "game": |
1efe1d79 | 336 | { |
6855163c | 337 | // Receive game from some player (+sid) |
6855163c | 338 | // NOTE: it may be correspondance (if newgame while we are connected) |
2ada153c BA |
339 | let newGame = data.game; |
340 | newGame.type = this.classifyObject(data.game); | |
7b626bdd | 341 | newGame.vname = newGame.vname; |
2ada153c | 342 | this.games.push(newGame); |
81d9ce72 | 343 | break; |
1efe1d79 | 344 | } |
b4d619d1 BA |
345 | // * - receive "new game": if live, store locally + redirect to game |
346 | // * If corr: notify "new game has started", give link, but do not redirect | |
9d58ef95 | 347 | case "newgame": |
1efe1d79 | 348 | { |
5bd05dba BA |
349 | // Delete corresponding challenge: |
350 | ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
351 | // New game just started: data contain all informations | |
352 | this.newGame(data.gameInfo); | |
b4d619d1 | 353 | break; |
1efe1d79 | 354 | } |
b4d619d1 | 355 | // * - receive "accept/withdraw/cancel challenge": apply action to challenges list |
a6bddfc6 | 356 | // NOTE: challenge "socket" actions accept+withdraw only for live challenges |
9d58ef95 | 357 | case "acceptchallenge": |
1efe1d79 | 358 | { |
bb7dd7db | 359 | // Someone accept an open (or targeted) challenge |
1efe1d79 | 360 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
a6bddfc6 BA |
361 | let c = this.challenges[cIdx]; |
362 | if (!c.seats) | |
363 | c.seats = [...Array(c.to.length)]; | |
bb7dd7db | 364 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
a6bddfc6 BA |
365 | // Put this player in the first empty seat we find: |
366 | let sIdx = 0; | |
367 | for (; sIdx<c.seats.length; sIdx++) | |
1efe1d79 | 368 | { |
a6bddfc6 | 369 | if (!c.seats[sIdx]) |
1efe1d79 | 370 | { |
a6bddfc6 | 371 | c.seats[sIdx] = this.players[pIdx]; |
1efe1d79 BA |
372 | break; |
373 | } | |
374 | } | |
a6bddfc6 BA |
375 | if (sIdx == c.seats.length - 1) |
376 | { | |
377 | // All seats are taken: game can start | |
378 | this.launchGame(c); | |
379 | } | |
9d58ef95 | 380 | break; |
1efe1d79 | 381 | } |
9d58ef95 | 382 | case "withdrawchallenge": |
1efe1d79 | 383 | { |
9d58ef95 | 384 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
a6bddfc6 BA |
385 | let seats = this.challenges[cIdx].seats; |
386 | const sIdx = seats.findIndex(s => s.sid == data.sid); | |
387 | seats[sIdx] = undefined; | |
9d58ef95 | 388 | break; |
1efe1d79 | 389 | } |
bb7dd7db BA |
390 | case "refusechallenge": |
391 | { | |
5bd05dba BA |
392 | alert(this.getPname(data.from) + " refused your challenge"); |
393 | ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
bb7dd7db BA |
394 | break; |
395 | } | |
1efe1d79 BA |
396 | case "deletechallenge": |
397 | { | |
d7371164 | 398 | console.log("receive delete"); |
9d58ef95 BA |
399 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
400 | break; | |
1efe1d79 | 401 | } |
b4d619d1 | 402 | case "connect": |
1efe1d79 | 403 | { |
5a3da968 BA |
404 | this.players.push({name:"", id:0, sid:data.sid}); |
405 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); | |
f05815d7 BA |
406 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid})); |
407 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid})); | |
9d58ef95 | 408 | break; |
1efe1d79 | 409 | } |
b4d619d1 | 410 | case "disconnect": |
1efe1d79 | 411 | { |
5a3da968 | 412 | ArrayFun.remove(this.players, p => p.sid == data.sid); |
a6bddfc6 | 413 | // Also remove all challenges sent by this player: |
2ada153c BA |
414 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
415 | // And all live games where he plays and no other opponent is online | |
416 | ArrayFun.remove(this.games, g => | |
417 | g.type == "live" && (g.players.every(p => p.sid == data.sid | |
418 | || !this.players.some(pl => pl.sid == p.sid))), "all"); | |
9d58ef95 | 419 | break; |
1efe1d79 | 420 | } |
9d58ef95 BA |
421 | } |
422 | }, | |
a6bddfc6 | 423 | // Challenge lifecycle: |
b4d619d1 BA |
424 | tryChallenge: function(player) { |
425 | if (player.id == 0) | |
426 | return; //anonymous players cannot be challenged | |
81d9ce72 | 427 | this.newchallenge.to[0] = player.name; |
b4d619d1 | 428 | doClick("modalNewgame"); |
fb54f098 | 429 | }, |
9d58ef95 | 430 | newChallenge: async function() { |
bb7dd7db | 431 | const vname = this.getVname(this.newchallenge.vid); |
1efe1d79 BA |
432 | const vModule = await import("@/variants/" + vname + ".js"); |
433 | window.V = vModule.VariantRules; | |
9d58ef95 BA |
434 | const error = checkChallenge(this.newchallenge); |
435 | if (!!error) | |
436 | return alert(error); | |
2ada153c | 437 | const ctype = this.classifyObject(this.newchallenge); |
1efe1d79 | 438 | const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers); |
bb7dd7db | 439 | // NOTE: "from" information is not required here |
1efe1d79 | 440 | let chall = |
dd75774d | 441 | { |
a6bddfc6 | 442 | fen: this.newchallenge.fen, |
1efe1d79 | 443 | to: cto, |
5578a7bf | 444 | timeControl: this.newchallenge.timeControl, |
1efe1d79 | 445 | vid: this.newchallenge.vid, |
5578a7bf | 446 | }; |
2ada153c | 447 | const finishAddChallenge = (cid,warnDisconnected) => { |
1efe1d79 | 448 | chall.id = cid || "c" + getRandString(); |
2ada153c BA |
449 | // Send challenge to peers (if connected) |
450 | this.sendSomethingTo(cto, "challenge", {chall:chall}, !!warnDisconnected); | |
1efe1d79 | 451 | chall.added = Date.now(); |
bb7dd7db BA |
452 | chall.type = ctype; |
453 | chall.vname = vname; | |
454 | chall.from = this.st.user; | |
1efe1d79 | 455 | this.challenges.push(chall); |
b4d619d1 BA |
456 | document.getElementById("modalNewgame").checked = false; |
457 | }; | |
1efe1d79 BA |
458 | const cIdx = this.challenges.findIndex( |
459 | c => c.from.sid == this.st.user.sid && c.type == ctype); | |
460 | if (cIdx >= 0) | |
b4d619d1 | 461 | { |
1efe1d79 | 462 | // Delete current challenge (will be replaced now) |
bb7dd7db | 463 | this.sendSomethingTo(this.challenges[cIdx].to, |
1efe1d79 BA |
464 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
465 | if (ctype == "corr") | |
466 | { | |
467 | ajax( | |
468 | "/challenges", | |
469 | "DELETE", | |
470 | {id: this.challenges[cIdx].id} | |
471 | ); | |
472 | } | |
473 | this.challenges.splice(cIdx, 1); | |
474 | } | |
475 | if (ctype == "live") | |
476 | { | |
477 | // Live challenges have a random ID | |
2ada153c | 478 | finishAddChallenge(null, "warnDisconnected"); |
03608482 | 479 | } |
b4d619d1 | 480 | else |
03608482 | 481 | { |
b4d619d1 | 482 | // Correspondance game: send challenge to server |
03608482 | 483 | ajax( |
1efe1d79 | 484 | "/challenges", |
03608482 | 485 | "POST", |
052d17ea | 486 | chall, |
1efe1d79 | 487 | response => { finishAddChallenge(response.cid); } |
03608482 | 488 | ); |
9d58ef95 | 489 | } |
fb54f098 | 490 | }, |
a6bddfc6 BA |
491 | // * - accept challenge (corr or live) --> send info to challenge creator |
492 | // * - cancel challenge (click on sent challenge) --> send info to all concerned players | |
493 | // * - withdraw from challenge (if >= 3 players and previously accepted) | |
494 | // * --> send info to challenge creator | |
495 | // * - refuse challenge: send "refuse" to challenge sender, and "delete" to others | |
496 | // * - prepare and start new game (if challenge is full after acceptation) | |
497 | // * --> include challenge ID (so that opponents can delete the challenge too) | |
498 | clickChallenge: function(c) { | |
d7371164 BA |
499 | |
500 | console.log(c); | |
501 | ||
36093eba | 502 | if (!!c.accepted) |
2ada153c | 503 | { |
36093eba BA |
504 | this.st.conn.send(JSON.stringify({code: "withdrawchallenge", |
505 | cid: c.id, target: c.from.sid})); | |
506 | if (c.type == "corr") | |
507 | { | |
2ada153c BA |
508 | ajax( |
509 | "/challenges", | |
510 | "PUT", | |
511 | {action:"withdraw", id: this.challenges[cIdx].id} | |
512 | ); | |
36093eba BA |
513 | } |
514 | c.accepted = false; | |
a6bddfc6 | 515 | } |
36093eba BA |
516 | else if (c.from.sid == this.st.user.sid |
517 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)) | |
a6bddfc6 | 518 | { |
d7371164 | 519 | console.log("send delete"); |
36093eba | 520 | // It's my challenge: cancel it |
a6bddfc6 BA |
521 | this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); |
522 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
36093eba BA |
523 | if (c.type == "corr") |
524 | { | |
525 | ajax( | |
526 | "/challenges", | |
527 | "DELETE", | |
528 | {id: this.challenges[cIdx].id} | |
529 | ); | |
530 | } | |
a6bddfc6 BA |
531 | } |
532 | else //accept (or refuse) a challenge | |
533 | { | |
534 | c.accepted = true; | |
535 | if (!!c.to[0]) | |
536 | { | |
537 | // TODO: if special FEN, show diagram after loading variant | |
538 | c.accepted = confirm("Accept challenge?"); | |
539 | } | |
540 | this.st.conn.send(JSON.stringify({ | |
4edfed6c | 541 | code: (c.accepted ? "accept" : "refuse") + "challenge", |
a6bddfc6 | 542 | cid: c.id, target: c.from.sid})); |
4edfed6c | 543 | if (c.type == "corr" && c.accepted) |
36093eba BA |
544 | { |
545 | ajax( | |
546 | "/challenges", | |
547 | "PUT", | |
4edfed6c | 548 | {action: "accept", id: this.challenges[cIdx].id} |
36093eba BA |
549 | ); |
550 | } | |
a6bddfc6 | 551 | if (!c.accepted) |
36093eba | 552 | { |
a6bddfc6 | 553 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
36093eba BA |
554 | if (c.type == "corr") |
555 | { | |
556 | ajax( | |
557 | "/challenges", | |
558 | "DELETE", | |
559 | {id: this.challenges[cIdx].id} | |
560 | ); | |
561 | } | |
562 | } | |
a6bddfc6 BA |
563 | } |
564 | }, | |
f05815d7 | 565 | // NOTE: for live games only (corr games are launched on server) |
36093eba | 566 | launchGame: async function(c) { |
a6bddfc6 BA |
567 | // Just assign colors and pass the message |
568 | const vname = this.getVname(c.vid); | |
569 | const vModule = await import("@/variants/" + vname + ".js"); | |
570 | window.V = vModule.VariantRules; | |
571 | let players = [c.from]; | |
572 | Array.prototype.push.apply(players, c.seats); | |
a6bddfc6 BA |
573 | let gameInfo = |
574 | { | |
a6bddfc6 BA |
575 | fen: c.fen || V.GenRandInitFen(), |
576 | // Shuffle players order (white then black then other colors). | |
5bd05dba | 577 | // Players' names may be required if game start when a player is offline |
36093eba | 578 | players: shuffle(players).map(p => { return {name:p.name, sid:p.sid} }), |
a6bddfc6 BA |
579 | vid: c.vid, |
580 | timeControl: c.timeControl, | |
581 | }; | |
582 | c.seats.forEach(s => { | |
5bd05dba | 583 | // NOTE: cid required to remove challenge |
a6bddfc6 | 584 | this.st.conn.send(JSON.stringify({code:"newgame", |
5bd05dba | 585 | gameInfo:gameInfo, cid:c.id, target:s.sid})); |
a6bddfc6 | 586 | }); |
5bd05dba BA |
587 | // Delete corresponding challenge: |
588 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
a6bddfc6 | 589 | this.newGame(gameInfo); //also! |
fb54f098 | 590 | }, |
7b626bdd | 591 | // NOTE: for live games only (corr games are launched on server) |
a6bddfc6 | 592 | newGame: function(gameInfo) { |
7b626bdd | 593 | localStorage["gid"] = getRandString(); |
5bd05dba BA |
594 | // Extract times (in [milli]seconds), set clocks, store in localStorage |
595 | const tc = extractTime(gameInfo.timeControl); | |
7b626bdd BA |
596 | localStorage["timeControl"] = gameInfo.timeControl; |
597 | localStorage["clocks"] = JSON.stringify( | |
598 | [...Array(gameInfo.players.length)].fill(tc.mainTime)); | |
599 | localStorage["increment"] = tc.increment; | |
600 | localStorage["started"] = JSON.stringify( | |
601 | [...Array(gameInfo.players.length)].fill(false)); | |
602 | localStorage["mysid"] = this.st.user.sid; | |
603 | localStorage["vname"] = this.getVname(gameInfo.vid); | |
604 | localStorage["fenInit"] = gameInfo.fen; | |
605 | localStorage["players"] = JSON.stringify(gameInfo.players); | |
606 | if (this.st.settings.sound >= 1) | |
607 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
1efe1d79 | 608 | }, |
fb54f098 | 609 | }, |
85e5b5c1 | 610 | }; |
ccd4a2b7 | 611 | </script> |
85e5b5c1 BA |
612 | |
613 | <style lang="sass"> | |
614 | // TODO | |
615 | </style> |