1 <!-- Main playing hall: online players + current challenges + button "new game" -->
5 input#modalNewgame.modal(type="checkbox")
6 div(role="dialog" aria-labelledby="titleFenedit")
8 label#closeNewgame.modal-close(for="modalNewgame")
10 label(for="selectVariant") {{ st.tr["Variant"] }}
11 select#selectVariant(v-model="newchallenge.vid")
12 option(v-for="v in st.variants" :value="v.id") {{ v.name }}
14 label(for="selectNbPlayers") {{ st.tr["Number of players"] }}
15 select#selectNbPlayers(v-model="newchallenge.nbPlayers")
16 option(v-show="possibleNbplayers(2)" value="2") 2
17 option(v-show="possibleNbplayers(3)" value="3") 3
18 option(v-show="possibleNbplayers(4)" value="4") 4
20 label(for="timeControl") {{ st.tr["Time control"] }}
21 input#timeControl(type="text" v-model="newchallenge.timeControl"
22 placeholder="3m+2s, 1h+30s, 7d+1d ...")
23 fieldset(v-if="st.user.id > 0")
24 label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }}
26 input(type="text" v-model="newchallenge.to[0].name")
27 input(v-show="newchallenge.nbPlayers>=3" type="text"
28 v-model="newchallenge.to[1].name")
29 input(v-show="newchallenge.nbPlayers==4" type="text"
30 v-model="newchallenge.to[2].name")
31 fieldset(v-if="st.user.id > 0")
32 label(for="inputFen") {{ st.tr["FEN (optional)"] }}
33 input#inputFen(type="text" v-model="newchallenge.fen")
34 button(@click="newChallenge") {{ st.tr["Send challenge"] }}
36 .col-sm-12.col-md-5.col-md-offset-1.col-lg-4.col-lg-offset-2
38 button(@click="cpdisplay='challenges'") Challenges
39 button(@click="cpdisplay='players'") Players
40 ChallengeList(v-show="cpdisplay=='challenges'"
41 :challenges="challenges" @click-challenge="clickChallenge")
42 #players(v-show="cpdisplay=='players'")
44 div(v-for="p in uniquePlayers" @click="tryChallenge(p)")
45 | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }}
47 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
48 button(onClick="doClick('modalNewgame')") New game
50 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
52 button(@click="gdisplay='live'") Live games
53 button(@click="gdisplay='corr'") Correspondance games
54 GameList(v-show="gdisplay=='live'" :games="liveGames"
55 @show-game="showGame")
56 GameList(v-show="gdisplay=='corr'" :games="corrGames"
57 @show-game="showGame")
61 import { store } from "@/store";
62 import { NbPlayers } from "@/data/nbPlayers";
63 import { checkChallenge } from "@/data/challengeCheck";
64 import { ArrayFun } from "@/utils/array";
65 import { ajax } from "@/utils/ajax";
66 import GameList from "@/components/GameList.vue";
67 import ChallengeList from "@/components/ChallengeList.vue";
77 cpdisplay: "challenges",
82 players: [], //online players
87 // NOTE: id (server DB) and sid (socket ID).
88 // Anonymous players just have a socket ID.
90 {id:0, sid:"", name:""},
91 {id:0, sid:"", name:""},
92 {id:0, sid:"", name:""}
99 uniquePlayers: function() {
100 // Show e.g. "5 @nonymous", and do nothing on click on anonymous
101 let playerList = [{id:0, name:"@nonymous", count:0}];
102 this.players.forEach(p => {
106 playerList[0].count++;
111 // TODO: this looks ugly... (use VueX ?!)
113 "st.conn": function() {
114 this.st.conn.onmessage = this.socketMessageListener;
115 this.st.conn.onclose = this.socketCloseListener;
118 created: function() {
119 // TODO: ask server for current corr games (all but mines: names, ID, time control)
120 // also ask for corr challenges
121 // TODO: add myself to players
122 // --> when sending something, send to all players but NOT me !
125 this.st.conn.onmessage = this.socketMessageListener;
126 this.st.conn.onclose = this.socketCloseListener;
128 this.players.push(this.st.user);
131 socketMessageListener: function(msg) {
132 const data = JSON.parse(msg.data);
135 // * - receive "new game": if live, store locally + redirect to game
136 // * If corr: notify "new game has started", give link, but do not redirect
138 // TODO: new game just started: data contain all informations
139 // (id, players, time control, fenStart ...)
140 // + cid to remove challenge from list
142 // * - receive "playergame": a live game by some connected player (NO corr)
144 // TODO: receive live game summary (update, count moves)
145 // (just players names, time control, and ID + player ID)
147 // * - receive "playerchallenges": list of challenges (sent) by some online player (NO corr)
148 case "playerchallenges":
149 // TODO: receive challenge + challenge updates
151 case "newmove": //live or corr
152 // TODO: name conflict ? (game "newmove" event)
154 // * - receive new challenge: if targeted, replace our name with sender name
156 // receive live or corr challenge
158 // * - receive "accept/withdraw/cancel challenge": apply action to challenges list
159 case "acceptchallenge":
160 if (true) //TODO: if challenge is full
161 this.newGame(data.challenge, data.user); //user.id et user.name
163 case "withdrawchallenge":
164 const cIdx = this.challenges.findIndex(c => c.id == data.cid);
165 let chall = this.challenges[cIdx]
166 ArrayFun.remove(chall.players, p => p.id == data.uid);
167 chall.players.push({id:0, name:""});
169 case "cancelchallenge":
170 ArrayFun.remove(this.challenges, c => c.id == data.cid);
172 // NOTE: finally only one connect / disconnect couple of events
173 // (because on server side we wouldn't know which to choose)
175 // * - receive "player connect": send all our current challenges (to him or global)
176 // * Also send all our games (live - max 1 - and corr) [in web worker ?]
177 // * + all our sent challenges.
178 this.players.push({name:data.name, id:data.uid});
179 // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté
180 // envoyer aussi nos défis
182 // * - receive "player disconnect": remove from players list
184 ArrayFun.remove(this.players, p => p.id == data.uid);
185 // TODO: also remove all challenges sent by this player,
186 // and all live games where he plays and no other opponent is online
190 socketCloseListener: function() {
191 // connexion is reinitialized in store.js
192 this.st.conn.addEventListener('message', this.socketMessageListener);
193 this.st.conn.addEventListener('close', this.socketCloseListener);
195 showGame: function(game) {
196 // NOTE: if we are an observer, the game will be found in main games list
197 // (sent by connected remote players)
198 // TODO: game path ? /vname/gameId seems better
199 this.$router.push("/" + game.id)
201 tryChallenge: function(player) {
203 return; //anonymous players cannot be challenged
204 this.newchallenge.players[0] = {
209 doClick("modalNewgame");
211 // * - accept challenge (corr or live) --> send info to all concerned players
212 // * - cancel challenge (click on sent challenge) --> send info to all concerned players
213 // * - withdraw from challenge (if >= 3 players and previously accepted)
214 // * --> send info to all concerned players
215 // * - refuse challenge (or receive refusal): send to all challenge players (from + to)
216 // * except us ; graphics: modal again ? (inline ?)
217 // * - prepare and start new game (if challenge is full after acceptation)
218 // * --> include challenge ID (so that opponents can delete the challenge too)
219 // * Also send to all connected players (only from me)
220 clickChallenge: function(challenge) {
221 const index = this.challenges.findIndex(c => c.id == challenge.id);
222 const toIdx = challenge.to.findIndex(p => p.id == user.id);
223 const me = {name:user.name,id:user.id};
226 // It's a multiplayer challenge I accepted: withdraw
227 this.st.conn.send(JSON.stringify({code:"withdrawchallenge",
228 cid:challenge.id, user:me}));
229 this.challenges.to.splice(toIdx, 1);
231 else if (challenge.from.id == user.id) //it's my challenge: cancel it
233 this.st.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id}));
234 this.challenges.splice(index, 1);
236 else //accept a challenge
238 this.st.conn.send(JSON.stringify({code:"acceptchallenge",
239 cid:challenge.id, user:me}));
240 this.challenges[index].to.push(me);
242 // TODO: accepter un challenge peut lancer une partie, il
243 // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici
244 // si pas le mien et FEN speciale :: (charger code variante et)
245 // montrer diagramme + couleur (orienté)
247 // user: last person to accept the challenge (TODO: revoir ça)
248 // newGame: function(chall, user) {
249 // const fen = chall.fen || V.GenRandInitFen();
250 // const game = {}; //TODO: fen, players, time ...
251 // //setStorage(game); //TODO
252 // game.players.forEach(p => { //...even if game is by corr (could be played live, why not...)
254 // JSON.stringify({code:"newgame", oppid:p.id, game:game}));
256 // if (this.settings.sound >= 1)
257 // new Audio("/sounds/newgame.mp3").play().catch(err => {});
259 // Send new challenge (corr or live, cf. time control), with button or click on player
260 newChallenge: async function() {
261 const idxInVariants =
262 this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
263 const vname = this.st.variants[idxInVariants].name;
264 const vModule = await import("@/variants/" + vname + ".js");
265 window.V = vModule.VariantRules;
266 // checkChallenge side-effect = set FEN, and mainTime + increment in seconds
267 const error = checkChallenge(this.newchallenge);
270 // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
272 this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60;
273 // Check that the players (if any indicated) are online
274 for (let p of this.newchallenge.to)
278 const pIdx = this.players.findIndex(pl => pl.name == p.name);
280 return alert(p.name + " is not connected");
281 p.id = this.players[pIdx].id;
282 p.sid = this.players[pIdx].sid;
285 // TODO: clarify challenge format (too many fields for now :/ )
286 const finishAddChallenge = (cid) => {
287 const chall = Object.assign(
297 this.challenges.push(chall);
298 // Send challenge to peers
299 const chall = JSON.stringify({
300 code: "newchallenge",
301 sender: {name:this.st.user.name, id:this.st.user.id, sid:this.st.user.sid},
303 if (this.newchallenge.to[0].id > 0)
305 // Challenge with targeted players
306 this.newchallenge.to.forEach(p => {
308 this.st.conn.send(Object.assign({}, chall, {receiver: p.sid}));
313 // Open challenge: send to all connected players
314 this.players.forEach(p => { this.st.conn.send(chall); });
316 document.getElementById("modalNewgame").checked = false;
320 // Live challenges have cid = 0
321 finishAddChallenge(0);
325 // Correspondance game: send challenge to server
327 "/challenges/" + this.newchallenge.vid,
330 response => { finishAddChallenge(cid); }
334 possibleNbplayers: function(nbp) {
335 if (this.newchallenge.vid == 0)
337 const variants = this.st.variants;
338 const idxInVariants =
339 variants.findIndex(v => v.id == this.newchallenge.vid);
340 return NbPlayers[variants[idxInVariants].name].includes(nbp);