- 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.user._id), (err,game) => {
- access.checkRequest(res, err, game, "Cannot find game", () => {
- res.json({});
- });
- });
-});
-
-// variant page
-router.get("/gamesbyvariant", access.logged, access.ajax, (req,res) => {
- if (req.query["uid"] != req.user._id)
- return res.json({errmsg: "Not your games"});
- let uid = ObjectId(req.query["uid"]);
- let vid = ObjectId(req.query["vid"]);
- GameModel.getByVariant(uid, vid, (err,gameArray) => {
- // NOTE: res.json already stringify, no need to do it manually
- res.json(err || {games: gameArray});
- });
-});
-
-// For index: only moves count + myColor
-router.get("/gamesbyplayer", access.logged, access.ajax, (req,res) => {
- if (req.query["uid"] != req.user._id)
- return res.json({errmsg: "Not your games"});
- let uid = ObjectId(req.query["uid"]);
- GameModel.getByPlayer(uid, (err,games) => {
- res.json(err || {games: games});
- });
-});
-
-// TODO: if newmove fail, takeback in GUI
-// TODO: check move structure
-// TODO: for corr games, 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({});
- });
- });
+ const gid = req.body.gid;
+ const obj = req.body.newObj;
+ if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj))
+ {
+ GameModel.getPlayers(gid, (err,players) => {
+ if (players.some(p => p.uid == 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);
+ }
+ res.json({});
+ }
+ });
+ }