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="newchallenge.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="newchallenge.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 (e.g. 3m, 1h+30s, 7d+1d)
19 input#timeControl(type="text" v-model="newchallenge.timeControl"
20 placeholder="Time control")
22 label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }}
24 input(type="text" v-model="newchallenge.players[0].name")
25 input(v-show="newchallenge.nbPlayers>=3" type="text"
26 v-model="newchallenge.players[1].name")
27 input(v-show="newchallenge.nbPlayers==4" type="text"
28 v-model="newchallenge.players[2].name")
30 label(for="inputFen") {{ st.tr["FEN (optional)"] }}
31 input#inputFen(type="text" v-model="newchallenge.fen")
32 button(@click="newChallenge") Send challenge
34 .col-sm-12.col-md-5.col-md-offset-1.col-lg-4.col-lg-offset-2
35 ChallengeList(:challenges="challenges" @click-challenge="clickChallenge")
36 .col-sm-12.col-md-5.col-lg-4
39 div(v-for="p in players" @click="challenge(p)") {{ p.name }}
41 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
42 button(onClick="doClick('modalNewgame')") New game
44 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
46 button(@click="gdisplay='live'") Live games
47 button(@click="gdisplay='corr'") Correspondance games
48 GameList(v-show="gdisplay=='live'" :games="liveGames"
49 @show-game="showGame")
50 GameList(v-show="gdisplay=='corr'" :games="corrGames"
51 @show-game="showGame")
55 // TODO: blank time control == untimed
56 // main playing hall: online players + current challenges + button "new game"
57 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
59 TODO: surligner si nouveau défi perso et pas affichage courant
60 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
61 --> correspondance: stocker sur serveur lastMove + uid + color + movesCount + gameId + variant + timeleft
62 fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire)
64 // TODO: au moins l'échange des coups en P2P ? et game chat ?
65 // TODO: objet game, objet challenge ? et player ?
68 * - send new challenge (corr or live, cf. time control), with button or click on player
69 * - accept challenge (corr or live) --> send info to all concerned players
70 * - cancel challenge (click on sent challenge) --> send info to all concerned players
71 * - withdraw from challenge (if >= 3 players and previously accepted)
72 * --> send info to all concerned players
73 * - prepare and start new game (if challenge is full after acceptation)
74 * Also send to all connected players (only from me)
75 * - receive "player connect": send all our current challenges (to him or global)
76 * Also send all our games (live - max 1 - and corr) [in web worker ?]
77 * + all our sent challenges.
78 * - receive "playergames": list of games by some connected player (NO corr)
79 * - receive "playerchallenges": list of challenges (sent) by some online player (NO corr)
80 * - receive "player disconnect": remove from players list
81 * - receive "accept/withdraw/cancel challenge": apply action to challenges list
82 * - receive "new game": if live, store locally + redirect to game
83 * If corr: notify "new game has started", give link, but do not redirect
85 import { store } from "@/store";
86 import { NbPlayers } from "@/data/nbPlayers";
87 import { checkChallenge } from "@/data/challengeCheck";
88 import { ArrayFun } from "@/utils/array";
89 import GameList from "@/components/GameList.vue";
90 import ChallengeList from "@/components/ChallengeList.vue";
103 players: [], //online players
104 challenges: [], //live challenges
105 willPlay: [], //IDs of challenges in which I decide to play (>= 3 players)
110 // TODO: distinguer uid et sid !
111 players: [{id:0,name:""},{id:0,name:""},{id:0,name:""}],
117 "st.conn": function() {
118 this.st.conn.onmessage = this.socketMessageListener;
119 this.st.conn.onclose = this.socketCloseListener;
122 created: function() {
123 // TODO: ask server for current corr games (all but mines: names, ID, time control)
126 this.st.conn.onmessage = this.socketMessageListener;
127 this.st.conn.onclose = this.socketCloseListener;
131 socketMessageListener: function(msg) {
132 const data = JSON.parse(msg.data);
136 // TODO: new game just started: data contain all informations
137 // (id, players, time control, fenStart ...)
139 // TODO: also receive live games summaries (update)
140 // (just players names, time control, and ID + player ID)
141 case "acceptchallenge":
142 if (true) //TODO: if challenge is full
143 this.newGame(data.challenge, data.user); //user.id et user.name
145 case "withdrawchallenge":
146 const cIdx = this.challenges.findIndex(c => c.id == data.cid);
147 let chall = this.challenges[cIdx]
148 ArrayFun.remove(chall.players, p => p.id == data.uid);
149 chall.players.push({id:0, name:""});
151 case "cancelchallenge":
152 ArrayFun.remove(this.challenges, c => c.id == data.cid);
155 this.players.push({name:data.name, id:data.uid});
157 case "halldisconnect":
158 ArrayFun.remove(this.players, p => p.id == data.uid);
162 socketCloseListener: function() {
163 this.st.conn.addEventListener('message', socketMessageListener);
164 this.st.conn.addEventListener('close', socketCloseListener);
166 clickPlayer: function() {
167 //this.newgameInfo.players[0].name = clickPlayer.name;
170 showGame: function(game) {
171 // NOTE: if we are an observer, the game will be found in main games list
172 // (sent by connected remote players)
173 this.$router.push("/" + game.id)
175 challenge: function(player) {
177 clickChallenge: function(challenge) {
178 const index = this.challenges.findIndex(c => c.id == challenge.id);
179 const toIdx = challenge.to.findIndex(p => p.id == user.id);
180 const me = {name:user.name,id:user.id};
183 // It's a multiplayer challenge I accepted: withdraw
184 this.st.conn.send(JSON.stringify({code:"withdrawchallenge",
185 cid:challenge.id, user:me}));
186 this.challenges.to.splice(toIdx, 1);
188 else if (challenge.from.id == user.id) //it's my challenge: cancel it
190 this.st.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
191 this.challenges.splice(index, 1);
193 else //accept a challenge
195 this.st.conn.send(JSON.stringify({code:"acceptchallenge",
196 cid:challenge.id, user:me}));
197 this.challenges[index].to.push(me);
199 // TODO: accepter un challenge peut lancer une partie, il
200 // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici
201 // si pas le mien et FEN speciale :: (charger code variante et)
202 // montrer diagramme + couleur (orienté)
204 // user: last person to accept the challenge
205 newGame: function(chall, user) {
206 const fen = chall.fen || V.GenRandInitFen();
207 const game = {}; //TODO: fen, players, time ...
208 //setStorage(game); //TODO
209 game.players.forEach(p => { //...even if game is by corr (could be played live, why not...)
211 JSON.stringify({code:"newgame", oppid:p.id, game:game}));
213 if (this.settings.sound >= 1)
214 new Audio("/sounds/newgame.mp3").play().catch(err => {});
216 newChallenge: async function() {
217 const idxInVariants =
218 this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
219 const vname = variants[idxInVariants].name;
220 const vModule = await import("@/variants/" + vname + ".js");
221 window.V = vModule.VariantRules;
222 // NOTE: side-effect = set FEN, and mainTime + increment in seconds
223 // TODO: (to limit cheating options) separate the GenRandInitFen() functions
224 // in separate files, load on server and generate FEN on server.
225 const error = checkChallenge(this.newchallenge);
228 // TODO: 40 = average number of moves ?
229 if (this.newchallenge.mainTime + 40 * this.newchallenge.increment
230 >= 3*24*60*60) //3 days (TODO: heuristic...)
232 // Correspondance game:
233 // Possible (server) error if filled player does not exist
235 "/challenges/" + this.newchallenge.vid,
239 const chall = Object.assign({},
243 uid: this.st.user.id,
247 this.challenges.push(chall);
248 document.getElementById("modalNewgame").checked = false;
254 // Considered live game
255 if (this.newchallenges.players[0].id > 0)
257 // Challenge with target players
258 this.newchallenges.players.forEach(p => {
259 this.st.conn.send(JSON.stringify({
260 code: "sendchallenge",
262 user: {name:this.st.user.name, id:this.st.user.id}
268 // Open challenge: send to all connected players
273 possibleNbplayers: function(nbp) {
274 if (this.newchallenge.vid == 0)
276 const variants = this.st.variants;
277 const idxInVariants =
278 variants.findIndex(v => v.id == this.newchallenge.vid);
279 return NbPlayers[variants[idxInVariants].name].includes(nbp);