-// TODO: check move structure
-// TODO: move should contain an optional "message" field ("corr chat" !)
-router.post("/moves", access.logged, access.ajax, (req,res) => {
- let gid = ObjectId(req.body.gid);
- let fen = req.body.fen;
- let vname = req.body.vname; //defined only if !!offlineOpp
- // NOTE: storing the moves encoded lead to double stringify --> error at parsing
- let move = JSON.parse(req.body.move);
- GameModel.addMove(gid, move, fen, req._user._id, (err,game) => {
- access.checkRequest(res, err, game, "Cannot find game", () => {
- if (!!req.body.offlineOpp)
- UserModel.tryNotify(ObjectId(req.body.offlineOpp), gid, vname, "New move");
- res.json({});
- });
- });
+router.put("/games", access.logged, access.ajax, (req,res) => {
+ 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, (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({});
+ });