- 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 " +
- "FROM Players " +
- "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
- db.all(query, (err,gameIds) => {
- if (!!err)
- return cb(err);
- gameIds = gameIds || []; //might be empty
- let gameArray = [];
- 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);
- });
- }
- });
- });
- },
+ // For display on MyGames or Hall: no need for moves or chats
+ getByUser: function(uid, excluded, cb)
+ {
+ // Some fields are not required when showing a games list:
+ const getOneLight = (id, cb2) => {
+ let query =
+ "SELECT g.id, g.vid, g.fen, g.cadence, g.created, g.score, " +
+ "g.scoreMsg, g.deletedByWhite, g.deletedByBlack, v.name AS vname " +
+ "FROM Games g " +
+ "JOIN Variants v " +
+ " ON g.vid = v.id " +
+ "WHERE g.id = " + id;
+ db.get(query, (err, gameInfo) => {
+ query =
+ "SELECT p.uid, p.color, u.name " +
+ "FROM Players p " +
+ "JOIN Users u " +
+ " ON p.uid = u.id " +
+ "WHERE p.gid = " + id;
+ db.all(query, (err2, players) => {
+ query =
+ "SELECT COUNT(*) AS nbMoves " +
+ "FROM Moves " +
+ "WHERE gid = " + id;
+ db.get(query, (err,ret) => {
+ const game = Object.assign(
+ {},
+ gameInfo,
+ {
+ players: players,
+ movesCount: ret.nbMoves
+ }
+ );
+ cb2(game);
+ });
+ });
+ });
+ };
+ db.serialize(function() {
+ 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 || gameIds.length == 0) cb(err, []);
+ else {
+ let gameArray = [];
+ let gCounter = 0;
+ for (let i=0; i<gameIds.length; i++) {
+ getOneLight(gameIds[i]["gid"], (game) => {
+ gameArray.push(game);
+ gCounter++; //TODO: let's hope this is atomic?!
+ // Call callback function only when gameArray is complete:
+ if (gCounter == gameIds.length)
+ cb(null, gameArray);
+ });
+ }
+ }
+ });
+ });
+ },