X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=77bbd27a602fe696d4978fb83a552fada88389c2;hb=83494c7fbd83fafa28c5a434a1c83f56c17e3d04;hp=0e42292eae3a4c148fada807452aa198cc54f8a8;hpb=afc426016772300cc2812e8f96dd964bc6ae739d;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index 0e42292e..77bbd27a 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -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 { 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;