Advance on client side
[vchess.git] / server / routes / challenges.js
1 // AJAX methods to get, create, update or delete a challenge
2
3 let router = require("express").Router();
4 const access = require("../utils/access");
5 const ChallengeModel = require("../models/Challenge");
6
7 router.post("/challenges/:vid([0-9]+)", access.logged, access.ajax, (req,res) => {
8 const vid = req.params["vid"];
9 const chall = {
10 uid: req.userId,
11 vid: vid,
12 fen: req.body["fen"],
13 mainTime: req.body["mainTime"],
14 increment: req.body["increment"],
15 nbPlayers: req.body["nbPlayers"],
16 players: req.body["players"],
17 };
18 const error = ChallengeModel.checkChallenge(chall);
19 ChallengeModel.create(chall, (err,lastId) => {
20 res.json(err || {cid: lastId["rowid"]});
21 });
22 });
23
24 //// index
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});
31 // });
32 //});
33 //
34 //function createChallenge(vid, from, to, res)
35 //{
36 // ChallengeModel.create(vid, from, to, (err, chall) => {
37 // res.json(err || {
38 // // A challenge can be sent using only name, thus 'to' is returned
39 // to: chall.to,
40 // cid: chall._id
41 // });
42 // });
43 //}
44 //
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)
54 // {
55 // UserModel.getByName(nameTo, (err,user) => {
56 // access.checkRequest(res, err, user, "Opponent not found", () => {
57 // createChallenge(vid, from, user._id, res);
58 // });
59 // });
60 // }
61 // else if (!!to)
62 // createChallenge(vid, from, to, res);
63 // else
64 // createChallenge(vid, from, undefined, res); //automatch
65 //});
66 //
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 || {});
75 // });
76 // });
77 // });
78 //});
79
80 module.exports = router;