3 <Home msg="Welcome to Your Vue.js Apppp"/>
8 // @ is an alias to /src
9 import HelloWorld from "@/components/HelloWorld.vue";
19 // main playing hall: chat + online players + current challenges + button "new game"
20 // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list
21 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
23 TODO: surligner si nouveau défi perso et pas affichage courant
24 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
25 --> correspondance: stocker sur serveur lastMove + uid + color + movesCount + gameId + variant + timeleft
26 fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire)
27 main time should be positive (no 0+2 & cie...)
29 // TODO: au moins l'échange des coups en P2P ?
30 // TODO: objet game, objet challenge ? et player ?
31 Vue.component('my-room', {
32 props: ["conn","settings"],
39 players: [], //online players
40 challenges: [], //live challenges
41 people: [], //people who connect to this room (or disconnect)
44 // Modal new game, and then sub-components
47 <input id="modalNewgame" type="checkbox" class="modal"/>
48 <div role="dialog" aria-labelledby="titleFenedit">
49 <div class="card smallpad">
50 <label id="closeNewgame" for="modalNewgame" class="modal-close">
52 <h3 id="titleFenedit" class="section">
53 {{ translate("Game state (FEN):") }}
55 <input id="input-fen" type="text"/>
56 <p>TODO: cadence, adversaire (pre-filled if click on name)</p>
57 <p>cadence 2m+12s ou 7d+1d (m,s ou d,d) --> main, increment</p>
58 <p>Note: leave FEN blank for random; FEN only for targeted challenge</p>
59 <button @click="newGame">Launch game</button>
63 <my-chat :conn="conn" :myname="user.name" :people="people"></my-chat>
64 <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
67 <button onClick="doClick('modalNewgame')">New game</button>
69 <div style="border:1px solid black">
70 <h3>Online players</h3>
71 <div v-for="p in players" @click="challenge(p)">
75 <div class="button-group">
76 <button @click="gdisplay='live'">Live games</button>
77 <button @click="gdisplay='corr'">Correspondance games</button>
79 <my-game-list v-show="gdisplay=='live'" :games="liveGames"
80 @show-game="showGame">
82 <my-game-list v-show="gdisplay=='corr'" :games="corrGames"
83 @show-game="showGame">
89 // TODO: ask server for current corr games (all but mines: names, ID, time control)
90 const socketMessageListener = msg => {
91 const data = JSON.parse(msg.data);
95 // TODO: new game just started: data contain all informations
96 // (id, players, time control, fenStart ...)
98 // TODO: also receive live games summaries (update)
99 // (just players names, time control, and ID + player ID)
100 case "acceptchallenge":
101 // oppid: opponent socket ID (or DB id if registered)
102 if (true) //TODO: if challenge is full
103 this.newGame(data.challenge, data.user); //user.id et user.name
105 case "withdrawchallenge":
108 case "cancelchallenge":
111 // TODO: distinguish these (dis)connect events from their analogs in game.js
113 this.players.push({name:data.name, id:data.uid});
116 const pIdx = this.players.findIndex(p => p.id == data.uid);
117 this.players.splice(pIdx);
121 const socketCloseListener = () => {
122 this.conn.addEventListener('message', socketMessageListener);
123 this.conn.addEventListener('close', socketCloseListener);
125 this.conn.onmessage = socketMessageListener;
126 this.conn.onclose = socketCloseListener;
129 translate: translate,
130 showGame: function(game) {
131 let hash = "#game?id=" + game.id;
133 hash += "&uid=" + game.uid;
134 location.hash = hash;
136 challenge: function(player) {
137 this.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id,
138 user:{name:user.name,id:user.id}}));
140 clickChallenge: function(challenge) {
141 const index = this.challenges.findIndex(c => c.id == challenge.id);
142 const toIdx = challenge.to.findIndex(p => p.id == user.id);
143 const me = {name:user.name,id:user.id};
146 // It's a multiplayer challenge I accepted: withdraw
147 this.conn.send(JSON.stringify({code:"withdrawchallenge",
148 cid:challenge.id, user:me}));
149 this.challenges.to.splice(toIdx, 1);
151 else if (challenge.from.id == user.id) //it's my challenge: cancel it
153 this.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
154 this.challenges.splice(index, 1);
156 else //accept a challenge
158 this.conn.send(JSON.stringify({code:"acceptchallenge",
159 cid:challenge.id, user:me}));
160 this.challenges[index].to.push(me);
163 // user: last person to accept the challenge
164 newGame: function(chall, user) {
165 const fen = chall.fen || V.GenRandInitFen();
166 const game = {}; //TODO: fen, players, time ...
167 //setStorage(game); //TODO
168 game.players.forEach(p => {
170 JSON.stringify({code:"newgame", oppid:p.id, game:game}));
172 if (this.settings.sound >= 1)
173 new Audio("/sounds/newgame.mp3").play().catch(err => {});