X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=24e97471c2a507a67d86e9472f6b19e43ea67539;hb=8c564f462f5406fcd311d4733f851daf6ada665d;hp=35beb250ff671574d599c7f56e88f0d86f01be4d;hpb=4edfed6c011cd97d58d5bd8e0451cc0c1006a0a0;p=vchess.git diff --git a/server/routes/games.js b/server/routes/games.js index 35beb250..24e97471 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,104 +1,73 @@ -router.get("/games", access.logged, access.ajax, (req,res) => { - const excluded = req.query["excluded"]; //TODO: think about query params here -}); - -// TODO: adapt for correspondance play - var router = require("express").Router(); var UserModel = require("../models/User"); +var ChallengeModel = require('../models/Challenge'); 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) -{ - 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. - }); - } - )}; -} - -// 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 = req.body.gameInfo; + if (!gameInfo.players.some(p => p.id == req.userId)) 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]]; + const cid = req.body.cid; + ChallengeModel.remove(cid); + const fen = req.body.fen; GameModel.create( - ObjectId(variant._id), [ObjectId(players[0]),ObjectId(players[1])], fen, - (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"]; - res.json({game: game}); + gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players, + (err,ret) => { + access.checkRequest(res, err, ret, "Cannot create game", () => { + const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0); + const oppId = gameInfo.players[oppIdx].id; + UserModel.tryNotify(oppId, + "New game: " + params.siteURL + "/game/" + ret.gid); + res.json({gameId: ret.gid}); }); } ); }); -// 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) => { + 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.user._id), (err,game) => { + GameModel.gameOver(gid, result, ObjectId(req.userId), (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" !) +// 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;