move scoring functions around

Conflicts:

	lib/app/index.js
	src/app/index.coffee
This commit is contained in:
Tyler Renelle 2012-09-23 22:01:41 -04:00
parent 1c0d8726c3
commit 0ba7ccd370
5 changed files with 70 additions and 72 deletions

View file

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

View file

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

View file

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

View file

@ -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 }
user.push 'history.exp', { date: new Date(), value: expTally }
module.exports = {
MODIFIER: MODIFIER
setUser: setUser
score: score
cron: cron
}

View file

@ -96,14 +96,6 @@
</div>
<div id=wrap class=container-fluid>
{#if _debug}
<div class="alert">
<i class="icon-warning-sign"></i> <b>Debugging Options</b><br/><br/>
<a x-bind=click:poormanscron class="btn">Cron</a>
<a x-bind=click:endOfDayTally class="btn">Tally</a>
</div>
{/}
<div id=main class=row-fluid>
<!-- would rather have one component: <app:taskList>, which handles taskList, task, and newTask. However,
can't pass references as paramters. so <app:taskList newModel={_newHabit} list={_habitList}> doesn't work
@ -305,11 +297,11 @@
<div class="task-controls">
<!-- Habits -->
{#if equal(:task.type, 'habit')}
{#if :task.up}<a data-direction=up x-bind=click:vote><img src="/img/add.png" /></a>{/}
{#if :task.down}<a data-direction=down x-bind=click:vote><img src="/img/remove.png" /></a>{/}
{#if :task.up}<a data-direction=up x-bind=click:score><img src="/img/add.png" /></a>{/}
{#if :task.down}<a data-direction=down x-bind=click:score><img src="/img/remove.png" /></a>{/}
<!-- Rewards -->
{else if equal(:task.type, 'reward')}
<a x-bind=click:vote class="buy-link" data-direction=down>{:task.value}<img src="/img/coin_single_gold.png"/></a>
<a x-bind=click:score class="buy-link" data-direction=down>{:task.value}<img src="/img/coin_single_gold.png"/></a>
<!-- Daily & Todos -->
{else}
<input type=checkbox checked="{:task.completed}"/>