2015-01-11 06:59:24 +00:00
|
|
|
var _ = require('lodash'),
|
2015-01-22 02:32:13 +00:00
|
|
|
debug = require('debug')('freecc:cntr:bonfires'),
|
2015-01-24 22:29:50 +00:00
|
|
|
Bonfire = require('./../models/Bonfire'),
|
2015-01-25 07:03:25 +00:00
|
|
|
User = require('./../models/User'),
|
2015-01-25 15:02:41 +00:00
|
|
|
resources = require('./resources');
|
2015-01-11 06:59:24 +00:00
|
|
|
|
2015-01-11 05:45:37 +00:00
|
|
|
/**
|
|
|
|
* Bonfire controller
|
|
|
|
*/
|
2015-01-22 02:32:13 +00:00
|
|
|
|
2015-02-01 22:22:34 +00:00
|
|
|
exports.bonfireNames = function(req, res) {
|
|
|
|
res.render('bonfires/showList', {
|
|
|
|
bonfireList: resources.allBonfireNames()
|
2015-01-30 01:01:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-01-11 06:59:24 +00:00
|
|
|
exports.index = function(req, res) {
|
2015-01-26 23:28:14 +00:00
|
|
|
res.render('bonfire/show.jade', {
|
2015-01-26 17:30:04 +00:00
|
|
|
completedWith: null,
|
2015-01-26 23:28:14 +00:00
|
|
|
title: 'Bonfire Playground',
|
|
|
|
name: 'Bonfire Playground',
|
|
|
|
difficulty: 0,
|
|
|
|
brief: 'Feel free to play around!',
|
|
|
|
details: '',
|
|
|
|
tests: [],
|
|
|
|
challengeSeed: '',
|
|
|
|
challengeEntryPoint: '',
|
2015-01-26 17:30:04 +00:00
|
|
|
cc: req.user ? req.user.bonfiresHash : undefined,
|
|
|
|
points: req.user ? req.user.points : undefined,
|
|
|
|
verb: resources.randomVerb(),
|
|
|
|
phrase: resources.randomPhrase(),
|
2015-01-26 23:28:14 +00:00
|
|
|
compliments: resources.randomCompliment(),
|
2015-01-26 17:30:04 +00:00
|
|
|
bonfires: [],
|
2015-01-26 23:28:14 +00:00
|
|
|
bonfireHash: 'test'
|
2015-01-26 17:30:04 +00:00
|
|
|
|
2015-01-22 02:32:13 +00:00
|
|
|
});
|
2015-01-16 23:58:27 +00:00
|
|
|
};
|
|
|
|
|
2015-02-02 07:35:27 +00:00
|
|
|
exports.returnNextBonfire = function(req, res) {
|
2015-01-26 19:38:19 +00:00
|
|
|
if (!req.user) {
|
2015-02-04 05:41:25 +00:00
|
|
|
return res.redirect('../bonfires/meet-bonfire');
|
2015-01-26 19:38:19 +00:00
|
|
|
}
|
2015-02-02 07:35:27 +00:00
|
|
|
var completed = req.user.completedBonfires.map(function (elem) {
|
|
|
|
return elem._id;
|
|
|
|
});
|
2015-01-27 22:39:53 +00:00
|
|
|
|
2015-02-02 07:35:27 +00:00
|
|
|
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function (elem) {
|
|
|
|
if (completed.indexOf(elem) === -1) {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
req.user.save();
|
2015-01-24 22:29:50 +00:00
|
|
|
|
2015-01-27 06:22:02 +00:00
|
|
|
var uncompletedBonfires = req.user.uncompletedBonfires;
|
|
|
|
|
|
|
|
var displayedBonfires = Bonfire.find({'_id': uncompletedBonfires[0]});
|
2015-01-24 22:29:50 +00:00
|
|
|
displayedBonfires.exec(function(err, bonfire) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
2015-02-02 06:39:59 +00:00
|
|
|
bonfire = bonfire.pop();
|
|
|
|
nameString = bonfire.name.toLowerCase().replace(/\s/g, '-');
|
2015-01-26 23:28:14 +00:00
|
|
|
return res.redirect('/bonfires/' + nameString);
|
2015-01-24 22:29:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.returnIndividualBonfire = function(req, res, next) {
|
2015-01-29 05:00:14 +00:00
|
|
|
var dashedName = req.params.bonfireName;
|
2015-01-26 23:28:14 +00:00
|
|
|
|
2015-01-29 05:00:14 +00:00
|
|
|
bonfireName = dashedName.replace(/\-/g, ' ');
|
2015-01-24 22:29:50 +00:00
|
|
|
|
2015-01-26 23:28:14 +00:00
|
|
|
Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
|
2015-01-22 02:32:13 +00:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
2015-02-04 19:59:45 +00:00
|
|
|
bonfire = bonfire.pop()
|
|
|
|
var dashedNameFull = bonfire.name.toLowerCase().replace(/\s/g, '-');
|
2015-01-31 01:27:24 +00:00
|
|
|
if (dashedNameFull != dashedName) {
|
2015-02-04 20:32:31 +00:00
|
|
|
return res.redirect('../bonfires/' + dashedNameFull);
|
2015-01-31 01:27:24 +00:00
|
|
|
}
|
2015-01-26 23:28:14 +00:00
|
|
|
if (bonfire.length < 1) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: "404: We couldn't find a bonfire with that name. Please double check the name."
|
|
|
|
});
|
2015-02-02 06:39:59 +00:00
|
|
|
return res.redirect('/bonfires')
|
2015-01-27 06:22:02 +00:00
|
|
|
} else {
|
|
|
|
res.render('bonfire/show', {
|
|
|
|
completedWith: null,
|
2015-02-02 06:39:59 +00:00
|
|
|
title: bonfire.name,
|
2015-01-29 05:00:14 +00:00
|
|
|
dashedName: dashedName,
|
2015-02-02 06:39:59 +00:00
|
|
|
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,
|
2015-01-27 06:22:02 +00:00
|
|
|
cc: !!req.user,
|
|
|
|
points: req.user ? req.user.points : undefined,
|
|
|
|
verb: resources.randomVerb(),
|
|
|
|
phrase: resources.randomPhrase(),
|
|
|
|
compliment: resources.randomCompliment(),
|
|
|
|
bonfires: bonfire,
|
2015-02-02 06:39:59 +00:00
|
|
|
bonfireHash: bonfire._id
|
2015-01-27 06:22:02 +00:00
|
|
|
|
|
|
|
});
|
2015-01-26 23:28:14 +00:00
|
|
|
}
|
2015-01-22 02:32:13 +00:00
|
|
|
});
|
2015-01-26 04:56:04 +00: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";
|
2015-01-26 23:28:14 +00:00
|
|
|
var string_length = 23;
|
|
|
|
var randomstring = 'a';
|
2015-01-26 04:56:04 +00:00
|
|
|
for (var i=0; i<string_length; i++) {
|
|
|
|
var rnum = Math.floor(Math.random() * chars.length);
|
|
|
|
randomstring += chars.substring(rnum,rnum+1);
|
|
|
|
}
|
|
|
|
return randomstring;
|
2015-02-02 06:39:59 +00:00
|
|
|
};
|
2015-01-26 04:56:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.testBonfire = function(req, res) {
|
2015-01-26 07:10:05 +00: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,
|
2015-01-28 03:46:18 +00:00
|
|
|
name: bonfireName,
|
2015-01-26 07:10:05 +00:00
|
|
|
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-26 04:56:04 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function getRidOfEmpties(elem) {
|
|
|
|
if (elem.length > 0) {
|
|
|
|
return elem;
|
|
|
|
}
|
2015-02-02 06:39:59 +00:00
|
|
|
};
|
2015-01-26 04:56:04 +00:00
|
|
|
|
2015-01-26 07:10:05 +00:00
|
|
|
exports.publicGenerator = function(req, res) {
|
|
|
|
res.render('bonfire/public-generator');
|
2015-02-02 06:39:59 +00:00
|
|
|
};
|
2015-01-26 07:10:05 +00:00
|
|
|
|
2015-01-26 04:56:04 +00: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-26 07:10:05 +00:00
|
|
|
_id: randomString(),
|
2015-01-26 04:56:04 +00:00
|
|
|
name: bonfireName,
|
|
|
|
difficulty: bonfireDifficulty,
|
|
|
|
description: bonfireDescription,
|
|
|
|
challengeEntryPoint: bonfireEntryPoint,
|
|
|
|
challengeSeed: bonfireChallengeSeed,
|
2015-01-26 07:10:05 +00:00
|
|
|
tests: bonfireTests
|
2015-01-26 04:56:04 +00:00
|
|
|
};
|
|
|
|
res.send(response);
|
2015-02-02 06:39:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2015-02-05 03:39:57 +00:00
|
|
|
req.user.points++;
|
2015-02-02 06:39:59 +00:00
|
|
|
req.user.uncompletedBonfires.splice(index, 1)
|
|
|
|
}
|
|
|
|
pairedWith = pairedWith.pop();
|
|
|
|
|
|
|
|
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
|
|
|
|
if (index > -1) {
|
2015-02-05 03:39:57 +00:00
|
|
|
pairedWith.points++;
|
|
|
|
pairedWith.uncompletedBonfires.splice(index, 1);
|
|
|
|
|
2015-02-02 06:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pairedWith.completedBonfires.push({
|
|
|
|
_id: bonfireHash,
|
|
|
|
completedWith: req.user._id,
|
|
|
|
completedDate: isCompletedDate,
|
|
|
|
solution: isSolution
|
2015-02-05 03:39:57 +00:00
|
|
|
});
|
2015-02-02 06:39:59 +00:00
|
|
|
|
|
|
|
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) {
|
2015-02-05 03:39:57 +00:00
|
|
|
req.user.points++;
|
2015-02-02 06:39:59 +00:00
|
|
|
req.user.uncompletedBonfires.splice(index, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.user.save(function (err, user) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (user) {
|
|
|
|
debug('Saving user');
|
|
|
|
res.send(true)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|