602531f4456c482d92112e3da2f107477c405af4
[vchess.git] / server / models / Challenge.js
1 var db = require("../utils/database");
2
3 /*
4 * Structure:
5 * id: integer
6 * added: datetime
7 * uid: user id (int)
8 * target: recipient id (optional)
9 * vid: variant id (int)
10 * fen: varchar (optional)
11 * timeControl: string (3m+2s, 7d+1d ...)
12 */
13
14 const ChallengeModel =
15 {
16 checkChallenge: function(c)
17 {
18 if (!c.vid.match(/^[0-9]+$/))
19 return "Wrong variant ID";
20
21 if (!c.timeControl.match(/^[0-9dhms +]+$/))
22 return "Wrong characters in time control";
23
24 if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
25 return "Bad FEN string";
26 },
27
28 // fen cannot be undefined
29 create: function(c, cb)
30 {
31 db.serialize(function() {
32 let query =
33 "INSERT INTO Challenges " +
34 "(added, uid, " + (!!c.to ? "target, " : "") +
35 "vid, fen, timeControl) VALUES " +
36 "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
37 c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
38 db.run(query, err => {
39 if (!!err)
40 return cb(err);
41 db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
42 return cb(err2, lastId);
43 });
44 });
45 });
46 },
47
48 getOne: function(id, cb)
49 {
50 db.serialize(function() {
51 let query =
52 "SELECT * " +
53 "FROM Challenges " +
54 "WHERE id = " + id;
55 db.get(query, (err,challengeInfo) => {
56 if (!!err)
57 return cb(err);
58 let condition = "";
59 if (!!challengeInfo.to)
60 condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")";
61 else
62 condition = "= " + challengeInfo.uid;
63 query =
64 "SELECT id, name " +
65 "FROM Users " +
66 "WHERE id " + condition;
67 db.run(query, (err2,players) => {
68 if (!!err2)
69 return cb(err2);
70 const challenge = {
71 id: id,
72 uid: challengeInfo.uid, //sender (but we don't know who ask)
73 vid: challengeInfo.vid,
74 added: challengeInfo.added,
75 players: players, //sender + potential receiver
76 fen: challengeInfo.fen,
77 timeControl: challengeInfo.timeControl,
78 };
79 return cb(null, challenge);
80 });
81 });
82 });
83 },
84
85 getByUser: function(uid, cb)
86 {
87 db.serialize(function() {
88 const query =
89 "SELECT cid " +
90 "FROM WillPlay " +
91 "WHERE uid = " + uid;
92 db.run(query, (err,challIds) => {
93 if (!!err)
94 return cb(err);
95 challIds = challIds || [];
96 let challenges = [];
97 challIds.forEach(cidRow => {
98 ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
99 if (!!err2)
100 return cb(err2);
101 challenges.push(chall);
102 });
103 });
104 return cb(null, challenges);
105 });
106 });
107 },
108
109 remove: function(id, uid)
110 {
111 db.serialize(function() {
112 let query =
113 "SELECT 1 " +
114 "FROM Challenges " +
115 "WHERE id = " + id + " AND uid = " + uid;
116 db.run(query, (err,rows) => {
117 if (rows.length == 0)
118 return res.json({errmsg: "Not your challenge"});
119 query =
120 "DELETE FROM Challenges " +
121 "WHERE id = " + id;
122 db.run(query);
123 });
124 });
125 },
126 }
127
128 module.exports = ChallengeModel;