+ GameList,
+ ChallengeList,
+ },
+ data: function () {
+ return {
+ st: store.state,
+ cdisplay: "live", //or corr
+ pdisplay: "players", //or chat
+ gdisplay: "live",
+ games: [],
+ challenges: [],
+ players: [], //online players (rename into "people" ?)
+ newchallenge: {
+ fen: "",
+ vid: 0,
+ nbPlayers: 0,
+ to: ["", "", ""], //name of challenged players
+ timeControl: "", //"2m+2s" ...etc
+ },
+ };
+ },
+ computed: {
+ uniquePlayers: function() {
+ // Show e.g. "@nonymous (5)", and do nothing on click on anonymous
+ let anonymous = {id:0, name:"@nonymous", count:0};
+ let playerList = [];
+ this.players.forEach(p => {
+ if (p.id > 0)
+ playerList.push(p);
+ else
+ anonymous.count++;
+ });
+ if (anonymous.count > 0)
+ playerList.push(anonymous);
+ return playerList;
+ },
+ },
+ created: function() {
+ // Always add myself to players' list
+ this.players.push(this.st.user);
+ // Ask server for current corr games (all but mines)
+// ajax(
+// "",
+// "GET",
+// response => {
+//
+// }
+// );
+// // Also ask for corr challenges (all)
+// ajax(
+// "",
+// "GET",
+// response => {
+//
+// }
+// );
+ // 0.1] Ask server for for room composition:
+ const socketOpenListener = () => {
+ this.st.conn.send(JSON.stringify({code:"pollclients"}));
+ };
+ this.st.conn.onopen = socketOpenListener;
+ // TODO: is this required here?
+ this.oldOnmessage = this.st.conn.onmessage || Function.prototype;
+ this.st.conn.onmessage = this.socketMessageListener;
+ const oldOnclose = this.st.conn.onclose;
+ const socketCloseListener = () => {
+ oldOnclose(); //reinitialize connexion (in store.js)
+ this.st.conn.addEventListener('message', this.socketMessageListener);
+ this.st.conn.addEventListener('close', socketCloseListener);
+ };
+ this.st.conn.onclose = socketCloseListener;
+ },
+ methods: {
+ filterChallenges: function(type) {
+ return this.challenges.filter(c => c.type == type);
+ },
+ filterGames: function(type) {
+ return this.games.filter(c => c.type == type);
+ },
+ classifyChallenge: function(c) {
+ // Heuristic: should work for most cases... (TODO)
+ return (c.timeControl.indexOf('d') === -1 ? "live" : "corr");
+ },
+ socketMessageListener: function(msg) {
+ // Save and call current st.conn.onmessage if one was already defined
+ // --> also needed in future Game.vue (also in Chat.vue component)
+ this.oldOnmessage(msg);
+ const data = JSON.parse(msg.data);
+ switch (data.code)
+ {
+ // 0.2] Receive clients list (just socket IDs)
+ case "pollclients":
+ {
+ data.sockIds.forEach(sid => {
+ this.players.push({sid:sid, id:0, name:""});
+ // Ask identity, challenges and game(s)
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askchallenges", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askgame", target:sid}));
+ });
+ break;
+ }
+ case "askidentity":
+ {
+ // Request for identification: reply if I'm not anonymous
+ if (this.st.user.id > 0)
+ {
+ this.st.conn.send(JSON.stringify(
+ {code:"identity", user:this.st.user, target:data.from}));
+ }
+ break;
+ }
+ case "askchallenge":
+ {
+ // Send my current live challenge (if any)
+ const cIdx = this.challenges
+ .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live");
+ if (cIdx >= 0)
+ {
+ const c = this.challenges[cIdx];
+ const myChallenge =
+ {
+ // Minimal challenge informations: (from not required)
+ to: c.to,
+ fen: c.fen,
+ vid: c.vid,
+ timeControl: c.timeControl
+ };
+ this.st.conn.send(JSON.stringify({code:"challenge",
+ challenge:myChallenge, target:data.from}));
+ }
+ break;
+ }
+ case "askgame":
+ {
+ // TODO: Send my current live game (if any): variant, players, movesCount
+ break;
+ }
+ case "identity":
+ {
+ const pIdx = this.players.findIndex(p => p.sid == data.user.sid);
+ this.players[pIdx].id = data.user.id;
+ this.players[pIdx].name = data.user.name;
+ break;
+ }
+ case "challenge":
+ {
+ // Receive challenge from some player (+sid)
+ let newChall = data.chall;
+ newChall.type = this.classifyChallenge(data.chall);
+ const pIdx = this.players.findIndex(p => p.sid == data.from);
+ newChall.from = this.players[pIdx]; //may be anonymous
+ newChall.added = Date.now();
+ newChall.vname = this.getVname(newChall.vid);
+ this.challenges.push(newChall);
+ break;
+ }
+ case "game":
+ {
+ // Receive game from some player (+sid)
+ // TODO: receive game summary (update, count moves)
+ // (just players names, time control, and ID + player ID)
+ // NOTE: it may be correspondance (if newgame while we are connected)
+ break;
+ }
+// * - receive "new game": if live, store locally + redirect to game
+// * If corr: notify "new game has started", give link, but do not redirect
+ case "newgame":
+ {
+ // TODO: new game just started: data contain all informations
+ // (id, players, time control, fenStart ...)
+ // + cid to remove challenge from list
+ break;
+ }
+// * - receive "accept/withdraw/cancel challenge": apply action to challenges list
+ case "acceptchallenge":
+ {
+ // Someone accept an open (or targeted) challenge
+ // TODO: keep SIDs, since we need them to notify newgame after chall is complete
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let players = this.challenges[cIdx].to;
+ const pIdx = this.players.findIndex(p => p.sid == data.from);
+ for (let i=0; i<players.length; i++)
+ {
+ if (!players[i])
+ {
+ players[i] = this.players[pIdx].name;
+ break;
+ }
+ }
+ // TODO: if challenge is complete, launch game
+ //this.newGame(data.challenge, data.user); //user.id et user.name
+ break;
+ }
+ case "withdrawchallenge":
+ {
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let chall = this.challenges[cIdx]
+ ArrayFun.remove(chall.players, p => p.id == data.uid);
+ chall.players.push({id:0, name:""});
+ break;
+ }
+ case "refusechallenge":
+ {
+ // TODO: show "player XXX refused challenge", and
+ // remove challenge from list.
+ break;
+ }
+ case "deletechallenge":
+ {
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ break;
+ }
+ // TODO: distinguish hallConnect and gameConnect ?
+ // Or global variable players
+ // + game variable: "observers"
+ case "connect":
+// * - receive "player connect": send our current challenge (to him or global)
+// * Also send all our games (live - max 1 - and corr) [in web worker ?]
+ {
+ this.players.push({name:"", id:0, sid:data.sid});
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid}));
+ break;
+ }
+// * - receive "player disconnect": remove from players list
+ case "disconnect":
+ {
+ ArrayFun.remove(this.players, p => p.sid == data.sid);
+ // TODO: also remove all challenges sent by this player,
+ // and all live games where he plays and no other opponent is online
+ break;
+ }
+ }
+ },
+ showGame: function(game) {
+ // NOTE: if we are an observer, the game will be found in main games list
+ // (sent by connected remote players)
+ // TODO: game path ? /vname/gameId seems better
+ this.$router.push("/" + game.id);
+ },
+ tryChallenge: function(player) {
+ if (player.id == 0)
+ return; //anonymous players cannot be challenged
+ this.newchallenge.to[0] = player.name;
+ doClick("modalNewgame");
+ },
+// * - accept challenge (corr or live) --> send info to challenge creator
+// * - cancel challenge (click on sent challenge) --> send info to all concerned players
+// * - withdraw from challenge (if >= 3 players and previously accepted)
+// * --> send info to challenge creator
+// * - refuse challenge: send "refuse" to challenge sender, and "delete" to others
+// * - prepare and start new game (if challenge is full after acceptation)
+// * --> include challenge ID (so that opponents can delete the challenge too)
+ clickChallenge: function(c) {
+ // TODO: also correspondance case (send to server)
+ if (!!c.accepted)
+ {
+ // It's a multiplayer challenge I accepted: withdraw
+ this.sendSomethingTo(c.from.sid, "withdrawchallenge",
+ {cid:c.id, user:this.st.user.sid});
+ c.accepted = false;
+ }
+ else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it
+ {
+ this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ }
+ else //accept (or refuse) a challenge
+ {
+ c.accepted = true;
+ if (!!c.to[0])
+ {
+ // TODO: if special FEN, show diagram after loading variant
+ c.accepted = confirm("Accept challenge?");
+ }
+ this.sendSomethingTo(c.from.sid,
+ (c.accepted ? "accept" : "refuse") + "challenge", {cid: c.id});
+ if (!c.accepted)
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ }
+ },
+// newGame: function(chall, user) {
+// const fen = chall.fen || V.GenRandInitFen();
+// const game = {}; //TODO: fen, players, time ...
+// //setStorage(game); //TODO
+// game.players.forEach(p => { //...even if game is by corr (could be played live, why not...)
+// this.conn.send(
+// JSON.stringify({code:"newgame", oppid:p.id, game:game}));
+// });
+// if (this.settings.sound >= 1)
+// new Audio("/sounds/newgame.mp3").play().catch(err => {});
+// },
+ getVname: function(vid) {
+ const vIdx = this.st.variants.findIndex(v => v.id == vid);
+ return this.st.variants[vIdx].name;
+ },
+ sendSomethingTo: function(to, code, obj, warnDisconnected) {
+ const doSend = (code, obj, sid) => {
+ this.st.conn.send(JSON.stringify(Object.assign(
+ {},
+ {code: code},
+ obj,
+ {target: sid}
+ )));
+ };
+ const getSid = (pname) => {
+ const pIdx = this.players.findIndex(pl => pl.name == pname);
+ if (!!warnDisconnected && ctype == "live" && pIdx === -1)
+ alert("Warning: " + p.name + " is not connected");
+ return this.players[pIdx].sid;
+ };
+ if (!Array.isArray(to)) //pass sid directly
+ doSend(code, obj, to);
+ else if (!!to[0])
+ {
+ // Challenge with targeted players
+ to.forEach(pname => { doSend(code, obj, getSid(pname)); });
+ }
+ else
+ {
+ // Open challenge: send to all connected players (except us)
+ this.players.forEach(p => {
+ if (p.sid != this.st.user.sid) //only sid is always set
+ doSend(code, obj, p.sid);
+ });
+ }
+ },
+ // Send new challenge (corr or live, cf. time control), with button or click on player
+ newChallenge: async function() {
+ // TODO: put this "load variant" block elsewhere
+ const vname = this.getVname(this.newchallenge.vid);
+ const vModule = await import("@/variants/" + vname + ".js");
+ window.V = vModule.VariantRules;
+ const error = checkChallenge(this.newchallenge);
+ if (!!error)
+ return alert(error);
+ const ctype = this.classifyChallenge(this.newchallenge);
+ const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers);
+ // NOTE: "from" information is not required here
+ let chall =
+ {
+ fen: this.newchallenge.fen || V.GenRandInitFen(),
+ to: cto,
+ timeControl: this.newchallenge.timeControl,
+ vid: this.newchallenge.vid,
+ };
+ const finishAddChallenge = (cid) => {
+ chall.id = cid || "c" + getRandString();
+ // Send challenge to peers
+ this.sendSomethingTo(cto, "challenge", {chall:chall}, "warnDisconnected");
+ chall.added = Date.now();
+ chall.type = ctype;
+ chall.vname = vname;
+ chall.from = this.st.user;
+ this.challenges.push(chall);
+ document.getElementById("modalNewgame").checked = false;
+ };
+ const cIdx = this.challenges.findIndex(
+ c => c.from.sid == this.st.user.sid && c.type == ctype);
+ if (cIdx >= 0)
+ {
+ // Delete current challenge (will be replaced now)
+ this.sendSomethingTo(this.challenges[cIdx].to,
+ "deletechallenge", {cid:this.challenges[cIdx].id});
+ if (ctype == "corr")
+ {
+ ajax(
+ "/challenges",
+ "DELETE",
+ {id: this.challenges[cIdx].id}
+ );
+ }
+ this.challenges.splice(cIdx, 1);
+ }
+ if (ctype == "live")
+ {
+ // Live challenges have a random ID
+ finishAddChallenge();
+ }
+ else
+ {
+ // Correspondance game: send challenge to server
+ ajax(
+ "/challenges",
+ "POST",
+ chall,
+ response => { finishAddChallenge(response.cid); }
+ );
+ }
+ },
+ possibleNbplayers: function(nbp) {
+ if (this.newchallenge.vid == 0)
+ return false;
+ const variants = this.st.variants;
+ const idxInVariants =
+ variants.findIndex(v => v.id == this.newchallenge.vid);
+ return NbPlayers[variants[idxInVariants].name].includes(nbp);
+ },
+ newGame: function(cid) {
+ // TODO: don't forget to send "deletechallenge" message to all concerned players
+ // + setup colors and send game infos to players (message "newgame")
+ },
+ },