fix current streak,
longest streak still broken
This commit is contained in:
@ -4,24 +4,33 @@ var _ = require('lodash'),
|
|||||||
debug = require('debug')('freecc:cntr:userController');
|
debug = require('debug')('freecc:cntr:userController');
|
||||||
|
|
||||||
|
|
||||||
|
const daysBetween = 1.5;
|
||||||
|
|
||||||
function calcCurrentStreak(cals) {
|
function calcCurrentStreak(cals) {
|
||||||
const revCals = cals.slice().reverse();
|
const revCals = cals.slice().reverse();
|
||||||
let streakBroken = false;
|
let streakBroken = false;
|
||||||
return revCals
|
const lastDayInStreak = revCals
|
||||||
.reduce((current, cal, index) => {
|
.reduce((current, cal, index) => {
|
||||||
// if streak not borken and diff between this cal and the call after it
|
const before = revCals[index === 0 ? 0 : index - 1];
|
||||||
// is equal to zero
|
|
||||||
// moment.diff will return the days between rounded down
|
|
||||||
if (
|
if (
|
||||||
!streakBroken &&
|
!streakBroken &&
|
||||||
moment(revCals[index === 0 ? 0 : index - 1]).diff(cal, 'days') === 0
|
moment(before).diff(cal, 'days', true) < daysBetween
|
||||||
) {
|
) {
|
||||||
return current + 1;
|
return index;
|
||||||
}
|
}
|
||||||
return 1;
|
return current;
|
||||||
}, 1);
|
}, 0);
|
||||||
|
|
||||||
|
const lastTimestamp = revCals[lastDayInStreak];
|
||||||
|
return Math.ceil(moment().diff(lastTimestamp, 'days', true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(berks): calc longest streak
|
||||||
|
/*
|
||||||
|
function longestStreak(cals) {
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
var router = app.loopback.Router();
|
var router = app.loopback.Router();
|
||||||
var User = app.models.User;
|
var User = app.models.User;
|
||||||
@ -129,9 +138,7 @@ module.exports = function(app) {
|
|||||||
objOrNum :
|
objOrNum :
|
||||||
objOrNum.timestamp;
|
objOrNum.timestamp;
|
||||||
})
|
})
|
||||||
.map(time => {
|
.sort();
|
||||||
return moment(time).format('YYYY-MM-DD');
|
|
||||||
});
|
|
||||||
|
|
||||||
user.currentStreak = calcCurrentStreak(cals);
|
user.currentStreak = calcCurrentStreak(cals);
|
||||||
|
|
||||||
@ -146,6 +153,9 @@ module.exports = function(app) {
|
|||||||
objOrNum :
|
objOrNum :
|
||||||
objOrNum.timestamp;
|
objOrNum.timestamp;
|
||||||
})
|
})
|
||||||
|
.filter((timestamp) => {
|
||||||
|
return !!timestamp;
|
||||||
|
})
|
||||||
.reduce((data, timeStamp) => {
|
.reduce((data, timeStamp) => {
|
||||||
data[(timeStamp / 1000)] = 1;
|
data[(timeStamp / 1000)] = 1;
|
||||||
return data;
|
return data;
|
||||||
|
Reference in New Issue
Block a user