Step toward a one-page application
[vchess.git] / client / client_OLD / javascripts / components / room.js
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
4 /*
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...)
10 */
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"],
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 };
25 },
26 // Modal new game, and then sub-components
27 template: `
28 <div>
29 <input id="modalNewgame" type="checkbox" class="modal"/>
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>
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>
41 <button @click="newGame">Launch game</button>
42 </div>
43 </div>
44 <div>
45 <my-chat :conn="conn" :myname="user.name" :people="people"></my-chat>
46 <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
47 </my-challenge-list>
48 </div>
49 <button onClick="doClick('modalNewgame')">New game</button>
50 <div>
51 <div style="border:1px solid black">
52 <h3>Online players</h3>
53 <div v-for="p in players" @click="challenge(p)">
54 {{ p.name }}
55 </div>
56 </div>
57 <div class="button-group">
58 <button @click="gdisplay='live'">Live games</button>
59 <button @click="gdisplay='corr'">Correspondance games</button>
60 </div>
61 <my-game-list v-show="gdisplay=='live'" :games="liveGames"
62 @show-game="showGame">
63 </my-game-list>
64 <my-game-list v-show="gdisplay=='corr'" :games="corrGames"
65 @show-game="showGame">
66 </my-game-list>
67 </div>
68 </div>
69 `,
70 created: function() {
71 // TODO: ask server for current corr games (all but mines: names, ID, time control)
72 const socketMessageListener = msg => {
73 const data = JSON.parse(msg.data);
74 switch (data.code)
75 {
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)
81 // (just players names, time control, and ID + player ID)
82 case "acceptchallenge":
83 // oppid: opponent socket ID (or DB id if registered)
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);
100 break;
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: {
111 translate: translate,
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,
120 user:{name:user.name,id:user.id}}));
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)
127 {
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);
132 }
133 else if (challenge.from.id == user.id) //it's my challenge: cancel it
134 {
135 this.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
136 this.challenges.splice(index, 1);
137 }
138 else //accept a challenge
139 {
140 this.conn.send(JSON.stringify({code:"acceptchallenge",
141 cid:challenge.id, user:me}));
142 this.challenges[index].to.push(me);
143 }
144 },
145 // user: last person to accept the challenge
146 newGame: function(chall, user) {
147 const fen = chall.fen || V.GenRandInitFen();
148 const game = {}; //TODO: fen, players, time ...
149 //setStorage(game); //TODO
150 game.players.forEach(p => {
151 this.conn.send(
152 JSON.stringify({code:"newgame", oppid:p.id, game:game}));
153 });
154 if (this.settings.sound >= 1)
155 new Audio("/sounds/newgame.mp3").play().catch(err => {});
156 },
157 },
158 });