+// * - accept challenge (corr or live) --> send info to challenge creator
+// * - cancel challenge (click on sent challenge) --> send info to all concerned players
+// * --> 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) {
+
+ console.log("click challenge");
+ console.log(c);
+
+ else if (c.from.sid == this.st.user.sid
+ || (this.st.user.id > 0 && c.from.id == this.st.user.id))
+ {
+ // It's my challenge: cancel it
+ this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ if (c.type == "corr")
+ {
+ ajax(
+ "/challenges",
+ "DELETE",
+ {id: this.challenges[cIdx].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.st.conn.send(JSON.stringify({
+ code: (c.accepted ? "accept" : "refuse") + "challenge",
+ cid: c.id, target: c.from.sid}));
+ if (c.type == "corr" && c.accepted)
+ {
+ ajax(
+ "/challenges",
+ "PUT",
+ {id: this.challenges[cIdx].id}
+ );
+ }
+ if (!c.accepted)
+ {
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ if (c.type == "corr")
+ {
+ ajax(
+ "/challenges",
+ "DELETE",
+ {id: this.challenges[cIdx].id}
+ );
+ }
+ }
+ }
+ },
+ // NOTE: for live games only (corr games are launched on server)
+ launchGame: async function(c) {
+ // Just assign colors and pass the message
+ const vname = this.getVname(c.vid);
+ const vModule = await import("@/variants/" + vname + ".js");
+ window.V = vModule.VariantRules;
+ let players = [c.from, c.seat];
+ // These game informations will be sent to other players
+ const gameInfo =
+ {
+ gameId: getRandString(),
+ fen: c.fen || V.GenRandInitFen(),
+ // Players' names may be required if game start when a player is offline
+ // Shuffle players order (white then black then other colors).
+ players: shuffle(players.map(p => { return {name:p.name, sid:p.sid} })),
+ vid: c.vid,
+ timeControl: c.timeControl,
+ };
+ // NOTE: cid required to remove challenge
+ this.st.conn.send(JSON.stringify({code:"newgame",
+ gameInfo:gameInfo, cid:c.id, target:c.seat.sid}));
+ // Delete corresponding challenge:
+ ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+ this.newGame(gameInfo); //also!
+ },
+ // NOTE: for live games only (corr games are launched on server)
+ newGame: function(gameInfo) {
+ // Extract times (in [milli]seconds), set clocks
+ const tc = extractTime(gameInfo.timeControl);
+ let initime = [...Array(gameInfo.players.length)];
+ initime[0] = Date.now();
+ const game =
+ {
+ // Game infos: constant
+ gameId: gameInfo.gameId,
+ vname: this.getVname(gameInfo.vid),
+ fenStart: gameInfo.fen,
+ players: gameInfo.players,
+ timeControl: gameInfo.timeControl,
+ increment: tc.increment,
+ mode: "live", //function for live games only
+ // Game state: will be updated
+ fen: gameInfo.fen,
+ moves: [],
+ clocks: [...Array(gameInfo.players.length)].fill(tc.mainTime),
+ initime: initime,
+ score: "*",
+ };
+ GameStorage.add(game);
+ if (this.st.settings.sound >= 1)
+ new Audio("/sounds/newgame.mp3").play().catch(err => {});
+ // TODO: redirect to game