Merge branch 'master' into challenges

This commit is contained in:
Tyler Renelle 2013-05-27 14:44:52 +01:00
commit 4e7233f100
2 changed files with 48 additions and 57 deletions

View file

@ -313,16 +313,6 @@ updateStats = (user, newStats, options={}) ->
gp = 0.0 if (!gp? or gp < 0)
user.stats.gp = newStats.gp
###
Test if we should run cron
{user} pass in the user object
{now} optional - we can decide which time is "now", which is especially helpful in tests
###
obj.shouldCron = (user, options={}) ->
now = options.now || +new Date
return true if !user.lastCron? or user.lastCron is 'new' or helpers.daysBetween(user.lastCron, now, user.preferences?.dayStart) > 0
return false
###
At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
For incomplete Dailys, deduct experience
@ -339,56 +329,57 @@ obj.cron = (user, options={}) ->
return
daysMissed = helpers.daysBetween(user.lastCron, now, user.preferences?.dayStart)
if daysMissed > 0
user.lastCron = now; paths['lastCron'] = true
return unless daysMissed > 0
# User is resting at the inn. Used to be we un-checked each daily without performing calculation (see commits before fb29e35)
# but to prevent abusing the inn (http://goo.gl/GDb9x) we now do *not* calculate dailies, and simply set lastCron to today
return if user.flags.rest is true
user.lastCron = now; paths['lastCron'] = true
# Tally each task
todoTally = 0
user.todos.concat(user.dailys).forEach (task) ->
{id, type, completed, repeat} = task
# Deduct experience for missed Daily tasks, but not for Todos (just increase todo's value)
unless completed
scheduleMisses = daysMissed
# for dailys which have repeat dates, need to calculate how many they've missed according to their own schedule
if (type is 'daily') and repeat
scheduleMisses = 0
_.times daysMissed, (n) ->
thatDay = moment(now).subtract('days', n + 1)
scheduleMisses++ if helpers.shouldDo(thatDay, repeat, {dayStart:obj.preferences?.dayStart})
obj.score(user, task, 'down', {times:scheduleMisses, cron:true, paths:paths}) if scheduleMisses > 0
# User is resting at the inn. Used to be we un-checked each daily without performing calculation (see commits before fb29e35)
# but to prevent abusing the inn (http://goo.gl/GDb9x) we now do *not* calculate dailies, and simply set lastCron to today
return if user.flags.rest is true
switch type
when 'daily'
(task.history ?= []).push({ date: +new Date, value: task.value }); paths["tasks.#{task.id}.history"] = true
task.completed = false; paths["tasks.#{task.id}.completed"] = true;
when 'todo'
#get updated value
absVal = if (completed) then Math.abs(task.value) else task.value
todoTally += absVal
# Tally each task
todoTally = 0
user.todos.concat(user.dailys).forEach (task) ->
{id, type, completed, repeat} = task
# Deduct experience for missed Daily tasks, but not for Todos (just increase todo's value)
unless completed
scheduleMisses = daysMissed
# for dailys which have repeat dates, need to calculate how many they've missed according to their own schedule
if (type is 'daily') and repeat
scheduleMisses = 0
_.times daysMissed, (n) ->
thatDay = moment(now).subtract('days', n + 1)
scheduleMisses++ if helpers.shouldDo(thatDay, repeat, {dayStart:obj.preferences?.dayStart})
obj.score(user, task, 'down', {times:scheduleMisses, cron:true, paths:paths}) if scheduleMisses > 0
user.habits.forEach (task) -> # slowly reset 'onlies' value to 0
if task.up is false or task.down is false
if Math.abs(task.value) < 0.1
task.value = 0
else
task.value = task.value / 2
paths["tasks.#{task.id}.value"] = true
switch type
when 'daily'
(task.history ?= []).push({ date: +new Date, value: task.value }); paths["tasks.#{task.id}.history"] = true
task.completed = false; paths["tasks.#{task.id}.completed"] = true;
when 'todo'
#get updated value
absVal = if (completed) then Math.abs(task.value) else task.value
todoTally += absVal
user.habits.forEach (task) -> # slowly reset 'onlies' value to 0
if task.up is false or task.down is false
if Math.abs(task.value) < 0.1
task.value = 0
else
task.value = task.value / 2
paths["tasks.#{task.id}.value"] = true
# Finished tallying
((user.history ?= {}).todos ?= []).push { date: now, value: todoTally }
# tally experience
expTally = user.stats.exp
lvl = 0 #iterator
while lvl < (user.stats.lvl - 1)
lvl++
expTally += obj.tnl(lvl)
(user.history.exp ?= []).push { date: now, value: expTally }
paths["history"] = true
user
# Finished tallying
((user.history ?= {}).todos ?= []).push { date: now, value: todoTally }
# tally experience
expTally = user.stats.exp
lvl = 0 #iterator
while lvl < (user.stats.lvl - 1)
lvl++
expTally += obj.tnl(lvl)
(user.history.exp ?= []).push { date: now, value: expTally }
paths["history"] = true
user

View file

@ -33,7 +33,7 @@ items = module.exports.items =
{index: 0, text: "No Shield", classes: 'shield_0', notes:'No Shield.', defense: 0, value:0}
{index: 1, text: "Wooden Shield", classes: 'shield_1', notes:'Decreases HP loss by 3%', defense: 3, value:20}
{index: 2, text: "Buckler", classes: 'shield_2', notes:'Decreases HP loss by 4%.', defense: 4, value:35}
{index: 3, text: "Enforced Shield", classes: 'shield_3', notes:'Decreases HP loss by 5%.', defense: 5, value:55}
{index: 3, text: "Reinforced Shield", classes: 'shield_3', notes:'Decreases HP loss by 5%.', defense: 5, value:55}
{index: 4, text: "Red Shield", classes: 'shield_4', notes:'Decreases HP loss by 7%.', defense: 7, value:70}
{index: 5, text: "Golden Shield", classes: 'shield_5', notes:'Decreases HP loss by 8%.', defense: 8, value:90}
{index: 6, text: "Tormented Skull", classes: 'shield_6', notes:'Decreases HP loss by 9%.', defense: 9, value:120}
@ -106,4 +106,4 @@ module.exports.updateStore = (user) ->
else if i is items[type].length
showNext = false
changes[type] = if showNext then items[type][i] else {hide:true}
return changes
return changes