X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=cc8a17bb0a5810bae1017fa9e379990b2d79b238;hb=2be5d6140901fc7bb2a33d672e52cfdc545a1912;hp=a35f07d40e8ce475eb7cc35ca4f9ba3a9e6fcaf9;hpb=88667455b8793075a0266d4477fa0f4d03257cb0;p=vchess.git diff --git a/server/routes/games.js b/server/routes/games.js index a35f07d4..cc8a17bb 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,6 +1,7 @@ var router = require("express").Router(); var UserModel = require("../models/User"); var sendEmail = require('../utils/mailer'); +var ChallengeModel = require('../models/Challenge'); var GameModel = require('../models/Game'); var VariantModel = require('../models/Variant'); var access = require("../utils/access"); @@ -26,21 +27,23 @@ 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; + const cid = req.body.cid; + ChallengeModel.remove(cid); + const fen = req.body.fen; GameModel.create( gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players, - (err,game) => { + (err,ret) => { access.checkRequest(res, err, game, "Cannot create game", () => { - if (!!req.body.offlineOpp) - UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name, - "New game: " + "game link"); //TODO: give game link - res.json({game: game}); + const oppIdx = gameInfo.players[0].id == req.user.id ? 1 : 0; + const oppId = gameInfo.players[oppIdx].id; + UserModel.tryNotify(oppId, + "New game: " + params.siteURL + "/game/" + gid); + res.json({gameId: ret.gid}); }); } ); }); -// game page router.get("/games", access.ajax, (req,res) => { const gameId = req.query["gid"]; if (!!gameId) @@ -57,9 +60,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}); }); } }); @@ -76,31 +79,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;