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