Strange user behavior, model not updating correctly

This commit is contained in:
Nathan Leniz
2015-01-26 18:28:14 -05:00
parent f8343d4c13
commit 2e5dd6ea73
7 changed files with 165 additions and 91 deletions

View File

@@ -8,26 +8,26 @@ var _ = require('lodash'),
* Bonfire controller
*/
var highestBonfireNumber = 2;
var highestBonfireNumber = resources.numberOfBonfires();
exports.index = function(req, res) {
res.render('bonfire/bonfire.jade', {
title: 'Learn to code with Bonfire',
res.render('bonfire/show.jade', {
completedWith: null,
title: null,
difficulty: null,
brief: null,
details: null,
tests: null,
challengeSeed: null,
challengeEntryPoint: null,
title: 'Bonfire Playground',
name: 'Bonfire Playground',
difficulty: 0,
brief: 'Feel free to play around!',
details: '',
tests: [],
challengeSeed: '',
challengeEntryPoint: '',
cc: req.user ? req.user.bonfiresHash : undefined,
points: req.user ? req.user.points : undefined,
verb: resources.randomVerb(),
phrase: resources.randomPhrase(),
compliment: resources.randomCompliment(),
compliments: resources.randomCompliment(),
bonfires: [],
bonfireHash: "test"
bonfireHash: 'test'
});
@@ -39,11 +39,12 @@ exports.index = function(req, res) {
});
};
exports.returnBonfire = function(req, res, next) {
exports.returnNextBonfire = function(req, res, next) {
var bonfireNumber;
if (!req.user) {
req.user = new User();
//return res.redirect('/bonfires/meet-bonfire');
}
var bonfireNumber = parseInt(req.params.bonfireNumber) || 0;
// This code is in bad need of refactoring
var bonfiresToFind = req.user ? req.user.bonfiresHash : [];
var bonfiresArray = _.map(bonfiresToFind, function(value, index) {
@@ -51,48 +52,65 @@ exports.returnBonfire = function(req, res, next) {
});
// Get rid of the first entry, which will be a useless function that causes an error.
bonfiresArray.shift();
unsolvedBonfires = [];
var 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,
difficulty: +bonfire[bonfireNumber].difficulty,
brief: bonfire[bonfireNumber].description[0],
details: bonfire[bonfireNumber].description.slice(1),
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: resources.randomVerb(),
phrase: resources.randomPhrase(),
compliments: resources.randomCompliment(),
bonfires: bonfire,
bonfireHash: bonfire[bonfireNumber]._id
});
debug('Finding next bonfire for user', bonfire);
nameString = bonfire[0].name.toLowerCase().replace(/\s/g, '-');
return res.redirect('/bonfires/' + nameString);
//res.render('bonfire/show', {
// completedWith: null,
// title: bonfire[bonfireNumber].name,
// name: bonfire[bonfireNumber].name,
// difficulty: +bonfire[bonfireNumber].difficulty,
// brief: bonfire[bonfireNumber].description[0],
// details: bonfire[bonfireNumber].description.slice(1),
// 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: resources.randomVerb(),
// phrase: resources.randomPhrase(),
// compliments: resources.randomCompliment(),
// bonfires: bonfire,
// bonfireHash: bonfire[bonfireNumber]._id
//});
});
};
exports.returnIndividualBonfire = function(req, res, next) {
var bonfireNumber = parseInt(req.params.bonfireNumber) || 0;
var bonfireName = req.params.bonfireName;
debug('Getting this as argument for bonfireName', bonfireName);
//if (!/[a-z\-]+/i.test(bonfireName)) {
// bonfireName = 'meet bonfire';
//}
if (bonfireNumber > highestBonfireNumber) { bonfireNumber = 0; }
Bonfire.find({}, null, { sort: { difficulty: 1 } }, function(err, bonfire) {
bonfireName = bonfireName.replace(/\-/g, ' ');
debug('Checking sanity of name of bonfire after replacing "-" characters', bonfireName);
var bonfireNumber = 0;
Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
if (err) {
next(err);
}
if (bonfire.length < 1) {
debug('Full Bonfire', bonfire);
req.flash('errors', {
msg: "404: We couldn't find a bonfire with that name. Please double check the name."
});
return res.redirect('/bonfires/meet-bonfire')
}
res.render('bonfire/show', {
completedWith: null,
title: bonfire[bonfireNumber].name,
@@ -137,8 +155,8 @@ exports.returnGenerator = function(req, res) {
function randomString() {
var chars = "0123456789abcdef";
var string_length = 24;
var randomstring = '';
var string_length = 23;
var randomstring = 'a';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);