- // 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) => {
- 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
- });
- });
- });
- });
- },
-
- getOne: function(id, cb)
- {
- 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,
- uid: challengeInfo.uid,
- vname: challengeInfo.name,
- added: challengeInfo.added,
- nbPlayers: challengeInfo.nbPlayers,
- players: players, //currently in
- fen: challengeInfo.fen,
- timeControl: challengeInfo.timeControl,
- };
- return cb(null, challenge);
- });
- });
- });
- },
-
- getByUser: function(uid, cb)
- {
- db.serialize(function() {
- const query =
- "SELECT cid " +
- "FROM WillPlay " +
- "WHERE uid = " + uid;
- db.run(query, (err,challIds) => {
- if (!!err)
- return cb(err);
- let challenges = [];
- challIds.forEach(cidRow => {
- ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
- if (!!err2)
- return cb(err2);
- challenges.push(chall);
- });
- });
- return cb(null, challenges);
- });
- });
- },
+ create: function(c, cb) {
+ db.serialize(function() {
+ const query =
+ "INSERT INTO Challenges " +
+ "(added, uid, " + (c.to ? "target, " : "") +
+ "vid, randomness, fen, cadence) " +
+ "VALUES " +
+ "(" + Date.now() + "," + c.uid + "," + (c.to ? c.to + "," : "") +
+ c.vid + "," + c.randomness + ",'" + c.fen + "','" + c.cadence + "')";
+ db.run(query, function(err) {
+ cb(err, { id: this.lastID });
+ });
+ });
+ },