2015-01-10 22:59:24 -08:00
|
|
|
var _ = require('lodash'),
|
2015-01-21 21:32:13 -05:00
|
|
|
debug = require('debug')('freecc:cntr:bonfires'),
|
2015-01-24 14:29:50 -08:00
|
|
|
Bonfire = require('./../models/Bonfire'),
|
2015-01-24 23:03:25 -08:00
|
|
|
User = require('./../models/User'),
|
2015-01-25 10:02:41 -05:00
|
|
|
resources = require('./resources');
|
2015-01-10 22:59:24 -08:00
|
|
|
|
2015-01-11 00:45:37 -05:00
|
|
|
/**
|
|
|
|
* Bonfire controller
|
|
|
|
*/
|
2015-01-21 21:32:13 -05:00
|
|
|
|
2015-01-25 10:02:41 -05:00
|
|
|
var highestBonfireNumber = 2;
|
2015-01-21 21:32:13 -05:00
|
|
|
|
2015-01-10 22:59:24 -08:00
|
|
|
exports.index = function(req, res) {
|
2015-01-21 21:32:13 -05:00
|
|
|
res.render('bonfire/bonfire.jade', {
|
|
|
|
title: 'Learn to code with Bonfire'
|
|
|
|
});
|
|
|
|
|
2015-01-25 23:10:05 -08:00
|
|
|
Bonfire.find({}, null, { sort: { difficulty: 1 } }, function(err, c) {
|
2015-01-21 21:32:13 -05:00
|
|
|
if (err) {
|
|
|
|
debug('bonfire err: ', err);
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
2015-01-16 18:58:27 -05:00
|
|
|
};
|
|
|
|
|
2015-01-21 21:32:13 -05:00
|
|
|
exports.returnBonfire = function(req, res, next) {
|
|
|
|
var bonfireNumber = parseInt(req.params.bonfireNumber) || 0;
|
2015-01-24 14:29:50 -08:00
|
|
|
// 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,
|
2015-01-25 23:10:05 -08:00
|
|
|
difficulty: +bonfire[bonfireNumber].difficulty,
|
2015-01-24 20:49:59 -05:00
|
|
|
brief: bonfire[bonfireNumber].description[0],
|
|
|
|
details: bonfire[bonfireNumber].description.slice(1),
|
2015-01-24 14:29:50 -08:00
|
|
|
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,
|
2015-01-25 10:02:41 -05:00
|
|
|
verb: resources.randomVerb(),
|
|
|
|
phrase: resources.randomPhrase(),
|
|
|
|
compliments: resources.randomCompliment(),
|
2015-01-24 14:29:50 -08:00
|
|
|
bonfires: bonfire,
|
|
|
|
bonfireHash: bonfire[bonfireNumber]._id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.returnIndividualBonfire = function(req, res, next) {
|
|
|
|
var bonfireNumber = parseInt(req.params.bonfireNumber) || 0;
|
|
|
|
|
2015-01-21 21:32:13 -05:00
|
|
|
if (bonfireNumber > highestBonfireNumber) { bonfireNumber = 0; }
|
2015-01-25 23:10:05 -08:00
|
|
|
Bonfire.find({}, null, { sort: { difficulty: 1 } }, function(err, bonfire) {
|
2015-01-21 21:32:13 -05:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
2015-01-22 13:20:46 -05:00
|
|
|
res.render('bonfire/show', {
|
2015-01-24 03:11:01 -05:00
|
|
|
completedWith: null,
|
2015-01-22 13:20:46 -05:00
|
|
|
title: bonfire[bonfireNumber].name,
|
|
|
|
name: bonfire[bonfireNumber].name,
|
2015-01-25 23:10:05 -08:00
|
|
|
difficulty: +bonfire[bonfireNumber].difficulty,
|
2015-01-24 20:49:59 -05:00
|
|
|
brief: bonfire[bonfireNumber].description[0],
|
|
|
|
details: bonfire[bonfireNumber].description.slice(1),
|
2015-01-24 00:44:08 -05:00
|
|
|
tests: bonfire[bonfireNumber].tests,
|
2015-01-22 13:20:46 -05:00
|
|
|
challengeSeed: bonfire[bonfireNumber].challengeSeed,
|
|
|
|
challengeEntryPoint: bonfire[bonfireNumber].challengeEntryPoint,
|
2015-01-24 00:44:08 -05:00
|
|
|
cc: req.user ? req.user.bonfiresHash : undefined,
|
|
|
|
points: req.user ? req.user.points : undefined,
|
2015-01-25 10:02:41 -05:00
|
|
|
verb: resources.randomVerb(),
|
|
|
|
phrase: resources.randomPhrase(),
|
|
|
|
compliment: resources.randomCompliment(),
|
2015-01-24 03:11:01 -05:00
|
|
|
bonfires: bonfire,
|
|
|
|
bonfireHash: bonfire[bonfireNumber]._id
|
2015-01-21 21:32:13 -05:00
|
|
|
});
|
|
|
|
});
|
2015-01-25 23:56:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bonfire generator
|
|
|
|
*/
|
|
|
|
exports.returnGenerator = function(req, res) {
|
|
|
|
res.render('bonfire/generator', {
|
|
|
|
title: null,
|
|
|
|
name: null,
|
|
|
|
difficulty: null,
|
|
|
|
brief: null,
|
|
|
|
details: null,
|
|
|
|
tests: null,
|
|
|
|
challengeSeed: null,
|
|
|
|
challengeEntryPoint: null,
|
|
|
|
bonfireHash: randomString()
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post for bonfire generation
|
|
|
|
*/
|
|
|
|
|
|
|
|
function randomString() {
|
|
|
|
var chars = "0123456789abcdef";
|
|
|
|
var string_length = 24;
|
|
|
|
var randomstring = '';
|
|
|
|
for (var i=0; i<string_length; i++) {
|
|
|
|
var rnum = Math.floor(Math.random() * chars.length);
|
|
|
|
randomstring += chars.substring(rnum,rnum+1);
|
|
|
|
}
|
|
|
|
return randomstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.testBonfire = function(req, res) {
|
2015-01-25 23:10:05 -08:00
|
|
|
var bonfireName = req.body.name,
|
|
|
|
bonfireTests = req.body.tests,
|
|
|
|
bonfireDifficulty = req.body.difficulty,
|
|
|
|
bonfireDescription = req.body.description,
|
|
|
|
bonfireEntryPoint = req.body.challengeEntryPoint,
|
|
|
|
bonfireChallengeSeed = req.body.challengeSeed;
|
|
|
|
bonfireTests = bonfireTests.split('\r\n');
|
|
|
|
bonfireDescription = bonfireDescription.split('\r\n');
|
|
|
|
bonfireTests.filter(getRidOfEmpties);
|
|
|
|
bonfireDescription.filter(getRidOfEmpties);
|
|
|
|
bonfireChallengeSeed = bonfireChallengeSeed.replace('\r', '');
|
|
|
|
res.render('bonfire/show', {
|
|
|
|
completedWith: null,
|
|
|
|
title: bonfireName,
|
|
|
|
difficulty: +bonfireDifficulty,
|
|
|
|
brief: bonfireDescription[0],
|
|
|
|
details: bonfireDescription.slice(1),
|
|
|
|
tests: bonfireTests,
|
|
|
|
challengeSeed: bonfireChallengeSeed,
|
|
|
|
challengeEntryPoint: bonfireEntryPoint,
|
|
|
|
cc: req.user ? req.user.bonfiresHash : undefined,
|
|
|
|
points: req.user ? req.user.points : undefined,
|
|
|
|
verb: resources.randomVerb(),
|
|
|
|
phrase: resources.randomPhrase(),
|
|
|
|
compliment: resources.randomCompliment(),
|
|
|
|
bonfires: [],
|
|
|
|
bonfireHash: "test"
|
2015-01-25 23:56:04 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function getRidOfEmpties(elem) {
|
|
|
|
if (elem.length > 0) {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:10:05 -08:00
|
|
|
exports.publicGenerator = function(req, res) {
|
|
|
|
res.render('bonfire/public-generator');
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:56:04 -05:00
|
|
|
exports.generateChallenge = function(req, res) {
|
|
|
|
var bonfireName = req.body.name,
|
|
|
|
bonfireTests = req.body.tests,
|
|
|
|
bonfireDifficulty = req.body.difficulty,
|
|
|
|
bonfireDescription = req.body.description,
|
|
|
|
bonfireEntryPoint = req.body.challengeEntryPoint,
|
|
|
|
bonfireChallengeSeed = req.body.challengeSeed;
|
|
|
|
bonfireTests = bonfireTests.split('\r\n');
|
|
|
|
bonfireDescription = bonfireDescription.split('\r\n');
|
|
|
|
bonfireTests.filter(getRidOfEmpties);
|
|
|
|
bonfireDescription.filter(getRidOfEmpties);
|
|
|
|
bonfireChallengeSeed = bonfireChallengeSeed.replace('\r', '');
|
|
|
|
|
|
|
|
|
|
|
|
var response = {
|
2015-01-25 23:10:05 -08:00
|
|
|
_id: randomString(),
|
2015-01-25 23:56:04 -05:00
|
|
|
name: bonfireName,
|
|
|
|
difficulty: bonfireDifficulty,
|
|
|
|
description: bonfireDescription,
|
|
|
|
challengeEntryPoint: bonfireEntryPoint,
|
|
|
|
challengeSeed: bonfireChallengeSeed,
|
2015-01-25 23:10:05 -08:00
|
|
|
tests: bonfireTests
|
2015-01-25 23:56:04 -05:00
|
|
|
};
|
|
|
|
res.send(response);
|
|
|
|
}
|