- if (!err)
- {
- const oppid = (players[0].uid == req.userId
- ? players[1].uid
- : players[0].uid);
- const messagePrefix = (!!obj.move
- ? "New move in game: "
- : "Game ended: ");
- UserModel.tryNotify(oppid,
- messagePrefix + params.siteURL + "/game/" + gid);
+ const myIdx = players.findIndex(p => p.uid == req.userId)
+ if (myIdx >= 0) {
+ // Did I mark the game for deletion?
+ if (!!obj.removeFlag) {
+ obj.deletedBy = ["w","b"][myIdx];
+ delete obj["removeFlag"];
+ }
+ GameModel.update(gid, obj, (err) => {
+ if (!err && (!!obj.move || !!obj.score))
+ {
+ // Notify opponent if he enabled notifications:
+ const oppid = players[0].uid == req.userId
+ ? players[1].uid
+ : players[0].uid;
+ const messagePrefix = obj.move
+ ? "New move in game: "
+ : "Game ended: ";
+ UserModel.tryNotify(oppid,
+ messagePrefix + params.siteURL + "/#/game/" + gid);
+ }
+ res.json(err || {});
+ });