- // NOTE: cid required to remove challenge
- this.st.conn.send(JSON.stringify({code:"newgame",
- gameInfo:gameInfo, cid:c.id, target:c.seat.sid}));
- // Delete corresponding challenge:
- ArrayFun.remove(this.challenges, ch => ch.id == c.id);
- this.startNewGame(gameInfo); //also!
+ let oppsid = c.from.sid; //may not be defined if corr + offline opp
+ if (!oppsid)
+ {
+ oppsid = Object.keys(this.people).find(sid =>
+ this.people[sid].id == c.from.id);
+ }
+ const tryNotifyOpponent = () => {
+ if (!!oppsid) //opponent is online
+ {
+ this.st.conn.send(JSON.stringify({code:"newgame",
+ gameInfo:gameInfo, target:oppsid, cid:c.id}));
+ }
+ };
+ if (c.type == "live")
+ {
+ // NOTE: in this case we are sure opponent is online
+ tryNotifyOpponent();
+ this.startNewGame(gameInfo);
+ }
+ else //corr: game only on server
+ {
+ ajax(
+ "/games",
+ "POST",
+ {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge
+ response => {
+ gameInfo.id = response.gameId;
+ tryNotifyOpponent();
+ this.$router.push("/game/" + response.gameId);
+ }
+ );
+ }
+ // Send game info to everyone except opponent (and me)
+ Object.keys(this.people).forEach(sid => {
+ if (![this.st.user.sid,oppsid].includes(sid))
+ {
+ this.st.conn.send(JSON.stringify({code:"game",
+ game: { //minimal game info:
+ id: gameInfo.id,
+ players: gameInfo.players,
+ vid: gameInfo.vid,
+ timeControl: gameInfo.timeControl,
+ },
+ target: sid}));
+ }
+ });