- 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"]});
- });
-});
-
-// Nothing to do if challenge is refused (just removal)
-router.put("/challenges", access.logged, access.ajax, (req,res) => {
- // Accept challenge: turn WillPlay to true; if then challenge is full, launch game
- ChallengeModel.getSeatCount(req.body.id, (scount) => {
- if (scount == 1)
- launchGame(req.body.id, req.userId);
- else
- ChallengeModel.setSeat(req.body.id, req.userId);
- });
- res.json({});
+ const insertChallenge = () => {
+ ChallengeModel.create(challenge, (err) => {
+ if (!!err)
+ return res.json(err);
+ });
+ };
+ if (!!req.body.chall.to)
+ {
+ UserModel.getOne("name", challenge.to, (err,user) => {
+ if (!!err || !user)
+ return res.json(err | {errmsg: "Typo in player name"});
+ challenge.to = user.id; //ready now to insert challenge
+ });
+ insertChallenge();
+ }
+ else
+ insertChallenge();