X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=de2818baac3d461356ba60e4b8357b719450b44f;hb=77fd729819118d491ff91c1d99960add2ef9cff3;hp=e1fb448bc678c0cd498a5b67ed4a914b47e68017;hpb=98db2082fd31e7a7bc0348e31ce119f39dbc31b3;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index e1fb448b..de2818ba 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -8,68 +8,59 @@ var db = require("../utils/database"); * vid: variant id (int) * nbPlayers: integer * fen: varchar (optional) - * mainTime: integer - * addTime: integer + * timeControl: string (3m+2s, 7d+1d ...) * * Structure table WillPlay: * cid: ref challenge id * uid: ref user id + * yes: boolean (false means "not decided yet") */ const ChallengeModel = { checkChallenge: function(c) { - const vid = parseInt(c.vid); - if (isNaN(vid) || vid <= 0) - return "Please select a variant"; + if (!c.vid.match(/^[0-9]+$/)) + return "Wrong variant ID"; - const mainTime = parseInt(c.mainTime); - const increment = parseInt(c.increment); - if (isNaN(mainTime) || mainTime <= 0) - return "Main time should be strictly positive"; - if (isNaN(increment) || increment < 0) - return "Increment must be positive"; + if (!c.timeControl.match(/^[0-9dhms +]+$/)) + return "Wrong characters in time control"; - // Basic alphanumeric check for players names - let playerCount = 0; - for (p of c.players) - { - if (p.name.length > 0) - { - if (!p.name.match(/^[\w]+$/)) - return "Wrong characters in players names"; - playerCount++; - } - } - - if (playerCount > 0 && playerCount != c.nbPlayers-1) - return "None, or all of the opponent names must be filled" - - // Just characters check on server: - if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/)) + if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/)) return "Bad FEN string"; }, - // fen cannot be undefined; TODO: generate fen on server instead + 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 = + query = "INSERT INTO WillPlay VALUES " + - "(" + lastId["rowid"] + "," + c.uid + ")"; - db.run(query, (err,ret) => { - cb(err, lastId); //all we need is the challenge ID - }); + "(true," + lastId["rowid"] + "," + c.uid + ")"; + db.run(query, (err,ret) => { + cb(err, lastId); //all we need is the challenge ID + }); }); }); }); @@ -104,8 +95,7 @@ const ChallengeModel = nbPlayers: challengeInfo.nbPlayers, players: players, //currently in fen: challengeInfo.fen, - mainTime: challengeInfo.mainTime, - increment: challengeInfo.addTime, + timeControl: challengeInfo.timeControl, }; return cb(null, challenge); }); @@ -136,17 +126,22 @@ const ChallengeModel = }); }, - remove: function(id) + remove: function(id, uid) { - db.parallelize(function() { + db.serialize(function() { let query = "DELETE FROM Challenges " + - "WHERE id = " + id; - db.run(query); - query = - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); + "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); + } + }); }); }, }