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");
6 const checkChallenge
= require("../public/javascripts/shared/challengeCheck.js");
8 router
.post("/challenges/:vid([0-9]+)", access
.logged
, access
.ajax
, (req
,res
) => {
9 const vid
= req
.params
["vid"];
14 mainTime: req
.body
["mainTime"],
15 increment: req
.body
["increment"],
16 nbPlayers: req
.body
["nbPlayers"],
17 players: req
.body
["players"],
19 const error
= checkChallenge(chall
);
20 ChallengeModel
.create(chall
, (err
,lastId
) => {
21 res
.json(err
|| {cid: lastId
["rowid"]});
26 //router.get("/challengesbyplayer", access.logged, access.ajax, (req,res) => {
27 // if (req.query["uid"] != req.user._id)
28 // return res.json({errmsg: "Not your challenges"});
29 // let uid = ObjectID(req.query["uid"]);
30 // ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
31 // res.json(err || {challenges: challengeArray});
35 //function createChallenge(vid, from, to, res)
37 // ChallengeModel.create(vid, from, to, (err, chall) => {
39 // // A challenge can be sent using only name, thus 'to' is returned
46 //// from[, to][,nameTo]
47 //router.post("/challenges", access.logged, access.ajax, (req,res) => {
48 // if (req.body.from != req.user._id)
49 // return res.json({errmsg: "Identity usurpation"});
50 // let from = ObjectID(req.body.from);
51 // let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
52 // let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
53 // let vid = ObjectID(req.body.vid);
54 // if (!to && !!nameTo)
56 // UserModel.getByName(nameTo, (err,user) => {
57 // access.checkRequest(res, err, user, "Opponent not found", () => {
58 // createChallenge(vid, from, user._id, res);
63 // createChallenge(vid, from, to, res);
65 // createChallenge(vid, from, undefined, res); //automatch
68 //router.delete("/challenges", access.logged, access.ajax, (req,res) => {
69 // let cid = ObjectID(req.query.cid);
70 // ChallengeModel.getById(cid, (err,chall) => {
71 // access.checkRequest(res, err, chall, "Challenge not found", () => {
72 // if (!chall.from.equals(req.user._id) && !!chall.to && !chall.to.equals(req.user._id))
73 // return res.json({errmsg: "Not your challenge"});
74 // ChallengeModel.remove(cid, err => {
75 // res.json(err || {});
81 module
.exports
= router
;