Wordslinger • Dissident • Webwright

Parsing Your Character's Loot

Published 01 July 2009

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(" ")

Character files from [D&DI Character Builder][cb] 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‽‽‽ [cb]: http://wizards.com/default.asp?x=dnd/insider/characterbuilder “D&D Character Builder”