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); |
f6f2bef1 BA |
116 | // Retrieve live challenge (not older than 30 minute) if any: |
117 | const chall = JSON.parse(localStorage.getItem("challenge") || "false"); | |
118 | if (!!chall) | |
119 | { | |
120 | if ((Date.now() - chall.added)/1000 <= 30*60) | |
121 | this.challenges.push(chall); | |
122 | else | |
123 | localStorage.removeItem("challenge"); | |
124 | } | |
98f48579 BA |
125 | if (this.st.user.id > 0) |
126 | { | |
127 | // Ask server for current corr games (all but mines) | |
128 | // ajax( | |
129 | // "/games", | |
130 | // "GET", | |
131 | // {excluded: this.st.user.id}, | |
132 | // response => { | |
133 | // this.games = this.games.concat(response.games); | |
134 | // } | |
135 | // ); | |
136 | // Also ask for corr challenges (open + sent to me) | |
137 | ajax( | |
138 | "/challenges", | |
139 | "GET", | |
140 | {uid: this.st.user.id}, | |
141 | response => { | |
142 | console.log(response.challenges); | |
143 | // TODO: post-treatment on challenges ? | |
40477190 | 144 | Array.prototype.push.apply(this.challenges, response.challenges); |
98f48579 BA |
145 | } |
146 | ); | |
147 | } | |
2ada153c | 148 | // 0.1] Ask server for room composition: |
7b01e447 | 149 | const funcPollClients = () => { |
81d9ce72 | 150 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
4d64881e | 151 | }; |
7b01e447 BA |
152 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
153 | funcPollClients(); | |
154 | else //socket not ready yet (initial loading) | |
3cb412e9 | 155 | this.st.conn.onopen = funcPollClients; |
4d64881e BA |
156 | this.st.conn.onmessage = this.socketMessageListener; |
157 | const socketCloseListener = () => { | |
cdb34c93 | 158 | store.socketCloseListener(); //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 | }, |
2ada153c | 176 | showGame: function(g) { |
5bd05dba | 177 | // NOTE: we are an observer, since only games I don't play are shown here |
2ada153c | 178 | // ==> Moves sent by connected remote player(s) if live game |
d9b86b16 | 179 | let url = "/game/" + g.id; |
2ada153c BA |
180 | if (g.type == "live") |
181 | { | |
d9b86b16 BA |
182 | const remotes = g.players.filter(p => this.people.some(pl => pl.sid == p.sid)); |
183 | const rIdx = (remotes.length == 1 ? 0 : Math.floor(Math.random()*2)); | |
184 | url += "?rid=" + remotes[rIdx].sid; | |
2ada153c BA |
185 | } |
186 | this.$router.push(url); | |
a6bddfc6 BA |
187 | }, |
188 | getVname: function(vid) { | |
189 | const vIdx = this.st.variants.findIndex(v => v.id == vid); | |
190 | return this.st.variants[vIdx].name; | |
191 | }, | |
192 | getSid: function(pname) { | |
6fba6e0c BA |
193 | const pIdx = this.people.findIndex(pl => pl.name == pname); |
194 | return (pIdx === -1 ? null : this.people[pIdx].sid); | |
a6bddfc6 | 195 | }, |
5bd05dba | 196 | getPname: function(sid) { |
6fba6e0c BA |
197 | const pIdx = this.people.findIndex(pl => pl.sid == sid); |
198 | return (pIdx === -1 ? null : this.people[pIdx].name); | |
5bd05dba | 199 | }, |
a6bddfc6 BA |
200 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
201 | const doSend = (code, obj, sid) => { | |
202 | this.st.conn.send(JSON.stringify(Object.assign( | |
203 | {}, | |
204 | {code: code}, | |
205 | obj, | |
206 | {target: sid} | |
207 | ))); | |
208 | }; | |
c9695cb1 | 209 | if (!!to) |
a6bddfc6 | 210 | { |
c9695cb1 BA |
211 | // Challenge with targeted players |
212 | const targetSid = this.getSid(to); | |
213 | if (!targetSid) | |
214 | { | |
215 | if (!!warnDisconnected) | |
216 | alert("Warning: " + pname + " is not connected"); | |
217 | } | |
218 | else | |
219 | doSend(code, obj, targetSid); | |
a6bddfc6 BA |
220 | } |
221 | else | |
222 | { | |
223 | // Open challenge: send to all connected players (except us) | |
6fba6e0c | 224 | this.people.forEach(p => { |
a6bddfc6 BA |
225 | if (p.sid != this.st.user.sid) //only sid is always set |
226 | doSend(code, obj, p.sid); | |
227 | }); | |
228 | } | |
229 | }, | |
230 | // Messaging center: | |
9d58ef95 BA |
231 | socketMessageListener: function(msg) { |
232 | const data = JSON.parse(msg.data); | |
233 | switch (data.code) | |
234 | { | |
f4f4c03c | 235 | // 0.2] Receive clients list (just socket IDs) |
81d9ce72 | 236 | case "pollclients": |
1efe1d79 | 237 | { |
5a3da968 | 238 | data.sockIds.forEach(sid => { |
6fba6e0c | 239 | this.people.push({sid:sid, id:0, name:""}); |
81d9ce72 | 240 | // Ask identity, challenges and game(s) |
5a3da968 | 241 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
2ada153c | 242 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
81d9ce72 | 243 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
5a3da968 BA |
244 | }); |
245 | break; | |
1efe1d79 | 246 | } |
81d9ce72 | 247 | case "askidentity": |
1efe1d79 | 248 | { |
6855163c BA |
249 | // Request for identification: reply if I'm not anonymous |
250 | if (this.st.user.id > 0) | |
251 | { | |
252 | this.st.conn.send(JSON.stringify( | |
253 | {code:"identity", user:this.st.user, target:data.from})); | |
254 | } | |
5a3da968 | 255 | break; |
1efe1d79 | 256 | } |
dd75774d | 257 | case "askchallenge": |
1efe1d79 | 258 | { |
6855163c | 259 | // Send my current live challenge (if any) |
dd75774d | 260 | const cIdx = this.challenges |
6855163c | 261 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
dd75774d BA |
262 | if (cIdx >= 0) |
263 | { | |
264 | const c = this.challenges[cIdx]; | |
265 | const myChallenge = | |
266 | { | |
81d9ce72 | 267 | // Minimal challenge informations: (from not required) |
2ada153c | 268 | id: c.id, |
81d9ce72 BA |
269 | to: c.to, |
270 | fen: c.fen, | |
271 | vid: c.vid, | |
272 | timeControl: c.timeControl | |
dd75774d BA |
273 | }; |
274 | this.st.conn.send(JSON.stringify({code:"challenge", | |
42c15a75 | 275 | chall:myChallenge, target:data.from})); |
81d9ce72 BA |
276 | } |
277 | break; | |
1efe1d79 | 278 | } |
81d9ce72 | 279 | case "askgame": |
1efe1d79 | 280 | { |
42c15a75 BA |
281 | // Send my current live game (if any) |
282 | GameStorage.getCurrent((game) => { | |
283 | if (!!game) | |
284 | { | |
285 | const myGame = | |
286 | { | |
287 | // Minimal game informations: | |
288 | id: game.id, | |
289 | players: game.players.map(p => p.name), | |
290 | vname: game.vname, | |
291 | timeControl: game.timeControl, | |
292 | }; | |
293 | this.st.conn.send(JSON.stringify({code:"game", | |
294 | game:myGame, target:data.from})); | |
295 | } | |
296 | }); | |
81d9ce72 | 297 | break; |
1efe1d79 | 298 | } |
5a3da968 | 299 | case "identity": |
1efe1d79 | 300 | { |
6fba6e0c BA |
301 | const pIdx = this.people.findIndex(p => p.sid == data.user.sid); |
302 | this.people[pIdx].id = data.user.id; | |
303 | this.people[pIdx].name = data.user.name; | |
5a3da968 | 304 | break; |
1efe1d79 | 305 | } |
dd75774d | 306 | case "challenge": |
1efe1d79 | 307 | { |
dd75774d | 308 | // Receive challenge from some player (+sid) |
6855163c | 309 | let newChall = data.chall; |
2ada153c | 310 | newChall.type = this.classifyObject(data.chall); |
6fba6e0c BA |
311 | const pIdx = this.people.findIndex(p => p.sid == data.from); |
312 | newChall.from = this.people[pIdx]; //may be anonymous | |
42c15a75 BA |
313 | newChall.added = Date.now(); //TODO: this is reception timestamp, not creation |
314 | newChall.vname = this.getVname(newChall.vid); //TODO: just send vname? | |
6855163c | 315 | this.challenges.push(newChall); |
81d9ce72 | 316 | break; |
1efe1d79 | 317 | } |
dd75774d | 318 | case "game": |
1efe1d79 | 319 | { |
6855163c | 320 | // Receive game from some player (+sid) |
6855163c | 321 | // NOTE: it may be correspondance (if newgame while we are connected) |
d9b86b16 BA |
322 | if (!this.games.some(g => g.id == data.game.id)) //ignore duplicates |
323 | { | |
324 | let newGame = data.game; | |
325 | newGame.type = this.classifyObject(data.game); | |
326 | newGame.rid = data.from; | |
327 | newGame.score = "*"; | |
328 | this.games.push(newGame); | |
329 | } | |
81d9ce72 | 330 | break; |
1efe1d79 | 331 | } |
9d58ef95 | 332 | case "newgame": |
1efe1d79 | 333 | { |
5bd05dba | 334 | // New game just started: data contain all informations |
42c15a75 | 335 | this.startNewGame(data.gameInfo); |
485fccd5 BA |
336 | // TODO: if live, redirect to game |
337 | // if corr, notify with link but do not redirect | |
9d58ef95 | 338 | break; |
1efe1d79 | 339 | } |
bb7dd7db BA |
340 | case "refusechallenge": |
341 | { | |
485fccd5 | 342 | alert(this.getPname(data.from) + " declined your challenge"); |
5bd05dba | 343 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
bb7dd7db BA |
344 | break; |
345 | } | |
1efe1d79 BA |
346 | case "deletechallenge": |
347 | { | |
1ba761c8 | 348 | // NOTE: the challenge may be already removed |
9d58ef95 BA |
349 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
350 | break; | |
1efe1d79 | 351 | } |
b4d619d1 | 352 | case "connect": |
1efe1d79 | 353 | { |
6fba6e0c | 354 | this.people.push({name:"", id:0, sid:data.sid}); |
5a3da968 | 355 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); |
f05815d7 BA |
356 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid})); |
357 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid})); | |
9d58ef95 | 358 | break; |
1efe1d79 | 359 | } |
b4d619d1 | 360 | case "disconnect": |
1efe1d79 | 361 | { |
6fba6e0c | 362 | ArrayFun.remove(this.people, p => p.sid == data.sid); |
a6bddfc6 | 363 | // Also remove all challenges sent by this player: |
2ada153c BA |
364 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
365 | // And all live games where he plays and no other opponent is online | |
366 | ArrayFun.remove(this.games, g => | |
367 | g.type == "live" && (g.players.every(p => p.sid == data.sid | |
6fba6e0c | 368 | || !this.people.some(pl => pl.sid == p.sid))), "all"); |
9d58ef95 | 369 | break; |
1efe1d79 | 370 | } |
9d58ef95 BA |
371 | } |
372 | }, | |
a6bddfc6 | 373 | // Challenge lifecycle: |
b4d619d1 BA |
374 | tryChallenge: function(player) { |
375 | if (player.id == 0) | |
376 | return; //anonymous players cannot be challenged | |
81d9ce72 | 377 | this.newchallenge.to[0] = player.name; |
b4d619d1 | 378 | doClick("modalNewgame"); |
fb54f098 | 379 | }, |
9d58ef95 | 380 | newChallenge: async function() { |
bb7dd7db | 381 | const vname = this.getVname(this.newchallenge.vid); |
1efe1d79 BA |
382 | const vModule = await import("@/variants/" + vname + ".js"); |
383 | window.V = vModule.VariantRules; | |
9d58ef95 BA |
384 | const error = checkChallenge(this.newchallenge); |
385 | if (!!error) | |
386 | return alert(error); | |
2ada153c | 387 | const ctype = this.classifyObject(this.newchallenge); |
bb7dd7db | 388 | // NOTE: "from" information is not required here |
1efe1d79 | 389 | let chall = |
dd75774d | 390 | { |
a6bddfc6 | 391 | fen: this.newchallenge.fen, |
6fba6e0c | 392 | to: this.newchallenge.to, |
5578a7bf | 393 | timeControl: this.newchallenge.timeControl, |
1efe1d79 | 394 | vid: this.newchallenge.vid, |
5578a7bf | 395 | }; |
2ada153c | 396 | const finishAddChallenge = (cid,warnDisconnected) => { |
1efe1d79 | 397 | chall.id = cid || "c" + getRandString(); |
2ada153c | 398 | // Send challenge to peers (if connected) |
c9695cb1 | 399 | this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected); |
1efe1d79 | 400 | chall.added = Date.now(); |
bb7dd7db BA |
401 | chall.type = ctype; |
402 | chall.vname = vname; | |
403 | chall.from = this.st.user; | |
1efe1d79 | 404 | this.challenges.push(chall); |
f6f2bef1 | 405 | localStorage.setItem("challenge", JSON.stringify(chall)); |
b4d619d1 BA |
406 | document.getElementById("modalNewgame").checked = false; |
407 | }; | |
1efe1d79 BA |
408 | const cIdx = this.challenges.findIndex( |
409 | c => c.from.sid == this.st.user.sid && c.type == ctype); | |
410 | if (cIdx >= 0) | |
b4d619d1 | 411 | { |
1efe1d79 | 412 | // Delete current challenge (will be replaced now) |
bb7dd7db | 413 | this.sendSomethingTo(this.challenges[cIdx].to, |
1efe1d79 BA |
414 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
415 | if (ctype == "corr") | |
416 | { | |
417 | ajax( | |
418 | "/challenges", | |
419 | "DELETE", | |
420 | {id: this.challenges[cIdx].id} | |
421 | ); | |
422 | } | |
423 | this.challenges.splice(cIdx, 1); | |
424 | } | |
425 | if (ctype == "live") | |
426 | { | |
427 | // Live challenges have a random ID | |
2ada153c | 428 | finishAddChallenge(null, "warnDisconnected"); |
03608482 | 429 | } |
b4d619d1 | 430 | else |
03608482 | 431 | { |
b4d619d1 | 432 | // Correspondance game: send challenge to server |
03608482 | 433 | ajax( |
1efe1d79 | 434 | "/challenges", |
03608482 | 435 | "POST", |
052d17ea | 436 | chall, |
1efe1d79 | 437 | response => { finishAddChallenge(response.cid); } |
03608482 | 438 | ); |
9d58ef95 | 439 | } |
fb54f098 | 440 | }, |
a6bddfc6 | 441 | clickChallenge: function(c) { |
485fccd5 BA |
442 | const myChallenge = (c.from.sid == this.st.user.sid //live |
443 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr | |
444 | if (!myChallenge) | |
a6bddfc6 BA |
445 | { |
446 | c.accepted = true; | |
485fccd5 | 447 | if (!!c.to) //c.to == this.st.user.name (connected) |
a6bddfc6 BA |
448 | { |
449 | // TODO: if special FEN, show diagram after loading variant | |
450 | c.accepted = confirm("Accept challenge?"); | |
451 | } | |
485fccd5 | 452 | if (c.accepted) |
36093eba | 453 | { |
485fccd5 BA |
454 | c.seat = this.st.user; |
455 | this.launchGame(c); | |
456 | } | |
457 | else | |
458 | { | |
459 | this.st.conn.send(JSON.stringify({ | |
460 | code: "refusechallenge", | |
461 | cid: c.id, target: c.from.sid})); | |
36093eba | 462 | } |
a6bddfc6 | 463 | } |
3cb412e9 BA |
464 | else |
465 | localStorage.removeItem("challenge"); | |
485fccd5 BA |
466 | // In all cases, the challenge is consumed: |
467 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); | |
468 | // NOTE: deletechallenge event might be redundant (but it's easier this way) | |
469 | this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); | |
470 | if (c.type == "corr") | |
471 | { | |
472 | ajax( | |
473 | "/challenges", | |
474 | "DELETE", | |
475 | {id: this.challenges[cIdx].id} | |
476 | ); | |
477 | } | |
a6bddfc6 | 478 | }, |
485fccd5 | 479 | // NOTE: when launching game, the challenge is already deleted |
36093eba | 480 | launchGame: async function(c) { |
a6bddfc6 BA |
481 | const vname = this.getVname(c.vid); |
482 | const vModule = await import("@/variants/" + vname + ".js"); | |
483 | window.V = vModule.VariantRules; | |
6fba6e0c | 484 | let players = [c.from, c.seat]; |
4b0384fa BA |
485 | // These game informations will be sent to other players |
486 | const gameInfo = | |
a6bddfc6 | 487 | { |
4b0384fa | 488 | gameId: getRandString(), |
a6bddfc6 | 489 | fen: c.fen || V.GenRandInitFen(), |
5bd05dba | 490 | // Players' names may be required if game start when a player is offline |
485fccd5 | 491 | // Shuffle players order (white then black). |
4b0384fa | 492 | players: shuffle(players.map(p => { return {name:p.name, sid:p.sid} })), |
a6bddfc6 BA |
493 | vid: c.vid, |
494 | timeControl: c.timeControl, | |
495 | }; | |
6fba6e0c | 496 | this.st.conn.send(JSON.stringify({code:"newgame", |
485fccd5 BA |
497 | gameInfo:gameInfo, target:c.seat.sid})); |
498 | if (c.type == "live") | |
499 | this.startNewGame(gameInfo); | |
500 | else //corr: game only on server | |
501 | { | |
502 | ajax( | |
503 | "/games", | |
504 | "POST", | |
505 | {gameInfo: gameInfo} | |
506 | ); | |
507 | } | |
fb54f098 | 508 | }, |
7b626bdd | 509 | // NOTE: for live games only (corr games are launched on server) |
42c15a75 | 510 | startNewGame: function(gameInfo) { |
967a2686 BA |
511 | // Extract times (in [milli]seconds), set clocks |
512 | const tc = extractTime(gameInfo.timeControl); | |
809ba2aa BA |
513 | let initime = [...Array(gameInfo.players.length)]; |
514 | initime[0] = Date.now(); | |
967a2686 BA |
515 | const game = |
516 | { | |
517 | // Game infos: constant | |
4b0384fa | 518 | gameId: gameInfo.gameId, |
6d01bb17 BA |
519 | vname: this.getVname(gameInfo.vid), |
520 | fenStart: gameInfo.fen, | |
521 | players: gameInfo.players, | |
522 | timeControl: gameInfo.timeControl, | |
967a2686 BA |
523 | increment: tc.increment, |
524 | mode: "live", //function for live games only | |
525 | // Game state: will be updated | |
526 | fen: gameInfo.fen, | |
527 | moves: [], | |
528 | clocks: [...Array(gameInfo.players.length)].fill(tc.mainTime), | |
809ba2aa | 529 | initime: initime, |
967a2686 BA |
530 | score: "*", |
531 | }; | |
532 | GameStorage.add(game); | |
7b626bdd BA |
533 | if (this.st.settings.sound >= 1) |
534 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
a6088c90 | 535 | // TODO: redirect to game |
1efe1d79 | 536 | }, |
fb54f098 | 537 | }, |
85e5b5c1 | 538 | }; |
ccd4a2b7 | 539 | </script> |
85e5b5c1 BA |
540 | |
541 | <style lang="sass"> | |
542 | // TODO | |
543 | </style> |