- this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
- ArrayFun.remove(this.challenges, ch => ch.id == c.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.sendSomethingTo(c.from.sid,
- (c.accepted ? "accept" : "refuse") + "challenge", {cid: c.id});
- if (!c.accepted)
- ArrayFun.remove(this.challenges, ch => ch.id == c.id);
- }
- },
-// 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 => {});
-// },
- getVname: function(vid) {
- const vIdx = this.st.variants.findIndex(v => v.id == vid);
- return this.st.variants[vIdx].name;
- },
- sendSomethingTo: function(to, code, obj, warnDisconnected) {
- 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 (!!warnDisconnected && ctype == "live" && pIdx === -1)
- alert("Warning: " + p.name + " is not connected");
- return this.players[pIdx].sid;
- };
- if (!Array.isArray(to)) //pass sid directly
- doSend(code, obj, to);
- else if (!!to[0])
- {
- // Challenge with targeted players
- to.forEach(pname => { doSend(code, obj, getSid(pname)); });