habitrpg-shared: cron bug-fixes

This commit is contained in:
Tyler Renelle 2013-05-20 10:56:38 +01:00
parent 83ac2adb79
commit f412a20203
2 changed files with 18 additions and 16 deletions

View file

@ -121,6 +121,7 @@ get '/', (page, model, params, next) ->
ready (model) ->
user = model.at('_user')
model.setNull '_user.apiToken', derby.uuid()
browser = require './browser'
require('./character').app(exports, model)
require('./tasks').app(exports, model)
@ -130,21 +131,22 @@ ready (model) ->
require('./pets').app(exports, model)
require('../server/private').app(exports, model)
require('./debug').app(exports, model) if model.flags.nodeEnv != 'production'
require('./browser').app(exports, model, app)
browser.app(exports, model, app)
require('./unlock').app(exports, model)
require('./filters').app(exports, model)
###
Cron
###
#FIXME optimize this - don't deepClone first, check if need to run first
uObj = _.cloneDeep user.get() # need to clone, else derby won't catch model.set()'s after obj property sets
# Set it up so it's uObj.habits, uObj.dailys etc instead of uObj.tasks (it's what habitrpg-shared/algos requires)
_.each ['habit','daily','todo','reward'], (type) ->
uObj["#{type}s"] = _.where(uObj.tasks, {type:type}); true
paths = {}
algos.cron(uObj, paths)
delete paths['stats.hp'] # we'll set this manually so we can get a cool animation
_.each paths, (v,k) ->
user.pass({cron:true}).set(k,helpers.dotGet(k, uObj)); true
setTimeout (-> user.set('stats.hp', uObj.stats.hp)), 1000
if algos.shouldCron(user)
uObj = _.cloneDeep user.get() # need to clone, else derby won't catch model.set()'s after obj property sets
# habitrpg-shared/algos requires uObj.habits, uObj.dailys etc instead of uObj.tasks
_.each ['habit','daily','todo','reward'], (type) ->
uObj["#{type}s"] = _.where(uObj.tasks, {type:type}); true
paths = {}
algos.cron(uObj, paths)
lostHp = delete paths['stats.hp'] # we'll set this manually so we can get a cool animation
_.each paths, (v,k) -> user.pass({cron:true}).set(k,helpers.dotGet(k, uObj)); true
if lostHp
browser.resetDom(model)
setTimeout (-> user.set('stats.hp', uObj.stats.hp)), 750

View file

@ -17,7 +17,7 @@ module.exports.app = (appExports, model) ->
clone our user object (if we don't do that, it screws with model.on() listeners, ping Tyler for an explaination),
perform the updates while tracking paths, then all the values at those paths
###
score = (user, taskId, direction) ->
score = (taskId, direction) ->
uObj = _.cloneDeep user.get() # need to clone, else derby won't catch model.set()'s after obj property sets
tObj = uObj.tasks[taskId]
@ -62,7 +62,7 @@ module.exports.app = (appExports, model) ->
if task.get('value') < 0
if confirm("Are you sure? Deleting this task will hurt you (to prevent deleting, then re-creating red tasks).") is true
task.set('type','habit') # hack to make sure it hits HP, instead of performing "undo checkbox"
score(user, id, 'down')
score(id, 'down')
else
return # Cancel. Don't delete, don't hurt user
@ -136,7 +136,7 @@ module.exports.app = (appExports, model) ->
appExports.score = (e, el) ->
task = model.at $(el).parents('li')[0]
direction = $(el).attr('data-direction')
score(user, task.get('id'), direction)
score(task.get('id'), direction)
###
This is how we handle appExports.score for todos & dailies. Due to Derby's special handling of `checked={:task.completd}`,
@ -145,7 +145,7 @@ module.exports.app = (appExports, model) ->
user.on 'set', 'tasks.*.completed', (i, completed, previous, isLocal, passed) ->
return if passed? && passed.cron # Don't do this stuff on cron
direction = if completed then 'up' else 'down'
score(user, i, direction)
score(i, direction)
###
Undo