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