X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=79bf071f7cb82e066e6b197fe45720f8fd237a52;hb=cf7fac89918e50d8c93f9a09842c9eb53e8841d6;hp=a3ddc3c4f793787b5fe84ed7a61052a18c71b905;hpb=dfeb96ea90e880a2557cbb5953dbb7258c912283;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index a3ddc3c4..79bf071f 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -81,7 +81,7 @@ const GameModel = // 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, " + - "g.scoreMsg, v.name AS vname " + + "g.scoreMsg, g.drawOffer, v.name AS vname " + "FROM Games g " + "JOIN Variants v " + " ON g.vid = v.id " + @@ -131,9 +131,8 @@ const GameModel = getByUser: function(uid, excluded, cb) { db.serialize(function() { - // Next query is fine because a player appear at most once in a game const query = - "SELECT gid " + + "SELECT DISTINCT gid " + "FROM Players " + "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; db.all(query, (err,gameIds) => { @@ -179,6 +178,8 @@ const GameModel = if (!obj.move.idx.toString().match(/^[0-9]+$/)) return "Wrong move index"; } + if (!!obj.drawOffer && !obj.drawOffer.match(/^[wbtn]$/)) + return "Wrong draw offer format"; if (!!obj.fen && !obj.fen.match(/^[a-zA-Z0-9, /-]*$/)) return "Wrong FEN string"; if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/)) @@ -200,10 +201,14 @@ const GameModel = let modifs = ""; if (!!obj.message) modifs += "message = message || ' ' || '" + obj.message + "',"; - // NOTE: if drawOffer is true, we should check that it's player's turn + // 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) - modifs += "drawOffer = " + obj.drawOffer + ","; + { + if (obj.drawOffer == "n") //Special "None" update + obj.drawOffer = ""; + modifs += "drawOffer = '" + obj.drawOffer + "',"; + } if (!!obj.fen) modifs += "fen = '" + obj.fen + "',"; if (!!obj.score) @@ -263,7 +268,7 @@ const GameModel = const day = 86400000; db.serialize(function() { let query = - "SELECT id,score " + + "SELECT id,score,created " + "FROM Games "; db.all(query, (err,games) => { games.forEach(g => { @@ -272,13 +277,19 @@ const GameModel = "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) + if (!updated.lastMaj) + { + if (tsNow - g.created > 7*day) + return GameModel.remove(g.id); + } + else //at least one move { - 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); + } } }); });