1 var db
= require("../utils/database");
8 * target: recipient id (optional)
9 * vid: variant id (int)
10 * fen: varchar (optional)
11 * timeControl: string (3m+2s, 7d+1d ...)
14 const ChallengeModel
=
16 checkChallenge: function(c
)
18 if (!c
.vid
.toString().match(/^[0-9]+$/))
19 return "Wrong variant ID";
20 if (!c
.timeControl
.match(/^[0-9dhms
+]+$/))
21 return "Wrong characters in time control";
22 if (!c
.fen
.match(/^[a-zA-Z0-9, /-]*$/))
23 return "Bad FEN string";
27 // fen cannot be undefined
28 create: function(c
, cb
)
30 db
.serialize(function() {
32 "INSERT INTO Challenges " +
33 "(added, uid, " + (!!c
.to
? "target, " : "") +
34 "vid, fen, timeControl) VALUES " +
35 "(" + Date
.now() + "," + c
.uid
+ "," + (!!c
.to
? c
.to
+ "," : "") +
36 c
.vid
+ ",'" + c
.fen
+ "','" + c
.timeControl
+ "')";
37 db
.run(query
, function(err
) {
38 return cb(err
, {cid: this.lastID
});
43 getOne: function(id
, cb
)
45 db
.serialize(function() {
50 db
.get(query
, (err
,challenge
) => {
51 return cb(err
, challenge
);
56 // all challenges except where target is defined and not me
57 getByUser: function(uid
, cb
)
59 db
.serialize(function() {
63 "WHERE target IS NULL OR target = " + uid
;
64 db
.all(query
, (err
,challenges
) => {
65 return cb(err
, challenges
);
72 db
.serialize(function() {
74 "DELETE FROM Challenges " +
80 safeRemove: function(id
, uid
, cb
)
82 db
.serialize(function() {
86 "WHERE id = " + id
+ " AND uid = " + uid
;
87 db
.get(query
, (err
,chall
) => {
89 return cb({errmsg: "Not your challenge"});
90 ChallengeModel
.remove(id
);
97 module
.exports
= ChallengeModel
;