remove term code campers from codebase - replace with camper. Move bulky function from app.js to bonfire.js

This commit is contained in:
Michael Q Larson
2015-02-01 22:39:59 -08:00
parent afab456ad9
commit 0ee291878a
11 changed files with 252 additions and 186 deletions

View File

@@ -1,64 +1,72 @@
var _ = require('lodash'),
debug = require('debug')('freecc:cntr:coursewares'),
Courseware = require('./../models/Courseware'),
debug = require('debug')('freecc:cntr:bonfires'),
Bonfire = require('./../models/Bonfire'),
User = require('./../models/User'),
resources = require('./resources');
/**
* Courseware controller
* Bonfire controller
*/
exports.courseware = function(req, res) {
res.render('courseware/show.jade', {
exports.bonfireNames = function(req, res) {
res.render('bonfires/showList', {
bonfireList: resources.allBonfireNames()
});
};
var highestCourswareNumber = resources.numberOfCoursewares();
exports.index = function(req, res) {
res.render('bonfire/show.jade', {
completedWith: 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(),
compliments: resources.randomCompliment(),
bonfires: [],
bonfireHash: 'test'
exports.returnNextCourseware = function(req, res, next) {
});
};
exports.returnNextBonfire = function(req, res, next) {
if (!req.user) {
return res.redirect('coursewares/welcome-to-courseware');
return res.redirect('bonfires/meet-bonfire');
}
var completed = req.user.completedBonfires.map(function (elem) {
return elem._id;
});
var currentTime = parseInt(+new Date() / 1000);
if (currentTime - req.user.lastContentSync > 10) {
req.user.lastContentSync = currentTime;
var completed = req.user.completedBonfires.map(function (elem) {
return elem._id;
});
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function (elem) {
if (completed.indexOf(elem) === -1) {
return elem;
}
});
req.user.save();
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function (elem) {
if (completed.indexOf(elem) === -1) {
return elem;
}
});
req.user.save();
}
var uncompletedCoursewares = req.user.uncompletedCoursewares;
var displayedCoursewares = Courseware.find({'_id': uncompletedCoursewares[0]});
displayedCoursewares.exec(function(err, bonfire) {
var uncompletedBonfires = req.user.uncompletedBonfires;
var displayedBonfires = Bonfire.find({'_id': uncompletedBonfires[0]});
displayedBonfires.exec(function(err, bonfire) {
if (err) {
next(err);
}
courseware = courseware.pop();
nameString = courseware[0].name.toLowerCase().replace(/\s/g, '-');
bonfire = bonfire.pop();
nameString = bonfire.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
//});
});
};
@@ -66,7 +74,6 @@ exports.returnIndividualBonfire = function(req, res, next) {
var dashedName = req.params.bonfireName;
bonfireName = dashedName.replace(/\-/g, ' ');
var bonfireNumber = 0;
Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
if (err) {
@@ -76,26 +83,27 @@ exports.returnIndividualBonfire = function(req, res, next) {
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')
return res.redirect('/bonfires')
} else {
bonfire = bonfire.pop();
res.render('bonfire/show', {
completedWith: null,
title: bonfire[bonfireNumber].name,
title: bonfire.name,
dashedName: dashedName,
name: bonfire[bonfireNumber].name,
difficulty: Math.floor(+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,
name: bonfire.name,
difficulty: Math.floor(+bonfire.difficulty),
brief: bonfire.description[0],
details: bonfire.description.slice(1),
tests: bonfire.tests,
challengeSeed: bonfire.challengeSeed,
challengeEntryPoint: bonfire.challengeEntryPoint,
cc: !!req.user,
points: req.user ? req.user.points : undefined,
verb: resources.randomVerb(),
phrase: resources.randomPhrase(),
compliment: resources.randomCompliment(),
bonfires: bonfire,
bonfireHash: bonfire[bonfireNumber]._id
bonfireHash: bonfire._id
});
}
@@ -132,7 +140,7 @@ function randomString() {
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
};
/**
*
@@ -174,11 +182,11 @@ function getRidOfEmpties(elem) {
if (elem.length > 0) {
return elem;
}
}
};
exports.publicGenerator = function(req, res) {
res.render('bonfire/public-generator');
}
};
exports.generateChallenge = function(req, res) {
var bonfireName = req.body.name,
@@ -204,4 +212,80 @@ exports.generateChallenge = function(req, res) {
tests: bonfireTests
};
res.send(response);
}
};
exports.completedBonfire = function (req, res) {
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;
if (isCompletedWith) {
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
paired.exec(function (err, pairedWith) {
if (err) {
return err;
} else {
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index, 1)
}
pairedWith = pairedWith.pop();
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
pairedWith.uncompletedBonfires.splice(index, 1)
}
pairedWith.completedBonfires.push({
_id: bonfireHash,
completedWith: req.user._id,
completedDate: isCompletedDate,
solution: isSolution
})
req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: pairedWith._id,
completedDate: isCompletedDate,
solution: isSolution
})
req.user.save(function (err, user) {
pairedWith.save(function (err, paired) {
if (err) {
throw err;
}
if (user && paired) {
res.send(true);
}
})
});
}
})
} else {
req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: null,
completedDate: isCompletedDate,
solution: isSolution
});
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index, 1)
}
req.user.save(function (err, user) {
if (err) {
throw err;
}
if (user) {
debug('Saving user');
res.send(true)
}
});
}
};