1 var db
= require("../utils/database");
8 * to: player ID, undefined if automatch
11 exports
.create = function(vid
, from, to
, callback
)
19 db
.challenges
.insert(chall
, callback
);
25 exports
.getById = function(cid
, callback
)
27 db
.challenges
.findOne({_id: cid
}, callback
);
30 // For index page: obtain challenges that the player can accept
31 exports
.getByPlayer = function(uid
, callback
)
33 db
.challenges
.aggregate(
36 {$and: [{"from": {$ne: uid
}}, {"to": {$exists: false}}]}
38 {$project: {_id:0, vid:1}},
39 {$group: {_id:"$vid", count:{$sum:1}}},
43 // For variant page (challenges related to a player)
44 exports
.getByVariant = function(uid
, vid
, callback
)
46 db
.challenges
.find({$and: [
51 {"to": {$exists: false}},
59 exports
.remove = function(cid
, callback
)
61 db
.challenges
.remove({_id: cid
}, callback
);
64 // Remove challenges older than 1 month, and 1to1 older than 36h
65 exports
.removeOld = function()
67 var tsNow
= new Date().getTime();
68 // 86400000 = 24 hours in milliseconds
70 db
.challenges
.find({}, (err
,challengeArray
) => {
71 challengeArray
.forEach( c
=> {
72 if (c
._id
.getTimestamp() + 30*day
< tsNow
//automatch
73 || (!!c
.to
&& c
._id
.getTimestamp() + 1.5*day
< tsNow
)) //1 to 1
75 db
.challenges
.remove({"_id": c
._id
});