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 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 my-challenge-list(: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 my-game-list(v-show="gdisplay=='live'" :games="liveGames"
51 @show-game="showGame")
52 my-game-list(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 GameList from "@/components/GameList.vue";
69 import ChallengeList from "@/components/ChallengeList.vue";
82 players: [], //online players
83 challenges: [], //live challenges
84 willPlay: [], //IDs of challenges in which I decide to play (>= 3 players)
89 players: [{id:0,name:""},{id:0,name:""},{id:0,name:""}],
96 // TODO: ask server for current corr games (all but mines: names, ID, time control)
97 const socketMessageListener = msg => {
98 const data = JSON.parse(msg.data);
102 // TODO: new game just started: data contain all informations
103 // (id, players, time control, fenStart ...)
105 // TODO: also receive live games summaries (update)
106 // (just players names, time control, and ID + player ID)
107 case "acceptchallenge":
108 // oppid: opponent socket ID (or DB id if registered)
109 if (true) //TODO: if challenge is full
110 this.newGame(data.challenge, data.user); //user.id et user.name
112 case "withdrawchallenge":
115 case "cancelchallenge":
118 // TODO: distinguish these (dis)connect events from their analogs in game.js
120 this.players.push({name:data.name, id:data.uid});
123 const pIdx = this.players.findIndex(p => p.id == data.uid);
124 this.players.splice(pIdx);
128 const socketCloseListener = () => {
129 this.st.conn.addEventListener('message', socketMessageListener);
130 this.st.conn.addEventListener('close', socketCloseListener);
132 this.st.conn.onmessage = socketMessageListener;
133 this.st.conn.onclose = socketCloseListener;
136 showGame: function(game) {
137 // NOTE: if we are an observer, the game will be found in main games list
138 // (sent by connected remote players)
139 this.$router.push("/" + game.id)
141 challenge: function(player) {
142 this.st.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id,
143 user:{name:this.st.user.name,id:this.st.user.id}}));
145 clickChallenge: function(challenge) {
146 const index = this.challenges.findIndex(c => c.id == challenge.id);
147 const toIdx = challenge.to.findIndex(p => p.id == user.id);
148 const me = {name:user.name,id:user.id};
151 // It's a multiplayer challenge I accepted: withdraw
152 this.st.conn.send(JSON.stringify({code:"withdrawchallenge",
153 cid:challenge.id, user:me}));
154 this.challenges.to.splice(toIdx, 1);
156 else if (challenge.from.id == user.id) //it's my challenge: cancel it
158 this.st.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
159 this.challenges.splice(index, 1);
161 else //accept a challenge
163 this.st.conn.send(JSON.stringify({code:"acceptchallenge",
164 cid:challenge.id, user:me}));
165 this.challenges[index].to.push(me);
167 // TODO: accepter un challenge peut lancer une partie, il
168 // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici
172 // annule l'acceptation d'un défi (si >= 3 joueurs)
174 // si pas le mien et FEN speciale :: (charger code variante et)
175 // montrer diagramme + couleur (orienté)
177 // user: last person to accept the challenge
178 newGameLive: function(chall, user) {
179 const fen = chall.fen || V.GenRandInitFen();
180 const game = {}; //TODO: fen, players, time ...
181 //setStorage(game); //TODO
182 game.players.forEach(p => {
184 JSON.stringify({code:"newgame", oppid:p.id, game:game}));
186 if (this.settings.sound >= 1)
187 new Audio("/sounds/newgame.mp3").play().catch(err => {});
189 newGame: function() {
190 const afterRulesAreLoaded = () => {
191 // NOTE: side-effect = set FEN
192 // TODO: (to avoid any cheating option) separate the GenRandInitFen() functions
193 // in separate files, load on server and generate FEN on server.
194 const error = checkChallenge(this.newgameInfo, vname);
197 // Possible (server) error if filled player does not exist
199 "/challenges/" + this.newgameInfo.vid,
203 const chall = Object.assign({},
211 this.challenges.push(response.challenge);
214 // TODO: else, if live game: send infos (socket), and...
216 const idxInVariants =
217 variantArray.findIndex(v => v.id == this.newgameInfo.vid);
218 const vname = variantArray[idxInVariants].name;
219 const scriptId = vname + "RulesScript";
220 if (!document.getElementById(scriptId))
222 // Load variant rules (only once)
223 var script = document.createElement("script");
224 script.id = scriptId;
225 script.onload = afterRulesAreLoaded;
226 //script.addEventListener ("load", afterRulesAreLoaded, false);
227 script.src = "/javascripts/variants/" + vname + ".js";
228 document.body.appendChild(script);
231 afterRulesAreLoaded();
233 possibleNbplayers: function(nbp) {
234 if (this.newgameInfo.vid == 0)
236 const idxInVariants =
237 variantArray.findIndex(v => v.id == this.newgameInfo.vid);
238 return NbPlayers[variantArray[idxInVariants].name].includes(nbp);