- const gid = req.body.gid;
- 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);
- res.json({});
- });
+ const gid = req.body.gid;
+ let error = "";
+ if (!gid.toString().match(/^[0-9]+$/))
+ error = "Wrong game ID";
+ const obj = req.body.newObj;
+ error = GameModel.checkGameUpdate(obj);
+ if (!!error)
+ return res.json({errmsg: error});
+ 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?