From ce901f7b93f520cd76898049232d28fa89958503 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 24 Sep 2012 20:09:38 -0400 Subject: [PATCH] back to syncronous calls on the client --- lib/app/index.js | 3 +-- lib/app/scoring.js | 43 +++++++++++++++++++++--------------------- src/app/index.coffee | 7 +------ src/app/scoring.coffee | 34 ++++++++++++++++++--------------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 52379328df..9d8147ee7c 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -49,8 +49,6 @@ get('/:uidParam?', function(page, model, _arg, next) { model.fn('_user._tnl', '_user.stats.lvl', function(lvl) { return (lvl * 100) / 5; }); - scoring.setModel(model); - scoring.cron(); return page.render(); }); }); @@ -304,5 +302,6 @@ ready(function(model) { model.set('_items.weapon', content.items.weapon[1]); return model.set('_user.balance', model.get('_user.balance') - 0.50); }; + setTimeout(scoring.cron, 2000); return setInterval(scoring.cron, 3600000); }); diff --git a/lib/app/scoring.js b/lib/app/scoring.js index b1373b967f..7c28b0edf5 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -200,18 +200,18 @@ score = function(taskId, direction, options) { }; 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 = helpers.daysBetween(today, lastCron); if (daysPassed > 0) { todoTally = 0; - tallyTask = function(taskObj, next) { + tallyTask = function(taskObj, callback) { var absVal, completed, dayMapping, daysFailed, id, repeat, task, type, value; id = taskObj.id, type = taskObj.type, completed = taskObj.completed, repeat = taskObj.repeat; if (id == null) { - return; + return callback('a task had a null id during cron, this should not be happening'); } task = user.at("tasks." + id); if (type === 'todo' || type === 'daily') { @@ -258,27 +258,26 @@ cron = function() { }).set('completed', false); } } - return next(); + return callback(); }; - 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; - } - user.push('history.exp', { - date: today, - value: expTally - }); - return user.set('lastCron', today); + _.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; + } + user.push('history.exp', { + date: today, + value: expTally + }); + return user.set('lastCron', today); } }; diff --git a/src/app/index.coffee b/src/app/index.coffee index 678f37d20f..24cb5c9659 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -49,11 +49,6 @@ get '/:uidParam?', (page, model, {uidParam}, next) -> # also update in scoring.coffee. TODO create a function accessible in both locations (lvl*100)/5 - # TODO, running on the server for now. It looks better when run on the client, - # but sometimes it doesn't work - scoring.setModel(model) - scoring.cron() - # Render Page page.render() @@ -261,5 +256,5 @@ ready (model) -> # FIXME seems can't call scoring.cron() instantly, have to call after some time (2s here) # Doesn't do anything otherwise. Don't know why... model not initialized enough yet? - # setTimeout scoring.cron, 2000 # Run once on refresh + setTimeout scoring.cron, 2000 # Run once on refresh setInterval scoring.cron, 3600000 # Then run once every hour diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 5faee912b5..dfc43e01f1 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -189,9 +189,12 @@ cron = -> # Tally function, which is called asyncronously below - but function is defined here. # We need access to some closure variables above todoTally = 0 - tallyTask = (taskObj, next) -> + tallyTask = (taskObj, callback) -> + # setTimeout {THIS_FUNCTION}, 1 # strange hack that seems necessary when using async {id, type, completed, repeat} = taskObj - return unless id? #this shouldn't be happening, some tasks seem to be corrupted + #don't know why this happens, but it does. need to investigate + unless id? + return callback('a task had a null id during cron, this should not be happening') task = user.at("tasks.#{id}") if type in ['todo', 'daily'] # Deduct experience for missed Daily tasks, @@ -217,22 +220,23 @@ cron = -> absVal = if (completed) then Math.abs(value) else value todoTally += absVal task.pass({cron:true}).set('completed', false) if type == 'daily' - next() + callback() # Tally each task - #Syncronous version: _.each user.get('tasks'), (taskObj) -> tallyTask(taskObj, ->) - tasks = _.toArray(user.get('tasks')) - async.forEach tasks, tallyTask, (err) -> + _.each user.get('tasks'), (taskObj) -> tallyTask(taskObj, ->) + # Asyncronous version: + # 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 } - user.set('lastCron', today) # reset cron + 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 } + user.set('lastCron', today) # reset cron module.exports = {