+ remove: function(id)
+ {
+ db.parallelize(function() {
+ let query =
+ "DELETE FROM Games " +
+ "WHERE id = " + id;
+ db.run(query);
+ query =
+ "DELETE FROM Players " +
+ "WHERE gid = " + id;
+ db.run(query);
+ query =
+ "DELETE FROM Moves " +
+ "WHERE gid = " + id;
+ db.run(query);
+ query =
+ "DELETE FROM Chats " +
+ "WHERE gid = " + id;
+ db.run(query);
+ });
+ },
+
+ cleanGamesDb: function()
+ {
+ const tsNow = Date.now();
+ // 86400000 = 24 hours in milliseconds
+ const day = 86400000;
+ db.serialize(function() {
+ let query =
+ "SELECT id, created " +
+ "FROM Games ";
+ db.all(query, (err,games) => {
+ games.forEach(g => {
+ query =
+ "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
+ "FROM Moves " +
+ "WHERE gid = " + g.id;
+ 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))
+ {
+ GameModel.remove(g.id);
+ }
+ });
+ });
+ });
+ });
+ },