back to syncronous calls on the client

This commit is contained in:
Tyler Renelle 2012-09-24 20:09:38 -04:00
parent 893b0bd383
commit ce901f7b93
4 changed files with 42 additions and 45 deletions

View file

@ -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);
});

View file

@ -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);
}
};

View file

@ -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

View file

@ -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 = {