From: Benjamin Auder Date: Fri, 6 Sep 2019 13:58:46 +0000 (+0200) Subject: Update on challenges X-Git-Url: https://git.auder.net/%7B%7B%20asset%28%27mixstore/images/img/current/assets/rpsls.css?a=commitdiff_plain;h=b1aa927be964db4fa6a3eb7cf6c04640951d7dbb;p=vchess.git Update on challenges --- 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; diff --git a/server/routes/challenges.js b/server/routes/challenges.js index 03fc1500..76756899 100644 --- a/server/routes/challenges.js +++ b/server/routes/challenges.js @@ -42,37 +42,20 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => { insertChallenge(); }); -function launchGame(cid, uid) -{ +// "Challenge update" --> someone accepted a challenge +router.put("/challenges", access.logged, access.ajax, (req,res) => { + // launchGame(cid, uid) // TODO: gather challenge infos - // Then create game, and remove challenge -} - -//// index -//router.get("/challenges", access.logged, access.ajax, (req,res) => { -// if (req.query["uid"] != req.user._id) -// return res.json({errmsg: "Not your challenges"}); -// let uid = ObjectID(req.query["uid"]); -// ChallengeModel.getByPlayer(uid, (err, challengeArray) => { -// res.json(err || {challenges: challengeArray}); -// }); -//}); -// -//function createChallenge(vid, from, to, res) -//{ -// ChallengeModel.create(vid, from, to, (err, chall) => { -// res.json(err || { -// // A challenge can be sent using only name, thus 'to' is returned -// to: chall.to, -// cid: chall._id -// }); -// }); -//} + // Then create game, and remove challenge: + ChallengeModel.remove(cid, req.userId, err => { + res.json(err || {}); + }); +}); router.delete("/challenges", access.logged, access.ajax, (req,res) => { const cid = req.query.id; ChallengeModel.remove(cid, req.userId, err => { - res.json(err || {}); + res.json(err || {}); //TODO: just "return err" because is empty if no errors }); });