Parsing Your Character's Loot
Published 1 July 2009
Hi! You've stumbled upon a blog post by a guy named Ryan. I'm not that guy anymore, but I've left his posts around because cool URIs don't change and to remind me how much I've learned and grown over time.
Ryan was a well-meaning but naïve and priviledged person. His views don't necessarily represent the views of anyone.
Here's a quick teaser for a new project I'm working on, almost ready for public beta.
def loot(character_sheet_file)
@xml ||= Hpricot::XML(character_sheet_file.read)
@loot ||= @xml.search("//LootTally/loot[@count!='0']").map do |item|
res = item.search("//RulesElement")
case res.size
when 0: nil
when 1: coder.decode(res.first.attributes["name"])
when 2: coder.decode(res.last.attributes["name"].sub(res.first.attributes["type"], res.first.attributes["name"]))
else res.map { |re| coder.decode(re.attributes["name"]) }.join(" ")
end
end.compact
end
Character files from D&DI Character Builder store loot as a list, but the names of magic items are concatenated with names of their connected mundane items, so you have to mix them based on the magic item's @type
.
Also, the Character Builder stores every item you've ever added, even if you deleted it immediately, so you have to filter out items with a count of zero. You have no way to tell if this was an intentional item purchase, an item accidentally clicked on or a found item from a module, so items with @count=0
are useless.
What could I possibly be building‽‽‽