c988aba5cd76433cbec08514a6fff57b8ffad5a0
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
, 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
);
70 remove: function(id
, uid
, cb
)
72 db
.serialize(function() {
76 "WHERE id = " + id
+ " AND uid = " + uid
;
77 db
.get(query
, (err
,chall
) => {
79 return cb({errmsg: "Not your challenge"});
81 "DELETE FROM Challenges " +
90 module
.exports
= ChallengeModel
;