X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=9bd9a30bcdb6e50f23f2a682d408336a6589fe2e;hb=92b82defacccf9c4d3755924a71fcb584002ccc6;hp=24e97471c2a507a67d86e9472f6b19e43ea67539;hpb=8c564f462f5406fcd311d4733f851daf6ada665d;p=vchess.git diff --git a/server/routes/games.js b/server/routes/games.js index 24e97471..9bd9a30b 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -34,7 +34,7 @@ router.get("/games", access.ajax, (req,res) => { { GameModel.getOne(gameId, (err,game) => { access.checkRequest(res, err, game, "Game not found", () => { - res.json({game: game}); + res.json({game: game}); }); }); } @@ -51,35 +51,23 @@ router.get("/games", access.ajax, (req,res) => { } }); -////////////////////////////////// - -// TODO: new move -router.put("/games", access.logged, access.ajax, (req,res) => { - let gid = ObjectId(req.body.gid); - let result = req.body.result; - // NOTE: only game-level life update is "gameover" - GameModel.gameOver(gid, result, ObjectId(req.userId), (err,game) => { - access.checkRequest(res, err, game, "Cannot find game", () => { - res.json({}); - }); - }); -}); - +// New move + fen update + score, potentially // TODO: if newmove fail, takeback in GUI -// 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; + const obj = req.body.newObj; + 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); + }); + res.json({}); }); });