mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
begin implementing new preenUserHistory method: port current code to es6 and setup tests
This commit is contained in:
2
common/dist/sprites/habitrpg-shared.css
vendored
2
common/dist/sprites/habitrpg-shared.css
vendored
File diff suppressed because one or more lines are too long
@@ -7,9 +7,10 @@ import {
|
||||
MAX_LEVEL,
|
||||
MAX_STAT_POINTS,
|
||||
} from './constants';
|
||||
import preenUserHistory from './preenUserHistory';
|
||||
import * as statHelpers from './statHelpers';
|
||||
|
||||
var $w, _, api, content, i18n, moment, preenHistory, sortOrder,
|
||||
var $w, _, api, content, i18n, moment, sortOrder,
|
||||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
||||
|
||||
moment = require('moment');
|
||||
@@ -79,48 +80,6 @@ api.planGemLimits = {
|
||||
convCap: 25
|
||||
};
|
||||
|
||||
/*
|
||||
Preen history for users with > 7 history entries
|
||||
This takes an infinite array of single day entries [day day day day day...], and turns it into a condensed array
|
||||
of averages, condensing more the further back in time we go. Eg, 7 entries each for last 7 days; 1 entry each week
|
||||
of this month; 1 entry for each month of this year; 1 entry per previous year: [day*7 week*4 month*12 year*infinite]
|
||||
*/
|
||||
|
||||
preenHistory = function(history) {
|
||||
var newHistory, preen, thisMonth;
|
||||
history = _.filter(history, function(h) {
|
||||
return !!h;
|
||||
});
|
||||
newHistory = [];
|
||||
preen = function(amount, groupBy) {
|
||||
var groups;
|
||||
groups = _.chain(history).groupBy(function(h) {
|
||||
return moment(h.date).format(groupBy);
|
||||
}).sortBy(function(h, k) {
|
||||
return k;
|
||||
}).value();
|
||||
groups = groups.slice(-amount);
|
||||
groups.pop();
|
||||
return _.each(groups, function(group) {
|
||||
newHistory.push({
|
||||
date: moment(group[0].date).toDate(),
|
||||
value: _.reduce(group, (function(m, obj) {
|
||||
return m + obj.value;
|
||||
}), 0) / group.length
|
||||
});
|
||||
return true;
|
||||
});
|
||||
};
|
||||
preen(50, "YYYY");
|
||||
preen(moment().format('MM'), "YYYYMM");
|
||||
thisMonth = moment().format('YYYYMM');
|
||||
newHistory = newHistory.concat(_.filter(history, function(h) {
|
||||
return moment(h.date).format('YYYYMM') === thisMonth;
|
||||
}));
|
||||
return newHistory;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Preen 3-day past-completed To-Dos from Angular & mobile app
|
||||
*/
|
||||
@@ -2571,15 +2530,7 @@ api.wrap = function(user, main) {
|
||||
date: now,
|
||||
value: expTally
|
||||
});
|
||||
if (!((ref1 = user.purchased) != null ? (ref2 = ref1.plan) != null ? ref2.customerId : void 0 : void 0)) {
|
||||
user.fns.preenUserHistory();
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('history');
|
||||
}
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('dailys');
|
||||
}
|
||||
}
|
||||
preenUserHistory(user);
|
||||
user.stats.buffs = perfect ? ((base3 = user.achievements).perfect != null ? base3.perfect : base3.perfect = 0, user.achievements.perfect++, lvlDiv2 = Math.ceil(api.capByLevel(user.stats.lvl) / 2), {
|
||||
str: lvlDiv2,
|
||||
int: lvlDiv2,
|
||||
@@ -2624,28 +2575,6 @@ api.wrap = function(user, main) {
|
||||
}
|
||||
return _progress;
|
||||
},
|
||||
preenUserHistory: function(minHistLen) {
|
||||
if (minHistLen == null) {
|
||||
minHistLen = 7;
|
||||
}
|
||||
_.each(user.habits.concat(user.dailys), function(task) {
|
||||
var ref;
|
||||
if (((ref = task.history) != null ? ref.length : void 0) > minHistLen) {
|
||||
task.history = preenHistory(task.history);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
_.defaults(user.history, {
|
||||
todos: [],
|
||||
exp: []
|
||||
});
|
||||
if (user.history.exp.length > minHistLen) {
|
||||
user.history.exp = preenHistory(user.history.exp);
|
||||
}
|
||||
if (user.history.todos.length > minHistLen) {
|
||||
return user.history.todos = preenHistory(user.history.todos);
|
||||
}
|
||||
},
|
||||
ultimateGear: function() {
|
||||
var base, owned;
|
||||
owned = typeof window !== "undefined" && window !== null ? user.items.gear.owned : user.items.gear.owned.toObject();
|
||||
|
||||
66
common/script/preenUserHistory.js
Normal file
66
common/script/preenUserHistory.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
/*
|
||||
Preen history for users with > 7 history entries
|
||||
This takes an infinite array of single day entries [day day day day day...], and turns it into a condensed array
|
||||
of averages, condensing more the further back in time we go. Eg, 7 entries each for last 7 days; 1 entry each week
|
||||
of this month; 1 entry for each month of this year; 1 entry per previous year: [day*7 week*4 month*12 year*infinite]
|
||||
*/
|
||||
function _preenHistory (history) {
|
||||
history = _.filter(history, h => {
|
||||
return Boolean(h); // filter missing entries
|
||||
});
|
||||
let newHistory = [];
|
||||
|
||||
function _preen (amount, groupBy) {
|
||||
let groups = _.chain(history).groupBy(h => {
|
||||
return moment(h.date).format(groupBy);
|
||||
}).sortBy((h, k) => {
|
||||
return k;
|
||||
}).value();
|
||||
groups = groups.slice(-amount);
|
||||
groups.pop();
|
||||
|
||||
return _.each(groups, group => {
|
||||
newHistory.push({
|
||||
date: moment(group[0].date).toDate(),
|
||||
value: _.reduce(group, (m, obj) => {
|
||||
return m + obj.value;
|
||||
}, 0) / group.length,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_preen(50, 'YYYY');
|
||||
_preen(moment().format('MM'), 'YYYYMM');
|
||||
|
||||
let thisMonth = moment().format('YYYYMM');
|
||||
newHistory = newHistory.concat(_.filter(history, h => {
|
||||
return moment(h.date).format('YYYYMM') === thisMonth;
|
||||
}));
|
||||
|
||||
return newHistory;
|
||||
}
|
||||
|
||||
export default function (user, minHistLen = 7) {
|
||||
_.each(user.habits.concat(user.dailys), (task, index) => {
|
||||
if (task.history && task.history.length > minHistLen) {
|
||||
task.history = _preenHistory(task.history);
|
||||
user.markModified(`user.${task.type}s.${index}.history`);
|
||||
}
|
||||
});
|
||||
|
||||
_.defaults(user.history, {
|
||||
todos: [],
|
||||
exp: [],
|
||||
});
|
||||
|
||||
if (user.history.exp.length > minHistLen) {
|
||||
user.history.exp = _preenHistory(user.history.exp);
|
||||
user.markModified('user.history.exp');
|
||||
}
|
||||
if (user.history.todos.length > minHistLen) {
|
||||
user.history.todos = _preenHistory(user.history.todos);
|
||||
user.markModified('user.history.todos');
|
||||
}
|
||||
}
|
||||
11
test/common/preenUserHistory.test.js
Normal file
11
test/common/preenUserHistory.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import preenUserHistory from '../../common/script/preenUserHistory'; // eslint-disable-line no-unused-vars
|
||||
|
||||
describe('preenUserHistory', () => {
|
||||
describe('preen tasks history', () => {
|
||||
it('works');
|
||||
});
|
||||
|
||||
describe('preen user history', () => {
|
||||
it('works');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user