Almost finished components drafts
[vchess.git] / public / javascripts / components / room.js
CommitLineData
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 5TODO: 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
8fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire)
9main 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
13Vue.component('my-room', {
14 props: ["conn","settings"],
15 data: {
214dfe16
BA
16 gdisplay: "live",
17 liveGames: [],
3ca7a846 18 corrGames: [],
214dfe16
BA
19 players: [], //online players
20 challenges: [], //live challenges
a3ab5fdb 21 },
a6403027
BA
22 // Modal new game, and then sub-components
23 template: `
24 <div>
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">
29 </label>
30 <h3 id="titleFenedit" class="section">
31 {{ translate("Game state (FEN):") }}
32 </h3>
33 <input id="input-fen" type="text"/>
34 <p>TODO: cadence, adversaire (pre-filled if click on name)</p>
214dfe16
BA
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>
a6403027
BA
37 <button @click="newGame">Launch game</button>
38 </div>
39 </div>
3ca7a846
BA
40 <div>
41 <my-chat :conn="conn" :myname="myname" :people="people"></my-chat>
214dfe16
BA
42 <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
43 </my-challenge-list>
3ca7a846 44 </div>
214dfe16 45 <button onClick="doClick('modalNewgame')">New game</button>
3ca7a846 46 <div>
214dfe16
BA
47 <div>
48 <div v-for="p in players" @click="challenge(p)">
49 {{ p.name }}
50 </div>
51 </div>
52 <div class="button-group">
53 <button @click="gdisplay='live'>Live games</button>
54 <button @click="gdisplay='corr'>Correspondance games</button>
55 </div>
56 <my-game-list v-show="display=='live'" :games="liveGames" @show-game="showGame">
57 </my-game-list>
58 <my-game-list v-show="display=='corr'" :games="corrGames" @show-game="showGame">
59 </my-game-list>
3ca7a846 60 </div>
a6403027
BA
61 </div>
62 `,
63 created: function() {
214dfe16 64 // TODO: ask server for current corr games (all but mines: names, ID, time control)
a6403027
BA
65 const socketMessageListener = msg => {
66 const data = JSON.parse(msg.data);
67 switch (data.code)
68 {
214dfe16
BA
69 case "newgame":
70 // TODO: new game just started: data contain all informations
71 // (id, players, time control, fenStart ...)
72 break;
73 // TODO: also receive live games summaries (update)
3ca7a846 74 // (just players names, time control, and ID + player ID)
214dfe16 75 case "acceptchallenge":
a3ab5fdb 76 // oppid: opponent socket ID (or DB id if registered)
214dfe16
BA
77 if (true) //TODO: if challenge is full
78 this.newGame(data.challenge, data.user); //user.id et user.name
79 break;
80 case "withdrawchallenge":
81 // TODO
82 break;
83 case "cancelchallenge":
84 // TODO
85 break;
86 // TODO: distinguish these (dis)connect events from their analogs in game.js
87 case "connect":
88 this.players.push({name:data.name, id:data.uid});
89 break;
90 case "disconnect":
91 const pIdx = this.players.findIndex(p => p.id == data.uid);
92 this.players.splice(pIdx);
81da2786 93 break;
a6403027
BA
94 }
95 };
96 const socketCloseListener = () => {
97 this.conn.addEventListener('message', socketMessageListener);
98 this.conn.addEventListener('close', socketCloseListener);
99 };
100 this.conn.onmessage = socketMessageListener;
101 this.conn.onclose = socketCloseListener;
102 },
103 methods: {
214dfe16
BA
104 showGame: function(game) {
105 let hash = "#game?id=" + game.id;
106 if (!!game.uid)
107 hash += "&uid=" + game.uid;
108 location.hash = hash;
109 },
110 challenge: function(player) {
111 this.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id,
112 user:{name:user.name,id:user.id}));
113 },
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};
118 if (toIdx >= 0)
81da2786 119 {
214dfe16
BA
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);
81da2786 124 }
214dfe16 125 else if (challenge.from.id == user.id) //it's my challenge: cancel it
81da2786 126 {
214dfe16
BA
127 this.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
128 this.challenges.splice(index, 1);
81da2786 129 }
214dfe16 130 else //accept a challenge
81da2786 131 {
214dfe16
BA
132 this.conn.send(JSON.stringify({code:"acceptchallenge",
133 cid:challenge.id, user:me}));
134 this.challenges[index].to.push(me);
81da2786 135 }
81da2786 136 },
214dfe16
BA
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});
144 });
145 if (this.settings.sound >= 1)
146 new Audio("/sounds/newgame.mp3").play().catch(err => {});
81da2786 147 },
a6403027
BA
148 },
149});