'update'
[vchess.git] / server / routes / games.js
index ec824f7..35a7f42 100644 (file)
@@ -1,7 +1,3 @@
-router.get("/games", access.logged, access.ajax, (req,res) => {
-  const excluded = req.query["excluded"]; //TODO: think about query params here
-});
-
 var router = require("express").Router();
 var UserModel = require("../models/User");
 var sendEmail = require('../utils/mailer');
@@ -22,17 +18,17 @@ function tryNotify(uid, gid, vname, subject)
                          }
       );
                }
-       )};
+       });
 }
 
-// From main hall, start game between player 0 and 1
+// From main hall, start game between players 0 and 1
 router.post("/games", access.logged, access.ajax, (req,res) => {
        const gameInfo = JSON.parse(req.body.gameInfo);
        if (!gameInfo.players.some(p => p.id == req.user.id))
                return res.json({errmsg: "Cannot start someone else's game"});
        let fen = req.body.fen;
-       GameModel.create(gameInfo.vid,
-    gameInfo.fen, gameInfo.mainTime, gameInfo.increment, gameInfo.players,
+       GameModel.create(
+    gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players,
                (err,game) => {
                        access.checkRequest(res, err, game, "Cannot create game", () => {
                                if (!!req.body.offlineOpp)
@@ -44,7 +40,6 @@ router.post("/games", access.logged, access.ajax, (req,res) => {
        );
 });
 
-// game page
 router.get("/games", access.ajax, (req,res) => {
        const gameId = req.query["gid"];
        if (!!gameId)
@@ -61,9 +56,9 @@ router.get("/games", access.ajax, (req,res) => {
     const userId = req.query["uid"];
     const excluded = !!req.query["excluded"];
     GameModel.getByUser(userId, excluded, (err,games) => {
-                 access.checkRequest(res, err, games, "Games not found", () => {
-                         res.json({games: games});
-                 });
+                 if (!!err)
+        return res.json({errmsg: err.errmsg || err.toString()});
+                       res.json({games: games});
          });
   }
 });
@@ -80,31 +75,9 @@ router.put("/games", access.logged, access.ajax, (req,res) => {
        });
 });
 
-// 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" !)
+// 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;