-// * - accept challenge (corr or live) --> send info to challenge creator
-// * - cancel challenge (click on sent challenge) --> send info to all concerned players
-// * - withdraw from challenge (if >= 3 players and previously accepted)
-// * --> 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) {
- // TODO: also correspondance case (send to server)
- if (!!c.accepted)
- {
- // It's a multiplayer challenge I accepted: withdraw
- this.sendSomethingTo(c.from.sid, "withdrawchallenge",
- {cid:c.id, user:this.st.user.sid});
- c.accepted = false;
- }
- else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it
- {
- 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)); });
- }
- else
- {
- // Open challenge: send to all connected players (except us)
- this.players.forEach(p => {
- if (p.sid != this.st.user.sid) //only sid is always set
- doSend(code, obj, p.sid);
- });
- }
- },
- // Send new challenge (corr or live, cf. time control), with button or click on player