Streaks!
This commit is contained in:
@ -3,7 +3,8 @@ var _ = require('lodash'),
|
||||
Courseware = require('./../models/Courseware'),
|
||||
User = require('./../models/User'),
|
||||
resources = require('./resources'),
|
||||
R = require('ramda');
|
||||
R = require('ramda'),
|
||||
moment = require('moment');
|
||||
|
||||
/**
|
||||
* Courseware controller
|
||||
@ -269,82 +270,86 @@ exports.completedCourseware = function (req, res, next) {
|
||||
};
|
||||
|
||||
exports.completedZiplineOrBasejump = function (req, res, next) {
|
||||
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
|
||||
var isCompletedWith = req.body.bonfireInfo.completedWith || false;
|
||||
var isCompletedDate = Math.round(+new Date());
|
||||
var coursewareHash = req.body.coursewareInfo.coursewareHash;
|
||||
var solutionLink = req.body.coursewareInfo.solutionLink;
|
||||
if (!solutionLink) {
|
||||
// flash error and redirect
|
||||
return next(new Error('No solution provided'));
|
||||
}
|
||||
|
||||
if (isCompletedWith) {
|
||||
var paired = User.find({"profile.username": isCompletedWith.toLowerCase()}).limit(1);
|
||||
var paired = User.find({'profile.username': isCompletedWith.toLowerCase()}).limit(1);
|
||||
paired.exec(function (err, pairedWith) {
|
||||
if (err) {
|
||||
return err;
|
||||
return next(err);
|
||||
} else {
|
||||
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
||||
var index = req.user.uncompletedCoursewares.indexOf(coursewareHash);
|
||||
if (index > -1) {
|
||||
req.user.progressTimestamps.push(Date.now() || 0);
|
||||
req.user.uncompletedBonfires.splice(index, 1)
|
||||
req.user.uncompletedCoursewares.splice(index, 1);
|
||||
}
|
||||
pairedWith = pairedWith.pop();
|
||||
|
||||
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
|
||||
index = pairedWith.uncompletedCoursewares.indexOf(coursewareHash);
|
||||
if (index > -1) {
|
||||
pairedWith.progressTimestamps.push(Date.now() || 0);
|
||||
pairedWith.uncompletedBonfires.splice(index, 1);
|
||||
pairedWith.uncompletedCoursewares.splice(index, 1);
|
||||
|
||||
}
|
||||
|
||||
pairedWith.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
pairedWith.completedCoursewares.push({
|
||||
_id: coursewareHash,
|
||||
completedWith: req.user._id,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
solution: solutionLink
|
||||
});
|
||||
|
||||
req.user.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
req.user.completedCoursewares.push({
|
||||
_id: coursewareHash,
|
||||
completedWith: pairedWith._id,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
solution: solutionLink
|
||||
});
|
||||
|
||||
req.user.save(function (err, user) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
pairedWith.save(function (err, paired) {
|
||||
if (err) {
|
||||
throw err;
|
||||
return next(err);
|
||||
}
|
||||
if (user && paired) {
|
||||
res.send(true);
|
||||
return res.send(true);
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
} else {
|
||||
|
||||
req.user.completedBonfires.push({
|
||||
_id: bonfireHash,
|
||||
req.user.completedCoursewares.push({
|
||||
_id: coursewareHash,
|
||||
completedWith: null,
|
||||
completedDate: isCompletedDate,
|
||||
solution: isSolution
|
||||
solution: solutionLink
|
||||
});
|
||||
|
||||
var index = req.user.uncompletedCourse.indexOf(bonfireHash);
|
||||
var index = req.user.uncompletedCourse.indexOf(coursewareHash);
|
||||
if (index > -1) {
|
||||
req.user.progressTimestamps.push(Date.now() || 0);
|
||||
req.user.uncompletedBonfires.splice(index, 1)
|
||||
req.user.uncompletedCoursewares.splice(index, 1);
|
||||
}
|
||||
|
||||
req.user.save(function (err, user) {
|
||||
if (err) {
|
||||
throw err;
|
||||
return next(err);
|
||||
}
|
||||
if (user) {
|
||||
debug('Saving user');
|
||||
res.send(true)
|
||||
return res.send(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -181,23 +181,33 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
var timeStamps = [];
|
||||
R.keys(req.user.challengesHash).forEach(function(key) {
|
||||
"use strict";
|
||||
var timeStamp = parseInt(challengesHash[key], 10);
|
||||
timeStamps.push({timeStamp: timeStamp.length !== 13 ? (+timeStamp) : (+timeStamp * 1000)});
|
||||
var oldChallengeKeys = R.keys(req.user.challengesHash);
|
||||
|
||||
var updatedTimesFromOldChallenges = oldChallengeKeys.map(function(timeStamp) {
|
||||
if (timeStamp.toString().length !== 13) {
|
||||
timeStamp *= 1000;
|
||||
}
|
||||
return timeStamp;
|
||||
});
|
||||
|
||||
req.user.completedCoursewares = Array.zip(timeStamps, coursewares,
|
||||
var newTimeStamps = R.map(function(timeStamp) {
|
||||
if (timeStamp.toString().length !== 13) {
|
||||
timeStamp *= 1000;
|
||||
}
|
||||
return timeStamp;
|
||||
}, req.user.progressTimestamps);
|
||||
|
||||
req.user.progressTimestamps = newTimeStamps;
|
||||
|
||||
|
||||
req.user.completedCoursewares = Array.zip(updatedTimesFromOldChallenges, coursewares,
|
||||
function(left, right) {
|
||||
"use strict";
|
||||
return ({
|
||||
completedDate: left.timeStamp,
|
||||
_id: right._id,
|
||||
name: right.name
|
||||
});
|
||||
}).filter(function(elem) {
|
||||
"use strict";
|
||||
return elem.completedDate !== 0;
|
||||
});
|
||||
req.user.pointsNeedMigration = false;
|
||||
|
@ -7,7 +7,8 @@ var _ = require('lodash'),
|
||||
secrets = require('../config/secrets'),
|
||||
moment = require('moment'),
|
||||
debug = require('debug')('freecc:cntr:challenges'),
|
||||
resources = require('./resources');
|
||||
resources = require('./resources'),
|
||||
R = require('ramda');
|
||||
|
||||
|
||||
|
||||
@ -199,54 +200,44 @@ exports.postEmailSignup = function(req, res, next) {
|
||||
* For Calendar display
|
||||
*/
|
||||
|
||||
exports.getStreak = function(req, res) {
|
||||
exports.getStreak = function(req, res, next) {
|
||||
|
||||
Array.prototype.timeReduce = function(combiner, initialValue) {
|
||||
var counter,
|
||||
accumulatedValue;
|
||||
req.user.progressTimestamps = req.user.progressTimestamps.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
// If the array is empty, do nothing
|
||||
if (this.length === 0) {
|
||||
return this;
|
||||
} else {
|
||||
// If the user didn't pass an initial value, use the first item.
|
||||
if (arguments.length === 1) {
|
||||
counter = 1;
|
||||
accumulatedValue = this[0];
|
||||
var timeObject = Object.create(null);
|
||||
R.forEach(function(time) {
|
||||
timeObject[moment(time).format('YYYY-MM-DD')] = time;
|
||||
}, req.user.progressTimestamps);
|
||||
|
||||
var tmpLongest = 1;
|
||||
var timeKeys = R.keys(timeObject);
|
||||
for (var i = 1; i <= timeKeys.length; i++) {
|
||||
if (moment(timeKeys[i - 1]).add(1, 'd').toString()
|
||||
=== moment(timeKeys[i]).toString()) {
|
||||
tmpLongest++;
|
||||
if (tmpLongest > req.user.currentStreak) {
|
||||
req.user.currentStreak = tmpLongest;
|
||||
}
|
||||
if ( req.user.currentStreak > req.user.longestStreak) {
|
||||
req.user.longestStreak = req.user.currentStreak;
|
||||
}
|
||||
else if (arguments.length >= 2) {
|
||||
counter = 0;
|
||||
accumulatedValue = initialValue;
|
||||
}
|
||||
else {
|
||||
throw "Invalid arguments.";
|
||||
}
|
||||
|
||||
// Loop through the array, feeding the current value and the result of
|
||||
// the previous computation back into the combiner function until
|
||||
// we've exhausted the entire array and are left with only one function.
|
||||
while (counter < this.length) {
|
||||
accumulatedValue = combiner(accumulatedValue, this[counter]);
|
||||
counter++;
|
||||
}
|
||||
|
||||
return [accumulatedValue];
|
||||
req.user.save(function(err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
});
|
||||
s
|
||||
var payload = {
|
||||
longest: req.user.longestStreak,
|
||||
timeObject: timeObject
|
||||
};
|
||||
|
||||
var timeObject = req.user.progressTimestamps.timeReduce(function(accumulatedTime, timeStamp) {
|
||||
|
||||
var copyOfAccumulatedTime = Object.create(accumulatedTime);
|
||||
|
||||
copyOfAccumulatedTime[moment(timeStamp)
|
||||
.format('MMMM Do YYYY')] = timeStamp;
|
||||
|
||||
return copyOfAccumulatedTime;
|
||||
},
|
||||
{});
|
||||
|
||||
debug('TimeObject is', timeObject);
|
||||
return res.send(timeObject);
|
||||
return res.send(payload);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -326,7 +317,7 @@ exports.returnUser = function(req, res, next) {
|
||||
var data = {};
|
||||
var progressTimestamps = user.progressTimestamps;
|
||||
for (var i = 0; i < progressTimestamps.length; i++) {
|
||||
data[progressTimestamps[i].toString()] = 1;
|
||||
data[(progressTimestamps[i] / 1000).toString()] = 1;
|
||||
}
|
||||
|
||||
res.render('account/show', {
|
||||
|
@ -1,7 +1,9 @@
|
||||
var bcrypt = require('bcrypt-nodejs');
|
||||
var crypto = require('crypto');
|
||||
var mongoose = require('mongoose');
|
||||
require('mongoose-long')(mongoose);
|
||||
|
||||
var Long = mongoose.Types.Long;
|
||||
var userSchema = new mongoose.Schema({
|
||||
email: {
|
||||
type: String,
|
||||
@ -21,7 +23,7 @@ var userSchema = new mongoose.Schema({
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
progressTimestamps: { type: Array, default: [Date] },
|
||||
progressTimestamps: [],
|
||||
challengesCompleted: { type: Array, default: [] },
|
||||
pointsNeedMigration: { type: Boolean, default: true },
|
||||
challengesHash: {
|
||||
@ -332,9 +334,30 @@ var userSchema = new mongoose.Schema({
|
||||
resetPasswordToken: String,
|
||||
resetPasswordExpires: Date,
|
||||
uncompletedBonfires: Array,
|
||||
completedBonfires: Array,
|
||||
completedBonfires: [
|
||||
{
|
||||
_id: String,
|
||||
completedWith: String,
|
||||
completedDate: Long,
|
||||
solution: String
|
||||
}
|
||||
],
|
||||
uncompletedCoursewares: Array,
|
||||
completedCoursewares: Array
|
||||
completedCoursewares: [
|
||||
{
|
||||
completedDate: Long,
|
||||
_id: String,
|
||||
name: String
|
||||
}
|
||||
],
|
||||
currentStreak: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
longestStreak: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user