X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=f82f9e817938c4bf51f80a2150a6564d53a8e499;hb=8c564f462f5406fcd311d4733f851daf6ada665d;hp=6d8f5c1e865d4f1d7d14f7d5af88ac8a4d41a28a;hpb=052d17ea6e199533cefb11f1ef51020b55cb1382;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 6d8f5c1e..f82f9e81 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,138 +1,97 @@ 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) * timeControl: string (3m+2s, 7d+1d ...) - * - * Structure table WillPlay: - * cid: ref challenge id - * uid: ref user id */ 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"; + return ""; + }, - if (!c.nbPlayers.match(/^[0-9]+$/)) - return "Wrong number of players"; - - if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/)) - return "Bad FEN string"; - }, + // fen cannot be undefined + create: function(c, cb) + { + db.serialize(function() { + const 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, function(err) { + return cb(err, {cid: this.lastID}); + }); + }); + }, - // 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) => { - - // TODO: also insert "will play" "no" for other players ? - // willplay = "maybe" by default ? - - 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() { + const query = + "SELECT * " + + "FROM Challenges " + + "WHERE id = " + id; + db.get(query, (err,challenge) => { + return cb(err, challenge); + }); + }); + }, - 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); - }); - }); - }); - }, + // 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.all(query, (err,challenges) => { + return cb(err, challenges); + }); + }); + }, - 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.serialize(function() { + const query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); + }); + }, - 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); - }); - }, + safeRemove: function(id, uid, cb) + { + db.serialize(function() { + const query = + "SELECT 1 " + + "FROM Challenges " + + "WHERE id = " + id + " AND uid = " + uid; + db.get(query, (err,chall) => { + if (!chall) + return cb({errmsg: "Not your challenge"}); + ChallengeModel.remove(id); + cb(null); + }); + }); + }, } module.exports = ChallengeModel;