X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=dea0ac3908d1a3baaecaeb77cf2e6662585cb558;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hp=8adf3cac1048791527cc0d1884df218fbdce19cc;hpb=36093ebabecde5a86451a4600babbecc971887c0;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 8adf3cac..dea0ac39 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,158 +1,82 @@ -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 + * randomness: integer in 0..2 * 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 ...) */ -const ChallengeModel = -{ - checkChallenge: function(c) - { - if (!c.vid.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"; - }, - - 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 - }); - }); - }); - }); - }, - - 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); - }); - }); - }, + 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, { id: this.lastID }); + }); + }); + }, - testfunc: function() - { + // All challenges related to user with ID uid + getByUser: function(uid, cb) { db.serialize(function() { - db.run("DELETE * FROM TableTest", (err,ret) => { - console.log(ret); + 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, uid) - { - db.serialize(function() { - let query = - "DELETE FROM Challenges " + - "WHERE id = " + id + " AND uid = " + uid; - db.run(query, (err,ret) => { - if (!err && ret >= 1) - { - // Also remove matching WillPlay entries if a challenge was deleted - query = - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); - } + 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;