From 893b0bd383bf9d879d17a915872c8378771f2915 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 24 Sep 2012 19:40:18 -0400 Subject: [PATCH] try running cron on the server instead --- lib/app/index.js | 3 ++- lib/app/scoring.js | 43 +++++++++++++++++++++--------------------- src/app/index.coffee | 8 ++++++-- src/app/scoring.coffee | 33 +++++++++++++++----------------- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 9d8147ee7c..52379328df 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -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); }); diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 86a01e8b08..b1373b967f 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -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("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("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(' 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); }); } }; diff --git a/src/app/index.coffee b/src/app/index.coffee index cf67183b08..678f37d20f 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -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 - diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 9dcead6ccb..5faee912b5 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -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 "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 "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(' 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 = {