mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-05-20 20:58:51 +00:00
back to syncronous _.each on cron - slower, but async doesn't seem to save items to database
This commit is contained in:
parent
0f9b70daf2
commit
7a894ceefd
6 changed files with 44 additions and 43 deletions
|
|
@ -3,6 +3,10 @@ var moment;
|
|||
|
||||
moment = require('moment');
|
||||
|
||||
module.exports.daysBetween = function(a, b) {
|
||||
return Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'));
|
||||
};
|
||||
|
||||
module.exports.viewHelpers = function(view) {
|
||||
view.fn('taskClasses', function(type, completed, value, repeat) {
|
||||
var classes, dayMapping;
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ module.exports.updateSchema = function(model) {
|
|||
model.del(userPath);
|
||||
return;
|
||||
}
|
||||
daysOld = moment().sod().diff(moment(userObj.lastCron), 'days');
|
||||
daysOld = helpers.daysBetween(new Date(), userObj.lastCron);
|
||||
if (daysOld > 30) {
|
||||
sameTasks = _.filter(require('./content').defaultTasks, function(defaultTask) {
|
||||
var foundSame;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// Generated by CoffeeScript 1.3.3
|
||||
var MODIFIER, async, content, cron, daysBetween, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, updateStats, user, _;
|
||||
var MODIFIER, async, content, cron, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, updateStats, user, _;
|
||||
|
||||
async = require('async');
|
||||
|
||||
|
|
@ -174,7 +174,7 @@ score = function(taskId, direction, options) {
|
|||
}
|
||||
}
|
||||
task.set('value', value);
|
||||
_ref2 = [userObj.stats.money, userObj.stats.hp, userObj.stats.exp, userObj.stats.lvl], money = _ref2[0], hp = _ref2[1], exp = _ref2[2], lvl = _ref2[3];
|
||||
_ref2 = userObj.stats, money = _ref2.money, hp = _ref2.hp, exp = _ref2.exp, lvl = _ref2.lvl;
|
||||
if (type === 'reward') {
|
||||
money -= task.get('value');
|
||||
num = parseFloat(task.get('value')).toFixed(2);
|
||||
|
|
@ -199,16 +199,12 @@ score = function(taskId, direction, options) {
|
|||
return delta;
|
||||
};
|
||||
|
||||
daysBetween = function(a, b) {
|
||||
return Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'));
|
||||
};
|
||||
|
||||
cron = function() {
|
||||
var daysPassed, lastCron, tallyTask, tasks, today, todoTally;
|
||||
var daysPassed, expTally, lastCron, lvl, tallyTask, today, todoTally;
|
||||
today = new Date();
|
||||
user.setNull('lastCron', today);
|
||||
lastCron = user.get('lastCron');
|
||||
daysPassed = daysBetween(today, lastCron);
|
||||
daysPassed = helpers.daysBetween(today, lastCron);
|
||||
if (daysPassed > 0) {
|
||||
user.set('lastCron', today);
|
||||
todoTally = 0;
|
||||
|
|
@ -265,23 +261,22 @@ cron = function() {
|
|||
}
|
||||
return next();
|
||||
};
|
||||
tasks = _.toArray(user.get('tasks'));
|
||||
return async.forEach(tasks, tallyTask, function(err) {
|
||||
var expTally, lvl;
|
||||
user.push('history.todos', {
|
||||
date: today,
|
||||
value: todoTally
|
||||
});
|
||||
expTally = user.get('stats.exp');
|
||||
lvl = 0;
|
||||
while (lvl < (user.get('stats.lvl') - 1)) {
|
||||
lvl++;
|
||||
expTally += (lvl * 100) / 5;
|
||||
}
|
||||
return user.push('history.exp', {
|
||||
date: today,
|
||||
value: expTally
|
||||
});
|
||||
_.each(user.get('tasks'), function(taskObj) {
|
||||
return tallyTask(taskObj, function() {});
|
||||
});
|
||||
user.push('history.todos', {
|
||||
date: today,
|
||||
value: todoTally
|
||||
});
|
||||
expTally = user.get('stats.exp');
|
||||
lvl = 0;
|
||||
while (lvl < (user.get('stats.lvl') - 1)) {
|
||||
lvl++;
|
||||
expTally += (lvl * 100) / 5;
|
||||
}
|
||||
return user.push('history.exp', {
|
||||
date: today,
|
||||
value: expTally
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
moment = require('moment')
|
||||
|
||||
# Absolute diff between two dates, based on 12am for both days
|
||||
module.exports.daysBetween = (a, b) ->
|
||||
Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'))
|
||||
|
||||
module.exports.viewHelpers = (view) ->
|
||||
view.fn 'taskClasses', (type, completed, value, repeat) ->
|
||||
#TODO figure out how to just pass in the task model, so i can access all these properties from one object
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ module.exports.updateSchema = (model) ->
|
|||
return
|
||||
|
||||
# Remove all users who haven't logged in for a month
|
||||
daysOld = moment().sod().diff(moment(userObj.lastCron), 'days')
|
||||
daysOld = helpers.daysBetween(new Date(), userObj.lastCron)
|
||||
if daysOld > 30
|
||||
# and who have mostly the default tasks
|
||||
sameTasks = _.filter require('./content').defaultTasks, (defaultTask) ->
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ score = (taskId, direction, options={cron:false, times:1}) ->
|
|||
task.set('value', value)
|
||||
|
||||
# Update the user's status
|
||||
[money, hp, exp, lvl] = [userObj.stats.money, userObj.stats.hp, userObj.stats.exp, userObj.stats.lvl]
|
||||
{money, hp, exp, lvl} = userObj.stats
|
||||
|
||||
if type == 'reward'
|
||||
# purchase item
|
||||
|
|
@ -178,17 +178,13 @@ score = (taskId, direction, options={cron:false, times:1}) ->
|
|||
|
||||
return delta
|
||||
|
||||
|
||||
daysBetween = (a, b) ->
|
||||
Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'))
|
||||
|
||||
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
|
||||
# For incomplete Dailys, deduct experience
|
||||
cron = ->
|
||||
today = new Date()
|
||||
user.setNull 'lastCron', today
|
||||
lastCron = user.get('lastCron')
|
||||
daysPassed = daysBetween(today, lastCron)
|
||||
daysPassed = helpers.daysBetween(today, lastCron)
|
||||
if daysPassed > 0
|
||||
# reset cron
|
||||
user.set('lastCron', today)
|
||||
|
|
@ -227,17 +223,19 @@ cron = ->
|
|||
next()
|
||||
|
||||
# Tally each task
|
||||
tasks = _.toArray(user.get('tasks'))
|
||||
async.forEach tasks, tallyTask, (err) ->
|
||||
# FIXME calling this as async doesn't seem to save to database, revisit
|
||||
# tasks = _.toArray(user.get('tasks'))
|
||||
# async.forEach tasks, tallyTask, (err) ->
|
||||
# Finished tallying, this is the 'completed' callback
|
||||
user.push 'history.todos', { date: today, value: todoTally }
|
||||
# tally experience
|
||||
expTally = user.get 'stats.exp'
|
||||
lvl = 0 #iterator
|
||||
while lvl < (user.get('stats.lvl')-1)
|
||||
lvl++
|
||||
expTally += (lvl*100)/5
|
||||
user.push 'history.exp', { date: today, value: expTally }
|
||||
_.each user.get('tasks'), (taskObj) -> tallyTask(taskObj, ->) #this will be replaced with above async call when i can get it working
|
||||
user.push 'history.todos', { date: today, value: todoTally }
|
||||
# tally experience
|
||||
expTally = user.get 'stats.exp'
|
||||
lvl = 0 #iterator
|
||||
while lvl < (user.get('stats.lvl')-1)
|
||||
lvl++
|
||||
expTally += (lvl*100)/5
|
||||
user.push 'history.exp', { date: today, value: expTally }
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Reference in a new issue