From c4bd0e4dc3447810bc728eb95ebf5e5852f77232 Mon Sep 17 00:00:00 2001 From: Michael Q Larson Date: Sat, 24 Jan 2015 14:29:50 -0800 Subject: [PATCH] make bonfire dynamically direct to best subsequent bonfire after completion --- app.js | 3 +- controllers/bonfire.js | 76 ++++++++++++++++++++++++++++++++++++-- models/User.js | 2 + views/partials/footer.jade | 4 +- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index ecc99cd09b..9966d52262 100644 --- a/app.js +++ b/app.js @@ -258,8 +258,9 @@ app.get('/account/api', userController.getAccountAngular); app.get('/bonfire', bonfireController.index); app.get( '/bonfires/:bonfireNumber', - bonfireController.returnBonfire + bonfireController.returnIndividualBonfire ); +app.get('/bonfires', bonfireController.returnBonfire); // Unique Check API route app.get('/api/checkUniqueUsername/:username', userController.checkUniqueUsername); diff --git a/controllers/bonfire.js b/controllers/bonfire.js index 34311cb3a3..495a27e1d2 100644 --- a/controllers/bonfire.js +++ b/controllers/bonfire.js @@ -1,6 +1,7 @@ var _ = require('lodash'), debug = require('debug')('freecc:cntr:bonfires'), - Bonfire = require('./../models/Bonfire'); + Bonfire = require('./../models/Bonfire'), + User = require('./../models/User'); /** * Bonfire controller @@ -49,9 +50,78 @@ exports.returnBonfire = function(req, res, next) { "Prove to mom that computers aren't just for games" ]; + + // This code is in bad need of refactoring + var bonfiresToFind = req.user.bonfiresHash; + var bonfiresArray = _.map(bonfiresToFind, function(value, index) { + return [index, value]; + }); + // Get rid of the first entry, which will be a useless function that causes an error. + bonfiresArray.shift(); + unsolvedBonfires = []; + for (i = 0; i < bonfiresArray.length; i++) { + if (bonfiresArray[i][1]["completedDate"] === 0) { + unsolvedBonfires.push(bonfiresArray[i][0]) + } + } + + //.where('likes').in(['vaporizing', 'talking']) + var displayedBonfires = Bonfire.find({}).where('_id').in(unsolvedBonfires).sort({ difficulty: 1 }); + displayedBonfires.exec(function(err, bonfire) { + if (err) { + next(err); + } + res.render('bonfire/show', { + completedWith: null, + title: bonfire[bonfireNumber].name, + name: bonfire[bonfireNumber].name, + number: bonfire[bonfireNumber].bonfireNumber, + difficulty: bonfire[bonfireNumber].difficulty, + description: bonfire[bonfireNumber].description, + tests: bonfire[bonfireNumber].tests, + challengeSeed: bonfire[bonfireNumber].challengeSeed, + challengeEntryPoint: bonfire[bonfireNumber].challengeEntryPoint, + cc: req.user ? req.user.bonfiresHash : undefined, + 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, + bonfireHash: bonfire[bonfireNumber]._id + }); + }); +}; + +exports.returnIndividualBonfire = function(req, res, next) { + var bonfireNumber = parseInt(req.params.bonfireNumber) || 0; + var verbs = [ + 'ACED', + 'NAILED', + 'ROCKED', + 'SCORCHED', + 'DEVASTATED', + 'OWNED', + 'CRUSHED', + 'CONQUERED', + 'KILLED', + 'SHREDDED', + 'ANNIHILATED', + 'NUKED' + ]; + var phrases = [ + "Shout it from on top of a mountain", + "Tell everyone and their dogs", + "Show them. Show them all!", + "Inspire your friends", + "Tell the world of your greatness", + "Look accomplished on social media", + "Share news of your grand endeavor", + "Establish your alibi for the past two hours", + "Prove to mom that computers aren't just for games" + ]; + + if (bonfireNumber > highestBonfireNumber) { bonfireNumber = 0; } - Bonfire.find({}, null, { sort: { difficulty: 1, bonfireNumber: 1 } }, function(err, bonfire) { - debug(bonfire[bonfireNumber].challengeEntryPoint); + Bonfire.find({}, null, { sort: { bonfireNumber: 1 } }, function(err, bonfire) { if (err) { next(err); } diff --git a/models/User.js b/models/User.js index 84cd18d600..f4a4faacce 100644 --- a/models/User.js +++ b/models/User.js @@ -355,6 +355,7 @@ var userSchema = new mongoose.Schema({ resetPasswordExpires: Date, bonfiresHash: { aaa48de84e1ecc7c742e1124: { + hash: String, completedWith: String, completedDate: { type: Number, @@ -363,6 +364,7 @@ var userSchema = new mongoose.Schema({ solution: String }, ff0395860f5d3034dc0bfc94: { + hash: String, completedWith: String, completedDate: { type: Number, diff --git a/views/partials/footer.jade b/views/partials/footer.jade index 0e1ccbe8a3..f4d23dd17d 100644 --- a/views/partials/footer.jade +++ b/views/partials/footer.jade @@ -15,4 +15,6 @@ |   a.ion-information-circled(title="About Free Code Camp", href="/learn-to-code") |   - a.ion-locked(title="Free Code Camp's Privacy Policy", href="/privacy") \ No newline at end of file + a.ion-locked(title="Free Code Camp's Privacy Policy", href="/privacy") + |   + a.ion-code-working(title="Bonfire Coding Playground", href="/bonfire") \ No newline at end of file