fbf02ee189904273792d41bcc9dab3f11caf7237
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"],
21 players: [], //online players
22 challenges: [], //live challenges
23 people: [], //people who connect to this room (or disconnect)
26 // Modal new game, and then sub-components
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">
34 <h3 id="titleFenedit" class="section">
35 {{ translate("Game state (FEN):") }}
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>
45 <my-chat :conn="conn" :myname="user.name" :people="people"></my-chat>
46 <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
49 <button onClick="doClick('modalNewgame')">New game</button>
51 <div style="border:1px solid black">
52 <h3>Online players</h3>
53 <div v-for="p in players" @click="challenge(p)">
57 <div class="button-group">
58 <button @click="gdisplay='live'">Live games</button>
59 <button @click="gdisplay='corr'">Correspondance games</button>
61 <my-game-list v-show="gdisplay=='live'" :games="liveGames"
62 @show-game="showGame">
64 <my-game-list v-show="gdisplay=='corr'" :games="corrGames"
65 @show-game="showGame">
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
);
77 // TODO: new game just started: data contain all informations
78 // (id, players, time control, fenStart ...)
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
87 case "withdrawchallenge":
90 case "cancelchallenge":
93 // TODO: distinguish these (dis)connect events from their analogs in game.js
95 this.players
.push({name:data
.name
, id:data
.uid
});
98 const pIdx
= this.players
.findIndex(p
=> p
.id
== data
.uid
);
99 this.players
.splice(pIdx
);
103 const socketCloseListener
= () => {
104 this.conn
.addEventListener('message', socketMessageListener
);
105 this.conn
.addEventListener('close', socketCloseListener
);
107 this.conn
.onmessage
= socketMessageListener
;
108 this.conn
.onclose
= socketCloseListener
;
111 translate: translate
,
112 showGame: function(game
) {
113 let hash
= "#game?id=" + game
.id
;
115 hash
+= "&uid=" + game
.uid
;
116 location
.hash
= hash
;
118 challenge: function(player
) {
119 this.conn
.send(JSON
.stringify({code:"sendchallenge", oppid:p
.id
,
120 user:{name:user
.name
,id:user
.id
}}));
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
};
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);
133 else if (challenge
.from.id
== user
.id
) //it's my challenge: cancel it
135 this.conn
.send(JSON
.stringify({code:"cancelchallenge", cid:challenge
.id
}));
136 this.challenges
.splice(index
, 1);
138 else //accept a challenge
140 this.conn
.send(JSON
.stringify({code:"acceptchallenge",
141 cid:challenge
.id
, user:me
}));
142 this.challenges
[index
].to
.push(me
);
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
=> {
152 JSON
.stringify({code:"newgame", oppid:p
.id
, game:game
}));
154 if (this.settings
.sound
>= 1)
155 new Audio("/sounds/newgame.mp3").play().catch(err
=> {});