X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=e32cd682065bb90e8bb5928bda3ed9ef9f9838be;hb=eb2d61de8d569470fa329a484efe9bab420b2b82;hp=e977267275f54cf39494671aeda6222713c507c1;hpb=1f49533df1dafc5f83b815a9c89b8ccaccde2d6d;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index e9772672..e32cd682 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,144 +1,83 @@ -var db = require("../utils/database"); +const 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 + * options: varchar * 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") + * cadence: string (3m+2s, 7d ...) + * options: string (js object) */ -const ChallengeModel = -{ - checkChallenge: function(c) - { - if (!c.vid.match(/^[0-9]+$/)) - return "Wrong variant ID"; +const ChallengeModel = { - if (!c.timeControl.match(/^[0-9dhms +]+$/)) - return "Wrong characters in time control"; - - if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/)) - return "Bad FEN string"; - }, + checkChallenge: function(c) { + return ( + c.vid.toString().match(/^[0-9]+$/) && + c.cadence.match(/^[0-9dhms +]+$/) && + c.fen.match(/^[a-zA-Z0-9, /-]*$/) && + (!c.to || UserModel.checkNameEmail({ name: c.to })) + ); + }, - initializeWillPlay: function(uids, cid, 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 - }); - }); - }); - }); - }, + // All challenges related to user with ID uid + getByUser: function(uid, cb) { + db.serialize(function() { + const query = + "SELECT * " + + "FROM Challenges " + + "WHERE target IS NULL" + + " OR uid = " + uid + + " OR target = " + uid; + db.all(query, (err, challenges) => { + 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, - timeControl: challengeInfo.timeControl, - }; - return cb(null, challenge); - }); - }); - }); - }, + remove: function(id) { + db.serialize(function() { + const query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); + }); + }, - 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); - }); - }); - }, + safeRemove: function(id, uid) { + db.serialize(function() { + const query = + "SELECT 1 " + + "FROM Challenges " + + "WHERE id = " + id + " " + + // Condition: I'm the sender or the target + "AND (uid = " + uid + " OR target = " + uid + ")"; + db.get(query, (err,chall) => { + if (!err && chall) + ChallengeModel.remove(id); + }); + }); + } - remove: function(id, uid) - { - db.serialize(function() { - let query = - "DELETE FROM Challenges " + - "WHERE id = " + id + " AND uid = " + uid; - db.run(query, (err,ret) => { - if (!!err && query = //TODO - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); - }); - }, -} +}; module.exports = ChallengeModel;