challenges: add indexedPath() function & view helper, which lets us

look up array indices by object id, similar to reflist benefits. very
handy, using for challenges & challnege tasks
This commit is contained in:
Tyler Renelle 2013-05-30 17:10:35 -04:00
parent 3754ce141d
commit 9180c9dfd4

View file

@ -21,6 +21,21 @@ module.exports.batchTxn = batchTxn = (model, cb, options) ->
user.set "update__", setOps user.set "update__", setOps
ret ret
#TODO put this in habitrpg-shared
###
We can't always use refLists, but we often still need to get a positional path by id: eg, users.1234.tasks.5678.value
For arrays (which use indexes, not id-paths), here's a helper function so we can run indexedPath('users',:user.id,'tasks',:task.id,'value)
###
indexedPath = ->
_.reduce arguments, (m,v) =>
return v if !m #first iteration
return "#{m}.#{v}" if _.isString v #string paths
return "#{m}." + _.findIndex(@model.get(m),v)
, ''
taskInChallenge = (task) ->
return undefined unless task?.challenge
@model.at indexedPath.call(@, "groups.#{task.group.id}.challenges", {id:task.challenge}, "#{task.type}s", {id:task.id})
### ###
algos.score wrapper for habitrpg-helpers to work in Derby. We need to do model.set() instead of simply setting the algos.score wrapper for habitrpg-helpers to work in Derby. We need to do model.set() instead of simply setting the
@ -124,6 +139,8 @@ module.exports.viewHelpers = (view) ->
view.fn 'int', view.fn 'int',
get: (num) -> num get: (num) -> num
set: (num) -> [parseInt(num)] set: (num) -> [parseInt(num)]
view.fn 'indexedPath', indexedPath
#iCal #iCal
view.fn "encodeiCalLink", helpers.encodeiCalLink view.fn "encodeiCalLink", helpers.encodeiCalLink
@ -159,22 +176,10 @@ module.exports.viewHelpers = (view) ->
view.fn 'noTags', helpers.noTags view.fn 'noTags', helpers.noTags
view.fn 'appliedTags', helpers.appliedTags view.fn 'appliedTags', helpers.appliedTags
#TODO put this in habitrpg-shared
taskInChallenge = (task) ->
return false unless task?.challenge
[tid, gid, cid, gType] = [task.id, task.group.id, task.challenge, task.group.type]
getTask = (challenges) ->
challenge = _.find(challenges,{id:cid})
challenge and _.find(challenge["#{task.type}s"],{id:tid})
switch gType
when 'party'
(party = @model.get '_party') and party and (getTask party.challenges)
when 'guild'
(guilds = @model.get "_guilds") and (guild = _.find guilds,{id:gid}) and guild and (getTask guild.challenges)
#Challenges #Challenges
view.fn 'taskInChallenge', taskInChallenge view.fn 'taskInChallenge', (task) ->
taskInChallenge.call(@,task)?.get()
view.fn 'taskAttrFromChallenge', (task, attr) -> view.fn 'taskAttrFromChallenge', (task, attr) ->
t = taskInChallenge.call(@,task) taskInChallenge.call(@,task)?.get(attr)
t and t[attr] view.fn 'brokenChallengeLink', (task) ->
view.fn 'brokenChallengeLink', (task) -> task?.challenge and !taskInChallenge.call(@,task) task?.challenge and !(taskInChallenge.call(@,task)?.get())