X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=d1f233bd8fc517ca6afd525cf9a891d2189311a3;hb=a9b131f10ee55bd96c60180c55666df4b1f4dc4d;hp=808c258cf6ebcff1191cea3ce9e6b97cd8c0617f;hpb=625022fdcf750f0aff8fcd699f7e9b89730e1d10;p=vchess.git diff --git a/server/routes/games.js b/server/routes/games.js index 808c258c..d1f233bd 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,11 +1,10 @@ -// TODO: adapt for correspondance play - var router = require("express").Router(); var UserModel = require("../models/User"); +var sendEmail = require('../utils/mailer'); var GameModel = require('../models/Game'); var VariantModel = require('../models/Variant'); -var ObjectId = require("bson-objectid"); var access = require("../utils/access"); +var params = require("../config/parameters"); // Notify about a game (start, new move) function tryNotify(uid, gid, vname, subject) @@ -13,52 +12,58 @@ function tryNotify(uid, gid, vname, subject) UserModel.getOne("id", uid, (err,user) => { if (!!err && user.notify) { - maild.send({ - from: params.mailFrom, - to: user.email, - subject: subject, - body: params.siteURL + "?v=" + vname + "&g=" + gid - }, err => { - // TODO: log error somewhere. - }); + sendEmail(params.mailFrom, user.email, subject, + params.siteURL + "?v=" + vname + "&g=" + gid, err => { + res.json(err || {}); // TODO: log error somewhere. + } + ); } - )}; + }); } -// From variant page, 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) => { - let variant = JSON.parse(req.body.variant); - let players = JSON.parse(req.body.players); - if (!players.includes(req.user._id.toString())) //TODO: should also check challenge... + 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; - // Randomly shuffle colors white/black - if (Math.random() < 0.5) - players = [players[1],players[0]]; GameModel.create( - ObjectId(variant._id), [ObjectId(players[0]),ObjectId(players[1])], fen, + gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players, (err,game) => { access.checkRequest(res, err, game, "Cannot create game", () => { if (!!req.body.offlineOpp) - UserModel.tryNotify(ObjectId(req.body.offlineOpp), game._id, variant.name, "New game"); - game.movesLength = game.moves.length; //no need for all moves here - delete game["moves"]; + UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name, + "New game: " + "game link"); //TODO: give game link res.json({game: game}); }); } ); }); -// game page router.get("/games", access.ajax, (req,res) => { - let gameID = req.query["gid"]; - GameModel.getById(ObjectId(gameID), (err,game) => { - access.checkRequest(res, err, game, "Game not found", () => { - res.json({game: game}); - }); - }); + const gameId = req.query["gid"]; + if (!!gameId) + { + GameModel.getOne(gameId, (err,game) => { + access.checkRequest(res, err, game, "Game not found", () => { + res.json({game: game}); + }); + }); + } + else + { + // Get by (non-)user ID: + 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}); + }); + }); + } }); +// TODO: router.put("/games", access.logged, access.ajax, (req,res) => { let gid = ObjectId(req.body.gid); let result = req.body.result; @@ -70,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;