- // fen cannot be undefined
- create: function(c, cb)
- {
- db.serialize(function() {
- let query =
- "INSERT INTO Challenges " +
- "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
- "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
- ",'" + c.fen + "'," + c.timeControl + ")";
- db.run(query, err => {
- if (!!err)
- return cb(err);
- db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
-
- // TODO: also insert "will play" "no" for other players ?
- // willplay = "maybe" by default ?
-
- query =
- "INSERT INTO WillPlay VALUES " +
- "(true," + lastId["rowid"] + "," + c.uid + ")";
- db.run(query, (err,ret) => {
- cb(err, lastId); //all we need is the challenge ID
- });
- });
- });
- });
- },
+ // All challenges except where target is defined and not me
+ getByUser: function(uid, cb)
+ {
+ db.serialize(function() {
+ const query =
+ "SELECT * " +
+ "FROM Challenges " +
+ "WHERE target IS NULL OR target = " + uid;
+ db.all(query, (err,challenges) => {
+ return cb(err, challenges);
+ });
+ });
+ },