- let chall = {
- "vid": vid,
- "from": from
- };
- if (!!to)
- chall.to = to;
- db.challenges.insert(chall, callback);
-}
-
-//////////
-// GETTERS
-
-exports.getById = function(cid, callback)
-{
- db.challenges.findOne({_id: cid}, callback);
-}
-
-// For index page: obtain challenges that the player can accept
-exports.getByPlayer = function(uid, callback)
-{
- db.challenges.aggregate(
- {$match: {$or: [
- {"to": uid},
- {$and: [{"from": {$ne: uid}}, {"to": {$exists: false}}]}
- ]}},
- {$project: {_id:0, vid:1}},
- {$group: {_id:"$vid", count:{$sum:1}}},
- callback);
-}
-
-// For variant page (challenges related to a player)
-exports.getByVariant = function(uid, vid, callback)
-{
- db.challenges.find({$and: [
- {"vid": vid},
- {$or: [
- {"to": uid},
- {"from": uid},
- {"to": {$exists: false}},
- ]}
- ]}, callback);
-}
+ // fen cannot be undefined; TODO: generate fen on server instead
+ create: function(c, cb)
+ {
+ db.serialize(function() {
+ let query =
+ "INSERT INTO Challenges " +
+ "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " +
+ "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
+ ",'" + c.fen + "'," + c.mainTime + "," + c.increment + ")";
+ db.run(query, err => {
+ if (!!err)
+ return cb(err);
+ db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
+ query =
+ "INSERT INTO WillPlay VALUES " +
+ "(" + lastId["rowid"] + "," + c.uid + ")";
+ db.run(query, (err,ret) => {
+ cb(err, lastId); //all we need is the challenge ID
+ });
+ });
+ });
+ });
+ },