massive redesign of user model and the way it interacts with bonfires, successfully returning next bonfire for the user, showing meet bonfire to unauthenticated users
This commit is contained in:
51
app.js
51
app.js
@ -311,25 +311,14 @@ app.post('/completed-challenge', function (req, res) {
|
||||
});
|
||||
|
||||
app.post('/completed-bonfire/', function (req, res) {
|
||||
req.user.bonfiresHash[parseInt(req.body.bonfireNumber)] =
|
||||
Math.round(+new Date() / 1000);
|
||||
var timestamp = req.user.bonfiresHash;
|
||||
var points = 0;
|
||||
for (var key in timestamp) {
|
||||
if (timestamp[key] > 0) {
|
||||
points += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: remove debug statement
|
||||
debug(req.body.bonfireInfo, 'This is bonfire info we got from posted');
|
||||
|
||||
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;
|
||||
req.user.bonfiresHash[bonfireHash] = {
|
||||
completedWith: isCompletedWith,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
};
|
||||
|
||||
if (isCompletedWith) {
|
||||
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
|
||||
@ -338,16 +327,30 @@ app.post('/completed-bonfire/', function (req, res) {
|
||||
return err;
|
||||
} else {
|
||||
pairedWith = pairedWith.pop();
|
||||
pairedWith.bonfiresHash[bonfireHash] = {
|
||||
pairedWith.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
completedWith: req.user._id,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
};
|
||||
req.user.bonfiresHash[bonfireHash] = {
|
||||
})
|
||||
|
||||
req.user.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
completedWith: pairedWith._id,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
};
|
||||
})
|
||||
|
||||
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
||||
|
||||
if (index > -1) {
|
||||
req.user.uncompletedBonfires.splice(index,1)
|
||||
}
|
||||
|
||||
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
|
||||
if (index > -1) {
|
||||
req.user.uncompletedBonfires.splice(index,1)
|
||||
}
|
||||
|
||||
req.user.save();
|
||||
pairedWith.save();
|
||||
@ -355,14 +358,20 @@ app.post('/completed-bonfire/', function (req, res) {
|
||||
}
|
||||
})
|
||||
} else {
|
||||
req.user.bonfiresHash[bonfireHash] = {
|
||||
req.user.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
completedWith: null,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
};
|
||||
})
|
||||
|
||||
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
||||
|
||||
if (index > -1) {
|
||||
req.user.uncompletedBonfires.splice(index,1)
|
||||
}
|
||||
req.user.save();
|
||||
}
|
||||
bonfireController.returnNextBonfire(req, res);
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -45,22 +45,23 @@ exports.returnNextBonfire = function(req, res, next) {
|
||||
req.user = new User();
|
||||
//return res.redirect('/bonfires/meet-bonfire');
|
||||
}
|
||||
// This code is in bad need of refactoring
|
||||
var bonfiresToFind = req.user ? 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();
|
||||
var unsolvedBonfires = [];
|
||||
for (i = 0; i < bonfiresArray.length; i++) {
|
||||
if (bonfiresArray[i][1]["completedDate"] === 0) {
|
||||
unsolvedBonfires.push(bonfiresArray[i][0])
|
||||
}
|
||||
var currentTime = parseInt(+new Date() / 1000)
|
||||
if (currentTime - req.user.lastContentSync > 86400) {
|
||||
req.user.lastContentSync = currentTime;
|
||||
var completed = req.user.completedBonfires;
|
||||
// TODO : remove debug statement
|
||||
debug(req.user, 'this is the user');
|
||||
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function(elem) {
|
||||
if (completed.indexOf(elem) === -1) {
|
||||
return elem;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var uncompletedBonfires = req.user.uncompletedBonfires;
|
||||
|
||||
var displayedBonfires = Bonfire.find({}).where('_id').in(unsolvedBonfires).sort({ difficulty: 1 });
|
||||
|
||||
var displayedBonfires = Bonfire.find({'_id': uncompletedBonfires[0]});
|
||||
displayedBonfires.exec(function(err, bonfire) {
|
||||
if (err) {
|
||||
next(err);
|
||||
@ -91,13 +92,8 @@ exports.returnNextBonfire = function(req, res, next) {
|
||||
|
||||
exports.returnIndividualBonfire = function(req, res, next) {
|
||||
var bonfireName = req.params.bonfireName;
|
||||
debug('Getting this as argument for bonfireName', bonfireName);
|
||||
//if (!/[a-z\-]+/i.test(bonfireName)) {
|
||||
// bonfireName = 'meet bonfire';
|
||||
//}
|
||||
|
||||
bonfireName = bonfireName.replace(/\-/g, ' ');
|
||||
debug('Checking sanity of name of bonfire after replacing "-" characters', bonfireName);
|
||||
var bonfireNumber = 0;
|
||||
|
||||
Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
|
||||
@ -110,25 +106,27 @@ exports.returnIndividualBonfire = function(req, res, next) {
|
||||
msg: "404: We couldn't find a bonfire with that name. Please double check the name."
|
||||
});
|
||||
return res.redirect('/bonfires/meet-bonfire')
|
||||
} else {
|
||||
res.render('bonfire/show', {
|
||||
completedWith: null,
|
||||
title: bonfire[bonfireNumber].name,
|
||||
name: bonfire[bonfireNumber].name,
|
||||
difficulty: +bonfire[bonfireNumber].difficulty,
|
||||
brief: bonfire[bonfireNumber].description[0],
|
||||
details: bonfire[bonfireNumber].description.slice(1),
|
||||
tests: bonfire[bonfireNumber].tests,
|
||||
challengeSeed: bonfire[bonfireNumber].challengeSeed,
|
||||
challengeEntryPoint: bonfire[bonfireNumber].challengeEntryPoint,
|
||||
cc: !!req.user,
|
||||
points: req.user ? req.user.points : undefined,
|
||||
verb: resources.randomVerb(),
|
||||
phrase: resources.randomPhrase(),
|
||||
compliment: resources.randomCompliment(),
|
||||
bonfires: bonfire,
|
||||
bonfireHash: bonfire[bonfireNumber]._id
|
||||
|
||||
});
|
||||
}
|
||||
res.render('bonfire/show', {
|
||||
completedWith: null,
|
||||
title: bonfire[bonfireNumber].name,
|
||||
name: bonfire[bonfireNumber].name,
|
||||
difficulty: +bonfire[bonfireNumber].difficulty,
|
||||
brief: bonfire[bonfireNumber].description[0],
|
||||
details: bonfire[bonfireNumber].description.slice(1),
|
||||
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,
|
||||
verb: resources.randomVerb(),
|
||||
phrase: resources.randomPhrase(),
|
||||
compliment: resources.randomCompliment(),
|
||||
bonfires: bonfire,
|
||||
bonfireHash: bonfire[bonfireNumber]._id
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -187,6 +187,12 @@ module.exports = {
|
||||
|
||||
numberOfBonfires: function() {
|
||||
return bonfires.length - 1;
|
||||
},
|
||||
|
||||
allBonfireIds: function() {
|
||||
return bonfires.map(function(elem) {
|
||||
return elem._id;
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -353,81 +353,12 @@ var userSchema = new mongoose.Schema({
|
||||
},
|
||||
resetPasswordToken: String,
|
||||
resetPasswordExpires: Date,
|
||||
bonfiresHash: {
|
||||
ab6137d4e35944e21037b769: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
a3566b1109230028080c9345: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
a26cbbe9ad8655a977e1ceb5: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
a302f7aae1aa3152a5b413bc: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
a202eed8fc186c8434cb6d61: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
aff0395860f5d3034dc0bfc9: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
aaa48de84e1ecc7c742e1124: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
},
|
||||
ad7123c8c441eddfaeb5bdef: {
|
||||
hash: String,
|
||||
completedWith: String,
|
||||
completedDate: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
solution: String
|
||||
}
|
||||
},
|
||||
bonfires: Array
|
||||
uncompletedBonfires: Array,
|
||||
completedBonfires: Array,
|
||||
lastContentSync: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -74,8 +74,7 @@ $(document).ready(function() {
|
||||
console.log(didCompleteWith);
|
||||
console.log(bonfireSolution, thisBonfireHash);
|
||||
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
|
||||
l = location.pathname.split('/');
|
||||
window.location = '/bonfires/' + (parseInt(l[l.length - 1]) + 1);
|
||||
window.location = '/bonfires/';
|
||||
});
|
||||
|
||||
// Bonfire instructions functions
|
||||
|
Reference in New Issue
Block a user