very weird fix. batchTxn() pass in option to skip hydration, which

sometimes achieves exactly the effect *hydrating* is supposed to
achieve. very strange, but anyway this fixes the habit scoring
This commit is contained in:
Tyler Renelle 2013-08-11 19:56:22 -04:00
parent 9c08c7f77b
commit a70e7599ab
2 changed files with 11 additions and 8 deletions

View file

@ -3,23 +3,26 @@ algos = require 'habitrpg-shared/script/algos'
items = require('habitrpg-shared/script/items').items
helpers = require('habitrpg-shared/script/helpers')
module.exports.batchTxn = batchTxn = (model, cb, options) ->
user = options?.user or model.at("_user")
uObj = helpers.hydrate(user.get()) # see https://github.com/codeparty/racer/issues/116
module.exports.batchTxn = batchTxn = (model, cb, options={}) ->
_.defaults options, {user: model.at("_user"), hydrate: true, cron: false, done: ->}
{user} = options
# see https://github.com/codeparty/racer/issues/116
# But sometimes we get the exact opposite effect if we hydrate. I don't understand it, and I can't wait to start using Mongoose instead.
uObj = if options.hydrate then helpers.hydrate(user.get()) else user.get()
batch =
set: (k,v) -> helpers.dotSet(k,v,uObj); paths[k] = true
get: (k) -> helpers.dotGet(k,uObj)
paths = {}
model._dontPersist = true
ret = cb uObj, paths, batch
_.each paths, (v,k) -> user.pass({cron:options?.cron}).set(k,batch.get(k));true
_.each paths, (v,k) -> user.pass({cron:options.cron}).set(k,batch.get(k));true
model._dontPersist = false
# some hackery in our own branched racer-db-mongo, see findAndModify of lefnire/racer-db-mongo#habitrpg index.js
# pass true if we have levelled to supress xp notification
unless _.isEmpty paths
setOps = _.reduce paths, ((m,v,k)-> m[k] = batch.get(k);m), {}
user.set "update__", setOps, options?.done
else options?.done?()
user.set "update__", setOps, options.done
else options.done()
ret
#TODO put this in habitrpg-shared

View file

@ -58,12 +58,12 @@ deleteTask = (user, task, cb) ->
taskIds = user.get "#{task.type}Ids"
user.remove "#{task.type}Ids", taskIds.indexOf(task.id), 1, cb
score = (model, user, taskId, direction, cb) ->
score = (model, user, taskId, direction, done) ->
delta = 0
misc.batchTxn model, (uObj, paths) ->
tObj = uObj.tasks[taskId]
delta = algos.score(uObj, tObj, direction, {paths})
, {user, done:cb}
, {user, done, hydrate: false}
delta
###