diff --git a/app.js b/app.js index bbae2fd4ba..824cbde19b 100644 --- a/app.js +++ b/app.js @@ -251,7 +251,7 @@ app.all('/account', passportConf.isAuthenticated); app.get('/account/api', userController.getAccountAngular); app.get('/bonfire', bonfireController.index); app.get( - '/bonfire/:bonfireNumber', + '/bonfires/:bonfireNumber', bonfireController.returnBonfire ); @@ -274,29 +274,68 @@ app.get('/account/unlink/:provider', userController.getOauthUnlink); app.post('/completed-challenge', function (req, res) { req.user.challengesHash[parseInt(req.body.challengeNumber)] = Math.round(+new Date() / 1000); - var ch = req.user.challengesHash; - var p = 0; - for (var k in ch) { - if (ch[k] > 0) { - p += 1; + var timestamp = req.user.challengesHash; + var points = 0; + for (var key in timestamp) { + if (timestamp[key] > 0) { + points += 1; } } - req.user.points = p; + req.user.points = points; req.user.save(); }); app.post('/completed-bonfire/', function (req, res) { - req.user.challengesHash[parseInt(req.body.challengeNumber)] = + debug(req.body, 'In post method'); // TODO: remove debug statement + req.user.bonfiresHash[parseInt(req.body.bonfireNumber)] = Math.round(+new Date() / 1000); - var ch = req.user.challengesHash; - var p = 0; - for (var k in ch) { - if (ch[k] > 0) { - p += 1; + var timestamp = req.user.bonfiresHash; + var points = 0; + for (var key in timestamp) { + if (timestamp[key] > 0) { + points += 1; } } - req.user.points = p; - req.user.save(); + + var isCompletedWith = req.body.bonfireInfo.completedWith || undefined; + var isCompletedDate = Math.round(+new Date() / 1000); + var bonfireHash = req.body.bonfireInfo.bonfireHash; + var isSolution = req.body.bonfireInfo.solution; + req.user.bonfiresHash[bonfireHash] = { + completedWith: isCompletedWith, + completedDate: isCompletedDate, + solution: isSolution + }; + + if (isCompletedWith) { + User.find({"profile.username": isCompletedWith}, function(err, pairedWith) { + if (err) { + return err; + } else { + pairedWith.bonfiresHash[bonfireHash] = { + completedWith: req.user._id, + completedDate: isCompletedDate, + solution: isSolution + }; + req.user.bonfiresHash[bonfireHash] = { + completedWith: pairedWith._id, + completedDate: isCompletedDate, + solution: isSolution + }; + + req.user.save(); + pairedWith.save(); + + } + }) + } else { + req.user.bonfiresHash[bonfireHash] = { + completedWith: null, + completedDate: isCompletedDate, + solution: isSolution + }; + req.user.save(); + } }); /** diff --git a/controllers/bonfire.js b/controllers/bonfire.js index 958663f6e4..580e47e71f 100644 --- a/controllers/bonfire.js +++ b/controllers/bonfire.js @@ -56,7 +56,9 @@ exports.returnBonfire = function(req, res, next) { debug('bonfire err: ', err); next(err); } + debug(bonfire[bonfireNumber]._id); res.render('bonfire/show', { + completedWith: null, title: bonfire[bonfireNumber].name, name: bonfire[bonfireNumber].name, number: bonfire[bonfireNumber].bonfireNumber, @@ -69,7 +71,8 @@ exports.returnBonfire = function(req, res, next) { points: req.user ? req.user.points : undefined, verb: verbs[Math.floor(Math.random() * verbs.length)], phrase: phrases[Math.floor(Math.random() * phrases.length)], - bonfires: bonfire + bonfires: bonfire, + bonfireHash: bonfire[bonfireNumber]._id }); }); }; \ No newline at end of file diff --git a/models/User.js b/models/User.js index 81a33ef72d..84cd18d600 100644 --- a/models/User.js +++ b/models/User.js @@ -354,247 +354,24 @@ var userSchema = new mongoose.Schema({ resetPasswordToken: String, resetPasswordExpires: Date, bonfiresHash: { - 0: { - type: Boolean, - default: 0 + aaa48de84e1ecc7c742e1124: { + completedWith: String, + completedDate: { + type: Number, + default: 0 + }, + solution: String }, - 1: { - type: Boolean, - default: 0 - }, - 2: { - type: Boolean, - default: 0 - }, - 3: { - type: Boolean, - default: 0 - }, - 4: { - type: Boolean, - default: 0 - }, - 5: { - type: Boolean, - default: 0 - }, - 6: { - type: Boolean, - default: 0 - }, - 7: { - type: Boolean, - default: 0 - }, - 8: { - type: Boolean, - default: 0 - }, - 9: { - type: Boolean, - default: 0 - }, - 10: { - type: Boolean, - default: 0 - }, - 11: { - type: Boolean, - default: 0 - }, - 12: { - type: Boolean, - default: 0 - }, - 13: { - type: Boolean, - default: 0 - }, - 14: { - type: Boolean, - default: 0 - }, - 15: { - type: Boolean, - default: 0 - }, - 16: { - type: Boolean, - default: 0 - }, - 17: { - type: Boolean, - default: 0 - }, - 18: { - type: Boolean, - default: 0 - }, - 19: { - type: Boolean, - default: 0 - }, - 20: { - type: Boolean, - default: 0 - }, - 21: { - type: Boolean, - default: 0 - }, - 22: { - type: Boolean, - default: 0 - }, - 23: { - type: Boolean, - default: 0 - }, - 24: { - type: Boolean, - default: 0 - }, - 25: { - type: Boolean, - default: 0 - }, - 26: { - type: Boolean, - default: 0 - }, - 27: { - type: Boolean, - default: 0 - }, - 28: { - type: Boolean, - default: 0 - }, - 29: { - type: Boolean, - default: 0 - }, - 30: { - type: Boolean, - default: 0 - }, - 31: { - type: Boolean, - default: 0 - }, - 32: { - type: Boolean, - default: 0 - }, - 33: { - type: Boolean, - default: 0 - }, - 34: { - type: Boolean, - default: 0 - }, - 35: { - type: Boolean, - default: 0 - }, - 36: { - type: Boolean, - default: 0 - }, - 37: { - type: Boolean, - default: 0 - }, - 38: { - type: Boolean, - default: 0 - }, - 39: { - type: Boolean, - default: 0 - }, - 40: { - type: Boolean, - default: 0 - }, - 41: { - type: Boolean, - default: 0 - }, - 42: { - type: Boolean, - default: 0 - }, - 43: { - type: Boolean, - default: 0 - }, - 44: { - type: Boolean, - default: 0 - }, - 45: { - type: Boolean, - default: 0 - }, - 46: { - type: Boolean, - default: 0 - }, - 47: { - type: Boolean, - default: 0 - }, - 48: { - type: Boolean, - default: 0 - }, - 49: { - type: Boolean, - default: 0 - }, - 50: { - type: Boolean, - default: 0 - }, - 51: { - type: Boolean, - default: 0 - }, - 52: { - type: Boolean, - default: 0 - }, - 53: { - type: Boolean, - default: 0 - }, - 54: { - type: Boolean, - default: 0 - }, - 55: { - type: Boolean, - default: 0 - }, - 56: { - type: Boolean, - default: 0 - }, - 57: { - type: Boolean, - default: 0 - }, - 58: { - type: Boolean, - default: 0 - }, - 59: { - type: Boolean, - default: 0 + ff0395860f5d3034dc0bfc94: { + completedWith: String, + completedDate: { + type: Number, + default: 0 + }, + solution: String } - } + }, + bonfires: Array }); /** diff --git a/public/js/lib/bonfire/bonfireFramework.js b/public/js/lib/bonfire/bonfireFramework.js index fe844286de..aea21a58ea 100644 --- a/public/js/lib/bonfire/bonfireFramework.js +++ b/public/js/lib/bonfire/bonfireFramework.js @@ -231,7 +231,6 @@ var runTests = function(err, data) { allTestsPassed = false; showCompletion(); } - console.log(allTestsPassed); }; function showCompletion() { diff --git a/public/js/main.js b/public/js/main.js index 60cd47f011..cbe0fca6c7 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -33,7 +33,7 @@ $(document).ready(function() { } }); - $('.completed-bonfire').on('click', function() { + function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) { $('#complete-bonfire-dialog').modal('show'); // Only post to server if there is an authenticated user if ($('.signup-btn-nav').length < 1) { @@ -41,11 +41,18 @@ $(document).ready(function() { cn = l[l.length - 1]; $.ajax({ type: 'POST', - data: {bonfireHash: cn}, + data: { + bonfireInfo: { + completedWith : didCompleteWith, + solution: bonfireSolution, + bonfireHash: thisBonfireHash + } + }, url: '/completed-bonfire/' }); + console.log(didCompleteWith, bonfireSolution, thisBonfireHash); // TODO: remove debug statement } - }); + } $('.all-challenges').on('click', function() { $('#all-challenges-dialog').modal('show'); @@ -55,10 +62,21 @@ $(document).ready(function() { $('#all-bonfires-dialog').modal('show'); }); - $('.next-button').on('click', function() { + $('.next-challenge-button').on('click', function() { l = location.pathname.split('/'); window.location = '/challenges/' + (parseInt(l[l.length - 1]) + 1); }); + + // TODO: refactor this to create meaningful variable names, what the heck i l? + $('.next-bonfire-button').on('click', function() { + var bonfireSolution = myCodeMirror.getValue(); + var thisBonfireHash = passedBonfireHash || null; + var didCompleteWith = $('#completed-with').text() || null; + console.log(bonfireSolution, thisBonfireHash); + completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash); + l = location.pathname.split('/'); + window.location = '/bonfires/' + (parseInt(l[l.length - 1]) + 1); + }); }); var profileValidation = angular.module('profileValidation',['ui.bootstrap']); diff --git a/views/bonfire/show.jade b/views/bonfire/show.jade index 9c420e283b..f91b79d695 100644 --- a/views/bonfire/show.jade +++ b/views/bonfire/show.jade @@ -45,8 +45,11 @@ block content var tests = !{JSON.stringify(tests)}; var challengeSeed = !{JSON.stringify(challengeSeed)}; var challengeEntryPoint = !{JSON.stringify(challengeEntryPoint)}; + var passedBonfireHash = !{JSON.stringify(bonfireHash)}; + console.log(passedBonfireHash); script(src='/js/lib/bonfire/bonfireFramework.js') + #complete-bonfire-dialog.modal(tabindex='-1') .modal-dialog.animated.zoomIn.fast-animation .modal-content @@ -57,9 +60,12 @@ block content .animated.zoomInDown.delay-half span.landing-icon.ion-checkmark-circled.text-primary - if (cc) - a.animated.fadeIn.btn.btn-lg.btn-primary.btn-block.next-button(name='_csrf', value=_csrf, aria-hidden='true') Take me to my next challenge + + a.animated.fadeIn.btn.btn-lg.btn-primary.btn-block.next-bonfire-button(name='_csrf', value=_csrf, aria-hidden='true') Take me to my next challenge + + - if (points && points > 2) - a.animated.fadeIn.btn.btn-lg.btn-block.btn-twitter(href="https://twitter.com/intent/tweet?text=I%20just%20#{verb}%20Free%20Code%20Camp%20Challenge%20%23#{number}:%20#{name}&url=http%3A%2F%2Ffreecodecamp.com/challenges/#{number}&hashtags=learntocode, javascript" target="_blank") + a.animated.fadeIn.btn.btn-lg.btn-block.btn-twitter(href="https://twitter.com/intent/tweet?text=I%20just%20#{verb}%20Free%20Code%20Camp%20Challenge%20%23#{number}:%20#{name}&url=http%3A%2F%2Ffreecodecamp.com/bonfires/#{number}&hashtags=learntocode, javascript" target="_blank") i.fa.fa-twitter   = phrase - else @@ -67,7 +73,7 @@ block content #all-bonfires-dialog.modal(tabindex='-1') .modal-dialog.animated.fadeInUp.fast-animation .modal-content - .modal-header.challenge-list-header Challenges + .modal-header.challenge-list-header Bonfires a.close.closing-x(href='#', data-dismiss='modal', aria-hidden='true') × .modal-body include ../partials/bonfires diff --git a/views/challenges/show.jade b/views/challenges/show.jade index 685f9d2bc9..aa1bd9960f 100644 --- a/views/challenges/show.jade +++ b/views/challenges/show.jade @@ -26,7 +26,7 @@ block content .animated.zoomInDown.delay-half span.landing-icon.ion-checkmark-circled.text-primary - if (cc) - a.animated.fadeIn.btn.btn-lg.btn-primary.btn-block.next-button(name='_csrf', value=_csrf, aria-hidden='true') Take me to my next challenge + a.animated.fadeIn.btn.btn-lg.btn-primary.btn-block.next-challenge-button(name='_csrf', value=_csrf, aria-hidden='true') Take me to my next challenge - if (points && points > 2) a.animated.fadeIn.btn.btn-lg.btn-block.btn-twitter(href="https://twitter.com/intent/tweet?text=I%20just%20#{verb}%20Free%20Code%20Camp%20Challenge%20%23#{number}:%20#{name}&url=http%3A%2F%2Ffreecodecamp.com/challenges/#{number}&hashtags=learntocode, javascript" target="_blank") i.fa.fa-twitter