From: Benjamin Auder Date: Sun, 16 Feb 2020 18:49:34 +0000 (+0100) Subject: Fix notifications in case of new corr move X-Git-Url: https://git.auder.net/variants/Chakart/%7B%7B%20asset%28%27mixstore/doc/html/up.jpg?a=commitdiff_plain;h=1ad003fffabb4c95b9fd2d76a2e7ae1f20302cda;p=vchess.git Fix notifications in case of new corr move --- diff --git a/server/models/Game.js b/server/models/Game.js index daae61c0..f1751644 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -166,7 +166,7 @@ const GameModel = { db.serialize(function() { const query = - "SELECT id " + + "SELECT uid " + "FROM Players " + "WHERE gid = " + id; db.all(query, (err,players) => { diff --git a/server/models/User.js b/server/models/User.js index 70d8b521..d0b105dc 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -142,7 +142,7 @@ const UserModel = tryNotify: function(id, message) { UserModel.getOne("id", id, (err,user) => { - if (!err || !user.notify) + if (!!err || !user.notify) return; //NOTE: error is ignored here UserModel.notify(user, message); }); diff --git a/server/routes/games.js b/server/routes/games.js index b5f59b0c..3e2122d6 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -32,7 +32,7 @@ router.post("/games", access.logged, access.ajax, (req,res) => { const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0); const oppId = gameInfo.players[oppIdx].id; UserModel.tryNotify(oppId, - "New game: " + params.siteURL + "/game/" + ret.gid); + "Game started: " + params.siteURL + "/game/" + ret.gid); res.json({gameId: ret.gid}); }); } @@ -77,28 +77,25 @@ router.put("/games", access.logged, access.ajax, (req,res) => { error = GameModel.checkGameUpdate(obj); if (!!error) return res.json({errmsg: error}); - GameModel.update(gid, obj, (err) => { - if (!!err) - return res.json(err); - if (!!obj.move || !!obj.score) - { - // Notify opponent if he enabled notifications: - GameModel.getPlayers(gid, (err2,players) => { - if (!err2) - { - const oppid = (players[0].id == req.userId - ? players[1].id - : players[0].id); - const messagePrefix = (!!obj.move - ? "New move in game: " - : "Game ended: "); - UserModel.tryNotify(oppid, - messagePrefix + params.siteURL + "/game/" + gid); - } - }); - } - res.json({}); - }); + GameModel.update(gid, obj); //no callback here (several operations) + if (!!obj.move || !!obj.score) + { + // Notify opponent if he enabled notifications: + GameModel.getPlayers(gid, (err,players) => { + 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); + } + }); + } + res.json({}); //TODO: what if some update action fails? }); module.exports = router;