3 input#modalNewgame.modal(type="checkbox")
4 div(role="dialog" aria-labelledby="titleFenedit")
6 label#closeNewgame.modal-close(for="modalNewgame")
8 label(for="selectVariant") {{ st.tr["Variant"] }}
9 select#selectVariant(v-model="newgameInfo.vid")
10 option(v-for="v in st.variants" :value="v.id") {{ v.name }}
12 label(for="selectNbPlayers") {{ st.tr["Number of players"] }}
13 select#selectNbPlayers(v-model="newgameInfo.nbPlayers")
14 option(v-show="possibleNbplayers(2)" value="2") 2
15 option(v-show="possibleNbplayers(3)" value="3") 3
16 option(v-show="possibleNbplayers(4)" value="4") 4
18 label(for="timeControl") Time control (in days)
20 input(type="number" v-model="newgameInfo.mainTime"
21 placeholder="Main time")
22 input(type="number" v-model="newgameInfo.increment"
23 placeholder="Increment")
25 label(for="selectPlayers") {{ st.tr["Play with?"] }}
27 input(type="text" v-model="newgameInfo.players[0].name")
28 input(v-show="newgameInfo.nbPlayers>=3" type="text"
29 v-model="newgameInfo.players[1].name")
30 input(v-show="newgameInfo.nbPlayers==4" type="text"
31 v-model="newgameInfo.players[2].name")
34 | {{ st.tr["FEN (ignored if players fields are blank)"] }}
35 input#inputFen(type="text" v-model="newgameInfo.fen")
36 button(@click="newGame") Launch game
37 p TODO: cadence, adversaire (pre-filled if click on name)
38 p cadence 2m+12s ou 7d+1d (m,s ou d,d) --> main, increment
39 p Note: leave FEN blank for random; FEN only for targeted challenge
41 ChallengeList(:challenges="challenges" @click-challenge="clickChallenge")
42 div(style="border:1px solid black")
44 div(v-for="p in players" @click="challenge(p)") {{ p.name }}
45 button(onClick="doClick('modalNewgame')") New game
48 button(@click="gdisplay='live'") Live games
49 button(@click="gdisplay='corr'") Correspondance games
50 GameList(v-show="gdisplay=='live'" :games="liveGames"
51 @show-game="showGame")
52 GameList(v-show="gdisplay=='corr'" :games="corrGames"
53 @show-game="showGame")
57 // main playing hall: online players + current challenges + button "new game"
58 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
60 TODO: surligner si nouveau défi perso et pas affichage courant
61 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
62 --> correspondance: stocker sur serveur lastMove + uid + color + movesCount + gameId + variant + timeleft
63 fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire)
65 // TODO: au moins l'échange des coups en P2P ? et game chat ?
66 // TODO: objet game, objet challenge ? et player ?
67 import { store } from "@/store";
68 import { NbPlayers } from "@/data/nbPlayers";
69 import GameList from "@/components/GameList.vue";
70 import ChallengeList from "@/components/ChallengeList.vue";
83 players: [], //online players
84 challenges: [], //live challenges
85 willPlay: [], //IDs of challenges in which I decide to play (>= 3 players)
90 players: [{id:0,name:""},{id:0,name:""},{id:0,name:""}],
97 "st.conn": function() {
98 // TODO: ask server for current corr games (all but mines: names, ID, time control)
99 const socketMessageListener = msg => {
100 const data = JSON.parse(msg.data);
104 // TODO: new game just started: data contain all informations
105 // (id, players, time control, fenStart ...)
107 // TODO: also receive live games summaries (update)
108 // (just players names, time control, and ID + player ID)
109 case "acceptchallenge":
110 // oppid: opponent socket ID (or DB id if registered)
111 if (true) //TODO: if challenge is full
112 this.newGame(data.challenge, data.user); //user.id et user.name
114 case "withdrawchallenge":
117 case "cancelchallenge":
120 // TODO: distinguish these (dis)connect events from their analogs in game.js
122 this.players.push({name:data.name, id:data.uid});
125 const pIdx = this.players.findIndex(p => p.id == data.uid);
126 this.players.splice(pIdx);
130 const socketCloseListener = () => {
131 this.st.conn.addEventListener('message', socketMessageListener);
132 this.st.conn.addEventListener('close', socketCloseListener);
134 this.st.conn.onmessage = socketMessageListener;
135 this.st.conn.onclose = socketCloseListener;
139 showGame: function(game) {
140 // NOTE: if we are an observer, the game will be found in main games list
141 // (sent by connected remote players)
142 this.$router.push("/" + game.id)
144 challenge: function(player) {
145 this.st.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id,
146 user:{name:this.st.user.name,id:this.st.user.id}}));
148 clickChallenge: function(challenge) {
149 const index = this.challenges.findIndex(c => c.id == challenge.id);
150 const toIdx = challenge.to.findIndex(p => p.id == user.id);
151 const me = {name:user.name,id:user.id};
154 // It's a multiplayer challenge I accepted: withdraw
155 this.st.conn.send(JSON.stringify({code:"withdrawchallenge",
156 cid:challenge.id, user:me}));
157 this.challenges.to.splice(toIdx, 1);
159 else if (challenge.from.id == user.id) //it's my challenge: cancel it
161 this.st.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
162 this.challenges.splice(index, 1);
164 else //accept a challenge
166 this.st.conn.send(JSON.stringify({code:"acceptchallenge",
167 cid:challenge.id, user:me}));
168 this.challenges[index].to.push(me);
170 // TODO: accepter un challenge peut lancer une partie, il
171 // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici
175 // annule l'acceptation d'un défi (si >= 3 joueurs)
177 // si pas le mien et FEN speciale :: (charger code variante et)
178 // montrer diagramme + couleur (orienté)
180 // user: last person to accept the challenge
181 newGameLive: function(chall, user) {
182 const fen = chall.fen || V.GenRandInitFen();
183 const game = {}; //TODO: fen, players, time ...
184 //setStorage(game); //TODO
185 game.players.forEach(p => {
187 JSON.stringify({code:"newgame", oppid:p.id, game:game}));
189 if (this.settings.sound >= 1)
190 new Audio("/sounds/newgame.mp3").play().catch(err => {});
192 newGame: function() {
193 const afterRulesAreLoaded = () => {
194 // NOTE: side-effect = set FEN
195 // TODO: (to avoid any cheating option) separate the GenRandInitFen() functions
196 // in separate files, load on server and generate FEN on server.
197 const error = checkChallenge(this.newgameInfo, vname);
200 // Possible (server) error if filled player does not exist
202 "/challenges/" + this.newgameInfo.vid,
206 const chall = Object.assign({},
214 this.challenges.push(chall);
217 // TODO: else, if live game: send infos (socket), and...
219 const idxInVariants =
220 variantArray.findIndex(v => v.id == this.newgameInfo.vid);
221 const vname = variantArray[idxInVariants].name;
222 const scriptId = vname + "RulesScript";
223 if (!document.getElementById(scriptId))
225 // Load variant rules (only once)
226 var script = document.createElement("script");
227 script.id = scriptId;
228 script.onload = afterRulesAreLoaded;
229 //script.addEventListener ("load", afterRulesAreLoaded, false);
230 script.src = "/javascripts/variants/" + vname + ".js";
231 document.body.appendChild(script);
234 afterRulesAreLoaded();
236 possibleNbplayers: function(nbp) {
237 if (this.newgameInfo.vid == 0)
239 const variants = this.st.variants;
240 const idxInVariants =
241 variants.findIndex(v => v.id == this.newgameInfo.vid);
242 return NbPlayers[variants[idxInVariants].name].includes(nbp);