- 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, (err) => {
- if (!!err)
- return res.json(err);
- // 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);
+ let obj = req.body.newObj;
+ if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) {
+ GameModel.getPlayers(gid, (err, players) => {
+ let myColor = '';
+ if (players.white == req.userId) myColor = 'w';
+ else if (players.black == req.userId) myColor = 'b';
+ if (!!myColor) {
+ // Did I mark the game for deletion?
+ if (!!obj.removeFlag) {
+ obj.deletedBy = myColor;
+ delete obj["removeFlag"];
+ }
+ GameModel.update(gid, obj, (err) => {
+ if (!err && (!!obj.move || !!obj.score)) {
+ // Notify opponent if he enabled notifications:
+ const oppid = (myColor == 'w' ? players.black : players.white);
+ const messagePrefix =
+ !!obj.move
+ ? "New move in game : "
+ : "Game ended : ";
+ UserModel.tryNotify(
+ oppid,
+ messagePrefix + params.siteURL + "/#/game/" + gid
+ );
+ }
+ res.json(err || {});
+ });
+ }