+ // obj can have fields move, chat, fen, drawOffer and/or score + message
+ update: function(id, obj, cb) {
+ db.parallelize(function() {
+ let query =
+ "UPDATE Games " +
+ "SET ";
+ let modifs = "";
+ // NOTE: if drawOffer is set, we should check that it's player's turn
+ // A bit overcomplicated. Let's trust the client on that for now...
+ if (!!obj.drawOffer)
+ {
+ if (obj.drawOffer == "n") //special "None" update
+ obj.drawOffer = "";
+ modifs += "drawOffer = '" + obj.drawOffer + "',";
+ }
+ if (!!obj.rematchOffer)
+ {
+ if (obj.rematchOffer == "n") //special "None" update
+ obj.rematchOffer = "";
+ modifs += "rematchOffer = '" + obj.rematchOffer + "',";
+ }
+ if (!!obj.fen)
+ modifs += "fen = '" + obj.fen + "',";
+ if (!!obj.score)
+ modifs += "score = '" + obj.score + "',";
+ if (!!obj.scoreMsg)
+ modifs += "scoreMsg = '" + obj.scoreMsg + "',";
+ if (!!obj.deletedBy) {
+ const myColor = obj.deletedBy == 'w' ? "White" : "Black";
+ modifs += "deletedBy" + myColor + " = true,";
+ }
+ modifs = modifs.slice(0,-1); //remove last comma
+ if (modifs.length > 0)
+ {
+ query += modifs + " WHERE id = " + id;
+ db.run(query);
+ }
+ // NOTE: move, chat and delchat are mutually exclusive
+ if (!!obj.move)
+ {
+ // Security: only update moves if index is right
+ query =
+ "SELECT MAX(idx) AS maxIdx " +
+ "FROM Moves " +
+ "WHERE gid = " + id;
+ db.get(query, (err,ret) => {
+ const m = obj.move;
+ if (!ret.maxIdx || ret.maxIdx + 1 == m.idx) {
+ query =
+ "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
+ "(" + id + ",?," + m.played + "," + m.idx + ")";
+ db.run(query, JSON.stringify(m.squares));
+ cb(null);
+ }
+ else cb({errmsg:"Wrong move index"});
+ });
+ }
+ else cb(null);
+ if (!!obj.chat)
+ {
+ query =
+ "INSERT INTO Chats (gid, msg, name, added) VALUES ("
+ + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")";
+ db.run(query, obj.chat.msg);
+ }
+ else if (obj.delchat)
+ {
+ query =
+ "DELETE " +
+ "FROM Chats " +
+ "WHERE gid = " + id;
+ db.run(query);
+ }
+ if (!!obj.deletedBy) {
+ // Did my opponent delete it too?
+ let selection =
+ "deletedBy" +
+ (obj.deletedBy == 'w' ? "Black" : "White") +
+ " AS deletedByOpp";
+ query =
+ "SELECT " + selection + " " +
+ "FROM Games " +
+ "WHERE id = " + id;
+ db.get(query, (err,ret) => {
+ // If yes: just remove game
+ if (!!ret.deletedByOpp) GameModel.remove(id);
+ });
+ }
+ });
+ },
+
+ remove: function(id_s) {
+ const suffix =
+ Array.isArray(id_s)
+ ? " IN (" + id_s.join(",") + ")"
+ : " = " + id_s;
+ db.parallelize(function() {
+ let query =
+ "DELETE FROM Games " +
+ "WHERE id " + suffix;
+ db.run(query);
+ query =
+ "DELETE FROM Moves " +
+ "WHERE gid " + suffix;
+ db.run(query);
+ query =
+ "DELETE FROM Chats " +
+ "WHERE gid " + suffix;
+ 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) => {
+ query =
+ "SELECT gid, count(*) AS nbMoves, MAX(played) AS lastMaj " +
+ "FROM Moves " +
+ "GROUP BY gid";
+ db.get(query, (err2, mstats) => {
+ // Reorganize moves data to avoid too many array lookups:
+ let movesGroups = {};
+ mstats.forEach(ms => {
+ movesGroups[ms.gid] = {
+ nbMoves: ms.nbMoves,
+ lastMaj: ms.lastMaj
+ };
+ });
+ // Remove games still not really started,
+ // with no action in the last 3 months:
+ let toRemove = [];
+ games.forEach(g => {
+ if (
+ (
+ !movesGroups[g.id] &&
+ tsNow - g.created > 91*day
+ )
+ ||
+ (
+ movesGroups[g.id].nbMoves == 1 &&
+ tsNow - movesGroups[g.id].lastMaj > 91*day
+ )
+ ) {
+ toRemove.push(g.id);
+ }
+ });
+ if (toRemove.length > 0) GameModel.remove(toRemove);
+ });
+ });
+ });
+ }