Commit | Line | Data |
---|---|---|
b4d619d1 BA |
1 | <!-- Main playing hall: online players + current challenges + button "new game" --> |
2 | ||
ccd4a2b7 | 3 | <template lang="pug"> |
9d58ef95 | 4 | main |
5b020e73 BA |
5 | input#modalNewgame.modal(type="checkbox") |
6 | div(role="dialog" aria-labelledby="titleFenedit") | |
7 | .card.smallpad | |
8 | label#closeNewgame.modal-close(for="modalNewgame") | |
9 | fieldset | |
10 | label(for="selectVariant") {{ st.tr["Variant"] }} | |
9d58ef95 | 11 | select#selectVariant(v-model="newchallenge.vid") |
85e5b5c1 | 12 | option(v-for="v in st.variants" :value="v.id") {{ v.name }} |
5b020e73 BA |
13 | fieldset |
14 | label(for="selectNbPlayers") {{ st.tr["Number of players"] }} | |
9d58ef95 | 15 | select#selectNbPlayers(v-model="newchallenge.nbPlayers") |
5b020e73 BA |
16 | option(v-show="possibleNbplayers(2)" value="2") 2 |
17 | option(v-show="possibleNbplayers(3)" value="3") 3 | |
18 | option(v-show="possibleNbplayers(4)" value="4") 4 | |
19 | fieldset | |
b4d619d1 | 20 | label(for="timeControl") {{ st.tr["Time control"] }} |
9d58ef95 | 21 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
b4d619d1 BA |
22 | placeholder="3m+2s, 1h+30s, 7d+1d ...") |
23 | fieldset(v-if="st.user.id > 0") | |
9d58ef95 | 24 | label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }} |
5b020e73 | 25 | #selectPlayers |
03608482 | 26 | input(type="text" v-model="newchallenge.to[0].name") |
9d58ef95 | 27 | input(v-show="newchallenge.nbPlayers>=3" type="text" |
03608482 | 28 | v-model="newchallenge.to[1].name") |
9d58ef95 | 29 | input(v-show="newchallenge.nbPlayers==4" type="text" |
03608482 | 30 | v-model="newchallenge.to[2].name") |
b4d619d1 | 31 | fieldset(v-if="st.user.id > 0") |
9d58ef95 BA |
32 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
33 | input#inputFen(type="text" v-model="newchallenge.fen") | |
b4d619d1 | 34 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
9d58ef95 BA |
35 | .row |
36 | .col-sm-12.col-md-5.col-md-offset-1.col-lg-4.col-lg-offset-2 | |
03608482 BA |
37 | .button-group |
38 | button(@click="cpdisplay='challenges'") Challenges | |
39 | button(@click="cpdisplay='players'") Players | |
40 | ChallengeList(v-show="cpdisplay=='challenges'" | |
41 | :challenges="challenges" @click-challenge="clickChallenge") | |
42 | #players(v-show="cpdisplay=='players'") | |
9d58ef95 | 43 | h3 Online players |
b4d619d1 BA |
44 | div(v-for="p in uniquePlayers" @click="tryChallenge(p)") |
45 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} | |
9d58ef95 BA |
46 | .row |
47 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
48 | button(onClick="doClick('modalNewgame')") New game | |
49 | .row | |
50 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
51 | .button-group | |
52 | button(@click="gdisplay='live'") Live games | |
53 | button(@click="gdisplay='corr'") Correspondance games | |
54 | GameList(v-show="gdisplay=='live'" :games="liveGames" | |
55 | @show-game="showGame") | |
56 | GameList(v-show="gdisplay=='corr'" :games="corrGames" | |
57 | @show-game="showGame") | |
625022fd BA |
58 | </template> |
59 | ||
60 | <script> | |
5b020e73 | 61 | import { store } from "@/store"; |
85e5b5c1 | 62 | import { NbPlayers } from "@/data/nbPlayers"; |
9d58ef95 BA |
63 | import { checkChallenge } from "@/data/challengeCheck"; |
64 | import { ArrayFun } from "@/utils/array"; | |
03608482 | 65 | import { ajax } from "@/utils/ajax"; |
5b020e73 BA |
66 | import GameList from "@/components/GameList.vue"; |
67 | import ChallengeList from "@/components/ChallengeList.vue"; | |
625022fd | 68 | export default { |
cf2343ce | 69 | name: "my-hall", |
5b020e73 BA |
70 | components: { |
71 | GameList, | |
72 | ChallengeList, | |
73 | }, | |
fb54f098 BA |
74 | data: function () { |
75 | return { | |
5b020e73 | 76 | st: store.state, |
b4d619d1 | 77 | cpdisplay: "challenges", |
fb54f098 BA |
78 | gdisplay: "live", |
79 | liveGames: [], | |
80 | corrGames: [], | |
b4d619d1 | 81 | challenges: [], |
fb54f098 | 82 | players: [], //online players |
9d58ef95 | 83 | newchallenge: { |
fb54f098 BA |
84 | fen: "", |
85 | vid: 0, | |
86 | nbPlayers: 0, | |
6faa92f2 BA |
87 | to: ["", "", ""], //name of challenged players |
88 | timeControl: "", //"2m+2s" ...etc | |
fb54f098 BA |
89 | }, |
90 | }; | |
91 | }, | |
b4d619d1 BA |
92 | computed: { |
93 | uniquePlayers: function() { | |
94 | // Show e.g. "5 @nonymous", and do nothing on click on anonymous | |
4d64881e BA |
95 | let anonymous = {id:0, name:"@nonymous", count:0}; |
96 | let playerList = []; | |
b4d619d1 BA |
97 | this.players.forEach(p => { |
98 | if (p.id > 0) | |
99 | playerList.push(p); | |
100 | else | |
4d64881e | 101 | anonymous.count++; |
b4d619d1 | 102 | }); |
4d64881e BA |
103 | if (anonymous.count > 0) |
104 | playerList.push(anonymous); | |
b4d619d1 BA |
105 | return playerList; |
106 | }, | |
107 | }, | |
9d58ef95 | 108 | created: function() { |
4d64881e BA |
109 | // Always add myself to players' list |
110 | this.players.push(this.st.user); | |
f4f4c03c BA |
111 | // Ask server for current corr games (all but mines) |
112 | ajax( | |
113 | "", | |
114 | "GET", | |
115 | response => { | |
116 | ||
117 | } | |
118 | ); | |
119 | // Also ask for corr challenges (all) | |
120 | ajax( | |
121 | "", | |
122 | "GET", | |
123 | response => { | |
124 | ||
125 | } | |
126 | ); | |
127 | // 0.1] Ask server for for room composition: | |
4d64881e | 128 | const socketOpenListener = () => { |
5a3da968 | 129 | this.st.conn.send(JSON.stringify({code:"askclients"})); |
4d64881e BA |
130 | }; |
131 | this.st.conn.onopen = socketOpenListener; | |
132 | this.st.conn.onmessage = this.socketMessageListener; | |
133 | const socketCloseListener = () => { | |
134 | // connexion is reinitialized in store.js | |
135 | this.st.conn.addEventListener('message', this.socketMessageListener); | |
136 | this.st.conn.addEventListener('close', socketCloseListener); | |
137 | }; | |
138 | this.st.conn.onclose = socketCloseListener; | |
9d58ef95 | 139 | }, |
fb54f098 | 140 | methods: { |
9d58ef95 BA |
141 | socketMessageListener: function(msg) { |
142 | const data = JSON.parse(msg.data); | |
143 | switch (data.code) | |
144 | { | |
f4f4c03c | 145 | // 0.2] Receive clients list (just socket IDs) |
5a3da968 BA |
146 | case "clients": |
147 | data.sockIds.forEach(sid => { | |
148 | this.players.push({sid:sid, id:0, name:""}); | |
149 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); | |
150 | }); | |
151 | break; | |
152 | // TODO: also receive "askchallenges", "askgames" | |
153 | case "identify": | |
154 | // Request for identification | |
f4f4c03c BA |
155 | this.st.conn.send(JSON.stringify( |
156 | {code:"identity", user:this.st.user, target:data.from})); | |
5a3da968 BA |
157 | break; |
158 | case "identity": | |
f4f4c03c | 159 | if (data.user.id > 0) //otherwise "anonymous", nothing to retrieve |
5a3da968 BA |
160 | { |
161 | const pIdx = this.players.findIndex(p => p.sid == data.user.sid); | |
162 | this.players[pIdx].id = data.user.id; | |
163 | this.players[pIdx].name = data.user.name; | |
164 | } | |
165 | break; | |
b4d619d1 BA |
166 | // * - receive "new game": if live, store locally + redirect to game |
167 | // * If corr: notify "new game has started", give link, but do not redirect | |
9d58ef95 BA |
168 | case "newgame": |
169 | // TODO: new game just started: data contain all informations | |
170 | // (id, players, time control, fenStart ...) | |
b4d619d1 BA |
171 | // + cid to remove challenge from list |
172 | break; | |
173 | // * - receive "playergame": a live game by some connected player (NO corr) | |
174 | case "playergame": | |
175 | // TODO: receive live game summary (update, count moves) | |
176 | // (just players names, time control, and ID + player ID) | |
177 | break; | |
178 | // * - receive "playerchallenges": list of challenges (sent) by some online player (NO corr) | |
179 | case "playerchallenges": | |
180 | // TODO: receive challenge + challenge updates | |
9d58ef95 | 181 | break; |
b4d619d1 BA |
182 | case "newmove": //live or corr |
183 | // TODO: name conflict ? (game "newmove" event) | |
184 | break; | |
185 | // * - receive new challenge: if targeted, replace our name with sender name | |
186 | case "newchallenge": | |
187 | // receive live or corr challenge | |
5a3da968 | 188 | this.challenges.push(data.chall); |
b4d619d1 BA |
189 | break; |
190 | // * - receive "accept/withdraw/cancel challenge": apply action to challenges list | |
9d58ef95 BA |
191 | case "acceptchallenge": |
192 | if (true) //TODO: if challenge is full | |
193 | this.newGame(data.challenge, data.user); //user.id et user.name | |
194 | break; | |
195 | case "withdrawchallenge": | |
196 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); | |
197 | let chall = this.challenges[cIdx] | |
198 | ArrayFun.remove(chall.players, p => p.id == data.uid); | |
199 | chall.players.push({id:0, name:""}); | |
200 | break; | |
201 | case "cancelchallenge": | |
202 | ArrayFun.remove(this.challenges, c => c.id == data.cid); | |
203 | break; | |
b4d619d1 BA |
204 | // NOTE: finally only one connect / disconnect couple of events |
205 | // (because on server side we wouldn't know which to choose) | |
206 | case "connect": | |
207 | // * - receive "player connect": send all our current challenges (to him or global) | |
208 | // * Also send all our games (live - max 1 - and corr) [in web worker ?] | |
209 | // * + all our sent challenges. | |
5a3da968 BA |
210 | this.players.push({name:"", id:0, sid:data.sid}); |
211 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); | |
b4d619d1 BA |
212 | // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté |
213 | // envoyer aussi nos défis | |
9d58ef95 | 214 | break; |
b4d619d1 BA |
215 | // * - receive "player disconnect": remove from players list |
216 | case "disconnect": | |
5a3da968 | 217 | ArrayFun.remove(this.players, p => p.sid == data.sid); |
03608482 BA |
218 | // TODO: also remove all challenges sent by this player, |
219 | // and all live games where he plays and no other opponent is online | |
9d58ef95 BA |
220 | break; |
221 | } | |
222 | }, | |
fb54f098 | 223 | showGame: function(game) { |
5b020e73 BA |
224 | // NOTE: if we are an observer, the game will be found in main games list |
225 | // (sent by connected remote players) | |
b4d619d1 | 226 | // TODO: game path ? /vname/gameId seems better |
5b020e73 | 227 | this.$router.push("/" + game.id) |
fb54f098 | 228 | }, |
b4d619d1 BA |
229 | tryChallenge: function(player) { |
230 | if (player.id == 0) | |
231 | return; //anonymous players cannot be challenged | |
232 | this.newchallenge.players[0] = { | |
233 | name: player.name, | |
234 | id: player.id, | |
235 | sid: player.sid, | |
236 | }; | |
237 | doClick("modalNewgame"); | |
fb54f098 | 238 | }, |
b4d619d1 BA |
239 | // * - accept challenge (corr or live) --> send info to all concerned players |
240 | // * - cancel challenge (click on sent challenge) --> send info to all concerned players | |
241 | // * - withdraw from challenge (if >= 3 players and previously accepted) | |
242 | // * --> send info to all concerned players | |
243 | // * - refuse challenge (or receive refusal): send to all challenge players (from + to) | |
244 | // * except us ; graphics: modal again ? (inline ?) | |
245 | // * - prepare and start new game (if challenge is full after acceptation) | |
246 | // * --> include challenge ID (so that opponents can delete the challenge too) | |
247 | // * Also send to all connected players (only from me) | |
fb54f098 BA |
248 | clickChallenge: function(challenge) { |
249 | const index = this.challenges.findIndex(c => c.id == challenge.id); | |
250 | const toIdx = challenge.to.findIndex(p => p.id == user.id); | |
251 | const me = {name:user.name,id:user.id}; | |
252 | if (toIdx >= 0) | |
253 | { | |
254 | // It's a multiplayer challenge I accepted: withdraw | |
255 | this.st.conn.send(JSON.stringify({code:"withdrawchallenge", | |
256 | cid:challenge.id, user:me})); | |
257 | this.challenges.to.splice(toIdx, 1); | |
258 | } | |
259 | else if (challenge.from.id == user.id) //it's my challenge: cancel it | |
260 | { | |
261 | this.st.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id})); | |
262 | this.challenges.splice(index, 1); | |
263 | } | |
264 | else //accept a challenge | |
265 | { | |
266 | this.st.conn.send(JSON.stringify({code:"acceptchallenge", | |
267 | cid:challenge.id, user:me})); | |
268 | this.challenges[index].to.push(me); | |
269 | } | |
270 | // TODO: accepter un challenge peut lancer une partie, il | |
271 | // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici | |
fb54f098 BA |
272 | // si pas le mien et FEN speciale :: (charger code variante et) |
273 | // montrer diagramme + couleur (orienté) | |
274 | }, | |
b4d619d1 BA |
275 | // user: last person to accept the challenge (TODO: revoir ça) |
276 | // newGame: function(chall, user) { | |
277 | // const fen = chall.fen || V.GenRandInitFen(); | |
278 | // const game = {}; //TODO: fen, players, time ... | |
279 | // //setStorage(game); //TODO | |
280 | // game.players.forEach(p => { //...even if game is by corr (could be played live, why not...) | |
281 | // this.conn.send( | |
282 | // JSON.stringify({code:"newgame", oppid:p.id, game:game})); | |
283 | // }); | |
284 | // if (this.settings.sound >= 1) | |
285 | // new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
286 | // }, | |
287 | // Send new challenge (corr or live, cf. time control), with button or click on player | |
9d58ef95 | 288 | newChallenge: async function() { |
5578a7bf BA |
289 | if (this.challenges.some(c => c.from.sid == this.st.user.sid)) |
290 | { | |
291 | document.getElementById("modalNewgame").checked = false; | |
292 | return alert("You already have a pending challenge"); | |
293 | } | |
9d58ef95 BA |
294 | const idxInVariants = |
295 | this.st.variants.findIndex(v => v.id == this.newchallenge.vid); | |
03608482 | 296 | const vname = this.st.variants[idxInVariants].name; |
9d58ef95 BA |
297 | const vModule = await import("@/variants/" + vname + ".js"); |
298 | window.V = vModule.VariantRules; | |
03608482 | 299 | // checkChallenge side-effect = set FEN, and mainTime + increment in seconds |
f4f4c03c | 300 | // TODO: should not be a side-effect but set here ; for received server challenges we do not have mainTime+increment |
9d58ef95 BA |
301 | const error = checkChallenge(this.newchallenge); |
302 | if (!!error) | |
303 | return alert(error); | |
03608482 BA |
304 | // Less than 3 days ==> live game (TODO: heuristic... 40 moves also) |
305 | const liveGame = | |
306 | this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60; | |
307 | // Check that the players (if any indicated) are online | |
5578a7bf BA |
308 | let chall = |
309 | { | |
310 | id: 0, //unknown yet (no ID for live challenges) | |
311 | from: this.st.user, | |
312 | added: Date.now(), | |
313 | fen: this.newchallenge.fen, | |
314 | variant: {id: this.newchallenge.vid, name: vname}, | |
315 | nbPlayers: this.newchallenge.nbPlayers, | |
316 | to: [ | |
317 | {id: 0, name: this.newchallenge.to[0], sid: ""}, | |
318 | {id: 0, name: this.newchallenge.to[1], sid: ""}, | |
319 | {id: 0, name: this.newchallenge.to[2], sid: ""}, | |
320 | ], | |
321 | timeControl: this.newchallenge.timeControl, | |
322 | mainTime: this.newchallenge.mainTime, | |
323 | increment: this.newchallenge.increment, | |
324 | }; | |
325 | for (let p of chall.to) | |
9d58ef95 | 326 | { |
03608482 BA |
327 | if (p.name != "") |
328 | { | |
329 | const pIdx = this.players.findIndex(pl => pl.name == p.name); | |
6faa92f2 BA |
330 | // NOTE: id (server DB) and sid (socket ID). |
331 | // Anonymous players just have a socket ID. | |
5578a7bf BA |
332 | // NOTE: for correspondance play we don't require players to be online |
333 | // (==> we don't have IDs, and no sid) | |
334 | if (liveGame && pIdx === -1) | |
03608482 BA |
335 | return alert(p.name + " is not connected"); |
336 | p.id = this.players[pIdx].id; | |
337 | p.sid = this.players[pIdx].sid; | |
338 | } | |
339 | } | |
5578a7bf | 340 | const finishAddChallenge = () => { |
03608482 | 341 | this.challenges.push(chall); |
b4d619d1 | 342 | // Send challenge to peers |
5a3da968 | 343 | let challSock = |
5578a7bf | 344 | { |
b4d619d1 | 345 | code: "newchallenge", |
5578a7bf | 346 | chall: chall, |
5a3da968 BA |
347 | target: "", |
348 | }; | |
349 | const sendChallengeTo = (sid) => { | |
350 | challSock.target = sid; | |
351 | this.st.conn.send(JSON.stringify(challSock)); | |
5578a7bf BA |
352 | }; |
353 | if (chall.to[0].id > 0) | |
9d58ef95 | 354 | { |
03608482 | 355 | // Challenge with targeted players |
5578a7bf | 356 | chall.to.forEach(p => { |
03608482 | 357 | if (p.id > 0) |
5a3da968 | 358 | sendChallengeTo(p.sid); |
9d58ef95 BA |
359 | }); |
360 | } | |
361 | else | |
362 | { | |
5578a7bf | 363 | // Open challenge: send to all connected players (except us) |
5578a7bf BA |
364 | this.players.forEach(p => { |
365 | if (p.sid != this.st.user.sid) //only sid is always set | |
5a3da968 | 366 | sendChallengeTo(p.sid); |
5578a7bf | 367 | }); |
9d58ef95 | 368 | } |
b4d619d1 BA |
369 | document.getElementById("modalNewgame").checked = false; |
370 | }; | |
f4f4c03c BA |
371 | |
372 | ||
373 | // TODO: challenges all have IDs: "c" + genRandString() | |
374 | ||
375 | ||
b4d619d1 BA |
376 | if (liveGame) |
377 | { | |
03608482 | 378 | // Live challenges have cid = 0 |
5578a7bf | 379 | finishAddChallenge(); |
03608482 | 380 | } |
b4d619d1 | 381 | else |
03608482 | 382 | { |
f4f4c03c BA |
383 | const chall = { |
384 | uid: req.body["from"], | |
385 | vid: req.body["vid"], | |
386 | fen: req.body["fen"], | |
387 | timeControl: req.body["timeControl"], | |
388 | nbPlayers: req.body["nbPlayers"], | |
389 | to: req.body["to"], //array of IDs | |
390 | }; | |
b4d619d1 | 391 | // Correspondance game: send challenge to server |
03608482 BA |
392 | ajax( |
393 | "/challenges/" + this.newchallenge.vid, | |
394 | "POST", | |
f4f4c03c | 395 | , |
5578a7bf BA |
396 | response => { |
397 | chall.id = response.cid; | |
398 | finishAddChallenge(); | |
399 | } | |
03608482 | 400 | ); |
9d58ef95 | 401 | } |
fb54f098 BA |
402 | }, |
403 | possibleNbplayers: function(nbp) { | |
9d58ef95 | 404 | if (this.newchallenge.vid == 0) |
fb54f098 | 405 | return false; |
85e5b5c1 | 406 | const variants = this.st.variants; |
fb54f098 | 407 | const idxInVariants = |
9d58ef95 | 408 | variants.findIndex(v => v.id == this.newchallenge.vid); |
fb54f098 BA |
409 | return NbPlayers[variants[idxInVariants].name].includes(nbp); |
410 | }, | |
411 | }, | |
85e5b5c1 | 412 | }; |
ccd4a2b7 | 413 | </script> |
85e5b5c1 BA |
414 | |
415 | <style lang="sass"> | |
416 | // TODO | |
417 | </style> |