{
if (gameId.match(/^[0-9]+$/))
{
- GameModel.getOne(gameId, false, (err,game) => {
+ GameModel.getOne(gameId, (err,game) => {
res.json({game: game});
});
}
}
});
-// New move + fen update + score + chats...
+// FEN update + score(Msg) + draw status / and new move + chats
router.put("/games", access.logged, access.ajax, (req,res) => {
const gid = req.body.gid;
- const obj = req.body.newObj;
+ let obj = req.body.newObj;
if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj))
{
GameModel.getPlayers(gid, (err,players) => {
- if (players.some(p => p.id == req.userId))
- {
- GameModel.update(gid, obj);
- if (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);
+ 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"];
}
- res.json({});
+ 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 || {});
+ });
}
});
}
});
+// 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.some(p => p.uid == req.userId))
+ {
+ GameModel.update(gid, {delchat: true}, () => {
+ res.json({});
+ });
+ }
+ });
+});
+
module.exports = router;