X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=2be700bd04f8152a1224183971f6929589a983a6;hb=51145f8c0e0595f195e26827e932306eb0083c10;hp=602531f4456c482d92112e3da2f107477c405af4;hpb=bf20f404705c622a7bb7e458dacce37ecb7405a9;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 602531f4..2be700bd 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,4 +1,5 @@ -var db = require("../utils/database"); +const db = require("../utils/database"); +const UserModel = require("./User"); /* * Structure: @@ -7,122 +8,81 @@ var db = require("../utils/database"); * uid: user id (int) * target: recipient id (optional) * vid: variant id (int) + * randomness: integer in 0..2 * fen: varchar (optional) - * timeControl: string (3m+2s, 7d+1d ...) + * cadence: string (3m+2s, 7d ...) */ const ChallengeModel = { - checkChallenge: function(c) - { - if (!c.vid.match(/^[0-9]+$/)) - return "Wrong variant ID"; + checkChallenge: function(c) + { + return ( + c.vid.toString().match(/^[0-9]+$/) && + c.cadence.match(/^[0-9dhms +]+$/) && + c.randomness.toString().match(/^[0-2]$/) && + c.fen.match(/^[a-zA-Z0-9, /-]*$/) && + (!c.to || UserModel.checkNameEmail({name: c.to})) + ); + }, - if (!c.timeControl.match(/^[0-9dhms +]+$/)) - return "Wrong characters in time control"; - - if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/)) - return "Bad FEN string"; - }, - - // 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 => { - if (!!err) - return cb(err); - db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => { - return cb(err2, lastId); - }); + create: function(c, cb) + { + db.serialize(function() { + const query = + "INSERT INTO Challenges " + + "(added, uid, " + (c.to ? "target, " : "") + + "vid, randomness, fen, cadence) " + + "VALUES " + + "(" + Date.now() + "," + c.uid + "," + (c.to ? c.to + "," : "") + + c.vid + "," + c.randomness + ",'" + c.fen + "','" + c.cadence + "')"; + db.run(query, function(err) { + cb(err, {cid: this.lastID}); }); - }); - }, + }); + }, - getOne: function(id, cb) - { - db.serialize(function() { - let query = - "SELECT * " + - "FROM Challenges " + - "WHERE id = " + id; - db.get(query, (err,challengeInfo) => { - if (!!err) - return cb(err); - let condition = ""; - if (!!challengeInfo.to) - condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")"; - else - condition = "= " + challengeInfo.uid; - query = - "SELECT id, name " + - "FROM Users " + - "WHERE id " + condition; - db.run(query, (err2,players) => { - if (!!err2) - return cb(err2); - const challenge = { - id: id, - uid: challengeInfo.uid, //sender (but we don't know who ask) - vid: challengeInfo.vid, - added: challengeInfo.added, - players: players, //sender + potential receiver - fen: challengeInfo.fen, - timeControl: challengeInfo.timeControl, - }; - return cb(null, challenge); - }); - }); - }); - }, + // 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); + }); + }); + }, - 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); - challIds = challIds || []; - 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, uid) - { - db.serialize(function() { - let query = + safeRemove: function(id, uid) + { + db.serialize(function() { + const 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); + "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;