+ 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)
+ id: c.id,
+ 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":
+ {
+ // Send my current live game (if any)
+ if (!!localStorage["gid"])
+ {
+ const myGame =
+ {
+ // Minimal game informations: (fen+clock not required)
+ id: localStorage["gid"],
+ players: JSON.parse(localStorage["players"]), //array sid+id+name
+ vname: localStorage["vname"],
+ timeControl: localStorage["timeControl"],
+ };
+ this.st.conn.send(JSON.stringify({code:"game",
+ game:myGame, target:data.from}));
+ }
+ 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.classifyObject(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)
+ // NOTE: it may be correspondance (if newgame while we are connected)
+ let newGame = data.game;
+ newGame.type = this.classifyObject(data.game);
+ newGame.vname = newGame.vname;
+ this.games.push(newGame);
+ 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":
+ {
+ // Delete corresponding challenge:
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ // New game just started: data contain all informations
+ this.newGame(data.gameInfo);
+ break;
+ }
+// * - receive "accept/withdraw/cancel challenge": apply action to challenges list
+ // NOTE: challenge "socket" actions accept+withdraw only for live challenges
+ case "acceptchallenge":
+ {
+ // Someone accept an open (or targeted) challenge
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let c = this.challenges[cIdx];
+ if (!c.seats)
+ c.seats = [...Array(c.to.length)];
+ const pIdx = this.players.findIndex(p => p.sid == data.from);
+ // Put this player in the first empty seat we find:
+ let sIdx = 0;
+ for (; sIdx<c.seats.length; sIdx++)
+ {
+ if (!c.seats[sIdx])
+ {
+ c.seats[sIdx] = this.players[pIdx];
+ break;
+ }
+ }
+ if (sIdx == c.seats.length - 1)
+ {
+ // All seats are taken: game can start
+ this.launchGame(c);
+ }
+ break;
+ }
+ case "withdrawchallenge":
+ {
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let seats = this.challenges[cIdx].seats;
+ const sIdx = seats.findIndex(s => s.sid == data.sid);
+ seats[sIdx] = undefined;
+ break;
+ }
+ case "refusechallenge":
+ {
+ alert(this.getPname(data.from) + " refused your challenge");
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ break;
+ }
+ case "deletechallenge":
+ {
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ break;
+ }
+ case "connect":
+ {
+ this.players.push({name:"", id:0, sid:data.sid});
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid}));
+ this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid}));
+ this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid}));
+ break;
+ }
+ case "disconnect":
+ {
+ ArrayFun.remove(this.players, p => p.sid == data.sid);
+ // Also remove all challenges sent by this player:
+ ArrayFun.remove(this.challenges, c => c.from.sid == data.sid);
+ // And all live games where he plays and no other opponent is online
+ ArrayFun.remove(this.games, g =>
+ g.type == "live" && (g.players.every(p => p.sid == data.sid
+ || !this.players.some(pl => pl.sid == p.sid))), "all");
+ break;
+ }
+ }
+ },
+ // Challenge lifecycle:
+ tryChallenge: function(player) {
+ if (player.id == 0)
+ return; //anonymous players cannot be challenged
+ this.newchallenge.to[0] = player.name;
+ doClick("modalNewgame");
+ },
+ newChallenge: async function() {
+ 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.classifyObject(this.newchallenge);
+ const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers - 1);
+ // NOTE: "from" information is not required here
+ let chall =
+ {
+ fen: this.newchallenge.fen,
+ to: cto,
+ timeControl: this.newchallenge.timeControl,
+ vid: this.newchallenge.vid,