Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "rexml/document"
- include REXML
- class CardInfo
- attr :rules, :name, :cost, :type, :subtypes
- def initialize(name , cost = "" , rules = [], types = {})
- @name = name
- @cost = cost
- @rules = rules
- @type = types['type']
- @subtypes = types['subtypes']
- end
- def to_s
- puts "\"#{@name}\"" + " #{@cost}"
- @subtypes.empty? ? puts("#{@type}") : puts("#{@type}" + " - " + "#{@subtypes.join(" ")}")
- puts "#{@rules.join("\n")}"
- end
- end
- class CardParser
- def self.parseFile(path)
- cards = []
- doc = Document.new File.open(path, "r")
- doc.root.elements.to_a("card").each { |x| cards << parseCard(x)}
- cards
- end
- def self.parseCard(card)
- rules = []
- card.elements["rulelist"].nil? ? rules = [] : card.elements["rulelist"].elements.to_a.each {|rule| rules << rule.text}
- types = []
- card.elements["typelist"].nil? ? types = {} : types = parseType(card.elements["typelist"].to_a)
- card.elements["cost"].nil? ? cost = "" : cost = card.elements["cost"].text
- CardInfo.new(card.elements["name"].text, cost, rules, types )
- end
- def self.parseType(typelist)
- types = {"type" => "","subtypes" => []}
- typelist.each {
- |type|
- if type.class == REXML::Element
- type.attributes['type'] == "card" ? types["type"] = type.text : types["subtypes"] << type.text
- end
- }
- types
- end
- end
- infos = CardParser.parseFile("cards.xml")
- infos.each {|c| c.to_s}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement