try running cron on the server instead

This commit is contained in:
Tyler Renelle 2012-09-24 19:40:18 -04:00
parent d5b233106c
commit 893b0bd383
4 changed files with 45 additions and 42 deletions

View file

@ -49,6 +49,8 @@ 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();
});
});
@ -302,6 +304,5 @@ 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

@ -42,7 +42,7 @@ setupNotifications = function() {
stackup_spacing: 10
});
};
user.on('set', 'stats.hp', function(captures, args, out, isLocal, passed) {
user.on('set', 'stats.hp', function(captures, args) {
var num, rounded;
num = captures - args;
rounded = Math.abs(num.toFixed(1));
@ -50,7 +50,7 @@ setupNotifications = function() {
return statsNotification("<i class='icon-heart'></i>HP -" + rounded, 'error');
}
});
user.on('set', 'stats.money', function(captures, args, out, isLocal, passed) {
user.on('set', 'stats.money', function(captures, args) {
var num, rounded;
num = captures - args;
rounded = Math.abs(num.toFixed(1));
@ -61,7 +61,7 @@ setupNotifications = function() {
return statsNotification("<i class='icon-star'></i>Exp,GP +" + rounded, 'success');
}
});
return user.on('set', 'stats.lvl', function(captures, args, out, isLocal, passed) {
return user.on('set', 'stats.lvl', function(captures, args) {
if (captures > args) {
return statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
}
@ -200,13 +200,12 @@ score = function(taskId, direction, options) {
};
cron = function() {
var daysPassed, expTally, lastCron, lvl, tallyTask, today, todoTally;
var daysPassed, lastCron, tallyTask, tasks, today, todoTally;
today = new Date();
user.setNull('lastCron', today);
lastCron = user.get('lastCron');
daysPassed = helpers.daysBetween(today, lastCron);
if (daysPassed > 0) {
user.set('lastCron', today);
todoTally = 0;
tallyTask = function(taskObj, next) {
var absVal, completed, dayMapping, daysFailed, id, repeat, task, type, value;
@ -261,22 +260,24 @@ cron = function() {
}
return next();
};
_.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
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);
});
}
};

View file

@ -48,6 +48,11 @@ get '/:uidParam?', (page, model, {uidParam}, next) ->
# see https://github.com/lefnire/habitrpg/issues/4
# 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()
@ -256,6 +261,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

@ -30,13 +30,13 @@ setupNotifications = ->
}
# Setup listeners which trigger notifications
user.on 'set', 'stats.hp', (captures, args, out, isLocal, passed) ->
user.on 'set', 'stats.hp', (captures, args) ->
num = captures - args
rounded = Math.abs(num.toFixed(1))
if num < 0
statsNotification "<i class='icon-heart'></i>HP -#{rounded}", 'error' # lost hp from purchase
user.on 'set', 'stats.money', (captures, args, out, isLocal, passed) ->
user.on 'set', 'stats.money', (captures, args) ->
num = captures - args
rounded = Math.abs(num.toFixed(1))
# made purchase
@ -48,7 +48,7 @@ setupNotifications = ->
num = Math.abs(num)
statsNotification "<i class='icon-star'></i>Exp,GP +#{rounded}", 'success'
user.on 'set', 'stats.lvl', (captures, args, out, isLocal, passed) ->
user.on 'set', 'stats.lvl', (captures, args) ->
if captures > args
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info')
@ -186,9 +186,6 @@ cron = ->
lastCron = user.get('lastCron')
daysPassed = helpers.daysBetween(today, lastCron)
if daysPassed > 0
# reset cron
user.set('lastCron', today)
# Tally function, which is called asyncronously below - but function is defined here.
# We need access to some closure variables above
todoTally = 0
@ -223,19 +220,19 @@ cron = ->
next()
# Tally each task
# FIXME calling this as async doesn't seem to save to database, revisit
# tasks = _.toArray(user.get('tasks'))
# async.forEach tasks, tallyTask, (err) ->
#Syncronous version: _.each user.get('tasks'), (taskObj) -> tallyTask(taskObj, ->)
tasks = _.toArray(user.get('tasks'))
async.forEach tasks, tallyTask, (err) ->
# Finished tallying, this is the 'completed' callback
_.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 }
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 = {