Implement cleaning methods (CRON tasks)
[vchess.git] / server / models / Game.js
index 0e42292..77bbd27 100644 (file)
@@ -8,6 +8,7 @@ var db = require("../utils/database");
  *   fen: varchar (current position)
  *   timeControl: string
  *   score: varchar (result)
+ *   created: datetime
  *
  * Structure table Players:
  *   gid: ref game id
@@ -20,7 +21,6 @@ var db = require("../utils/database");
  *   message: text
  *   played: datetime
  *   idx: integer
- *   color: character
  */
 
 const GameModel =
@@ -29,8 +29,9 @@ const GameModel =
        {
                db.serialize(function() {
                        let query =
-                               "INSERT INTO Games (vid, fenStart, fen, score, timeControl) VALUES " +
-        "(" + vid + ",'" + fen + "','" + fen + "','*','" + timeControl + "')";
+                               "INSERT INTO Games (vid, fenStart, fen, score, timeControl, created)"
+        + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
+        + timeControl + "'," + Date.now() + ")";
       db.run(query, function(err) {
         if (!!err)
           return cb(err);
@@ -71,7 +72,7 @@ const GameModel =
                                        if (!!err2)
                                                return cb(err2);
                                        query =
-                                               "SELECT squares, message, played, idx, color " +
+                                               "SELECT squares, message, played, idx " +
                                                "FROM Moves " +
                                                "WHERE gid = " + id;
                                        db.all(query, (err3,moves) => {
@@ -99,19 +100,22 @@ const GameModel =
                                "SELECT gid " +
                                "FROM Players " +
                                "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
-                       db.run(query, (err,gameIds) => {
+                       db.all(query, (err,gameIds) => {
                                if (!!err)
                                        return cb(err);
         gameIds = gameIds || []; //might be empty
                                let gameArray = [];
-                               gameIds.forEach(gidRow => {
-                                       GameModel.getOne(gidRow["gid"], (err2,game) => {
+                               for (let i=0; i<gameIds.length; i++)
+                               {
+                                       GameModel.getOne(gameIds[i]["gid"], (err2,game) => {
                                                if (!!err2)
                                                        return cb(err2);
                                                gameArray.push(game);
+                                               // Call callback function only when gameArray is complete:
+                                               if (i == gameIds.length - 1)
+                                                       return cb(null, gameArray);
                                        });
-                               });
-                               return cb(null, gameArray);
+                               }
                        });
                });
        },
@@ -147,9 +151,9 @@ const GameModel =
       {
         const m = obj.move;
         query =
-          "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " +
+          "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
           "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
-            "'," + m.played + "," + m.idx + ",'" + m.color + "')";
+            "'," + m.played + "," + m.idx + ")";
         db.run(query);
       }
     });
@@ -172,6 +176,36 @@ const GameModel =
                        db.run(query);
                });
        },
+
+  cleanGamesDb: function()
+  {
+    const tsNow = Date.now();
+    // 86400000 = 24 hours in milliseconds
+    const day = 86400000;
+    db.serialize(function() {
+      let query =
+        "SELECT id,score " +
+        "FROM Games ";
+      db.all(query, (err,games) => {
+        games.forEach(g => {
+          query =
+            "SELECT max(played) AS lastMaj " +
+            "FROM Moves " +
+            "WHERE gid = " + g.id;
+          db.get(query, (err2,updated) {
+            if (!updated && tsNow - g.created > 7*day)
+              return GameModel.remove(g.id);
+            const lastMaj = updated.lastMaj;
+            if (g.score != "*" && tsNow - lastMaj > 7*day ||
+              g.score == "*" && tsNow - lastMaj > 91*day)
+            {
+              GameModel.remove(g.id);
+            }
+          });
+        });
+      });
+    });
+  },
 }
 
 module.exports = GameModel;