X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FChallenge.js;h=602531f4456c482d92112e3da2f107477c405af4;hb=bf20f404705c622a7bb7e458dacce37ecb7405a9;hp=de2818baac3d461356ba60e4b8357b719450b44f;hpb=77fd729819118d491ff91c1d99960add2ef9cff3;p=vchess.git diff --git a/server/models/Challenge.js b/server/models/Challenge.js index de2818ba..602531f4 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,19 +1,14 @@ var db = require("../utils/database"); /* - * Structure table Challenges: + * Structure: * id: integer * added: datetime * uid: user id (int) + * target: recipient id (optional) * vid: variant id (int) - * nbPlayers: integer * 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") */ const ChallengeModel = @@ -30,39 +25,23 @@ const ChallengeModel = 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 - }); - }); - }); + return cb(err2, lastId); + }); + }); }); }, @@ -71,29 +50,29 @@ const ChallengeModel = db.serialize(function() { let query = "SELECT * " + - "FROM Challenges c " + - "JOIN Variants v " + - " ON c.vid = v.id " + "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 w.uid AS id, u.name " + - "FROM WillPlay w " + - "JOIN Users u " + - " ON w.uid = u.id " + - "WHERE w.cid = " + id; + "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, - vname: challengeInfo.name, + uid: challengeInfo.uid, //sender (but we don't know who ask) + vid: challengeInfo.vid, added: challengeInfo.added, - nbPlayers: challengeInfo.nbPlayers, - players: players, //currently in + players: players, //sender + potential receiver fen: challengeInfo.fen, timeControl: challengeInfo.timeControl, }; @@ -113,6 +92,7 @@ const ChallengeModel = db.run(query, (err,challIds) => { if (!!err) return cb(err); + challIds = challIds || []; let challenges = []; challIds.forEach(cidRow => { ChallengeModel.getOne(cidRow["cid"], (err2,chall) => { @@ -130,17 +110,16 @@ const ChallengeModel = { 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); - } + "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); }); }); },