From 0ba7ccd370ebacd183b233219877cc91c6aae466 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sun, 23 Sep 2012 22:01:41 -0400 Subject: [PATCH] move scoring functions around Conflicts: lib/app/index.js src/app/index.coffee --- lib/app/index.js | 28 +++++----------------------- lib/app/scoring.js | 37 +++++++++++++++++++++++++++++++------ src/app/index.coffee | 28 ++++------------------------ src/app/scoring.coffee | 35 +++++++++++++++++++++++++++-------- views/app/index.html | 14 +++----------- 5 files changed, 70 insertions(+), 72 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 1d2cfdd888..1b492e34c1 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -52,7 +52,7 @@ get('/:uidParam?', function(page, model, _arg, next) { }); ready(function(model) { - var poormanscron, setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2; + var setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2; scoring.setUser(model.at('_user')); $('[rel=tooltip]').tooltip(); $('[rel=popover]').popover(); @@ -280,8 +280,8 @@ ready(function(model) { return user.set('stats.hp', hp); } }; - exports.vote = function(e, el, next) { - var direction, task, user; + exports.score = function(e, el, next) { + var direction, task; direction = $(el).attr('data-direction'); if (direction === 'true/') { direction = 'up'; @@ -289,7 +289,6 @@ ready(function(model) { if (direction === 'false/') { direction = 'down'; } - user = model.at('_user'); task = model.at($(el).parents('li')[0]); return scoring.score({ task: task, @@ -309,24 +308,7 @@ ready(function(model) { model.set('_items.weapon', content.items.weapon[1]); return model.set('_user.balance', model.get('_user.balance') - 0.50); }; - exports.poormanscron = poormanscron = function() { - var daysPassed, lastCron, today; - today = new Date(); - model.setNull('_user.lastCron', today); - lastCron = model.get('_user.lastCron'); - daysPassed = helpers.daysBetween(lastCron, today); - if (daysPassed > 0) { - model.set('_user.lastCron', today); - return _(daysPassed).times(function(n) { - var tallyFor; - tallyFor = moment(lastCron).add('d', n); - return scoring.tally(tallyFor); - }); - } - }; - setTimeout(poormanscron, 2000); - setInterval(poormanscron, 3600000); - return exports.endOfDayTally = function(e, el) { - return scoring.tally(); + setTimeout(scoring.cron, 2000); + setInterval(scoring.cron, 3600000); }; }); diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 6c4cbb2512..f93df906d2 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -1,16 +1,18 @@ // Generated by CoffeeScript 1.3.3 -var MODIFIER, content, expModifier, hpModifier, score, statsNotification, updateStats, user; +var MODIFIER, content, cron, expModifier, helpers, hpModifier, score, setUser, statsNotification, tally, updateStats, user; content = require('./content'); +helpers = require('./helpers'); + +MODIFIER = .03; + user = void 0; -module.exports.setUser = function(u) { +setUser = function(u) { return user = u; }; -module.exports.MODIFIER = MODIFIER = .03; - statsNotification = function(html, type) { if (user.get('stats.lvl') === 0) { return; @@ -85,7 +87,7 @@ updateStats = function(stats) { } }; -module.exports.score = score = function(spec) { +score = function(spec) { var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, num, sign, task, type, value, _ref, _ref1, _ref2; if (spec == null) { spec = { @@ -165,7 +167,23 @@ module.exports.score = score = function(spec) { return delta; }; -module.exports.tally = function(momentDate) { +cron = function() { + var daysPassed, lastCron, today; + today = new Date(); + user.setNull('lastCron', today); + lastCron = user.get('lastCron'); + daysPassed = helpers.daysBetween(lastCron, today); + if (daysPassed > 0) { + user.set('lastCron', today); + return _(daysPassed).times(function(n) { + var tallyFor; + tallyFor = moment(lastCron).add('d', n); + return tally(tallyFor); + }); + } +}; + +tally = function(momentDate) { var expTally, lvl, todoTally; todoTally = 0; _.each(user.get('tasks'), function(taskObj, taskId, list) { @@ -227,3 +245,10 @@ module.exports.tally = function(momentDate) { value: expTally }); }; + +module.exports = { + MODIFIER: MODIFIER, + setUser: setUser, + score: score, + cron: cron +}; diff --git a/src/app/index.coffee b/src/app/index.coffee index 2faf09baca..032cb20a8b 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -231,11 +231,10 @@ ready (model) -> hp = 50 if hp > 50 user.set 'stats.hp', hp - exports.vote = (e, el, next) -> + exports.score = (e, el, next) -> direction = $(el).attr('data-direction') direction = 'up' if direction == 'true/' direction = 'down' if direction == 'false/' - user = model.at('_user') task = model.at $(el).parents('li')[0] scoring.score({task:task, direction:direction}) @@ -250,27 +249,8 @@ ready (model) -> # ========== CRON ========== - exports.poormanscron = poormanscron = () -> - today = new Date() - model.setNull('_user.lastCron', today) - lastCron = model.get('_user.lastCron') - daysPassed = helpers.daysBetween(lastCron, today) - if daysPassed > 0 - model.set('_user.lastCron', today) # reset cron - _(daysPassed).times (n) -> - tallyFor = moment(lastCron).add('d',n) - scoring.tally(tallyFor) - # FIXME seems can't call poormanscron() instantly, have to call after some time (2s here) + # 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 poormanscron, 2000 # Run once on refresh - setInterval poormanscron, 3600000 # Then run once every hour + setTimeout scoring.cron, 2000 # Run once on refresh + setInterval scoring.cron, 3600000 # Then run once every hour - # ========== DEBUGGING ========== - - exports.endOfDayTally = (e, el) -> - scoring.tally() - - # Temporary solution to running updates against the schema when the code changes - # exports.updateSchema = (e, el) -> - # schema.updateSchema(model) - \ No newline at end of file diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index e85eb805ce..13b5aac535 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -1,13 +1,13 @@ content = require('./content') +helpers = require('./helpers') +MODIFIER = .03 # each new level, armor, weapon add 3% modifier (this number may change) +user = undefined # This is required by all the functions, make sure it's set before anythign else is called -user = undefined -module.exports.setUser = (u) -> +setUser = (u) -> user = u - -# each new level, armor, weapon add 3% modifier (this number may change) -module.exports.MODIFIER = MODIFIER = .03 +# FIXME move to index.coffee as module.on('set','*') statsNotification = (html, type) -> #don't show notifications if user dead return if user.get('stats.lvl') == 0 @@ -77,7 +77,7 @@ updateStats = (stats) -> money = 0.0 if (!money? or money<0) user.set 'stats.money', stats.money -module.exports.score = score = (spec = {task:null, direction:null, cron:null}) -> +score = (spec = {task:null, direction:null, cron:null}) -> [task, direction, cron] = [spec.task, spec.direction, spec.cron] # up / down was called by itself, probably as REST from 3rd party service @@ -151,9 +151,20 @@ module.exports.score = score = (spec = {task:null, direction:null, cron:null}) - return delta +cron = -> + today = new Date() + user.setNull('lastCron', today) + lastCron = user.get('lastCron') + daysPassed = helpers.daysBetween(lastCron, today) + if daysPassed > 0 + user.set('lastCron', today) # reset cron + _(daysPassed).times (n) -> + tallyFor = moment(lastCron).add('d',n) + tally(tallyFor) + # At end of day, add value to all incomplete Daily & Todo tasks (further incentive) # For incomplete Dailys, deduct experience -module.exports.tally = (momentDate) -> +tally = (momentDate) -> todoTally = 0 _.each user.get('tasks'), (taskObj, taskId, list) -> #FIXME is it hiccuping here? taskId == "$_65255f4e-3728-4d50-bade-3b05633639af_2", & taskObj.id = undefined @@ -182,4 +193,12 @@ module.exports.tally = (momentDate) -> while lvl < (user.get('stats.lvl')-1) lvl++ expTally += (lvl*100)/5 - user.push 'history.exp', { date: new Date(), value: expTally } \ No newline at end of file + user.push 'history.exp', { date: new Date(), value: expTally } + + +module.exports = { + MODIFIER: MODIFIER + setUser: setUser + score: score + cron: cron +} \ No newline at end of file diff --git a/views/app/index.html b/views/app/index.html index 93f4d69b08..f006fc466a 100644 --- a/views/app/index.html +++ b/views/app/index.html @@ -96,14 +96,6 @@
- {#if _debug} -
- Debugging Options

- Cron - Tally -
- {/} -
{#if equal(:task.type, 'habit')} - {#if :task.up}{/} - {#if :task.down}{/} + {#if :task.up}{/} + {#if :task.down}{/} {else if equal(:task.type, 'reward')} - {:task.value} + {:task.value} {else}