fix moment() bugs (word from the wise, moment() functions modify the

original object)
This commit is contained in:
Tyler Renelle 2012-09-24 17:26:25 -04:00
parent 33f8c43e93
commit 0f9b70daf2
3 changed files with 36 additions and 26 deletions

View file

@ -1,5 +1,5 @@
// Generated by CoffeeScript 1.3.3
var MODIFIER, async, content, cron, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, updateStats, user, _;
var MODIFIER, async, content, cron, daysBetween, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, updateStats, user, _;
async = require('async');
@ -199,13 +199,18 @@ score = function(taskId, direction, options) {
return delta;
};
daysBetween = function(a, b) {
return Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'));
};
cron = function() {
var daysPassed, lastCron, tallyTask, tasks, today, todoTally;
today = moment().sod();
user.setNull('lastCron', today.toDate());
lastCron = moment(user.get('lastCron'));
daysPassed = today.diff(lastCron, 'days');
today = new Date();
user.setNull('lastCron', today);
lastCron = user.get('lastCron');
daysPassed = 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;
@ -231,7 +236,7 @@ cron = function() {
daysFailed = 0;
_.times(daysPassed, function(n) {
var thatDay;
thatDay = today.subtract('days', n + 1);
thatDay = moment().subtract('days', n + 1);
if (repeat[dayMapping[thatDay.day()]] === true) {
return daysFailed++;
}
@ -245,7 +250,7 @@ cron = function() {
value = task.get('value');
if (type === 'daily') {
task.push("history", {
date: today.toDate(),
date: today,
value: value
});
} else {
@ -264,7 +269,7 @@ cron = function() {
return async.forEach(tasks, tallyTask, function(err) {
var expTally, lvl;
user.push('history.todos', {
date: today.toDate(),
date: today,
value: todoTally
});
expTally = user.get('stats.exp');
@ -273,11 +278,10 @@ cron = function() {
lvl++;
expTally += (lvl * 100) / 5;
}
user.push('history.exp', {
date: today.toDate(),
return user.push('history.exp', {
date: today,
value: expTally
});
return user.set('lastCron', today.toDate());
});
}
};

View file

@ -179,14 +179,20 @@ score = (taskId, direction, options={cron:false, times:1}) ->
return delta
daysBetween = (a, b) ->
Math.abs(moment(a).sod().diff(moment(b).sod(), 'days'))
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
# For incomplete Dailys, deduct experience
cron = ->
today = moment().sod() # start of day
user.setNull 'lastCron', today.toDate()
lastCron = moment(user.get('lastCron'))
daysPassed = today.diff(lastCron, 'days')
today = new Date()
user.setNull 'lastCron', today
lastCron = user.get('lastCron')
daysPassed = 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
@ -206,14 +212,14 @@ cron = ->
dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'}
daysFailed = 0
_.times daysPassed, (n) ->
thatDay = today.subtract('days', n+1)
thatDay = moment().subtract('days', n+1)
if repeat[dayMapping[thatDay.day()]]==true
daysFailed++
score(id, 'down', {cron:true, times:daysFailed})
value = task.get('value') #get updated value
if type == 'daily'
task.push "history", { date: today.toDate(), value: value }
task.push "history", { date: today, value: value }
else
absVal = if (completed) then Math.abs(value) else value
todoTally += absVal
@ -221,18 +227,17 @@ cron = ->
next()
# Tally each task
tasks = _.toArray(user.get('tasks'))
async.forEach tasks, tallyTask, (err) ->
tasks = _.toArray(user.get('tasks'))
async.forEach tasks, tallyTask, (err) ->
# Finished tallying, this is the 'completed' callback
user.push 'history.todos', { date: today.toDate(), value: todoTally }
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.toDate(), value: expTally }
user.set('lastCron', today.toDate()) # reset cron
user.push 'history.exp', { date: today, value: expTally }
module.exports = {

View file

@ -141,8 +141,9 @@ describe 'User', ->
it 'calculates user.stats & task.value properly on cron', ->
[statsBefore, taskBefore] = pathSnapshots(['_user.stats', taskPath])
# Set lastCron to yesterday
today = moment().sod()
model.set '_user.lastCron', today.subtract('days',1).sod().toDate()
today = new moment()
yesterday = new moment().subtract('days',1)
model.set '_user.lastCron', yesterday.toDate()
# Run run
scoring.cron()
[statsAfter, taskAfter] = pathSnapshots(['_user.stats', taskPath])
@ -152,9 +153,9 @@ describe 'User', ->
expect(today.diff(lastCron, 'days')).to.eql 0
# Should have updated points properly
expect(taskAfter.value).to.eql -1
expect(statsBefore.hp).to.be.lessThan statsAfter.hp
expect(statsBefore.hp).to.be.greaterThan statsAfter.hp
expect(taskBefore.value).to.eql 0
expect(taskAfter.value).to.eql -1
#TODO clicking repeat dates on newly-created item doesn't refresh until you refresh the page
#TODO dates on dailies is having issues, possibility: date cusps? my saturday exempts were set to exempt at 8pm friday