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