X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=e32cd682065bb90e8bb5928bda3ed9ef9f9838be;hb=eb2d61de8d569470fa329a484efe9bab420b2b82;hp=96db0a2b431ad5237d8b08b4bcfceca3c4155212;hpb=625022fdcf750f0aff8fcd699f7e9b89730e1d10;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 96db0a2b..e32cd682 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,121 +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) - * mainTime: integer - * addTime: integer - * - * Structure table WillPlay: - * cid: ref challenge id - * uid: ref user id + * cadence: string (3m+2s, 7d ...) + * options: string (js object) */ -const ChallengeModel = -{ - // 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 - }); - }); - }); - }); - }, +const ChallengeModel = { - 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); - }); - }); - }); - }, + 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 })) + ); + }, - 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, options, fen, cadence) " + + "VALUES (" + Date.now() + "," + c.uid + "," + (c.to ? c.to + "," : "") + + c.vid + ",?,'" + c.fen + "','" + c.cadence + "')"; + db.run(query, c.options, function(err) { + cb(err, { id: this.lastID }); + }); + }); + }, - 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); - }); - }, -} + // 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); + }); + }); + }, + + remove: function(id) { + db.serialize(function() { + const query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); + }); + }, + + 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); + }); + }); + } + +}; module.exports = ChallengeModel;