Skip to content

Instantly share code, notes, and snippets.

@pol
Created November 11, 2008 07:33
Show Gist options
  • Save pol/23775 to your computer and use it in GitHub Desktop.
Save pol/23775 to your computer and use it in GitHub Desktop.
# makes sure that all of the monsters and characters have participants
def update_participants
epm = {}
# make sure each character is accounted for, each one is only in there once.
self.participants.each do |p|
unless p.is_monster? || self.characters.include?(p.character)
self.add_participants([p], :type => :character)
elsif p.is_monster?
epm[p.monster_id] = epm[p.monster_id] ? 0 : epm[p.monster_id] + 1 # build a monster count hash
end
end
# make sure each monster is accounted for, this is tougher since there can be more than one.
# strategy: monster lists can only grow here, they are deleted elsewhere
self.monster_ids.each do |mid|
# if the monster is in the emp list, decrement it
if (emp[mid] && emp[mid] > 0)
emp[mid] -= 1
else # otherwise, add it (inefficiently one at a time, meh)
self.add_participants([Monster.find(mid)], :type => :monster)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment