From 1d45e88bcb119d0789fee72954c8fc2b88a4007d Mon Sep 17 00:00:00 2001 From: Kartikey Pant Date: Sun, 25 Sep 2016 21:35:49 +0530 Subject: [PATCH 1/2] Added Level Functionality --- src/config.js | 2 +- src/models/createSampleModels.js | 12 ++++---- src/models/index.js | 1 + src/models/mapping.js | 22 ++++++++++++++ src/models/question.js | 12 ++++++-- src/question.js | 50 ++++++++++++++++++++------------ 6 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 src/models/mapping.js diff --git a/src/config.js b/src/config.js index d12c8c1..e88f075 100644 --- a/src/config.js +++ b/src/config.js @@ -1,6 +1,6 @@ module.exports = { secret: 'thisisthebiggestsecretyouhaveeverknowninyourwholelife', scoreIncrementor: 10, - scoreDecrementor: 5, + scoreDecrementor: 0, saltRounds: 10, } diff --git a/src/models/createSampleModels.js b/src/models/createSampleModels.js index 45a299d..2b89c52 100644 --- a/src/models/createSampleModels.js +++ b/src/models/createSampleModels.js @@ -6,15 +6,15 @@ module.exports = function () { models.sequelize.sync().then(function () { return models.User.bulkCreate([ { email: 'megh@gmail.com', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh'}, - { email: 'megh2@gmail.com', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh2'} + { email: 'megh2@gmail.com', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh2'}, ], { ignoreDuplicates: true }) }).then(function () { return models.Question.bulkCreate([ - { qno: 1, title: 'Q1 title', body: 'Question body 1', answer: 1 }, - { qno: 3, title: 'Q3 title', body: 'Question body 3', answer: 3 }, - { qno: 2, title: 'Q2 title', body: 'Question body 2', answer: 2 }, - { qno: 4, title: 'Q4 title', body: 'Question body 4', answer: 4 }, - { qno: 5, title: 'Q5 title', body: 'Question body 5', answer: 5 }, + { qno: 1, title: 'Q1 title', body: 'Question body 1', answer: 1,unlock_points:0 }, + { qno: 2, title: 'Q2 title', body: 'Question body 2', answer: 2,unlock_points:10 }, + { qno: 3, title: 'Q3 title', body: 'Question body 3', answer: 3,unlock_points:20 }, + { qno: 4, title: 'Q4 title', body: 'Question body 4', answer: 4,unlock_points:20 }, + { qno: 5, title: 'Q5 title', body: 'Question body 5', answer: 5,unlock_points:30 }, ], { ignoreDuplicates: true }) }) } diff --git a/src/models/index.js b/src/models/index.js index b6d117b..d636ba8 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -2,4 +2,5 @@ module.exports = { sequelize: require('./dbconfig'), User: require('./user'), Question: require('./question'), + Mapping: require('./mapping'), } diff --git a/src/models/mapping.js b/src/models/mapping.js new file mode 100644 index 0000000..284c994 --- /dev/null +++ b/src/models/mapping.js @@ -0,0 +1,22 @@ +var Sequelize = require('sequelize'); +var sequelize = require('./dbconfig'); +var Mapping = sequelize.define('mapping', { + qno: { + type: Sequelize.INTEGER, + allowNull: false, + validate: { + notEmpty: true, + isInt: true, + } + }, + uid: { + type: Sequelize.INTEGER, + allowNull: false, + validate: { + isInt: true, + notEmpty: true, + } + }, +}); + +module.exports = Mapping; diff --git a/src/models/question.js b/src/models/question.js index 89ac66d..87fc1fd 100644 --- a/src/models/question.js +++ b/src/models/question.js @@ -1,6 +1,6 @@ var Sequelize = require('sequelize'); var sequelize = require('./dbconfig'); - +var User = require('./user'); var Question = sequelize.define('question', { qno: { type: Sequelize.INTEGER, @@ -32,7 +32,15 @@ var Question = sequelize.define('question', { validate: { notEmpty: true, } + }, + unlock_points: { + type: Sequelize.INTEGER, + allowNull: false, + validate: { + notEmpty: true, + isInt: true, + } } }); -module.exports = Question +module.exports = Question; diff --git a/src/question.js b/src/question.js index f79cd91..58849a4 100644 --- a/src/question.js +++ b/src/question.js @@ -4,36 +4,48 @@ var middleware = require('./middleware'); var config = require('./config'); router.get('/:qno(\\d+)?', middleware.isAuthenticated, (req, res) => { - var { qno } = req.params; - const { lastQuestionAllowed } = req.user; + var qno = req.params.qno; + var score = req.user.score; + var lastQuestionAllowed =req.user.lastQuestionAllowed; if (!qno) qno = lastQuestionAllowed; - if (qno > lastQuestionAllowed) {res.sendStatus(403); return false;} models.Question.findOne({ where : { qno }, attributes: {exclude: ['answer']} }).then(question => { - if (question) res.send(question); - else res.sendStatus(400); + if (question){ + if ( question.unlock_points > score) + {res.status(403).send({points:question.unlock_points});return false;} + else res.send(question); + } + else { + res.sendStatus(400); + } }) }); router.post('/check/:qno(\\d+)?', middleware.isAuthenticated, (req, res) => { - var { qno } = req.params; - const { answer } = req.body; - const { lastQuestionAllowed } = req.user; - const { score } = req.user; + var qno = req.params.qno; + const uid = req.user.id; + const answer = req.body.answer; + const lastQuestionAllowed = req.user.lastQuestionAllowed; + const score = req.user.score; if (!qno) qno = lastQuestionAllowed; - if (qno > lastQuestionAllowed) {res.sendStatus(403); return false;} models.Question.findOne({where : { qno }}) - .then(question => { - if (question) { - if (question.answer == answer && qno ==lastQuestionAllowed) - req.user.update({ score: score + config.scoreIncrementor, lastQuestionAllowed: lastQuestionAllowed + 1 }); - else if (question.answer != answer && qno == lastQuestionAllowed) - req.user.update({ score: score - config.scoreDecrementor }); - res.send({result: question.answer == answer}); - } else res.sendStatus(400); - }) + .then(question => { + if (question) { + models.Mapping.findOne({where: {qno:qno,uid:uid}}).then(mapping => { + if(!mapping){ + if (question.answer == answer && question.unlock_points <= score){ + req.user.update({ score: score + config.scoreIncrementor, lastQuestionAllowed: lastQuestionAllowed + 1 }); + models.Mapping.create({qno,uid}); + } + else if (question.answer != answer && question.unlock_points <= score) + req.user.update({ score: score - config.scoreDecrementor }); + } + }) + res.send({result: question.answer == answer}); + } else res.sendStatus(400); + }) }) module.exports = router; From 647535ac2310966ce86eda87881cacb807b487f1 Mon Sep 17 00:00:00 2001 From: Kartikey Pant Date: Sun, 25 Sep 2016 22:08:30 +0530 Subject: [PATCH 2/2] Removing Unused Variable --- src/models/mapping.js | 1 + src/models/question.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/mapping.js b/src/models/mapping.js index 284c994..497d1b8 100644 --- a/src/models/mapping.js +++ b/src/models/mapping.js @@ -1,5 +1,6 @@ var Sequelize = require('sequelize'); var sequelize = require('./dbconfig'); + var Mapping = sequelize.define('mapping', { qno: { type: Sequelize.INTEGER, diff --git a/src/models/question.js b/src/models/question.js index 87fc1fd..087ac73 100644 --- a/src/models/question.js +++ b/src/models/question.js @@ -1,6 +1,5 @@ var Sequelize = require('sequelize'); var sequelize = require('./dbconfig'); -var User = require('./user'); var Question = sequelize.define('question', { qno: { type: Sequelize.INTEGER,