+ // These games could be deleted on some side. movesCount not required
+ getCompleted: function(uid, cursor, cb) {
+ db.serialize(function() {
+ let query =
+ "SELECT id, vid, cadence, options, created, score, scoreMsg, " +
+ "white, black, deletedByWhite, deletedByBlack " +
+ "FROM Games " +
+ "WHERE " +
+ " score <> '*' AND" +
+ " created < " + cursor + " AND" +
+ " (" +
+ " (" +
+ " white = " + uid + " AND" +
+ " (deletedByWhite IS NULL OR NOT deletedByWhite)" +
+ " )" +
+ " OR " +
+ " (" +
+ " black = " + uid + " AND" +
+ " (deletedByBlack IS NULL OR NOT deletedByBlack)" +
+ " )" +
+ " ) ";
+ query +=
+ "ORDER BY created DESC " +
+ "LIMIT 20";
+ db.all(query, (err, games) => {
+ // Query player names
+ let pids = {};
+ games.forEach(g => {
+ if (!pids[g.white]) pids[g.white] = true;
+ if (!pids[g.black]) pids[g.black] = true;
+ });
+ UserModel.getByIds(Object.keys(pids), (err2, users) => {
+ let names = {};
+ users.forEach(u => { names[u.id] = u.name; });
+ cb(
+ null,
+ games.map(
+ g => {
+ return {
+ id: g.id,
+ vid: g.vid,
+ cadence: g.cadence,
+ options: g.options,
+ created: g.created,
+ score: g.score,
+ scoreMsg: g.scoreMsg,
+ players: [
+ { id: g.white, name: names[g.white] },
+ { id: g.black, name: names[g.black] }
+ ],
+ deletedByWhite: g.deletedByWhite,
+ deletedByBlack: g.deletedByBlack
+ };
+ }
+ )
+ );
+ });
+ });
+ });
+ },
+
+ getPlayers: function(id, cb) {