1 // AJAX methods to get, create, update or delete a challenge
3 let router
= require("express").Router();
4 const access
= require("../utils/access");
5 const ChallengeModel
= require("../models/Challenge");
7 router
.post("/challenges/:vid([0-9]+)", access
.logged
, access
.ajax
, (req
,res
) => {
8 const vid
= req
.params
["vid"];
13 mainTime: req
.body
["mainTime"],
14 increment: req
.body
["increment"],
15 nbPlayers: req
.body
["nbPlayers"],
16 players: req
.body
["players"],
18 const error
= ChallengeModel
.checkChallenge(chall
);
19 ChallengeModel
.create(chall
, (err
,lastId
) => {
20 res
.json(err
|| {cid: lastId
["rowid"]});
25 //router.get("/challengesbyplayer", access.logged, access.ajax, (req,res) => {
26 // if (req.query["uid"] != req.user._id)
27 // return res.json({errmsg: "Not your challenges"});
28 // let uid = ObjectID(req.query["uid"]);
29 // ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
30 // res.json(err || {challenges: challengeArray});
34 //function createChallenge(vid, from, to, res)
36 // ChallengeModel.create(vid, from, to, (err, chall) => {
38 // // A challenge can be sent using only name, thus 'to' is returned
45 //// from[, to][,nameTo]
46 //router.post("/challenges", access.logged, access.ajax, (req,res) => {
47 // if (req.body.from != req.user._id)
48 // return res.json({errmsg: "Identity usurpation"});
49 // let from = ObjectID(req.body.from);
50 // let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
51 // let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
52 // let vid = ObjectID(req.body.vid);
53 // if (!to && !!nameTo)
55 // UserModel.getByName(nameTo, (err,user) => {
56 // access.checkRequest(res, err, user, "Opponent not found", () => {
57 // createChallenge(vid, from, user._id, res);
62 // createChallenge(vid, from, to, res);
64 // createChallenge(vid, from, undefined, res); //automatch
67 //router.delete("/challenges", access.logged, access.ajax, (req,res) => {
68 // let cid = ObjectID(req.query.cid);
69 // ChallengeModel.getById(cid, (err,chall) => {
70 // access.checkRequest(res, err, chall, "Challenge not found", () => {
71 // if (!chall.from.equals(req.user._id) && !!chall.to && !chall.to.equals(req.user._id))
72 // return res.json({errmsg: "Not your challenge"});
73 // ChallengeModel.remove(cid, err => {
74 // res.json(err || {});
80 module
.exports
= router
;