Some adjustments, next: fix time control handling + finish corr play
[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
8d7e2786 7
f05815d7 8router.get("/challenges", (req,res) => {
4edfed6c
BA
9 ChallengeModel.getByUser(req.query["uid"], (err,challenges) => {
10 res.json(err || {challenges:challenges});
11 });
12});
13
052d17ea 14router.post("/challenges", access.logged, access.ajax, (req,res) => {
1f49533d
BA
15 const error = ChallengeModel.checkChallenge(req.body.chall);
16 if (!!error)
17 return res.json({errmsg:error});
18 const challenge =
19 {
20 fen: req.body.chall.fen,
21 timeControl: req.body.chall.timeControl,
22 vid: req.body.chall.vid,
23 uid: req.userId,
24 nbPlayers: req.body.chall.to.length,
25 };
26 ChallengeModel.create(challenge, (err,lastId) => {
27 if (!!err)
28 return res.json(err);
29 if (!!req.body.chall.to[0])
30 {
31 UserModel.getByName(req.body.chall.to, (err,users) => {
32 if (!!err)
33 return res.json(err);
34 if (users.length < req.body.chall.to.length)
35 return res.json({errmsg: "Typo in player(s) name(s)"});
36 ChallengeModel.initializeWillPlay(
37 users.map(u => u.id),
38 lastId["rowid"],
39 (err) => {
40 if (!!err)
41 return res.json(err);
42 res.json({cid: lastId["rowid"]});
43 }
44 );
45 });
46 }
47 else
48 res.json({cid: lastId["rowid"]});
49 });
8d7e2786
BA
50});
51
4edfed6c
BA
52// Nothing to do if challenge is refused (just removal)
53router.put("/challenges", access.logged, access.ajax, (req,res) => {
6fba6e0c
BA
54 // Accept challenge: turn WillPlay to true; if then challenge is full, launch game
55 ChallengeModel.getSeatCount(req.body.id, (scount) => {
56 if (scount == 1)
57 launchGame(req.body.id, req.userId);
58 else
59 ChallengeModel.setSeat(req.body.id, req.userId);
60 });
4edfed6c
BA
61 res.json({});
62});
63
64function launchGame(cid, uid)
65{
66 // TODO: gather challenge infos + WillPlay
67 // Then create game, and remove challenge + WillPlay
68}
69
ab4f4bf2 70//// index
f4f4c03c 71//router.get("/challenges", access.logged, access.ajax, (req,res) => {
1f49533d
BA
72// if (req.query["uid"] != req.user._id)
73// return res.json({errmsg: "Not your challenges"});
74// let uid = ObjectID(req.query["uid"]);
75// ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
76// res.json(err || {challenges: challengeArray});
77// });
ab4f4bf2
BA
78//});
79//
80//function createChallenge(vid, from, to, res)
81//{
1f49533d
BA
82// ChallengeModel.create(vid, from, to, (err, chall) => {
83// res.json(err || {
84// // A challenge can be sent using only name, thus 'to' is returned
85// to: chall.to,
86// cid: chall._id
87// });
88// });
ab4f4bf2 89//}
1f49533d
BA
90
91router.delete("/challenges", access.logged, access.ajax, (req,res) => {
77fd7298 92 const cid = req.query.id;
1f49533d
BA
93 ChallengeModel.remove(cid, req.userId, err => {
94 res.json(err || {});
95 });
96});
8d7e2786
BA
97
98module.exports = router;