A few fixes, drop planned problems support (replaced by forum + mode analyze)
[vchess.git] / server / routes / games.js
index 24e9747..a444acd 100644 (file)
@@ -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});
                  });
          });
   }
@@ -44,42 +44,30 @@ router.get("/games", access.ajax, (req,res) => {
     const userId = req.query["uid"];
     const excluded = !!req.query["excluded"];
     GameModel.getByUser(userId, excluded, (err,games) => {
-                 if (!!err)
+                       if (!!err)
         return res.json({errmsg: err.errmsg || err.toString()});
                        res.json({games: games});
          });
   }
 });
 
-//////////////////////////////////
-
-// 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({});
        });
 });