var cron = require('node-cron');
var UserModel = require("../models/User");
-var ChallengeModel = require("../models/Challenge");
var GameModel = require("../models/Game");
cron.schedule('0 0 0 * * *', function() {
- // Remove some old users, challenges and games every 24h
+ // Remove unlogged users and unstarted games every 24h
UserModel.cleanUsersDb();
- ChallengeModel.removeOld();
GameModel.cleanGamesDb();
});
});
});
},
-
- // Remove challenges older than 1 month, and 1to1 older than 2 days
- removeOld: function()
- {
- const tsNow = Date.now();
- // 86400000 = 24 hours in milliseconds
- const day = 86400000;
- db.serialize(function() {
- const query =
- "SELECT id, target, added " +
- "FROM Challenges";
- db.all(query, (err, challenges) => {
- challenges.forEach(c => {
- if ((!c.target && tsNow - c.added > 30*day) ||
- (!!c.target && tsNow - c.added > 2*day))
- {
- db.run("DELETE FROM Challenges WHERE id = " + c.id);
- }
- });
- });
- });
- },
}
module.exports = ChallengeModel;
const day = 86400000;
db.serialize(function() {
let query =
- "SELECT id,score,created " +
+ "SELECT id,created " +
"FROM Games ";
db.all(query, (err,games) => {
games.forEach(g => {
query =
- "SELECT max(played) AS lastMaj " +
+ "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
"FROM Moves " +
"WHERE gid = " + g.id;
- db.get(query, (err2,updated) => {
- if (!updated.lastMaj)
+ db.get(query, (err2,mstats) => {
+ // Remove games still not really started,
+ // with no action in the last 3 months:
+ if ((mstats.nbMoves == 0 && tsNow - g.created > 91*day) ||
+ (mstats.nbMoves == 1 && tsNow - mstats.lastMaj > 91*day))
{
- if (tsNow - g.created > 7*day)
- return GameModel.remove(g.id);
- }
- else //at least one move
- {
- const lastMaj = updated.lastMaj;
- if (g.score != "*" && tsNow - lastMaj > 7*day ||
- g.score == "*" && tsNow - lastMaj > 91*day)
- {
- GameModel.remove(g.id);
- }
+ return GameModel.remove(g.id);
}
});
});