X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=7ccaa76fbf135bcf64a24c2c9906a9e535e1ca07;hb=cf7fac89918e50d8c93f9a09842c9eb53e8841d6;hp=6f8ba0be6047ea8f3ed7609b47dcb3c25ef5814e;hpb=4edfed6c011cd97d58d5bd8e0451cc0c1006a0a0;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 6f8ba0be..7ccaa76f 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,190 +1,122 @@ var db = require("../utils/database"); +const UserModel = require("./User"); /* - * 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) * timeControl: string (3m+2s, 7d+1d ...) - * - * Structure table WillPlay: - * cid: ref challenge id - * uid: ref user id - * yes: boolean (false means "not decided yet") */ const ChallengeModel = { - checkChallenge: function(c) - { - if (!c.vid.match(/^[0-9]+$/)) - return "Wrong variant ID"; - + checkChallenge: function(c) + { + if (!c.vid.toString().match(/^[0-9]+$/)) + return "Wrong variant ID"; if (!c.timeControl.match(/^[0-9dhms +]+$/)) return "Wrong characters in time control"; + if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/)) + return "Bad FEN string"; + if (!!c.to) + return UserModel.checkNameEmail({name: c.to}); + return ""; + }, - if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/)) - return "Bad FEN string"; - }, - - initializeWillPlay: function(uids, cid, cb) + // fen cannot be undefined + create: function(c, cb) { - let query = "INSERT INTO WillPlay VALUES "; - for (let i=0; i { - 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); - }); - }); - }, + getOne: function(id, cb) + { + db.serialize(function() { + const query = + "SELECT * " + + "FROM Challenges " + + "WHERE id = " + id; + db.get(query, (err,challenge) => { + return cb(err, challenge); + }); + }); + }, - getSeatCount: function(id, cb) + // All challenges except where target is defined and not me + getByUser: function(uid, cb) { - db.serialize(function() { - let query = - "SELECT COUNT(*) AS scount " + - "FROM WillPlay " + - "WHERE cid = " + id; - db.get(query, (err,scRow) => { - if (!!err) - return cb(err); - query = - "SELECT nbPlayers " + - "FROM Challenges " + - "WHERE id = " + id; - db.get(query, (err2,chRow) => { - if (!!err2) - return cb(err2); - cb(chRow["nbPlayers"] - scRow["scount"]); - }); + db.serialize(function() { + const query = + "SELECT * " + + "FROM Challenges " + + "WHERE target IS NULL OR target = " + uid; + db.all(query, (err,challenges) => { + return cb(err, challenges); }); }); }, - setSeat: function(id, uid) + remove: function(id) { - // TODO: remove extra "db.serialize" (parallelize by default) - //db.serialize(function() { - const query = - "INSERT OR REPLACE INTO WillPlay " + - "VALUES (true," + id + "," + uid +")"; - db.run(query); - //}); + db.serialize(function() { + const query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); + }); }, - remove: function(id, uid) - { - db.serialize(function() { - let query = + safeRemove: function(id, uid, cb) + { + db.serialize(function() { + const query = "SELECT 1 " + "FROM Challenges " + "WHERE id = " + id + " AND uid = " + uid; - db.run(query, (err,rows) => { - if (rows.length > 0) //it's my challenge - { - db.parallelize(function() { - query = - "DELETE FROM Challenges " + - "WHERE id = " + id; - db.run(query); - // Also remove matching WillPlay entries if a challenge was deleted - query = - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); - }); - } + db.get(query, (err,chall) => { + if (!chall) + return cb({errmsg: "Not your challenge"}); + ChallengeModel.remove(id); + cb(null); + }); + }); + }, + + // Remove challenges older than 1 month, and 1to1 older than 2 days + removeOld: function() + { + const tsNow = Date.now(); + // 86400000 = 24 hours in milliseconds + const day = 86400000; + db.serialize(function() { + const query = + "SELECT id, target, added " + + "FROM Challenges"; + db.all(query, (err, challenges) => { + challenges.forEach(c => { + if ((!c.target && tsNow - c.added > 30*day) || + (!!c.target && tsNow - c.added > 2*day)) + { + db.run("DELETE FROM Challenges WHERE id = " + c.id); + } + }); }); - }); - }, + }); + }, } module.exports = ChallengeModel;