Create helper methods in resources. Get map view working.

This commit is contained in:
terakilobyte
2015-05-20 13:07:41 -04:00
parent 77ddaa5b3a
commit 27a14dbf0f
4 changed files with 108 additions and 37 deletions

View File

@ -253,6 +253,10 @@ exports.returnIndividualChallenge = function(req, res, next) {
challengeId: challenge._id,
challengeType: challenge.challengeType
});
},
5: function() {
// bonfire
}
};

View File

@ -4,13 +4,11 @@ var async = require('async'),
Story = require('./../models/Story'),
Nonprofit = require('./../models/Nonprofit'),
Comment = require('./../models/Comment'),
Courseware = require('./../models/Courseware'),
Challenge = require('./../models/Challenge'),
resources = require('./resources'),
steps = resources.steps,
secrets = require('./../config/secrets'),
bonfires = require('../seed_data/bonfires.json'),
nonprofits = require('../seed_data/nonprofits.json'),
coursewares = require('../seed_data/coursewares.json'),
moment = require('moment'),
https = require('https'),
debug = require('debug')('freecc:cntr:resources'),
@ -18,38 +16,94 @@ var async = require('async'),
request = require('request'),
R = require('ramda');
var challengeTypes = {
'HTML_CSS_JQ': 0,
'JAVASCRIPT': 1,
'VIDEO': 2,
'ZIPLINE': 3,
'BASEJUMP': 4,
'BONFIRE': 5
};
module.exports = {
challengeMap: function challengeMap(req, res) {
var completedBonfires = [];
var completedList = [];
if (req.user) {
completedBonfires = req.user.completedBonfires.map(function (elem) {
return elem._id;
});
completedBonfires = req.user.completedChallenges
.filter(function (elem) {
return elem.challengeType === challengeTypes.BONFIRE;
})
.map(function(elem) {
return elem._id;
});
}
if (req.user) {
completedList = req.user.completedCoursewares.map(function (elem) {
return elem._id;
});
completedList = req.user.completedChallenges
.filter(function (elem) {
return elem.challengeType !== challengeTypes.BONFIRE;
});
}
var noDuplicateBonfires = R.uniq(completedBonfires);
var noDuplicatedCoursewares = R.uniq(completedList);
var noDuplicatedChallenges = R.uniq(completedList);
bonfireList = resources.allBonfireNames();
completedBonfireList = noDuplicateBonfires;
coursewareList = resources.allCoursewareNames();
completedCoursewareList = noDuplicatedCoursewares;
waypoints = coursewareList.filter(function(challenge) {
if (challenge.challengeType === 2 || challenge.challengeType === 0) { return challenge }
var completedBonfireList = noDuplicateBonfires
.map(function(bonfire) {
return bonfire._id;
});
var challengeList = resources.allChallenges();
var completedChallengeList = noDuplicatedChallenges
.map(function(challenge) {
return challenge._id;
});
var bonfireList = challengeList
.filter(function(challenge) {
return challenge.challengeType === challengeTypes.BONFIRE;
})
.map(function(bonfire) {
return ({
'_id': bonfire._id,
'name': bonfire.name
});
});
var waypoints = challengeList.filter(function(challenge) {
if (challenge.challengeType === challengeTypes.VIDEO
|| challenge.challengeType === challengeTypes.HTML_CSS_JQ
|| challenge.challengeType === challengeTypes.JAVASCRIPT) {
return challenge;
}
}).map(function(waypoint) {
return ({
'_id': waypoint._id,
'name': waypoint.name
});
});
ziplines = coursewareList.filter(function(challenge) {
if (challenge.challengeType === 3) { return challenge }
var ziplines = challengeList.filter(function(challenge) {
if (challenge.challengeType === challengeTypes.ZIPLINE) {
return challenge;
}
}).map(function(zipline) {
return ({
'_id': zipline._id,
'name': zipline.name
});
});
basejumps = coursewareList.filter(function(challenge) {
if (challenge.challengeType === 4) { return challenge }
var basejumps = challengeList.filter(function(challenge) {
if (challenge.challengeType === challengeTypes.BASEJUMP) {
return challenge;
}
}).map(function(basejump) {
return ({
'_id': basejump._id,
'name': basejump.name
});
});
function numberWithCommas(x) {
@ -66,6 +120,7 @@ module.exports = {
debug('User err: ', err);
return next(err);
}
debug('Data for render is: ', completedBonfireList, completedChallengeList);
res.render('challengeMap/show', {
daysRunning: daysRunning,
camperCount: numberWithCommas(camperCount),
@ -75,7 +130,7 @@ module.exports = {
ziplines: ziplines,
basejumps: basejumps,
completedBonfireList: completedBonfireList,
completedCoursewareList: completedCoursewareList
completedChallengeList: completedChallengeList
});
});
}

View File

@ -29,7 +29,7 @@ var async = require('async'),
var allBonfireIds, allBonfireNames, allCoursewareIds, allCoursewareNames,
allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
allBonfireIndexesAndNames, challengeMap, challengeMapWithIds,
challengeMapWithNames, allChallengeIds;
challengeMapWithNames, allChallengeIds, allChallenges;
/**
* GET /
@ -70,9 +70,7 @@ Array.zip = function(left, right, combinerFunction) {
module.exports = {
getChallengeMapWithIds: function() {
if (challengeMapWithIds) {
return challengeMapWithIds;
} else {
if (!challengeMapWithIds) {
challengeMapWithIds = {};
Object.keys(challengeMap).forEach(function (key) {
var onlyIds = challengeMap[key].challenges.map(function (elem) {
@ -80,17 +78,15 @@ module.exports = {
});
challengeMapWithIds[key] = onlyIds;
});
return challengeMapWithIds;
}
return challengeMapWithIds;
},
allChallengeIds: function() {
if (allChallengeIds) {
return allChallengeIds;
} else {
if (!allChallengeIds) {
allChallengeIds = [];
Object.keys(challengeMapWithIds).forEach(function(key) {
Object.keys(this.getChallengeMapWithIds()).forEach(function(key) {
allChallengeIds.push(challengeMapWithIds[key]);
});
allChallengeIds = R.flatten(allChallengeIds);
@ -98,10 +94,19 @@ module.exports = {
return allChallengeIds;
},
allChallenges: function() {
if (!allChallenges) {
allChallenges = [];
Object.keys(this.getChallengeMapWithNames()).forEach(function(key) {
allChallenges.push(challengeMap[key].challenges);
});
allChallenges = R.flatten(allChallenges);
}
return allChallenges;
},
getChallengeMapWithNames: function() {
if (challengeMapWithNames) {
return challengeMapWithNames;
} else {
if (!challengeMapWithNames) {
challengeMapWithNames = {};
Object.keys(challengeMap).
forEach(function (key) {
@ -110,8 +115,8 @@ module.exports = {
});
challengeMapWithNames[key] = onlyNames;
});
return challengeMapWithNames;
}
return challengeMapWithNames;
},
sitemap: function sitemap(req, res, next) {

View File

@ -1,5 +1,12 @@
extends ../layout
block content
script.
var bonfireList = !{JSON.stringify(bonfires)};
var waypointList = !{JSON.stringify(waypoints)};
var ziplineList = !{JSON.stringify(ziplines)};
var basejumpList = !{JSON.stringify(basejumps)};
var completedBonfires = !{JSON.stringify(completedBonfireList)};
var completedChallenges = !{JSON.stringify(completedChallengeList)};
.panel.panel-info
.panel-heading.text-center
h1 Challenge Map
@ -24,7 +31,7 @@ block content
h3.negative-15
ol
for waypoint in waypoints
if completedCoursewareList.indexOf(waypoint._id) > -1
if completedChallengeList.indexOf(waypoint._id) > -1
.row
.hidden-xs.col-sm-3.col-md-2.text-primary.ion-checkmark-circled.padded-ionic-icon.text-center
.col-xs-12.col-sm-9.col-md-10
@ -65,7 +72,7 @@ block content
h3.negative-15
ol
for zipline in ziplines
if completedCoursewareList.indexOf(zipline._id) > -1
if completedChallengeList.indexOf(zipline._id) > -1
.row
.hidden-xs.col-sm-3.col-md-2.text-primary.ion-checkmark-circled.padded-ionic-icon.text-center
.col-xs-12.col-sm-9.col-md-10
@ -85,7 +92,7 @@ block content
h3.negative-15
ol
for basejump in basejumps
if completedCoursewareList.indexOf(basejump._id) > -1
if completedChallengeList.indexOf(basejump._id) > -1
.row
.hidden-xs.col-sm-3.col-md-2.text-primary.ion-checkmark-circled.padded-ionic-icon.text-center
.col-xs-12.col-sm-9.col-md-10