+ clickChallenge: function(c) {
+ const myChallenge = (c.from.sid == this.st.user.sid //live
+ || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr
+ if (!myChallenge)
+ {
+ if (c.type == "corr" && this.st.user.id <= 0)
+ return alert("Please log in to accept corr challenges");
+ c.accepted = true;
+ if (!!c.to) //c.to == this.st.user.name (connected)
+ {
+ // TODO: if special FEN, show diagram after loading variant
+ c.accepted = confirm("Accept challenge?");
+ }
+ if (c.accepted)
+ {
+ c.seat = { //again, avoid c.seat = st.user to not reveal email
+ sid: this.st.user.sid,
+ id: this.st.user.id,
+ name: this.st.user.name,
+ };
+ this.launchGame(c);
+ }
+ else
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "refusechallenge",
+ cid: c.id, target: c.from.sid}));
+ }
+ }
+ else //my challenge
+ {
+ if (c.type == "corr")
+ {
+ ajax(
+ "/challenges",
+ "DELETE",
+ {id: c.id}
+ );
+ }
+ else //live
+ localStorage.removeItem("challenge");
+ }
+ // In (almost) all cases, the challenge is consumed:
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ // NOTE: deletechallenge event might be redundant (but it's easier this way)
+ this.sendSomethingTo((!!c.to ? c.from : null), "deletechallenge", {cid:c.id});
+ },
+ // NOTE: when launching game, the challenge is already deleted
+ launchGame: async function(c) {
+ const vModule = await import("@/variants/" + c.vname + ".js");
+ window.V = vModule.VariantRules;
+ // These game informations will be sent to other players
+ const gameInfo =
+ {
+ id: getRandString(),
+ fen: c.fen || V.GenRandInitFen(),
+ players: shuffle([c.from, c.seat]), //white then black
+ vid: c.vid,
+ vname: c.vname, //theoretically vid is enough, but much easier with vname
+ timeControl: c.timeControl,
+ };
+ let target = c.from.sid; //may not be defined if corr + offline opp
+ if (!target)
+ {
+ target = Object.keys(this.people).find(sid =>
+ this.people[sid].id == c.from.id);
+ }
+ const tryNotifyOpponent = () => {
+ if (!!target) //opponent is online
+ {
+ this.st.conn.send(JSON.stringify({code:"newgame",
+ gameInfo:gameInfo, target:target, cid:c.id}));
+ }
+ };
+ if (c.type == "live")
+ {
+ tryNotifyOpponent();
+ this.startNewGame(gameInfo);
+ }
+ else //corr: game only on server
+ {
+ ajax(
+ "/games",
+ "POST",
+ {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge
+ response => {
+ gameInfo.id = response.gameId;
+ tryNotifyOpponent();
+ this.$router.push("/game/" + response.gameId);
+ }
+ );
+ }
+ // Send game info to everyone except opponent (and me)
+ this.st.conn.send(JSON.stringify({code:"game",
+ game: { //minimal game info:
+ id: gameInfo.id,
+ players: gameInfo.players.map(p => p.name),
+ vid: gameInfo.vid,
+ timeControl: gameInfo.timeControl,
+ },
+ oppsid: target}));