X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=a86a76ddedce9301a40c44232774252373f45356;hp=d1f233bd8fc517ca6afd525cf9a891d2189311a3;hb=584f81b93154313a3856112400b7df98e0eb2632;hpb=a9b131f10ee55bd96c60180c55666df4b1f4dc4d diff --git a/server/routes/games.js b/server/routes/games.js index d1f233bd..a86a76dd 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,96 +1,106 @@ -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 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) - { - sendEmail(params.mailFrom, user.email, subject, - params.siteURL + "?v=" + vname + "&g=" + gid, err => { - res.json(err || {}); // TODO: log error somewhere. - } - ); - } - }); -} +let router = require("express").Router(); +const UserModel = require("../models/User"); +const ChallengeModel = require('../models/Challenge'); +const GameModel = require('../models/Game'); +const access = require("../utils/access"); +const params = require("../config/parameters"); // 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.timeControl, gameInfo.players, - (err,game) => { - 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 gameInfo = req.body.gameInfo; + // Challenge ID is provided if game start from Hall: + const cid = req.body.cid; + if ( + Array.isArray(gameInfo.players) && + gameInfo.players.some(p => p.uid == req.userId) && + (!cid || cid.toString().match(/^[0-9]+$/)) && + GameModel.checkGameInfo(gameInfo) + ) { + if (!!cid) ChallengeModel.remove(cid); + GameModel.create( + gameInfo.vid, gameInfo.fen, gameInfo.cadence, gameInfo.players, + (err,ret) => { + const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0); + const oppId = gameInfo.players[oppIdx].id; + UserModel.tryNotify(oppId, + "Game started: " + params.siteURL + "/#/game/" + ret.gid); + res.json({gameId: ret.gid}); + } + ); + } }); router.get("/games", access.ajax, (req,res) => { - const gameId = req.query["gid"]; - if (!!gameId) + const gameId = req.query["gid"]; + if (gameId) { - GameModel.getOne(gameId, (err,game) => { - access.checkRequest(res, err, game, "Game not found", () => { - res.json({game: game}); - }); - }); + if (gameId.match(/^[0-9]+$/)) + { + GameModel.getOne(gameId, (err,game) => { + 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}); - }); - }); + if (userId.match(/^[0-9]+$/)) + { + const excluded = !!req.query["excluded"]; + GameModel.getByUser(userId, excluded, (err,games) => { + res.json({games: games}); + }); + } } }); -// TODO: +// FEN update + score(Msg) + draw status / and new move + chats 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) => { - access.checkRequest(res, err, game, "Cannot find game", () => { - res.json({}); - }); - }); + const gid = req.body.gid; + let obj = req.body.newObj; + if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) + { + GameModel.getPlayers(gid, (err,players) => { + const myIdx = players.findIndex(p => p.uid == req.userId) + if (myIdx >= 0) { + // Did I mark the game for deletion? + if (!!obj.removeFlag) { + obj.deletedBy = ["w","b"][myIdx]; + delete obj["removeFlag"]; + } + GameModel.update(gid, obj, (err) => { + if (!err && (!!obj.move || !!obj.score)) + { + // Notify opponent if he enabled notifications: + const oppid = players[0].uid == req.userId + ? players[1].uid + : players[0].uid; + const messagePrefix = obj.move + ? "New move in game: " + : "Game ended: "; + UserModel.tryNotify(oppid, + messagePrefix + params.siteURL + "/#/game/" + gid); + } + res.json(err || {}); + }); + } + }); + } }); -// 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({}); - }); - }); +// TODO: chats deletion here, but could/should be elsewhere. +// Moves update also could, although logical unit in a game. +router.delete("/chats", access.logged, access.ajax, (req,res) => { + const gid = req.query["gid"]; + GameModel.getPlayers(gid, (err,players) => { + if (players.some(p => p.uid == req.userId)) + { + GameModel.update(gid, {delchat: true}, () => { + res.json({}); + }); + } + }); }); module.exports = router;