gdisplay: "live",
games: [],
challenges: [],
- people: [], //(all) online players
+ people: [], //people in main hall
infoMessage: "",
newchallenge: {
fen: "",
});
this.games.forEach(g => {
if (g.vname == "")
- g.vname = this.getVname(g.vid)
+ g.vname = this.getVname(g.vid);
});
},
},
}
this.$router.push(url);
},
- // TODO: ...filter(...)[0].name, one-line, just remove this function
getVname: function(vid) {
- const vIdx = this.st.variants.findIndex(v => v.id == vid);
- return vIdx >= 0 ? this.st.variants[vIdx].name : "";
+ const variant = this.st.variants.find(v => v.id == vid);
+ // this.st.variants might be uninitialized (variant == null)
+ return (!!variant ? variant.name : "");
},
getSid: function(pname) {
const pIdx = this.people.findIndex(pl => pl.name == pname);
else
{
this.infoMessage = "New game started: " +
- "<a href='/game/" + data.gameInfo.gameId + "'>" +
- "/game/" + data.gameInfo.gameId + "</a>";
+ "<a href='#/game/" + data.gameInfo.id + "'>" +
+ "#/game/" + data.gameInfo.id + "</a>";
let modalBox = document.getElementById("modalInfo");
modalBox.checked = true;
setTimeout(() => { modalBox.checked = false; }, 3000);
chall.vname = vname;
chall.from = this.people[0]; //avoid sending email
this.challenges.push(chall);
- localStorage.setItem("challenge", JSON.stringify(chall));
+ if (ctype == "live")
+ localStorage.setItem("challenge", JSON.stringify(chall));
document.getElementById("modalNewgame").checked = false;
};
const cIdx = this.challenges.findIndex(
}
else //my challenge
{
- localStorage.removeItem("challenge");
if (c.type == "corr")
{
ajax(
{id: c.id}
);
}
+ else //live
+ localStorage.removeItem("challenge");
}
// In (almost) all cases, the challenge is consumed:
ArrayFun.remove(this.challenges, ch => ch.id == c.id);
// These game informations will be sent to other players
const gameInfo =
{
- gameId: getRandString(),
+ id: getRandString(),
fen: c.fen || V.GenRandInitFen(),
players: shuffle([c.from, c.seat]), //white then black
vid: c.vid,
+ vname: c.vname, //theoretically vid is enough, but much easier with vname
timeControl: c.timeControl,
};
let target = c.from.sid; //may not be defined if corr + offline opp
if (!!opponent)
target = opponent.sid
}
- if (!!target) //opponent is online
- {
- this.st.conn.send(JSON.stringify({code:"newgame",
- gameInfo:gameInfo, target:target, cid:c.id}));
- }
+ const tryNotifyOpponent = () => {
+ if (!!target) //opponent is online
+ {
+ this.st.conn.send(JSON.stringify({code:"newgame",
+ gameInfo:gameInfo, target:target, cid:c.id}));
+ }
+ };
if (c.type == "live")
+ {
+ tryNotifyOpponent();
this.startNewGame(gameInfo);
+ }
else //corr: game only on server
{
ajax(
"/games",
"POST",
{gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge
- response => { this.$router.push("/game/" + response.gameId); }
+ response => {
+ gameInfo.id = response.gameId;
+ tryNotifyOpponent();
+ this.$router.push("/game/" + response.gameId);
+ }
);
}
},
const game = Object.assign({}, gameInfo, {
// (other) Game infos: constant
fenStart: gameInfo.fen,
- created: Date.now(),
+ added: Date.now(),
// Game state (including FEN): will be updated
moves: [],
clocks: [-1, -1], //-1 = unstarted
GameStorage.add(game);
if (this.st.settings.sound >= 1)
new Audio("/sounds/newgame.mp3").play().catch(err => {});
- this.$router.push("/game/" + gameInfo.gameId);
+ this.$router.push("/game/" + gameInfo.id);
},
},
};