X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=3f713203f1adeaacbd59ae4af7dfb0434e02b5db;hp=602531f4456c482d92112e3da2f107477c405af4;hb=b1aa927be964db4fa6a3eb7cf6c04640951d7dbb;hpb=bf20f404705c622a7bb7e458dacce37ecb7405a9 diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 602531f4..3f713203 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -13,103 +13,65 @@ var db = require("../utils/database"); const ChallengeModel = { - checkChallenge: function(c) - { + checkChallenge: function(c) + { if (!c.vid.match(/^[0-9]+$/)) - return "Wrong variant ID"; + 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"; - }, + 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() { + let 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, err => { + return cb(err); }); - }); - }, + }); + }, - 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() { + let 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.run(query, (err,challenges) => { + return cb(err, challenges); + }); + }); + }, - remove: function(id, uid) - { - db.serialize(function() { - let query = + remove: function(id, uid) + { + db.serialize(function() { + let query = "SELECT 1 " + "FROM Challenges " + "WHERE id = " + id + " AND uid = " + uid; @@ -121,8 +83,8 @@ const ChallengeModel = "WHERE id = " + id; db.run(query); }); - }); - }, + }); + }, } module.exports = ChallengeModel;