var db = require("../utils/database");
/*
- * Structure:
- * _id: BSON id
- * vid: variant ID
- * from: player ID
- * to: player ID, undefined if automatch
+ * Structure table Challenges:
+ * id: integer
+ * added: datetime
+ * uid: user id (int)
+ * vid: variant id (int)
+ * nbPlayers: integer
+ *
+ * Structure table WillPlay:
+ * cid: ref challenge id
+ * uid: ref user id
*/
-exports.create = function(vid, from, to, callback)
+exports.create = function(uid, vid, nbPlayers, cb)
{
- 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);
+ db.serialize({
+ let query =
+ "INSERT INTO Challenges (added, uid, vid, nbPlayers) " +
+ "VALUES (" + Date.now() + "," + uid + "," + vid + "," + nbPlayers + ")";
+ db.run(insertQuery, err => {
+ if (!!err)
+ return cb(err);
+ db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
+ query =
+ "INSERT INTO WillPlay VALUES " +
+ "(" + lastId["rowid"] + "," + uid + ")";
+ db.run(query, cb);
+ });
+ });
+ });
+ });
}
-//////////
-// REMOVAL
-
-exports.remove = function(cid, callback)
+exports.getOne = function(id, cb)
{
- db.challenges.remove({_id: cid}, callback);
+ db.serialize(function() {
+ let query =
+ "SELECT * " +
+ "FROM Challenges c " +
+ "JOIN Variants v " +
+ " ON c.vid = v.id "
+ "WHERE id = " + id;
+ db.get(query, (err,challengeInfo) => {
+ if (!!err)
+ return cb(err);
+ query =
+ "SELECT w.uid AS id, u.name " +
+ "FROM WillPlay w " +
+ "JOIN Users u " +
+ " ON w.uid = u.id " +
+ "WHERE w.cid = " + id;
+ db.run(query, (err2,players) => {
+ if (!!err2)
+ return cb(err2);
+ const challenge = {
+ id: id,
+ vname: challengeInfo.name,
+ added: challengeInfo.added,
+ nbPlayers: challengeInfo.nbPlayers,
+ players: players, //currently in
+ };
+ return cb(null, challenge);
+ });
+ });
+ });
}
-// Remove challenges older than 1 month, and 1to1 older than 36h
-exports.removeOld = function()
+exports.remove = function(id)
{
- var tsNow = new Date().getTime();
- // 86400000 = 24 hours in milliseconds
- var day = 86400000;
- db.challenges.find({}, (err,challengeArray) => {
- challengeArray.forEach( c => {
- if (c._id.getTimestamp() + 30*day < tsNow //automatch
- || (!!c.to && c._id.getTimestamp() + 1.5*day < tsNow)) //1 to 1
- {
- db.challenges.remove({"_id": c._id});
- }
- });
+ db.parallelize(function() {
+ let query =
+ "DELETE FROM Challenges " +
+ "WHERE id = " + id;
+ db.run(query);
+ query =
+ "DELETE FROM WillPlay " +
+ "WHERE cid = " + id;
+ db.run(query);
});
}