X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=01de6b54c014c959f860212780b664ab011465eb;hb=83494c7fbd83fafa28c5a434a1c83f56c17e3d04;hp=602531f4456c482d92112e3da2f107477c405af4;hpb=bf20f404705c622a7bb7e458dacce37ecb7405a9;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 602531f4..01de6b54 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -13,116 +13,107 @@ var db = require("../utils/database"); 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.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, " : "") + + // 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 + "," : "") + + "(" + 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); - }); + db.run(query, function(err) { + return 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); - }); - }); - }); - }, + getOne: function(id, cb) + { + db.serialize(function() { + const query = + "SELECT * " + + "FROM Challenges " + + "WHERE id = " + id; + db.get(query, (err,challenge) => { + return cb(err, 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); - 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); - }); - }); - }, + // 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); + }); + }); + }, + + 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, cb) + { + 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); + db.get(query, (err,chall) => { + if (!chall) + return cb({errmsg: "Not your challenge"}); + ChallengeModel.remove(id); + cb(null); + }); + }); + }, + + // Remove challenges older than 1 month, and 1to1 older than 2 days + removeOld: function() + { + const tsNow = Date.now(); + // 86400000 = 24 hours in milliseconds + const day = 86400000; + db.serialize(function() { + const query = + "SELECT id, target " + + "FROM Challenges"; + db.all(query, (err, challenges) => { + challenges.forEach(c => { + if ((!c.target && tsNow - c.added > 30*day) || + (!!c.target && tsNow - c.added > 2*day)) + { + db.run("DELETE FROM CHallenges WHERE id = " + c.id); + } + }); }); - }); - }, + }); + }, } module.exports = ChallengeModel;