* fen: varchar (current position)
* timeControl: string
* score: varchar (result)
+ * created: datetime
*
* Structure table Players:
* gid: ref game id
* Structure table Moves:
* gid: ref game id
* squares: varchar (description)
- * message: text
* played: datetime
* idx: integer
- * color: character
+ *
+ * Structure table Chats:
+ * gid: game id (int)
+ * msg: varchar
+ * name: varchar
+ * sid: varchar (socket ID when sending message)
+ * added: datetime
*/
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, drawOffer)"
+ + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
+ + timeControl + "'," + Date.now() + "," + false + ")";
db.run(query, function(err) {
if (!!err)
return cb(err);
if (!!err2)
return cb(err2);
query =
- "SELECT squares, message, played, idx, color " +
+ "SELECT squares, played, idx " +
"FROM Moves " +
"WHERE gid = " + id;
db.all(query, (err3,moves) => {
if (!!err3)
return cb(err3);
- const game = Object.assign({},
- gameInfo,
- {
- players: players,
- moves: moves
- }
- );
- return cb(null, game);
+ query =
+ "SELECT msg, name, sid, added " +
+ "FROM Chats " +
+ "WHERE gid = " + id;
+ db.all(query, (err4,chats) => {
+ if (!!err4)
+ return cb(err4);
+ const game = Object.assign({},
+ gameInfo,
+ {
+ players: players,
+ moves: moves,
+ chats: chats,
+ }
+ );
+ return cb(null, game);
+ });
});
});
});
"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);
+ }
});
});
},
});
},
- // obj can have fields move, fen and/or score
+ // obj can have fields move, chat, fen, drawOffer and/or score
update: function(id, obj)
{
db.parallelize(function() {
let query =
"UPDATE Games " +
"SET ";
+ let modifs = "";
+ if (!!obj.message)
+ modifs += "message = message || ' ' || '" + obj.message + "',";
+ if (!!obj.drawOffer)
+ modifs += "drawOffer = " + obj.drawOffer + ",";
if (!!obj.fen)
- query += "fen = '" + obj.fen + "',";
+ modifs += "fen = '" + obj.fen + "',";
if (!!obj.score)
- query += "score = '" + obj.score + "',";
- query = query.slice(0,-1); //remove last comma
- query += " WHERE id = " + id;
- db.run(query);
+ modifs += "score = '" + obj.score + "',";
+ modifs = modifs.slice(0,-1); //remove last comma
+ if (modifs.length > 0)
+ {
+ query += modifs + " WHERE id = " + id;
+ db.run(query);
+ }
if (!!obj.move)
{
const m = obj.move;
query =
- "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " +
- "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
- "'," + m.played + "," + m.idx + ",'" + m.color + "')";
+ "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
+ "(" + id + ",'" + JSON.stringify(m.squares) + "',"
+ + m.played + "," + m.idx + ")";
+ db.run(query);
+ }
+ if (!!obj.chat)
+ {
+ query =
+ "INSERT INTO Chats (gid, msg, name, sid, added) VALUES " +
+ "(" + id + ",'" + obj.chat.msg + "','" + obj.chat.name +
+ "','" + obj.chat.sid + "'," + Date.now() + ")";
db.run(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,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;