X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=b4f128b124a5646c7c6701f67b28e0fced5c70df;hb=2f258c37c19c5be20ec68695ddfaec2c21f7f0ae;hp=32b2a6624b8fdc9127d9e88d61ead7096787ed49;hpb=99f12ac0a1dc9633db08bf2804d7920d060fc214;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index 32b2a662..b4f128b1 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -7,7 +7,7 @@ const UserModel = require("./User"); * 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 @@ -36,9 +36,7 @@ const GameModel = 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"; @@ -49,14 +47,14 @@ const GameModel = 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); @@ -72,15 +70,14 @@ const GameModel = }); }, - // 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.created, g.score, " + "g.scoreMsg, g.drawOffer, v.name AS vname " + "FROM Games g " + "JOIN Variants v " + @@ -98,6 +95,14 @@ const GameModel = 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 " + @@ -128,27 +133,42 @@ const GameModel = }); }, + // For display on MyGames or Hall: no need for moves or chats getByUser: function(uid, excluded, cb) { db.serialize(function() { - const query = - "SELECT DISTINCT gid " + - "FROM Players " + - "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; + let query = ""; + if (uid == 0) + { + // Special case anonymous user: show all games + query = + "SELECT id AS gid " + + "FROM Games"; + } + else + { + // Registered user: + query = + "SELECT gid " + + "FROM Players " + + "GROUP BY gid " + + "HAVING COUNT(uid = " + uid + " OR NULL) " + + (excluded ? " = 0" : " > 0"); + } db.all(query, (err,gameIds) => { - if (!!err) - return cb(err); - if (gameIds.length == 0) - return cb(null, []); + if (!!err || gameIds.length == 0) + return cb(err, []); let gameArray = []; + let kounter = 0; for (let i=0; i { + GameModel.getOne(gameIds[i]["gid"], true, (err2,game) => { if (!!err2) return cb(err2); gameArray.push(game); + kounter++; //TODO: let's hope this is atomic?! // Call callback function only when gameArray is complete: - if (i == gameIds.length - 1) + if (kounter == gameIds.length) return cb(null, gameArray); }); } @@ -160,7 +180,7 @@ const GameModel = { db.serialize(function() { const query = - "SELECT id " + + "SELECT uid " + "FROM Players " + "WHERE gid = " + id; db.all(query, (err,players) => { @@ -269,28 +289,21 @@ const GameModel = 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) - { - if (tsNow - g.created > 7*day) - return GameModel.remove(g.id); - } - else //at least one move + 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)) { - const lastMaj = updated.lastMaj; - if (g.score != "*" && tsNow - lastMaj > 7*day || - g.score == "*" && tsNow - lastMaj > 91*day) - { - GameModel.remove(g.id); - } + GameModel.remove(g.id); } }); });