- // TODO: accepter un challenge peut lancer une partie, il
- // faut alors supprimer challenge + creer partie + la retourner et l'ajouter ici
- // si pas le mien et FEN speciale :: (charger code variante et)
- // montrer diagramme + couleur (orienté)
- //this.newGame(data.challenge, data.user); //user.id et user.name
- },
- // user: last person to accept the challenge (TODO: revoir ça)
-// 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 => {});
-// },
- // 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 vIdx = this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
- const vname = this.st.variants[vIdx].name;
- 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);
- let chall =
- {
- fen: this.newchallenge.fen || V.GenRandInitFen(),
- to: cto,
- timeControl: this.newchallenge.timeControl,
- from: this.st.user.sid,
- vid: this.newchallenge.vid,
- };
- const sendSomethingTo = (to, code, obj) => {
- 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 (ctype == "live" && pIdx === -1)
- alert("Warning: " + p.name + " is not connected");
- return this.players[pIdx].sid;
- };
- if (!!to[0])
- {
- // Challenge with targeted players
- to.forEach(pname => { doSend(code, obj, getSid(pname)); });
+ if (error) {
+ alert(error);
+ return;
+ }
+ window.V = this.newchallenge.V;
+ error = checkChallenge(this.newchallenge);
+ if (error) {
+ alert(error);
+ return;
+ }
+ // NOTE: "from" information is not required here
+ let chall = Object.assign({}, this.newchallenge);
+ // Add only if not already issued (not counting target or FEN):
+ if (this.challenges.some(c =>
+ (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) &&
+ c.vid == chall.vid &&
+ c.cadence == chall.cadence &&
+ c.randomness == chall.randomness
+ )) {
+ alert(this.st.tr["Challenge already exists"]);
+ return;
+ }
+ if (this.newchallenge.memorize) this.addPresetChall(this.newchallenge);
+ delete chall["V"];
+ delete chall["diag"];
+ const finishAddChallenge = cid => {
+ chall.id = cid || "c" + getRandString();
+ const MAX_ALLOWED_CHALLS = 3;
+ // Remove oldest challenge if 3 found: only 3 at a time of a given type
+ let countMyChalls = 0;
+ let challToDelIdx = 0;
+ let oldestAdded = Number.MAX_SAFE_INTEGER;
+ for (let i=0; i<this.challenges.length; i++) {
+ const c = this.challenges[i];
+ if (
+ c.type == ctype &&
+ (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
+ ) {
+ countMyChalls++;
+ if (c.added < oldestAdded) {
+ challToDelIdx = i;
+ oldestAdded = c.added;
+ }
+ }