| 
									
										
										
										
											2015-01-10 22:59:24 -08:00
										 |  |  | var _ = require('lodash'), | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   debug = require('debug')('freecc:cntr:bonfires'), | 
					
						
							|  |  |  |   Bonfire = require('./../models/Bonfire'), | 
					
						
							|  |  |  |   User = require('./../models/User'), | 
					
						
							|  |  |  |   resources = require('./resources'), | 
					
						
							|  |  |  |   R = require('ramda'); | 
					
						
							| 
									
										
										
										
											2015-03-29 13:05:39 -07:00
										 |  |  |   MDNlinks = require('./../seed_data/bonfireMDNlinks'); | 
					
						
							| 
									
										
										
										
											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-02-22 16:27:38 +09:00
										 |  |  | exports.showAllBonfires = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-30 13:09:24 -07:00
										 |  |  |   var completedBonfires = []; | 
					
						
							| 
									
										
										
										
											2015-03-29 16:47:49 -07:00
										 |  |  |   if(req.user) { | 
					
						
							| 
									
										
										
										
											2015-03-30 13:09:24 -07:00
										 |  |  |       completedBonfires = req.user.completedBonfires.map(function (elem) { | 
					
						
							| 
									
										
										
										
											2015-03-29 16:47:49 -07:00
										 |  |  |           return elem._id; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var noDuplicateBonfires = R.uniq(completedBonfires); | 
					
						
							|  |  |  |   var data = {}; | 
					
						
							|  |  |  |   data.bonfireList = resources.allBonfireNames(); | 
					
						
							|  |  |  |   data.completedList = noDuplicateBonfires; | 
					
						
							|  |  |  |   res.send(data); | 
					
						
							| 
									
										
										
										
											2015-01-29 17:01:50 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-10 22:59:24 -08:00
										 |  |  | exports.index = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   res.render('bonfire/show.jade', { | 
					
						
							|  |  |  |     completedWith: null, | 
					
						
							|  |  |  |     title: 'Bonfire Playground', | 
					
						
							|  |  |  |     name: 'Bonfire Playground', | 
					
						
							|  |  |  |     difficulty: 0, | 
					
						
							|  |  |  |     brief: 'Feel free to play around!', | 
					
						
							|  |  |  |     details: '', | 
					
						
							|  |  |  |     tests: [], | 
					
						
							|  |  |  |     challengeSeed: '', | 
					
						
							|  |  |  |     cc: req.user ? req.user.bonfiresHash : undefined, | 
					
						
							|  |  |  |     progressTimestamps: req.user ? req.user.progressTimestamps : undefined, | 
					
						
							|  |  |  |     verb: resources.randomVerb(), | 
					
						
							|  |  |  |     phrase: resources.randomPhrase(), | 
					
						
							|  |  |  |     compliments: resources.randomCompliment(), | 
					
						
							|  |  |  |     bonfires: [], | 
					
						
							|  |  |  |     bonfireHash: 'test' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-01-16 18:58:27 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | exports.returnNextBonfire = function(req, res, next) { | 
					
						
							|  |  |  |   if (!req.user) { | 
					
						
							|  |  |  |     return res.redirect('../bonfires/meet-bonfire'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2015-01-26 11:38:19 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   }); | 
					
						
							|  |  |  |   req.user.save(); | 
					
						
							| 
									
										
										
										
											2015-01-27 17:39:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var uncompletedBonfires = req.user.uncompletedBonfires; | 
					
						
							| 
									
										
										
										
											2015-01-24 14:29:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var displayedBonfires =  Bonfire.find({'_id': uncompletedBonfires[0]}); | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |   displayedBonfires.exec(function(err, bonfireFromMongo) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |     if (err) { | 
					
						
							|  |  |  |       return next(err); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |     var bonfire = bonfireFromMongo.pop(); | 
					
						
							|  |  |  |     if (typeof bonfire === 'undefined') { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       req.flash('errors', { | 
					
						
							|  |  |  |         msg: "It looks like you've completed all the bonfires we have available. Good job!" | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return res.redirect('../bonfires/meet-bonfire'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var nameString = bonfire.name.toLowerCase().replace(/\s/g, '-'); | 
					
						
							|  |  |  |     return res.redirect('../bonfires/' + nameString); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-01-24 14:29:50 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports.returnIndividualBonfire = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var dashedName = req.params.bonfireName; | 
					
						
							| 
									
										
										
										
											2015-01-26 18:28:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var bonfireName = dashedName.replace(/\-/g, ' '); | 
					
						
							| 
									
										
										
										
											2015-02-06 14:39:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |   Bonfire.find({'name': new RegExp(bonfireName, 'i')}, function(err, bonfireFromMongo) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |     if (err) { | 
					
						
							|  |  |  |       next(err); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-06 14:39:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 01:22:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |     if (bonfireFromMongo.length < 1) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       req.flash('errors', { | 
					
						
							|  |  |  |         msg: "404: We couldn't find a bonfire with that name. Please double check the name." | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-06 14:39:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       return res.redirect('/bonfires'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-06 01:51:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |     var bonfire = bonfireFromMongo.pop(); | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |     var dashedNameFull = bonfire.name.toLowerCase().replace(/\s/g, '-'); | 
					
						
							| 
									
										
										
										
											2015-04-24 13:15:15 -04:00
										 |  |  |     if (dashedNameFull !== dashedName) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       return res.redirect('../bonfires/' + dashedNameFull); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     res.render('bonfire/show', { | 
					
						
							| 
									
										
										
										
											2015-03-29 13:05:39 -07:00
										 |  |  |         completedWith: null, | 
					
						
							|  |  |  |         title: bonfire.name, | 
					
						
							|  |  |  |         dashedName: dashedName, | 
					
						
							|  |  |  |         name: bonfire.name, | 
					
						
							|  |  |  |         difficulty: Math.floor(+bonfire.difficulty), | 
					
						
							| 
									
										
										
										
											2015-04-20 00:28:55 -04:00
										 |  |  |         brief: bonfire.description.shift(), | 
					
						
							|  |  |  |         details: bonfire.description, | 
					
						
							| 
									
										
										
										
											2015-03-29 13:05:39 -07:00
										 |  |  |         tests: bonfire.tests, | 
					
						
							|  |  |  |         challengeSeed: bonfire.challengeSeed, | 
					
						
							|  |  |  |         points: req.user ? req.user.points : undefined, | 
					
						
							|  |  |  |         verb: resources.randomVerb(), | 
					
						
							|  |  |  |         phrase: resources.randomPhrase(), | 
					
						
							|  |  |  |         compliment: resources.randomCompliment(), | 
					
						
							|  |  |  |         bonfires: bonfire, | 
					
						
							|  |  |  |         bonfireHash: bonfire._id, | 
					
						
							|  |  |  |         MDNkeys: bonfire.MDNlinks, | 
					
						
							|  |  |  |         MDNlinks: getMDNlinks(bonfire.MDNlinks) | 
					
						
							| 
									
										
										
										
											2015-01-21 21:32:13 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |  * Bonfire Generator | 
					
						
							|  |  |  |  * @param req Request Object | 
					
						
							|  |  |  |  * @param res Response Object | 
					
						
							|  |  |  |  * @returns void | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | exports.returnGenerator = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   res.render('bonfire/generator', { | 
					
						
							|  |  |  |     title: null, | 
					
						
							|  |  |  |     name: null, | 
					
						
							|  |  |  |     difficulty: null, | 
					
						
							|  |  |  |     brief: null, | 
					
						
							|  |  |  |     details: null, | 
					
						
							|  |  |  |     tests: null, | 
					
						
							|  |  |  |     challengeSeed: null, | 
					
						
							|  |  |  |     bonfireHash: randomString() | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Post for bonfire generation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function randomString() { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var chars = '0123456789abcdef'; | 
					
						
							|  |  |  |   var string_length = 23; | 
					
						
							|  |  |  |   var randomstring = 'a'; | 
					
						
							|  |  |  |   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-01-25 23:56:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Helper function to populate the MDN links array. | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getMDNlinks(links) { | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |   // takes in an array of links, which are strings
 | 
					
						
							|  |  |  |   var populatedLinks = []; | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |   // for each key value, push the corresponding link from the MDNlinks object into a new array
 | 
					
						
							|  |  |  |   links.forEach(function(value, index) { | 
					
						
							|  |  |  |     populatedLinks.push(MDNlinks[value]); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |   return populatedLinks; | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:11:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-23 23:20:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports.testBonfire = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var bonfireName = req.body.name, | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |     bonfireTests = req.body.tests, | 
					
						
							|  |  |  |     bonfireDifficulty = req.body.difficulty, | 
					
						
							|  |  |  |     bonfireDescription = req.body.description, | 
					
						
							|  |  |  |     bonfireChallengeSeed = req.body.challengeSeed; | 
					
						
							| 
									
										
										
										
											2015-01-25 23:10:05 -08:00
										 |  |  |     bonfireTests = bonfireTests.split('\r\n'); | 
					
						
							|  |  |  |     bonfireDescription = bonfireDescription.split('\r\n'); | 
					
						
							|  |  |  |     bonfireTests.filter(getRidOfEmpties); | 
					
						
							|  |  |  |     bonfireDescription.filter(getRidOfEmpties); | 
					
						
							|  |  |  |     bonfireChallengeSeed = bonfireChallengeSeed.replace('\r', ''); | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   res.render('bonfire/show', { | 
					
						
							|  |  |  |     completedWith: null, | 
					
						
							|  |  |  |     title: bonfireName, | 
					
						
							|  |  |  |     name: bonfireName, | 
					
						
							|  |  |  |     difficulty: +bonfireDifficulty, | 
					
						
							|  |  |  |     brief: bonfireDescription[0], | 
					
						
							|  |  |  |     details: bonfireDescription.slice(1), | 
					
						
							|  |  |  |     tests: bonfireTests, | 
					
						
							|  |  |  |     challengeSeed: bonfireChallengeSeed, | 
					
						
							|  |  |  |     cc: req.user ? req.user.bonfiresHash : undefined, | 
					
						
							|  |  |  |     progressTimestamps: req.user ? req.user.progressTimestamps : undefined, | 
					
						
							|  |  |  |     verb: resources.randomVerb(), | 
					
						
							|  |  |  |     phrase: resources.randomPhrase(), | 
					
						
							|  |  |  |     compliment: resources.randomCompliment(), | 
					
						
							|  |  |  |     bonfires: [], | 
					
						
							|  |  |  |     bonfireHash: 'test' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getRidOfEmpties(elem) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   if (elem.length > 0) { | 
					
						
							|  |  |  |     return elem; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 23:10:05 -08:00
										 |  |  | exports.publicGenerator = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   res.render('bonfire/public-generator'); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-01-25 23:10:05 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  | exports.generateChallenge = function(req, res) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |   var bonfireName = req.body.name, | 
					
						
							| 
									
										
										
										
											2015-03-29 20:39:41 +09:00
										 |  |  |     bonfireTests = req.body.tests, | 
					
						
							|  |  |  |     bonfireDifficulty = req.body.difficulty, | 
					
						
							|  |  |  |     bonfireDescription = req.body.description, | 
					
						
							|  |  |  |     bonfireChallengeSeed = req.body.challengeSeed; | 
					
						
							| 
									
										
										
										
											2015-01-25 23:56:04 -05:00
										 |  |  |     bonfireTests = bonfireTests.split('\r\n'); | 
					
						
							|  |  |  |     bonfireDescription = bonfireDescription.split('\r\n'); | 
					
						
							|  |  |  |     bonfireTests.filter(getRidOfEmpties); | 
					
						
							|  |  |  |     bonfireDescription.filter(getRidOfEmpties); | 
					
						
							|  |  |  |     bonfireChallengeSeed = bonfireChallengeSeed.replace('\r', ''); | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var response = { | 
					
						
							|  |  |  |     _id: randomString(), | 
					
						
							|  |  |  |     name: bonfireName, | 
					
						
							|  |  |  |     difficulty: bonfireDifficulty, | 
					
						
							|  |  |  |     description: bonfireDescription, | 
					
						
							|  |  |  |     challengeSeed: bonfireChallengeSeed, | 
					
						
							|  |  |  |     tests: bonfireTests | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   res.send(response); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | exports.completedBonfire = function (req, res, next) { | 
					
						
							|  |  |  |   var isCompletedWith = req.body.bonfireInfo.completedWith || ''; | 
					
						
							|  |  |  |   var isCompletedDate = Math.round(+new Date()); | 
					
						
							|  |  |  |   var bonfireHash = req.body.bonfireInfo.bonfireHash; | 
					
						
							|  |  |  |   var isSolution = req.body.bonfireInfo.solution; | 
					
						
							| 
									
										
										
										
											2015-04-05 00:01:51 -07:00
										 |  |  |   var bonfireName = req.body.bonfireInfo.bonfireName; | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (isCompletedWith) { | 
					
						
							|  |  |  |     var paired = User.find({'profile.username': isCompletedWith | 
					
						
							|  |  |  |       .toLowerCase()}).limit(1); | 
					
						
							|  |  |  |     paired.exec(function (err, pairedWith) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         return next(err); | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |         var index = req.user.uncompletedBonfires.indexOf(bonfireHash); | 
					
						
							|  |  |  |         if (index > -1) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |           req.user.progressTimestamps.push(Date.now() || 0); | 
					
						
							|  |  |  |           req.user.uncompletedBonfires.splice(index, 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         pairedWith = pairedWith.pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         index = pairedWith.uncompletedBonfires.indexOf(bonfireHash); | 
					
						
							|  |  |  |         if (index > -1) { | 
					
						
							|  |  |  |           pairedWith.progressTimestamps.push(Date.now() || 0); | 
					
						
							|  |  |  |           pairedWith.uncompletedBonfires.splice(index, 1); | 
					
						
							| 
									
										
										
										
											2015-03-28 12:22:08 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |         pairedWith.completedBonfires.push({ | 
					
						
							|  |  |  |           _id: bonfireHash, | 
					
						
							| 
									
										
										
										
											2015-04-05 00:01:51 -07:00
										 |  |  |           name: bonfireName, | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |           completedWith: req.user._id, | 
					
						
							|  |  |  |           completedDate: isCompletedDate, | 
					
						
							|  |  |  |           solution: isSolution | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         req.user.completedBonfires.push({ | 
					
						
							|  |  |  |           _id: bonfireHash, | 
					
						
							| 
									
										
										
										
											2015-04-05 00:01:51 -07:00
										 |  |  |           name: bonfireName, | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |           completedWith: pairedWith._id, | 
					
						
							|  |  |  |           completedDate: isCompletedDate, | 
					
						
							|  |  |  |           solution: isSolution | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |         req.user.save(function (err, user) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |           if (err) { | 
					
						
							|  |  |  |             return next(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           pairedWith.save(function (err, paired) { | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |             if (err) { | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |               return next(err); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |             if (user && paired) { | 
					
						
							|  |  |  |               res.send(true); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     req.user.completedBonfires.push({ | 
					
						
							|  |  |  |       _id: bonfireHash, | 
					
						
							| 
									
										
										
										
											2015-04-05 00:01:51 -07:00
										 |  |  |       name: bonfireName, | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  |       completedWith: null, | 
					
						
							|  |  |  |       completedDate: isCompletedDate, | 
					
						
							|  |  |  |       solution: isSolution | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var index = req.user.uncompletedBonfires.indexOf(bonfireHash); | 
					
						
							|  |  |  |     if (index > -1) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       req.user.progressTimestamps.push(Date.now() || 0); | 
					
						
							|  |  |  |       req.user.uncompletedBonfires.splice(index, 1); | 
					
						
							| 
									
										
										
										
											2015-02-01 22:39:59 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-28 23:42:07 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     req.user.save(function (err, user) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         return next(err); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (user) { | 
					
						
							|  |  |  |         res.send(true); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; |