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) {
|
app.post('/completed-bonfire/', function (req, res) {
|
||||||
req.user.bonfiresHash[parseInt(req.body.bonfireNumber)] =
|
|
||||||
Math.round(+new Date() / 1000);
|
// TODO: remove debug statement
|
||||||
var timestamp = req.user.bonfiresHash;
|
debug(req.body.bonfireInfo, 'This is bonfire info we got from posted');
|
||||||
var points = 0;
|
|
||||||
for (var key in timestamp) {
|
|
||||||
if (timestamp[key] > 0) {
|
|
||||||
points += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
|
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
|
||||||
var isCompletedDate = Math.round(+new Date() / 1000);
|
var isCompletedDate = Math.round(+new Date() / 1000);
|
||||||
var bonfireHash = req.body.bonfireInfo.bonfireHash;
|
var bonfireHash = req.body.bonfireInfo.bonfireHash;
|
||||||
var isSolution = req.body.bonfireInfo.solution;
|
var isSolution = req.body.bonfireInfo.solution;
|
||||||
req.user.bonfiresHash[bonfireHash] = {
|
|
||||||
completedWith: isCompletedWith,
|
|
||||||
completedDate: isCompletedDate,
|
|
||||||
solution: isSolution
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isCompletedWith) {
|
if (isCompletedWith) {
|
||||||
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
|
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
|
||||||
@ -338,16 +327,30 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
return err;
|
return err;
|
||||||
} else {
|
} else {
|
||||||
pairedWith = pairedWith.pop();
|
pairedWith = pairedWith.pop();
|
||||||
pairedWith.bonfiresHash[bonfireHash] = {
|
pairedWith.completedBonfires.push({
|
||||||
|
_id: bonfireHash,
|
||||||
completedWith: req.user._id,
|
completedWith: req.user._id,
|
||||||
completedDate: isCompletedDate,
|
completedDate: isCompletedDate,
|
||||||
solution: isSolution
|
solution: isSolution
|
||||||
};
|
})
|
||||||
req.user.bonfiresHash[bonfireHash] = {
|
|
||||||
|
req.user.completedBonfires.push({
|
||||||
|
_id: bonfireHash,
|
||||||
completedWith: pairedWith._id,
|
completedWith: pairedWith._id,
|
||||||
completedDate: isCompletedDate,
|
completedDate: isCompletedDate,
|
||||||
solution: isSolution
|
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();
|
req.user.save();
|
||||||
pairedWith.save();
|
pairedWith.save();
|
||||||
@ -355,14 +358,20 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
req.user.bonfiresHash[bonfireHash] = {
|
req.user.completedBonfires.push({
|
||||||
|
_id: bonfireHash,
|
||||||
completedWith: null,
|
completedWith: null,
|
||||||
completedDate: isCompletedDate,
|
completedDate: isCompletedDate,
|
||||||
solution: isSolution
|
solution: isSolution
|
||||||
};
|
})
|
||||||
|
|
||||||
|
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
||||||
|
|
||||||
|
if (index > -1) {
|
||||||
|
req.user.uncompletedBonfires.splice(index,1)
|
||||||
|
}
|
||||||
req.user.save();
|
req.user.save();
|
||||||
}
|
}
|
||||||
bonfireController.returnNextBonfire(req, res);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,22 +45,23 @@ exports.returnNextBonfire = function(req, res, next) {
|
|||||||
req.user = new User();
|
req.user = new User();
|
||||||
//return res.redirect('/bonfires/meet-bonfire');
|
//return res.redirect('/bonfires/meet-bonfire');
|
||||||
}
|
}
|
||||||
// This code is in bad need of refactoring
|
var currentTime = parseInt(+new Date() / 1000)
|
||||||
var bonfiresToFind = req.user ? req.user.bonfiresHash : [];
|
if (currentTime - req.user.lastContentSync > 86400) {
|
||||||
var bonfiresArray = _.map(bonfiresToFind, function(value, index) {
|
req.user.lastContentSync = currentTime;
|
||||||
return [index, value];
|
var completed = req.user.completedBonfires;
|
||||||
});
|
// TODO : remove debug statement
|
||||||
// Get rid of the first entry, which will be a useless function that causes an error.
|
debug(req.user, 'this is the user');
|
||||||
bonfiresArray.shift();
|
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function(elem) {
|
||||||
var unsolvedBonfires = [];
|
if (completed.indexOf(elem) === -1) {
|
||||||
for (i = 0; i < bonfiresArray.length; i++) {
|
return elem;
|
||||||
if (bonfiresArray[i][1]["completedDate"] === 0) {
|
}
|
||||||
unsolvedBonfires.push(bonfiresArray[i][0])
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
displayedBonfires.exec(function(err, bonfire) {
|
||||||
if (err) {
|
if (err) {
|
||||||
next(err);
|
next(err);
|
||||||
@ -91,13 +92,8 @@ exports.returnNextBonfire = function(req, res, next) {
|
|||||||
|
|
||||||
exports.returnIndividualBonfire = function(req, res, next) {
|
exports.returnIndividualBonfire = function(req, res, next) {
|
||||||
var bonfireName = req.params.bonfireName;
|
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, ' ');
|
bonfireName = bonfireName.replace(/\-/g, ' ');
|
||||||
debug('Checking sanity of name of bonfire after replacing "-" characters', bonfireName);
|
|
||||||
var bonfireNumber = 0;
|
var bonfireNumber = 0;
|
||||||
|
|
||||||
Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
|
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."
|
msg: "404: We couldn't find a bonfire with that name. Please double check the name."
|
||||||
});
|
});
|
||||||
return res.redirect('/bonfires/meet-bonfire')
|
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() {
|
numberOfBonfires: function() {
|
||||||
return bonfires.length - 1;
|
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,
|
resetPasswordToken: String,
|
||||||
resetPasswordExpires: Date,
|
resetPasswordExpires: Date,
|
||||||
bonfiresHash: {
|
uncompletedBonfires: Array,
|
||||||
ab6137d4e35944e21037b769: {
|
completedBonfires: Array,
|
||||||
hash: String,
|
lastContentSync: {
|
||||||
completedWith: String,
|
type: Number,
|
||||||
completedDate: {
|
default: 0
|
||||||
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
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,8 +74,7 @@ $(document).ready(function() {
|
|||||||
console.log(didCompleteWith);
|
console.log(didCompleteWith);
|
||||||
console.log(bonfireSolution, thisBonfireHash);
|
console.log(bonfireSolution, thisBonfireHash);
|
||||||
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
|
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
|
||||||
l = location.pathname.split('/');
|
window.location = '/bonfires/';
|
||||||
window.location = '/bonfires/' + (parseInt(l[l.length - 1]) + 1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Bonfire instructions functions
|
// Bonfire instructions functions
|
||||||
|
Reference in New Issue
Block a user