Remove test instructions, fix CORS for DELETE method
[vchess.git] / server / models / Challenge.js
1 var db = require("../utils/database");
2
3 /*
4 * Structure table Challenges:
5 * id: integer
6 * added: datetime
7 * uid: user id (int)
8 * vid: variant id (int)
9 * nbPlayers: integer
10 * fen: varchar (optional)
11 * timeControl: string (3m+2s, 7d+1d ...)
12 *
13 * Structure table WillPlay:
14 * cid: ref challenge id
15 * uid: ref user id
16 * yes: boolean (false means "not decided yet")
17 */
18
19 const ChallengeModel =
20 {
21 checkChallenge: function(c)
22 {
23 if (!c.vid.match(/^[0-9]+$/))
24 return "Wrong variant ID";
25
26 if (!c.timeControl.match(/^[0-9dhms +]+$/))
27 return "Wrong characters in time control";
28
29 if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
30 return "Bad FEN string";
31 },
32
33 initializeWillPlay: function(uids, cid, cb)
34 {
35 let query = "INSERT INTO WillPlay VALUES ";
36 for (let i=0; i<uids.length; i++)
37 {
38 query += "(false," + cid + "," + uids[i] + ")";
39 if (i < uids.length-1)
40 query += ",";
41 }
42 db.run(query, cb);
43 },
44
45 // fen cannot be undefined
46 create: function(c, cb)
47 {
48 db.serialize(function() {
49 let query =
50 "INSERT INTO Challenges " +
51 "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
52 "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
53 ",'" + c.fen + "'," + c.timeControl + ")";
54 db.run(query, err => {
55 if (!!err)
56 return cb(err);
57 db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
58 query =
59 "INSERT INTO WillPlay VALUES " +
60 "(true," + lastId["rowid"] + "," + c.uid + ")";
61 db.run(query, (err,ret) => {
62 cb(err, lastId); //all we need is the challenge ID
63 });
64 });
65 });
66 });
67 },
68
69 getOne: function(id, cb)
70 {
71 db.serialize(function() {
72 let query =
73 "SELECT * " +
74 "FROM Challenges c " +
75 "JOIN Variants v " +
76 " ON c.vid = v.id "
77 "WHERE id = " + id;
78 db.get(query, (err,challengeInfo) => {
79 if (!!err)
80 return cb(err);
81 query =
82 "SELECT w.uid AS id, u.name " +
83 "FROM WillPlay w " +
84 "JOIN Users u " +
85 " ON w.uid = u.id " +
86 "WHERE w.cid = " + id;
87 db.run(query, (err2,players) => {
88 if (!!err2)
89 return cb(err2);
90 const challenge = {
91 id: id,
92 uid: challengeInfo.uid,
93 vname: challengeInfo.name,
94 added: challengeInfo.added,
95 nbPlayers: challengeInfo.nbPlayers,
96 players: players, //currently in
97 fen: challengeInfo.fen,
98 timeControl: challengeInfo.timeControl,
99 };
100 return cb(null, challenge);
101 });
102 });
103 });
104 },
105
106 getByUser: function(uid, cb)
107 {
108 db.serialize(function() {
109 const query =
110 "SELECT cid " +
111 "FROM WillPlay " +
112 "WHERE uid = " + uid;
113 db.run(query, (err,challIds) => {
114 if (!!err)
115 return cb(err);
116 let challenges = [];
117 challIds.forEach(cidRow => {
118 ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
119 if (!!err2)
120 return cb(err2);
121 challenges.push(chall);
122 });
123 });
124 return cb(null, challenges);
125 });
126 });
127 },
128
129 remove: function(id, uid)
130 {
131 db.serialize(function() {
132 let query =
133 "DELETE FROM Challenges " +
134 "WHERE id = " + id + " AND uid = " + uid;
135 db.run(query, (err,ret) => {
136 if (!err && ret >= 1)
137 {
138 // Also remove matching WillPlay entries if a challenge was deleted
139 query =
140 "DELETE FROM WillPlay " +
141 "WHERE cid = " + id;
142 db.run(query);
143 }
144 });
145 });
146 },
147 }
148
149 module.exports = ChallengeModel;