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