From 7a894ceefd1bee3774f0ba8c3d98beab7e6aacbc Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 24 Sep 2012 17:35:15 -0400 Subject: [PATCH] back to syncronous _.each on cron - slower, but async doesn't seem to save items to database --- lib/app/helpers.js | 4 ++++ lib/app/schema.js | 2 +- lib/app/scoring.js | 45 +++++++++++++++++++----------------------- src/app/helpers.coffee | 4 ++++ src/app/schema.coffee | 2 +- src/app/scoring.coffee | 30 +++++++++++++--------------- 6 files changed, 44 insertions(+), 43 deletions(-) diff --git a/lib/app/helpers.js b/lib/app/helpers.js index 2a908b3c0e..ddfe9b95ac 100644 --- a/lib/app/helpers.js +++ b/lib/app/helpers.js @@ -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; diff --git a/lib/app/schema.js b/lib/app/schema.js index f84ab5fbf9..9f40e24859 100644 --- a/lib/app/schema.js +++ b/lib/app/schema.js @@ -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; diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 19b26ba643..3d76d00876 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -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 }); } }; diff --git a/src/app/helpers.coffee b/src/app/helpers.coffee index 8a8557ff13..4fe6dd38be 100644 --- a/src/app/helpers.coffee +++ b/src/app/helpers.coffee @@ -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 diff --git a/src/app/schema.coffee b/src/app/schema.coffee index a7659ae202..858b9a9e5e 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -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) -> diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 065ca9babe..b547454670 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -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 = {