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