add some derby-specific routes that are required by the new api, upgrade

lodash so we can use _.transofrm
This commit is contained in:
Tyler Renelle 2013-08-10 23:30:17 -04:00
parent c88fa701a5
commit 6b56067668
2 changed files with 37 additions and 3 deletions

View file

@ -5,10 +5,10 @@
"browserify": "~2.14.1",
"coffeeify": "0.4.0",
"coffee-script": "1.6.2",
"lodash": "~1.2.1",
"moment": "~2.0.0",
"relative-date": "~1.1.1",
"habitrpg-shared": "git://github.com/HabitRPG/habitrpg-shared#master"
"habitrpg-shared": "git://github.com/HabitRPG/habitrpg-shared#master",
"lodash": "~1.3.1"
},
"devDependencies": {
"mocha": "*",

View file

@ -333,4 +333,38 @@ module.exports =
itemStat: (type, item=0) ->
i = items.getItem(type, item)
if type is 'weapon' then i.strength else i.defense
if type is 'weapon' then i.strength else i.defense
###
----------------------------------------------------------------------
Derby-specific helpers. Will remove after the rewrite, need them here for now
----------------------------------------------------------------------
###
###
Make sure model.get() returns all properties, see https://github.com/codeparty/racer/issues/116
###
hydrate: (spec) ->
if _.isObject(spec) and !_.isArray(spec)
hydrated = {}
keys = _.keys(spec).concat(_.keys(spec.__proto__))
_.each keys, (k) => hydrated[k] = @hydrate(spec[k])
hydrated
else spec
###
Derby stores the user schema a bit different than other apps prefer. The biggest difference is it stores
tasks as "refLists" - user[taskType + "Ids"] & user.tasks - where 3rd party apps prefer user[taskType + "s"] (array)
This function transforms the derby-stored data into 3rd-party consumable data
{userScope} the user racer-model scope, NOT an object
{withoutTasks} true if you don't want to return the user.tasks obj & id-lists. We keep them around when doing
local ops, because the var-by-reference lets us edit the original tasks
###
derbyUserToAPI: (userScope, keepTasks=true) ->
uObj = @hydrate userScope.get()
_.each ['habit','daily','todo','reward'], (type) ->
# we use _.transform instead of a simple _.where in order to maintain sort-order
uObj["#{type}s"] = _.transform uObj["#{type}Ids"], (result, tid) -> result.push(uObj.tasks[tid])
delete uObj["#{type}Ids"] unless keepTasks
delete uObj.tasks unless keepTasks
uObj