Users can now jump around and continue a linear progression through challenges. Added getstuff for easy debugging

This commit is contained in:
terakilobyte
2015-05-20 00:38:25 -04:00
parent a174a1c630
commit 56bae0ca98
4 changed files with 54 additions and 41 deletions

View File

@ -41,7 +41,10 @@ exports.showAllChallenges = function(req, res) {
}; };
exports.getStuff = function(req, res, next) { exports.getStuff = function(req, res, next) {
res.send({withNames: challengeMapWithNames, withIds: challengeMapWithIds}); res.render('coursewares/getstuff', {
stuff: resources.allChallengeIds(),
stuffWithIds: challengeMapWithIds
});
}; };
exports.returnNextChallenge = function(req, res, next) { exports.returnNextChallenge = function(req, res, next) {
@ -68,58 +71,52 @@ exports.returnNextChallenge = function(req, res, next) {
var nextChallengeId; var nextChallengeId;
var nextChallengeBlock; var nextChallengeBlock;
var challengeId = req.user.currentChallenge.challengeId; var challengeId = String(req.user.currentChallenge.challengeId);
var challengeBlock = req.user.currentChallenge.challengeBlock; var challengeBlock = req.user.currentChallenge.challengeBlock;
debug('this is the user challenge block', challengeBlock);
var indexOfChallenge = challengeMapWithIds[challengeBlock] var indexOfChallenge = challengeMapWithIds[challengeBlock]
.indexOf(challengeId); .indexOf(challengeId);
debug('Logging first two challenge blocks for sanity', challengeMapWithIds[0], challengeMapWithIds[1]);
debug('these are the damn keys on challengeMapWithIds...', Object.keys(challengeMapWithIds));
if (indexOfChallenge + 1 if (indexOfChallenge + 1
< challengeMapWithIds[challengeBlock].length) { < challengeMapWithIds[challengeBlock].length) {
debug('advancing to next challange in current block'); debug('this is index', indexOfChallenge);
debug('current block advance');
debug(challengeMapWithNames[challengeBlock][+indexOfChallenge + 1])
nextChallengeName = nextChallengeName =
challengeMapWithNames[challengeBlock][indexOfChallenge + 1]; challengeMapWithNames[challengeBlock][++indexOfChallenge];
nextChallengeId = challengeMapWithIds[challengeBlock][indexOfChallenge + 1];
nextChallengeBlock = challengeBlock;
} else if (typeof challengeMapWithIds[++challengeBlock] !== 'undefined') { } else if (typeof challengeMapWithIds[++challengeBlock] !== 'undefined') {
debug('Advancing to next block'); debug('next block advance');
nextChallengeName = R.head(challengeMapWithNames[challengeBlock]); nextChallengeName = R.head(challengeMapWithNames[challengeBlock]);
nextChallengeId = R.head(challengeMapWithNames[challengeBlock]);
nextChallengeBlock = challengeBlock;
} else { } else {
debug('completed all challenges'); debug('finished, no advance');
req.flash('errors', { req.flash('errors', {
msg: 'It looks like you have finished all of our challenges.' + msg: 'It looks like you have finished all of our challenges.' +
' Great job! Now on to helping nonprofits!' ' Great job! Now on to helping nonprofits!'
}); });
nextChallengeName = R.head(challengeMapWithNames[0].challenges); nextChallengeName = R.head(challengeMapWithNames[0].challenges);
nextChallengeId = R.head(challengeMapWithNames[0].challenges);
nextChallengeBlock = 0;
} }
req.user.currentChallenge = {
challengeId: nextChallengeId, debug('Should be sending user to challenge %s', nextChallengeName);
challengeName: nextChallengeName,
challengeBlock: nextChallengeBlock
};
req.user.save();
var nameString = nextChallengeName.trim() var nameString = nextChallengeName.trim()
.toLowerCase() .toLowerCase()
.replace(/[^\w\s]/g, '')
.replace(/\s/g, '-') .replace(/\s/g, '-')
debug('this is the namestring we\'re going to look up', nameString);
return res.redirect('../challenges/' + nameString); return res.redirect('../challenges/' + nameString);
}; };
exports.returnCurrentChallenge = function(req, res, next) { exports.returnCurrentChallenge = function(req, res, next) {
debug('these are the damn keys on challengeMapWithIds...', Object.keys(challengeMapWithIds));
debug('sanity check', challengeMapWithIds[0], challengeMapWithIds[1]);
if (!req.user) { if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works'); return res.redirect('../challenges/learn-how-free-code-camp-works');
} }
var completed = req.user.completedChallenges.map(function (elem) {
return elem._id;
});
req.user.uncompletedChallenges = resources.allChallengeIds()
.filter(function (elem) {
if (completed.indexOf(elem) === -1) {
return elem;
}
});
req.user.save();
if (!req.user.currentChallenge) { if (!req.user.currentChallenge) {
req.user.currentChallenge = {}; req.user.currentChallenge = {};
req.user.currentChallenge.challengeId = challengeMapWithIds['0'][0]; req.user.currentChallenge.challengeId = challengeMapWithIds['0'][0];
@ -130,17 +127,15 @@ exports.returnCurrentChallenge = function(req, res, next) {
} }
var nameString = req.user.currentChallenge.challengeName.trim() var nameString = req.user.currentChallenge.challengeName.trim()
.toLowerCase() .toLowerCase()
.replace(/[^\w\s]/g, '')
.replace(/\s/g, '-'); .replace(/\s/g, '-');
debug('this is the namestring we\'re going to look up', nameString);
return res.redirect('../challenges/' + nameString); return res.redirect('../challenges/' + nameString);
}; };
exports.returnIndividualChallenge = function(req, res, next) { exports.returnIndividualChallenge = function(req, res, next) {
debug('this is the user\'s current challenge info', req.user.currentChallenge);
var dashedName = req.params.challengeName; var dashedName = req.params.challengeName;
var challengeName = dashedName.replace(/\-/g, ' '); var challengeName = dashedName.replace(/\-/g, ' ');
debug('looking for %s', challengeName);
Challenge.find({'name': new RegExp(challengeName, 'i')}, Challenge.find({'name': new RegExp(challengeName, 'i')},
function(err, challengeFromMongo) { function(err, challengeFromMongo) {
@ -149,7 +144,6 @@ exports.returnIndividualChallenge = function(req, res, next) {
} }
// Handle not found // Handle not found
if (challengeFromMongo.length < 1) { if (challengeFromMongo.length < 1) {
debug(challengeFromMongo);
req.flash('errors', { req.flash('errors', {
msg: '404: We couldn\'t find a challenge with that name. ' + msg: '404: We couldn\'t find a challenge with that name. ' +
'Please double check the name.' 'Please double check the name.'
@ -157,13 +151,28 @@ exports.returnIndividualChallenge = function(req, res, next) {
return res.redirect('/challenges'); return res.redirect('/challenges');
} }
var challenge = challengeFromMongo.pop(); var challenge = challengeFromMongo.pop();
debug(challenge);
// Redirect to full name if the user only entered a partial // Redirect to full name if the user only entered a partial
var dashedNameFull = challenge.name.toLowerCase().replace(/\s/g, '-'); var dashedNameFull = challenge.name.toLowerCase().replace(/\s/g, '-');
if (dashedNameFull !== dashedName) { if (dashedNameFull !== dashedName) {
return res.redirect('../challenges/' + dashedNameFull); return res.redirect('../challenges/' + dashedNameFull);
} else {
req.user.currentChallenge = {
challengeId: challenge._id,
challengeName: challenge.name,
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
map(function(key) {
return challengeMapWithIds[key]
.filter(function(elem) {
return String(elem) === String(challenge._id);
}).map(function() {
return key;
});
})
))
};
} }
req.user.save();
var challengeType = { var challengeType = {
0: function() { 0: function() {
@ -289,8 +298,7 @@ exports.completedChallenge = function (req, res, next) {
}; };
exports.completedZiplineOrBasejump = function (req, res, next) { exports.completedZiplineOrBasejump = function (req, res, next) {
debug('Inside controller for completed zipline or basejump with data %s',
req.body.coursewareInfo);
var isCompletedWith = req.body.coursewareInfo.completedWith || false; var isCompletedWith = req.body.coursewareInfo.completedWith || false;
var isCompletedDate = Math.round(+new Date()); var isCompletedDate = Math.round(+new Date());
var coursewareHash = req.body.coursewareInfo.coursewareHash; var coursewareHash = req.body.coursewareInfo.coursewareHash;
@ -332,10 +340,7 @@ exports.completedZiplineOrBasejump = function (req, res, next) {
if (err) { if (err) {
return next(err); return next(err);
} }
debug('this is the user object returned %s,' +
' this is the req.user._id %s, ' +
'this is the pairedWith._id %s', user, req.user._id, pairedWith._id);
debug(req.user._id.toString() === pairedWith._id.toString());
if (req.user._id.toString() === pairedWith._id.toString()) { if (req.user._id.toString() === pairedWith._id.toString()) {
return res.sendStatus(200); return res.sendStatus(200);
} }

View File

@ -91,7 +91,7 @@ module.exports = {
} else { } else {
allChallengeIds = []; allChallengeIds = [];
Object.keys(challengeMapWithIds).forEach(function(key) { Object.keys(challengeMapWithIds).forEach(function(key) {
allChallengeIds.push(challengeMapWithIds[key].challenges); allChallengeIds.push(challengeMapWithIds[key]);
}); });
allChallengeIds = R.flatten(allChallengeIds); allChallengeIds = R.flatten(allChallengeIds);
} }

View File

@ -0,0 +1,9 @@
//
Created by nathanleniz on 5/19/15.
extends ../layout-wide
block content
script.
var stuff = !{JSON.stringify(stuff)};
var challengeMapWithIds = !{JSON.stringify(stuffWithIds)};

View File

@ -56,9 +56,8 @@ block content
$('#next-courseware-button').attr('disabled', 'disabled'); $('#next-courseware-button').attr('disabled', 'disabled');
var tests = !{JSON.stringify(tests)}; var tests = !{JSON.stringify(tests)};
var challengeSeed = !{JSON.stringify(challengeSeed)}; var challengeSeed = !{JSON.stringify(challengeSeed)};
var passedCoursewareHash = !{JSON.stringify(coursewareHash)}; var challengeId = !{JSON.stringify(coursewareHash)};
var challengeName = !{JSON.stringify(name)}; var challengeName = !{JSON.stringify(name)};
var passedCoursewareName = challengeName;
var prodOrDev = !{JSON.stringify(environment)}; var prodOrDev = !{JSON.stringify(environment)};
var challengeType = !{JSON.stringify(challengeType)}; var challengeType = !{JSON.stringify(challengeType)};
var started = Math.floor(Date.now()); var started = Math.floor(Date.now());