* vid: integer (variant id)
* fenStart: varchar (initial position)
* fen: varchar (current position)
- * timeControl: string
+ * cadence: string
* score: varchar (result)
* scoreMsg: varchar ("Time", "Mutual agreement"...)
* created: datetime
checkGameInfo: function(g) {
if (!g.vid.toString().match(/^[0-9]+$/))
return "Wrong variant ID";
- if (!g.vname.match(/^[a-zA-Z0-9]+$/))
- return "Wrong variant name";
- if (!g.timeControl.match(/^[0-9dhms +]+$/))
+ if (!g.cadence.match(/^[0-9dhms +]+$/))
return "Wrong characters in time control";
if (!g.fen.match(/^[a-zA-Z0-9, /-]*$/))
return "Bad FEN string";
return "";
},
- create: function(vid, fen, timeControl, players, cb)
+ create: function(vid, fen, cadence, players, cb)
{
db.serialize(function() {
let query =
- "INSERT INTO Games"
- + " (vid, fenStart, fen, score, timeControl, created, drawOffer)"
- + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
- + timeControl + "'," + Date.now() + ",'')";
+ "INSERT INTO Games " +
+ "(vid, fenStart, fen, score, cadence, created, drawOffer) " +
+ "VALUES " +
+ "(" + vid + ",'" + fen + "','" + fen + "','*','" + cadence + "'," + Date.now() + ",'')";
db.run(query, function(err) {
if (!!err)
return cb(err);
});
},
- // TODO: queries here could be async, and wait for all to complete
- getOne: function(id, cb)
+ // TODO: some queries here could be async
+ getOne: function(id, light, cb)
{
db.serialize(function() {
- // TODO: optimize queries?
let query =
// NOTE: g.scoreMsg can be NULL
// (in this case score = "*" and no reason to look at it)
- "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
+ "SELECT g.id, g.vid, g.fen, g.fenStart, g.cadence, g.score, " +
"g.scoreMsg, g.drawOffer, v.name AS vname " +
"FROM Games g " +
"JOIN Variants v " +
db.all(query, (err2,players) => {
if (!!err2)
return cb(err2);
+ if (light)
+ {
+ const game = Object.assign({},
+ gameInfo,
+ {players: players}
+ );
+ return cb(null, game);
+ }
query =
"SELECT squares, played, idx " +
"FROM Moves " +
});
},
+ // For display on MyGames or Hall: no need for moves or chats
getByUser: function(uid, excluded, cb)
{
db.serialize(function() {
db.all(query, (err,gameIds) => {
if (!!err)
return cb(err);
- gameIds = gameIds || []; //might be empty
+ if (gameIds.length == 0)
+ return cb(null, []);
let gameArray = [];
for (let i=0; i<gameIds.length; i++)
{
- GameModel.getOne(gameIds[i]["gid"], (err2,game) => {
+ GameModel.getOne(gameIds[i]["gid"], true, (err2,game) => {
if (!!err2)
return cb(err2);
gameArray.push(game);
const day = 86400000;
db.serialize(function() {
let query =
- "SELECT id,score,created " +
+ "SELECT id,created " +
"FROM Games ";
db.all(query, (err,games) => {
games.forEach(g => {
query =
- "SELECT max(played) AS lastMaj " +
+ "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
"FROM Moves " +
"WHERE gid = " + g.id;
- db.get(query, (err2,updated) => {
- if (!updated.lastMaj)
+ 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))
{
- if (tsNow - g.created > 7*day)
- return GameModel.remove(g.id);
- }
- else //at least one move
- {
- const lastMaj = updated.lastMaj;
- if (g.score != "*" && tsNow - lastMaj > 7*day ||
- g.score == "*" && tsNow - lastMaj > 91*day)
- {
- GameModel.remove(g.id);
- }
+ return GameModel.remove(g.id);
}
});
});