ba805aa1701fc8480608ec7bacf2cb2d38ee858b
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
);
98 // Remove challenges older than 1 month, and 1to1 older than 36h
99 //exports.removeOld = function()
101 // var tsNow = new Date().getTime();
102 // // 86400000 = 24 hours in milliseconds
103 // var day = 86400000;
104 // db.challenges.find({}, (err,challengeArray) => {
105 // challengeArray.forEach( c => {
106 // if (c._id.getTimestamp() + 30*day < tsNow //automatch
107 // || (!!c.to && c._id.getTimestamp() + 1.5*day < tsNow)) //1 to 1
109 // db.challenges.remove({"_id": c._id});
115 module
.exports
= ChallengeModel
;