rid: ""
},
game: { }, //passed to BaseGame
- oppConnected: false,
+ oppConnected: false, //TODO: use for styling
corrMsg: "", //to send offline messages in corr games
virtualClocks: [0, 0], //initialized with true game.clocks
vr: null, //"variant rules" object initialized from FEN
case "pong": //received if we sent a ping (game still alive on our side)
{
this.oppConnected = true;
- // Send our "last state" informations to opponent(s)
- const L = this.game.moves.length;
- this.st.conn.send(JSON.stringify({
- code: "lastate",
- target: this.game.oppid,
- gameId: this.gameRef.id,
- lastMove: (L>0 ? this.game.moves[L-1] : undefined),
- score: this.game.score,
- movesCount: L,
- drawOffer: this.drawOffer,
- clocks: this.game.clocks,
- }));
+ if (this.game.type == "live") //corr games are always complete
+ {
+ // Send our "last state" informations to opponent(s)
+ const L = this.game.moves.length;
+ this.st.conn.send(JSON.stringify({
+ code: "lastate",
+ target: this.game.oppid,
+ gameId: this.gameRef.id,
+ lastMove: (L>0 ? this.game.moves[L-1] : undefined),
+ score: this.game.score,
+ movesCount: L,
+ drawOffer: this.drawOffer,
+ clocks: this.game.clocks,
+ }));
+ }
break;
}
case "lastate": //got opponent infos about last move
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.gameId = response.gameId;
+ tryNotifyOpponent();
+ this.$router.push("/game/" + response.gameId);
+ }
);
}
},
});
},
+ getPlayers: function(id, cb)
+ {
+ db.serialize(function() {
+ const query =
+ "SELECT id " +
+ "FROM Players " +
+ "WHERE gid = " + id;
+ db.all(query, (err,players) => {
+ return cb(err, players);
+ });
+ });
+ },
+
// obj can have fields move, fen and/or score
update: function(id, obj, cb)
{
// TODO: if newmove fail, takeback in GUI
router.put("/games", access.logged, access.ajax, (req,res) => {
const gid = req.body.gid;
+ const oppId = req.body.oppId;
const obj = req.body.newObj;
GameModel.update(gid, obj, (err) => {
if (!!err)
return res.json(err);
- if (!!req.body.offlineOpp) //TODO: refresh this...
- UserModel.tryNotify(req.body.offlineOpp, "New move in game " + gid);
+ // Notify opponent if he enabled notifications:
+ GameModel.getPlayers(gid, (err2,players) => {
+ if (!!err2)
+ return res.json(err);
+ const oppid = (players[0].id == req.userId ? players[1].id : players[0].id);
+ UserModel.tryNotify(oppid,
+ "New move in game: " + params.siteURL + "/game/" + gid);
+ });
res.json({});
});
});