- const error = ChallengeModel.checkChallenge(req.body.chall);
- // TODO: treat "to" field separately (search users by name)
- // --> replace "to" by an array of uid (in chall), then call:
- const from = req.userId;
-// let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
-// let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
-// let vid = ObjectID(req.body.vid);
-// if (!to && !!nameTo)
-// {
-// UserModel.getByName(nameTo, (err,user) => {
-// access.checkRequest(res, err, user, "Opponent not found", () => {
-// createChallenge(vid, from, user._id, res);
-// });
-// });
-// }
-// else if (!!to)
-// createChallenge(vid, from, to, res);
-// else
-// createChallenge(vid, from, undefined, res); //automatch
- ChallengeModel.create(req.body.chall, (err,lastId) => {
- res.json(err || {cid: lastId["rowid"]});
- });
+ const error = ChallengeModel.checkChallenge(req.body.chall);
+ if (!!error)
+ return res.json({errmsg:error});
+ const challenge =
+ {
+ fen: req.body.chall.fen,
+ timeControl: req.body.chall.timeControl,
+ vid: req.body.chall.vid,
+ uid: req.userId,
+ nbPlayers: req.body.chall.to.length,
+ };
+ ChallengeModel.create(challenge, (err,lastId) => {
+ if (!!err)
+ return res.json(err);
+ if (!!req.body.chall.to[0])
+ {
+ UserModel.getByName(req.body.chall.to, (err,users) => {
+ if (!!err)
+ return res.json(err);
+ if (users.length < req.body.chall.to.length)
+ return res.json({errmsg: "Typo in player(s) name(s)"});
+ ChallengeModel.initializeWillPlay(
+ users.map(u => u.id),
+ lastId["rowid"],
+ (err) => {
+ if (!!err)
+ return res.json(err);
+ res.json({cid: lastId["rowid"]});
+ }
+ );
+ });
+ }
+ else
+ res.json({cid: lastId["rowid"]});
+ });