- 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;
+ 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.cadence, 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,
+ "Game started: " + params.siteURL + "/#/game/" + ret.gid);
+ res.json({gameId: ret.gid});
+ });
+ }
+ );