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

7
app.js
View File

@ -267,13 +267,13 @@ app.all('/account', passportConf.isAuthenticated);
app.get('/account/api', userController.getAccountAngular);
app.get('/playground', bonfireController.index);
app.get(
'/bonfires/:bonfireNumber',
'/bonfires/:bonfireName',
bonfireController.returnIndividualBonfire
);
app.get('/bonfire', function(req, res) {
res.redirect(301, '/playground');
});
app.get('/bonfires', bonfireController.returnBonfire);
app.get('/bonfires', bonfireController.returnNextBonfire);
app.get('/bonfire/generator', bonfireController.returnGenerator);
app.post('/bonfire/generator', bonfireController.generateChallenge);
app.get('/bonfire/public-generator', bonfireController.publicGenerator);
@ -311,8 +311,6 @@ app.post('/completed-challenge', function (req, res) {
});
app.post('/completed-bonfire/', function (req, res) {
debug(req.body, 'In post method'
); // TODO: remove debug statement
req.user.bonfiresHash[parseInt(req.body.bonfireNumber)] =
Math.round(+new Date() / 1000);
var timestamp = req.user.bonfiresHash;
@ -364,6 +362,7 @@ app.post('/completed-bonfire/', function (req, res) {
};
req.user.save();
}
bonfireController.returnNextBonfire(req, res);
});
/**

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);

View File

@ -4,6 +4,7 @@ var User = require('../models/User'),
steps = resources.steps,
secrets = require('./../config/secrets'),
Challenge = require('./../models/Challenge'),
bonfires = require('../seed_data/bonfires.json');
Client = require('node-rest-client').Client,
client = new Client();
@ -182,6 +183,10 @@ module.exports = {
randomCompliment: function() {
var compliments = resources.compliments;
return compliments[Math.floor(Math.random() * compliments.length)];
},
numberOfBonfires: function() {
return bonfires.length - 1;
}
};

View File

@ -354,6 +354,60 @@ var userSchema = new mongoose.Schema({
resetPasswordToken: String,
resetPasswordExpires: Date,
bonfiresHash: {
ab6137d4e35944e21037b769: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
a3566b1109230028080c9345: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
a26cbbe9ad8655a977e1ceb5: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
a302f7aae1aa3152a5b413bc: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
a202eed8fc186c8434cb6d61: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
aff0395860f5d3034dc0bfc9: {
hash: String,
completedWith: String,
completedDate: {
type: Number,
default: 0
},
solution: String
},
aaa48de84e1ecc7c742e1124: {
hash: String,
completedWith: String,
@ -363,7 +417,7 @@ var userSchema = new mongoose.Schema({
},
solution: String
},
ff0395860f5d3034dc0bfc94: {
ad7123c8c441eddfaeb5bdef: {
hash: String,
completedWith: String,
completedDate: {

View File

@ -25,8 +25,7 @@ editor.setSize("100%", "auto");
// Default value for editor if one isn't provided in (i.e. a challenge)
var nonChallengeValue = '/*Welcome to Bonfire, Free Code Camp\'s future CoderByte replacement.\n' +
'Please feel free to use Bonfire as an in-browser playground and linting tool.\n' +
'Note that you can also write tests using Chai.js\n' +
' by using the keywords assert and expect */\n\n' +
'Note that you can also write tests using Chai.js by using the keywords assert and expect */\n\n' +
'function test() {\n' +
' assert(2 !== 3, "2 is not equal to 3");\n' +
' return [1,2,3].map(function(elem) {\n' +
@ -37,8 +36,7 @@ var nonChallengeValue = '/*Welcome to Bonfire, Free Code Camp\'s future CoderByt
'assert.deepEqual(test(), [1,4,9]);\n\n' +
'var foo = test();\n' +
'foo.should.be.a("array");\n\n' +
'test();\n' +
'function test(str) {\r\n return str;\r\n}';
'test();\n';
var codeOutput = CodeMirror.fromTextArea(document.getElementById("codeOutput"), {
lineNumbers: false,

View File

@ -1,6 +1,6 @@
[
{
"_id" : "7123c8c441eddfaeb5bdef0d",
"_id" : "ad7123c8c441eddfaeb5bdef",
"name": "Meet Bonfire",
"difficulty": "0",
"description": [
@ -40,7 +40,7 @@
"challengeEntryPoint": "palindrome(\"eye\");"
},
{
"_id" : "ff0395860f5d3034dc0bfc94",
"_id" : "aff0395860f5d3034dc0bfc9",
"name": "Validate US Telephone Numbers",
"difficulty": "3",
"description": [
@ -77,7 +77,7 @@
"challengeEntryPoint": "telephoneCheck(\"555-555-5555\");"
},
{
"_id": "202eed8fc186c8434cb6d618",
"_id": "a202eed8fc186c8434cb6d61",
"name": "Reverse a String",
"difficulty": "1",
"tests": [
@ -95,7 +95,7 @@
"challengeSeed": "function reverseString(str) {\n return str;\r\n}"
},
{
"_id": "302f7aae1aa3152a5b413bca",
"_id": "a302f7aae1aa3152a5b413bc",
"name": "Factorialize a Number",
"tests": [
"expect(factorialize(5)).to.be.a(\"Number\");",
@ -132,7 +132,7 @@
]
},
{
"_id": "3566b1109230028080c9345d",
"_id": "a3566b1109230028080c9345",
"name": "Sum All Numbers in a Range",
"difficulty": "2",
"description": [
@ -150,7 +150,7 @@
]
},
{
"_id": "fb6137d4e35944e21037b769",
"_id": "ab6137d4e35944e21037b769",
"name": "Title Case a Sentence",
"difficulty": "1",
"description": [