var db = require("../utils/database");
/*
- * Structure table Challenges:
+ * Structure:
* id: integer
* added: datetime
* uid: user id (int)
+ * target: recipient id (optional)
* vid: variant id (int)
- * nbPlayers: integer
* fen: varchar (optional)
- * mainTime: integer
- * addTime: integer
- *
- * Structure table WillPlay:
- * cid: ref challenge id
- * uid: ref user id
+ * timeControl: string (3m+2s, 7d+1d ...)
*/
const ChallengeModel =
{
- checkChallenge: function(c)
- {
- const vid = parseInt(c.vid);
- if (isNaN(vid) || vid <= 0)
- return "Please select a variant";
+ checkChallenge: function(c)
+ {
+ if (!c.vid.match(/^[0-9]+$/))
+ return "Wrong variant ID";
- const mainTime = parseInt(c.mainTime);
- const increment = parseInt(c.increment);
- if (isNaN(mainTime) || mainTime <= 0)
- return "Main time should be strictly positive";
- if (isNaN(increment) || increment < 0)
- return "Increment must be positive";
+ if (!c.timeControl.match(/^[0-9dhms +]+$/))
+ return "Wrong characters in time control";
- // Basic alphanumeric check for players names
- let playerCount = 0;
- for (p of c.players)
- {
- if (p.name.length > 0)
- {
- if (!p.name.match(/^[\w]+$/))
- return "Wrong characters in players names";
- playerCount++;
- }
- }
+ if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
+ return "Bad FEN string";
+ },
- if (playerCount > 0 && playerCount != c.nbPlayers-1)
- return "None, or all of the opponent names must be filled"
+ // fen cannot be undefined
+ create: function(c, cb)
+ {
+ db.serialize(function() {
+ let query =
+ "INSERT INTO Challenges " +
+ "(added, uid, " + (!!c.to ? "target, " : "") +
+ "vid, fen, timeControl) VALUES " +
+ "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
+ c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
+ db.run(query, err => {
+ return cb(err);
+ });
+ });
+ },
- // Just characters check on server:
- if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
- return "Bad FEN string";
- },
+ getOne: function(id, cb)
+ {
+ db.serialize(function() {
+ let query =
+ "SELECT * " +
+ "FROM Challenges " +
+ "WHERE id = " + id;
+ db.get(query, (err,challenge) => {
+ return cb(err, challenge);
+ });
+ });
+ },
- // 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
- });
- });
- });
- });
- },
+ // 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.run(query, (err,challenges) => {
+ return cb(err, challenges);
+ });
+ });
+ },
- 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,
- mainTime: challengeInfo.mainTime,
- increment: challengeInfo.addTime,
- };
- 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);
- });
- });
- },
-
- remove: function(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);
- });
- },
+ remove: function(id, uid)
+ {
+ db.serialize(function() {
+ let query =
+ "SELECT 1 " +
+ "FROM Challenges " +
+ "WHERE id = " + id + " AND uid = " + uid;
+ db.run(query, (err,rows) => {
+ if (rows.length == 0)
+ return res.json({errmsg: "Not your challenge"});
+ query =
+ "DELETE FROM Challenges " +
+ "WHERE id = " + id;
+ db.run(query);
+ });
+ });
+ },
}
module.exports = ChallengeModel;