+ cb(null, game);
+ });
+ }
+ else
+ {
+ // Full game requested:
+ query =
+ "SELECT squares, played, idx " +
+ "FROM Moves " +
+ "WHERE gid = " + id;
+ db.all(query, (err3,moves) => {
+ query =
+ "SELECT msg, name, added " +
+ "FROM Chats " +
+ "WHERE gid = " + id;
+ db.all(query, (err4,chats) => {
+ const game = Object.assign({},
+ gameInfo,
+ {
+ players: players,
+ moves: moves,
+ chats: chats,
+ }
+ );
+ cb(null, game);
+ });
+ });
+ }
+ });
+ });
+ });
+ },
+
+ // For display on MyGames or Hall: no need for moves or chats
+ getByUser: function(uid, excluded, cb)
+ {
+ 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++)
+ {
+ GameModel.getOne(gameIds[i]["gid"], true, (err2,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);