- // user: last person to accept the challenge
- newGameLive: function(chall, user) {
- const fen = chall.fen || V.GenRandInitFen();
- const game = {}; //TODO: fen, players, time ...
- //setStorage(game); //TODO
- game.players.forEach(p => {
- 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 => {});
- },
- newGame: function() {
- const afterRulesAreLoaded = () => {
- // NOTE: side-effect = set FEN
- // TODO: (to avoid any cheating option) separate the GenRandInitFen() functions
- // in separate files, load on server and generate FEN on server.
- const error = checkChallenge(this.newgameInfo, vname);
- if (!!error)
- return alert(error);
- // Possible (server) error if filled player does not exist
+ // 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() {
+ if (this.challenges.some(c => c.from.sid == this.st.user.sid))
+ {
+ document.getElementById("modalNewgame").checked = false;
+ return alert("You already have a pending challenge");
+ }
+ const idxInVariants =
+ this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
+ const vname = this.st.variants[idxInVariants].name;
+ const vModule = await import("@/variants/" + vname + ".js");
+ window.V = vModule.VariantRules;
+ // checkChallenge side-effect = set FEN, and mainTime + increment in seconds
+ const error = checkChallenge(this.newchallenge);
+ if (!!error)
+ return alert(error);
+ // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
+ const liveGame =
+ this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60;
+ // Check that the players (if any indicated) are online
+ let chall =
+ {
+ id: 0, //unknown yet (no ID for live challenges)
+ from: this.st.user,
+ added: Date.now(),
+ fen: this.newchallenge.fen,
+ variant: {id: this.newchallenge.vid, name: vname},
+ nbPlayers: this.newchallenge.nbPlayers,
+ to: [
+ {id: 0, name: this.newchallenge.to[0], sid: ""},
+ {id: 0, name: this.newchallenge.to[1], sid: ""},
+ {id: 0, name: this.newchallenge.to[2], sid: ""},
+ ],
+ timeControl: this.newchallenge.timeControl,
+ mainTime: this.newchallenge.mainTime,
+ increment: this.newchallenge.increment,
+ };
+ for (let p of chall.to)
+ {
+ if (p.name != "")
+ {
+ const pIdx = this.players.findIndex(pl => pl.name == p.name);
+ // NOTE: id (server DB) and sid (socket ID).
+ // Anonymous players just have a socket ID.
+ // NOTE: for correspondance play we don't require players to be online
+ // (==> we don't have IDs, and no sid)
+ if (liveGame && pIdx === -1)
+ return alert(p.name + " is not connected");
+ p.id = this.players[pIdx].id;
+ p.sid = this.players[pIdx].sid;
+ }
+ }
+ const finishAddChallenge = () => {
+ this.challenges.push(chall);
+ // Send challenge to peers
+ const challSock =
+ {
+ code: "newchallenge",
+ chall: chall,
+ };
+ if (chall.to[0].id > 0)
+ {
+ // Challenge with targeted players
+ chall.to.forEach(p => {
+ if (p.id > 0)
+ {
+ this.st.conn.send(JSON.stringify(Object.assign(
+ {},
+ challSock,
+ {receiver: p.sid}
+ )));
+ }
+ });
+ }
+ else
+ {
+ // Open challenge: send to all connected players (except us)
+ const strChallSock = JSON.stringify(challSock);
+ this.players.forEach(p => {
+ if (p.sid != this.st.user.sid) //only sid is always set
+ this.st.conn.send(strChallSock);
+ });
+ }
+ document.getElementById("modalNewgame").checked = false;
+ };
+ if (liveGame)
+ {
+ // Live challenges have cid = 0
+ finishAddChallenge();
+ }
+ else
+ {
+ // Correspondance game: send challenge to server