- // TODO: queries here could be async, and wait for all to complete
- getOne: function(id, cb)
- {
- db.serialize(function() {
- let query =
- "SELECT * " +
- "FROM Games " +
- "WHERE id = " + id;
- db.get(query, (err,gameInfo) => {
- if (!!err)
- return cb(err);
- query =
- "SELECT uid, color, rtime " +
- "FROM Players " +
- "WHERE gid = " + id;
- db.all(query, (err2,players) => {
- if (!!err2)
- return cb(err2);
- query =
- "SELECT move, message, played, idx, color " +
- "FROM Moves " +
- "WHERE gid = " + id;
- db.all(query, (err3,moves) => {
- if (!!err3)
- return cb(err3);
- const game = Object.assign({},
- gameInfo,
- {
- players: players,
- moves: moves
- }
- );
- return cb(null, game);
- });
- });
- });
- });
- },
+ // TODO: queries here could be async, and wait for all to complete
+ getOne: function(id, 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, " +
+ "g.scoreMsg, v.name AS vname " +
+ "FROM Games g " +
+ "JOIN Variants v " +
+ " ON g.vid = v.id " +
+ "WHERE g.id = " + id;
+ db.get(query, (err,gameInfo) => {
+ if (!!err)
+ return cb(err);
+ 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) => {
+ if (!!err2)
+ return cb(err2);
+ query =
+ "SELECT squares, played, idx " +
+ "FROM Moves " +
+ "WHERE gid = " + id;
+ db.all(query, (err3,moves) => {
+ if (!!err3)
+ return cb(err3);
+ query =
+ "SELECT msg, name, added " +
+ "FROM Chats " +
+ "WHERE gid = " + id;
+ db.all(query, (err4,chats) => {
+ if (!!err4)
+ return cb(err4);
+ const game = Object.assign({},
+ gameInfo,
+ {
+ players: players,
+ moves: moves,
+ chats: chats,
+ }
+ );
+ return cb(null, game);
+ });
+ });
+ });
+ });
+ });
+ },