-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');
-var GameModel = require('../models/Game');
-var VariantModel = require('../models/Variant');
-var access = require("../utils/access");
-var params = require("../config/parameters");
+let router = require("express").Router();
+const UserModel = require("../models/User");
+const ChallengeModel = require('../models/Challenge');
+const GameModel = require('../models/Game');
+const VariantModel = require('../models/Variant');
+const access = require("../utils/access");
+const 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.
- }
- );
- }
- )};
-}
-
-// 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,
- (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;
+ if (!Array.isArray(gameInfo.players) ||
+ gameInfo.players.every(p => p.id != req.userId))
+ {
+ return res.json({errmsg: "Cannot start someone else's game"});
+ }
+ const cid = req.body.cid;
+ // Check all entries of gameInfo + cid:
+ let error = GameModel.checkGameInfo(gameInfo);
+ if (!error)
+ {
+ if (!cid.toString().match(/^[0-9]+$/))
+ error = "Wrong challenge ID";
+ }
+ if (!!error)
+ return res.json({errmsg:error});
+ ChallengeModel.remove(cid);
+ GameModel.create(
+ 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) => {
- const gameId = req.query["gid"];
- if (!!gameId)
+ const gameId = req.query["gid"];
+ if (!!gameId)
{
+ if (!gameId.match(/^[0-9]+$/))
+ return res.json({errmsg: "Wrong game ID"});
GameModel.getOne(gameId, (err,game) => {
- access.checkRequest(res, err, game, "Game not found", () => {
- res.json({game: game});
- });
- });
+ access.checkRequest(res, err, game, "Game not found", () => {
+ res.json({game: game});
+ });
+ });
}
else
{
// Get by (non-)user ID:
const userId = req.query["uid"];
+ if (!userId.match(/^[0-9]+$/))
+ return res.json({errmsg: "Wrong user ID"});
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});
+ });
}
});
-// TODO:
-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({});
- });
- });
-});
-
-// 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});
- });
-});
-
+// New move + fen update + score, potentially
// TODO: if newmove fail, takeback in GUI
-// TODO: check move structure
-// TODO: for corr games, 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({});
- });
- });
+router.put("/games", access.logged, access.ajax, (req,res) => {
+ const gid = req.body.gid;
+ let error = "";
+ if (!gid.toString().match(/^[0-9]+$/))
+ error = "Wrong game ID";
+ const obj = req.body.newObj;
+ error = GameModel.checkGameUpdate(obj);
+ if (!!error)
+ return res.json({errmsg: error});
+ GameModel.update(gid, obj, (err) => {
+ if (!!err)
+ return res.json(err);
+ if (!!obj.move || !!obj.score)
+ {
+ // Notify opponent if he enabled notifications:
+ GameModel.getPlayers(gid, (err2,players) => {
+ if (!err2)
+ {
+ const oppid = (players[0].id == req.userId
+ ? players[1].id
+ : players[0].id);
+ const messagePrefix = (!!obj.move
+ ? "New move in game: "
+ : "Game ended: ");
+ UserModel.tryNotify(oppid,
+ messagePrefix + params.siteURL + "/game/" + gid);
+ }
+ });
+ }
+ res.json({});
+ });
});
module.exports = router;