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