Small fixes + add some debug traces
[vchess.git] / server / models / Challenge.js
CommitLineData
8d7e2786
BA
1var db = require("../utils/database");
2
3/*
00f2759e
BA
4 * Structure table Challenges:
5 * id: integer
6 * added: datetime
7 * uid: user id (int)
8 * vid: variant id (int)
9 * nbPlayers: integer
ab4f4bf2 10 * fen: varchar (optional)
052d17ea 11 * timeControl: string (3m+2s, 7d+1d ...)
00f2759e
BA
12 *
13 * Structure table WillPlay:
14 * cid: ref challenge id
15 * uid: ref user id
1f49533d 16 * yes: boolean (false means "not decided yet")
8d7e2786
BA
17 */
18
ab4f4bf2 19const ChallengeModel =
8d7e2786 20{
98db2082
BA
21 checkChallenge: function(c)
22 {
052d17ea
BA
23 if (!c.vid.match(/^[0-9]+$/))
24 return "Wrong variant ID";
98db2082 25
052d17ea
BA
26 if (!c.timeControl.match(/^[0-9dhms +]+$/))
27 return "Wrong characters in time control";
98db2082 28
052d17ea 29 if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
98db2082
BA
30 return "Bad FEN string";
31 },
32
1f49533d
BA
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
052d17ea 45 // fen cannot be undefined
ab4f4bf2
BA
46 create: function(c, cb)
47 {
48 db.serialize(function() {
49 let query =
74ea2e8d 50 "INSERT INTO Challenges " +
052d17ea 51 "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
74ea2e8d 52 "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
052d17ea 53 ",'" + c.fen + "'," + c.timeControl + ")";
ab4f4bf2
BA
54 db.run(query, err => {
55 if (!!err)
56 return cb(err);
57 db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
052d17ea 58 query =
ab4f4bf2 59 "INSERT INTO WillPlay VALUES " +
052d17ea 60 "(true," + lastId["rowid"] + "," + c.uid + ")";
1f49533d
BA
61 db.run(query, (err,ret) => {
62 cb(err, lastId); //all we need is the challenge ID
63 });
ab4f4bf2
BA
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);
f05815d7
BA
81 let condition = "";
82 if (!!challengeInfo.to[0])
83 condition = " AND u.name in (" + challengeInfo.to.join(",") + ")";
00f2759e 84 query =
ab4f4bf2
BA
85 "SELECT w.uid AS id, u.name " +
86 "FROM WillPlay w " +
87 "JOIN Users u " +
88 " ON w.uid = u.id " +
f05815d7 89 "WHERE w.cid = " + id + condition;
ab4f4bf2
BA
90 db.run(query, (err2,players) => {
91 if (!!err2)
92 return cb(err2);
93 const challenge = {
94 id: id,
74ea2e8d 95 uid: challengeInfo.uid,
ab4f4bf2
BA
96 vname: challengeInfo.name,
97 added: challengeInfo.added,
98 nbPlayers: challengeInfo.nbPlayers,
99 players: players, //currently in
100 fen: challengeInfo.fen,
052d17ea 101 timeControl: challengeInfo.timeControl,
ab4f4bf2
BA
102 };
103 return cb(null, challenge);
00f2759e
BA
104 });
105 });
106 });
ab4f4bf2 107 },
8d7e2786 108
ab4f4bf2
BA
109 getByUser: function(uid, cb)
110 {
111 db.serialize(function() {
112 const query =
113 "SELECT cid " +
114 "FROM WillPlay " +
115 "WHERE uid = " + uid;
116 db.run(query, (err,challIds) => {
117 if (!!err)
118 return cb(err);
f05815d7 119 challIds = challIds || [];
ab4f4bf2
BA
120 let challenges = [];
121 challIds.forEach(cidRow => {
122 ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
123 if (!!err2)
124 return cb(err2);
125 challenges.push(chall);
126 });
127 });
128 return cb(null, challenges);
00f2759e
BA
129 });
130 });
ab4f4bf2 131 },
8d7e2786 132
4edfed6c
BA
133 getSeatCount: function(id, cb)
134 {
135 db.serialize(function() {
136 let query =
137 "SELECT COUNT(*) AS scount " +
138 "FROM WillPlay " +
139 "WHERE cid = " + id;
140 db.get(query, (err,scRow) => {
141 if (!!err)
142 return cb(err);
143 query =
144 "SELECT nbPlayers " +
145 "FROM Challenges " +
146 "WHERE id = " + id;
147 db.get(query, (err2,chRow) => {
148 if (!!err2)
149 return cb(err2);
150 cb(chRow["nbPlayers"] - scRow["scount"]);
151 });
152 });
153 });
154 },
155
156 setSeat: function(id, uid)
157 {
158 // TODO: remove extra "db.serialize" (parallelize by default)
159 //db.serialize(function() {
160 const query =
161 "INSERT OR REPLACE INTO WillPlay " +
162 "VALUES (true," + id + "," + uid +")";
163 db.run(query);
164 //});
165 },
166
1f49533d 167 remove: function(id, uid)
ab4f4bf2 168 {
1f49533d 169 db.serialize(function() {
ab4f4bf2 170 let query =
4edfed6c
BA
171 "SELECT 1 " +
172 "FROM Challenges " +
173 "WHERE id = " + id + " AND uid = " + uid;
174 db.run(query, (err,rows) => {
175 if (rows.length > 0) //it's my challenge
36093eba 176 {
4edfed6c
BA
177 db.parallelize(function() {
178 query =
179 "DELETE FROM Challenges " +
180 "WHERE id = " + id;
181 db.run(query);
182 // Also remove matching WillPlay entries if a challenge was deleted
183 query =
184 "DELETE FROM WillPlay " +
185 "WHERE cid = " + id;
186 db.run(query);
187 });
36093eba
BA
188 }
189 });
ab4f4bf2
BA
190 });
191 },
8d7e2786 192}
ab4f4bf2
BA
193
194module.exports = ChallengeModel;