Forbid castling when king doesn't move and is under check
[vchess.git] / server / routes / challenges.js
CommitLineData
ab4f4bf2 1// AJAX methods to get, create, update or delete a challenge
582df349 2
ab4f4bf2
BA
3let router = require("express").Router();
4const access = require("../utils/access");
5const ChallengeModel = require("../models/Challenge");
052d17ea 6const UserModel = require("../models/User"); //for name check
fe4c7e67 7const params = require("../config/parameters");
8d7e2786 8
f05815d7 9router.get("/challenges", (req,res) => {
99b7a14c
BA
10 if (!req.query["uid"].match(/^[0-9]+$/))
11 res.json({errmsg: "Bad user ID"});
4edfed6c
BA
12 ChallengeModel.getByUser(req.query["uid"], (err,challenges) => {
13 res.json(err || {challenges:challenges});
14 });
15});
16
052d17ea 17router.post("/challenges", access.logged, access.ajax, (req,res) => {
1f49533d
BA
18 const error = ChallengeModel.checkChallenge(req.body.chall);
19 if (!!error)
20 return res.json({errmsg:error});
bf20f404 21 let challenge =
1f49533d
BA
22 {
23 fen: req.body.chall.fen,
24 timeControl: req.body.chall.timeControl,
25 vid: req.body.chall.vid,
26 uid: req.userId,
bf20f404 27 to: req.body.chall.to, //string: user name (may be empty)
1f49533d 28 };
bf20f404 29 const insertChallenge = () => {
b4de2e73
BA
30 ChallengeModel.create(challenge, (err,ret) => {
31 return res.json(err || {cid:ret.cid});
bf20f404
BA
32 });
33 };
34 if (!!req.body.chall.to)
35 {
36 UserModel.getOne("name", challenge.to, (err,user) => {
37 if (!!err || !user)
38 return res.json(err | {errmsg: "Typo in player name"});
39 challenge.to = user.id; //ready now to insert challenge
bebcc8d4 40 insertChallenge();
fe4c7e67
BA
41 if (user.notify)
42 UserModel.notify(user, "New challenge: " + params.siteURL + "/");
bf20f404 43 });
bf20f404
BA
44 }
45 else
46 insertChallenge();
4edfed6c
BA
47});
48
1f49533d 49router.delete("/challenges", access.logged, access.ajax, (req,res) => {
77fd7298 50 const cid = req.query.id;
99b7a14c
BA
51 if (!cid.match(/^[0-9]+$/))
52 res.json({errmsg: "Bad challenge ID"});
2be5d614 53 ChallengeModel.safeRemove(cid, req.userId, err => {
b1aa927b 54 res.json(err || {}); //TODO: just "return err" because is empty if no errors
1f49533d
BA
55 });
56});
8d7e2786
BA
57
58module.exports = router;