- 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 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 || {});
+ });
+ }
+ });
+ }
+});
+
+// TODO: chats deletion here, but could/should be elsewhere.
+// Moves update also could, although logical unit in a game.
+router.delete("/chats", access.logged, access.ajax, (req,res) => {
+ const gid = req.query["gid"];
+ GameModel.getPlayers(gid, (err, players) => {
+ if ([players.white, players.black].includes(req.userId))
+ {
+ GameModel.update(gid, { delchat: true }, () => {
+ res.json({});
+ });
+ }
+ });