back to syncronous _.each on cron - slower, but async doesn't seem to save items to database

This commit is contained in:
Tyler Renelle
2012-09-24 17:35:15 -04:00
parent 0f9b70daf2
commit 7a894ceefd
6 changed files with 44 additions and 43 deletions
+4
View File
@@ -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;
+1 -1
View File
@@ -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;
+20 -25
View File
@@ -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
});
}
};