Commit | Line | Data |
---|---|---|
214dfe16 | 1 | // main playing hall: chat + online players + current challenges + button "new game" |
a3ab5fdb | 2 | // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list |
2305d34a | 3 | // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés |
b6487fb9 | 4 | /* |
214dfe16 | 5 | TODO: surligner si nouveau défi perso et pas affichage courant |
b6487fb9 | 6 | (cadences base + incrément, corr == incr >= 1jour ou base >= 7j) |
214dfe16 BA |
7 | --> correspondance: stocker sur serveur lastMove + uid + color + movesCount + gameId + variant + timeleft |
8 | fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire) | |
9 | main time should be positive (no 0+2 & cie...) | |
b6487fb9 | 10 | */ |
a6403027 | 11 | // TODO: au moins l'échange des coups en P2P ? |
214dfe16 | 12 | // TODO: objet game, objet challenge ? et player ? |
a3ab5fdb BA |
13 | Vue.component('my-room', { |
14 | props: ["conn","settings"], | |
60d9063f BA |
15 | data: function () { |
16 | return { | |
17 | gdisplay: "live", | |
18 | user: user, | |
19 | liveGames: [], | |
20 | corrGames: [], | |
21 | players: [], //online players | |
22 | challenges: [], //live challenges | |
23 | people: [], //people who connect to this room (or disconnect) | |
24 | }; | |
a3ab5fdb | 25 | }, |
a6403027 BA |
26 | // Modal new game, and then sub-components |
27 | template: ` | |
28 | <div> | |
60d9063f | 29 | <input id="modalNewgame" type="checkbox" class="modal"/> |
a6403027 BA |
30 | <div role="dialog" aria-labelledby="titleFenedit"> |
31 | <div class="card smallpad"> | |
32 | <label id="closeNewgame" for="modalNewgame" class="modal-close"> | |
33 | </label> | |
34 | <h3 id="titleFenedit" class="section"> | |
35 | {{ translate("Game state (FEN):") }} | |
36 | </h3> | |
37 | <input id="input-fen" type="text"/> | |
38 | <p>TODO: cadence, adversaire (pre-filled if click on name)</p> | |
214dfe16 BA |
39 | <p>cadence 2m+12s ou 7d+1d (m,s ou d,d) --> main, increment</p> |
40 | <p>Note: leave FEN blank for random; FEN only for targeted challenge</p> | |
a6403027 BA |
41 | <button @click="newGame">Launch game</button> |
42 | </div> | |
43 | </div> | |
3ca7a846 | 44 | <div> |
60d9063f | 45 | <my-chat :conn="conn" :myname="user.name" :people="people"></my-chat> |
214dfe16 BA |
46 | <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge"> |
47 | </my-challenge-list> | |
3ca7a846 | 48 | </div> |
214dfe16 | 49 | <button onClick="doClick('modalNewgame')">New game</button> |
3ca7a846 | 50 | <div> |
60d9063f BA |
51 | <div style="border:1px solid black"> |
52 | <h3>Online players</h3> | |
214dfe16 BA |
53 | <div v-for="p in players" @click="challenge(p)"> |
54 | {{ p.name }} | |
55 | </div> | |
56 | </div> | |
57 | <div class="button-group"> | |
60d9063f BA |
58 | <button @click="gdisplay='live'">Live games</button> |
59 | <button @click="gdisplay='corr'">Correspondance games</button> | |
214dfe16 | 60 | </div> |
60d9063f BA |
61 | <my-game-list v-show="gdisplay=='live'" :games="liveGames" |
62 | @show-game="showGame"> | |
214dfe16 | 63 | </my-game-list> |
60d9063f BA |
64 | <my-game-list v-show="gdisplay=='corr'" :games="corrGames" |
65 | @show-game="showGame"> | |
214dfe16 | 66 | </my-game-list> |
3ca7a846 | 67 | </div> |
a6403027 BA |
68 | </div> |
69 | `, | |
70 | created: function() { | |
214dfe16 | 71 | // TODO: ask server for current corr games (all but mines: names, ID, time control) |
a6403027 BA |
72 | const socketMessageListener = msg => { |
73 | const data = JSON.parse(msg.data); | |
74 | switch (data.code) | |
75 | { | |
214dfe16 BA |
76 | case "newgame": |
77 | // TODO: new game just started: data contain all informations | |
78 | // (id, players, time control, fenStart ...) | |
79 | break; | |
80 | // TODO: also receive live games summaries (update) | |
3ca7a846 | 81 | // (just players names, time control, and ID + player ID) |
214dfe16 | 82 | case "acceptchallenge": |
a3ab5fdb | 83 | // oppid: opponent socket ID (or DB id if registered) |
214dfe16 BA |
84 | if (true) //TODO: if challenge is full |
85 | this.newGame(data.challenge, data.user); //user.id et user.name | |
86 | break; | |
87 | case "withdrawchallenge": | |
88 | // TODO | |
89 | break; | |
90 | case "cancelchallenge": | |
91 | // TODO | |
92 | break; | |
93 | // TODO: distinguish these (dis)connect events from their analogs in game.js | |
94 | case "connect": | |
95 | this.players.push({name:data.name, id:data.uid}); | |
96 | break; | |
97 | case "disconnect": | |
98 | const pIdx = this.players.findIndex(p => p.id == data.uid); | |
99 | this.players.splice(pIdx); | |
81da2786 | 100 | break; |
a6403027 BA |
101 | } |
102 | }; | |
103 | const socketCloseListener = () => { | |
104 | this.conn.addEventListener('message', socketMessageListener); | |
105 | this.conn.addEventListener('close', socketCloseListener); | |
106 | }; | |
107 | this.conn.onmessage = socketMessageListener; | |
108 | this.conn.onclose = socketCloseListener; | |
109 | }, | |
110 | methods: { | |
60d9063f | 111 | translate: translate, |
214dfe16 BA |
112 | showGame: function(game) { |
113 | let hash = "#game?id=" + game.id; | |
114 | if (!!game.uid) | |
115 | hash += "&uid=" + game.uid; | |
116 | location.hash = hash; | |
117 | }, | |
118 | challenge: function(player) { | |
119 | this.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id, | |
60d9063f | 120 | user:{name:user.name,id:user.id}})); |
214dfe16 BA |
121 | }, |
122 | clickChallenge: function(challenge) { | |
123 | const index = this.challenges.findIndex(c => c.id == challenge.id); | |
124 | const toIdx = challenge.to.findIndex(p => p.id == user.id); | |
125 | const me = {name:user.name,id:user.id}; | |
126 | if (toIdx >= 0) | |
81da2786 | 127 | { |
214dfe16 BA |
128 | // It's a multiplayer challenge I accepted: withdraw |
129 | this.conn.send(JSON.stringify({code:"withdrawchallenge", | |
130 | cid:challenge.id, user:me})); | |
131 | this.challenges.to.splice(toIdx, 1); | |
81da2786 | 132 | } |
214dfe16 | 133 | else if (challenge.from.id == user.id) //it's my challenge: cancel it |
81da2786 | 134 | { |
214dfe16 BA |
135 | this.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id})); |
136 | this.challenges.splice(index, 1); | |
81da2786 | 137 | } |
214dfe16 | 138 | else //accept a challenge |
81da2786 | 139 | { |
214dfe16 BA |
140 | this.conn.send(JSON.stringify({code:"acceptchallenge", |
141 | cid:challenge.id, user:me})); | |
142 | this.challenges[index].to.push(me); | |
81da2786 | 143 | } |
81da2786 | 144 | }, |
214dfe16 BA |
145 | // user: last person to accept the challenge |
146 | newGame: function(chall, user) { | |
ab4f4bf2 | 147 | const fen = chall.fen || V.GenRandInitFen(); |
214dfe16 BA |
148 | const game = {}; //TODO: fen, players, time ... |
149 | //setStorage(game); //TODO | |
150 | game.players.forEach(p => { | |
60d9063f BA |
151 | this.conn.send( |
152 | JSON.stringify({code:"newgame", oppid:p.id, game:game})); | |
214dfe16 BA |
153 | }); |
154 | if (this.settings.sound >= 1) | |
155 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
81da2786 | 156 | }, |
a6403027 BA |
157 | }, |
158 | }); |