47370fb28c30c6ffaa9968cefb7390ec97f1ffd5
1 // main playing hall: chat + online players + current challenges + button "new game"
2 // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list
3 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
5 TODO: surligner si nouveau défi perso et pas affichage courant
6 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
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...)
11 // TODO: au moins l'échange des coups en P2P ?
12 // TODO: objet game, objet challenge ? et player ?
13 Vue
.component('my-room', {
14 props: ["conn","settings"],
19 players: [], //online players
20 challenges: [], //live challenges
22 // Modal new game, and then sub-components
25 <input id="modalNewgame" type="checkbox" class"="modal"/>
26 <div role="dialog" aria-labelledby="titleFenedit">
27 <div class="card smallpad">
28 <label id="closeNewgame" for="modalNewgame" class="modal-close">
30 <h3 id="titleFenedit" class="section">
31 {{ translate("Game state (FEN):") }}
33 <input id="input-fen" type="text"/>
34 <p>TODO: cadence, adversaire (pre-filled if click on name)</p>
35 <p>cadence 2m+12s ou 7d+1d (m,s ou d,d) --> main, increment</p>
36 <p>Note: leave FEN blank for random; FEN only for targeted challenge</p>
37 <button @click="newGame">Launch game</button>
41 <my-chat :conn="conn" :myname="myname" :people="people"></my-chat>
42 <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
45 <button onClick="doClick('modalNewgame')">New game</button>
48 <div v-for="p in players" @click="challenge(p)">
52 <div class="button-group">
53 <button @click="gdisplay='live'>Live games</button>
54 <button @click="gdisplay='corr'>Correspondance games</button>
56 <my-game-list v-show="display=='live'" :games="liveGames" @show-game="showGame">
58 <my-game-list v-show="display=='corr'" :games="corrGames" @show-game="showGame">
64 // TODO: ask server for current corr games (all but mines: names, ID, time control)
65 const socketMessageListener
= msg
=> {
66 const data
= JSON
.parse(msg
.data
);
70 // TODO: new game just started: data contain all informations
71 // (id, players, time control, fenStart ...)
73 // TODO: also receive live games summaries (update)
74 // (just players names, time control, and ID + player ID)
75 case "acceptchallenge":
76 // oppid: opponent socket ID (or DB id if registered)
77 if (true) //TODO: if challenge is full
78 this.newGame(data
.challenge
, data
.user
); //user.id et user.name
80 case "withdrawchallenge":
83 case "cancelchallenge":
86 // TODO: distinguish these (dis)connect events from their analogs in game.js
88 this.players
.push({name:data
.name
, id:data
.uid
});
91 const pIdx
= this.players
.findIndex(p
=> p
.id
== data
.uid
);
92 this.players
.splice(pIdx
);
96 const socketCloseListener
= () => {
97 this.conn
.addEventListener('message', socketMessageListener
);
98 this.conn
.addEventListener('close', socketCloseListener
);
100 this.conn
.onmessage
= socketMessageListener
;
101 this.conn
.onclose
= socketCloseListener
;
104 showGame: function(game
) {
105 let hash
= "#game?id=" + game
.id
;
107 hash
+= "&uid=" + game
.uid
;
108 location
.hash
= hash
;
110 challenge: function(player
) {
111 this.conn
.send(JSON
.stringify({code:"sendchallenge", oppid:p
.id
,
112 user:{name:user
.name
,id:user
.id
}));
114 clickChallenge: function(challenge
) {
115 const index
= this.challenges
.findIndex(c
=> c
.id
== challenge
.id
);
116 const toIdx
= challenge
.to
.findIndex(p
=> p
.id
== user
.id
);
117 const me
= {name:user
.name
,id:user
.id
};
120 // It's a multiplayer challenge I accepted: withdraw
121 this.conn
.send(JSON
.stringify({code:"withdrawchallenge",
122 cid:challenge
.id
, user:me
}));
123 this.challenges
.to
.splice(toIdx
, 1);
125 else if (challenge
.from.id
== user
.id
) //it's my challenge: cancel it
127 this.conn
.send(JSON
.stringify({code:"cancelchallenge", cid:challenge
.id
}));
128 this.challenges
.splice(index
, 1);
130 else //accept a challenge
132 this.conn
.send(JSON
.stringify({code:"acceptchallenge",
133 cid:challenge
.id
, user:me
}));
134 this.challenges
[index
].to
.push(me
);
137 // user: last person to accept the challenge
138 newGame: function(chall
, user
) {
139 const fen
= chall
.fen
|| VariantRules
.GenRandInitFen();
140 const game
= {}; //TODO: fen, players, time ...
141 //setStorage(game); //TODO
142 game
.players
.forEach(p
=> {
143 this.conn
.send(JSON
.stringify({code:"newgame", oppid:p
.id
, game:game
});
145 if (this.settings
.sound
>= 1)
146 new Audio("/sounds/newgame.mp3").play().catch(err
=> {});