Class: RuneterraCards::Metadata
- Inherits:
-
Object
- Object
- RuneterraCards::Metadata
- Defined in:
- lib/runeterra_cards/metadata.rb
Overview
This class cannot yet handle metadata for multiple locales at the same time. You will need multiple instances of this class, one for each locale, if you wish to handle multiple locales at this time.
Loads, stores, and retrieves data for cards from Legends of Runeterra Data Dragon set files. N.B. this does not ship with any metadata of its own, you must provide set files yourself and load them before this class will do anything.
Instance Method Summary collapse
-
#add_set_file(file_path) ⇒ Object
Load card data from a Legends of Runeterra Data Dragon set file, e.g.
-
#all_collectible ⇒ Hash<String,CardMetadata>
Returns all cards in the metadata set that are collectible.
-
#cost_of(card_set) ⇒ Cost
The cost of crafting all the cards in the supplied CardSet.
-
#full_set ⇒ CardSet
Returns a [CardSet] that represents a complete card collection.
-
#initialize ⇒ Metadata
constructor
Create a new, empty, metadata repository.
-
#lookup_card(card_code) ⇒ CardMetadata
Fetch card metadata for a card via its card code.
Constructor Details
#initialize ⇒ Metadata
Create a new, empty, metadata repository.
24 25 26 |
# File 'lib/runeterra_cards/metadata.rb', line 24 def initialize @cards = {} end |
Instance Method Details
#add_set_file(file_path) ⇒ Object
document and test exceptions that can be thrown here
Load card data from a Legends of Runeterra Data Dragon set file, e.g. “set1-en_us.json”
31 32 33 34 35 36 37 38 |
# File 'lib/runeterra_cards/metadata.rb', line 31 def add_set_file(file_path) file = File.read(file_path) data = JSON.parse(file) data.each do |card_data| card = CardMetadata.new(card_data) @cards[card.card_code] = card # TODO: warn or error if there are duplicate card codes! end end |
#all_collectible ⇒ Hash<String,CardMetadata>
Returns all cards in the metadata set that are collectible
51 52 53 |
# File 'lib/runeterra_cards/metadata.rb', line 51 def all_collectible @cards.select { |_, card| card.collectible? } end |
#cost_of(card_set) ⇒ Cost
Returns the cost of crafting all the cards in the supplied CardSet.
64 65 66 67 68 69 70 |
# File 'lib/runeterra_cards/metadata.rb', line 64 def cost_of(card_set) rarity_and_count = card_set.cards.map { |card, count| [lookup_card(card).rarity, count] } total = { common: 0, rare: 0, epic: 0, champion: 0 } rarity_and_count.each { |(rarity, count)| total[rarity] += count } Cost.new(total.fetch(:common), total.fetch(:rare), total.fetch(:epic), total.fetch(:champion)) end |
#full_set ⇒ CardSet
Returns a [CardSet] that represents a complete card collection. That is: 3x of every card that is collectible.
58 59 60 |
# File 'lib/runeterra_cards/metadata.rb', line 58 def full_set CardSet.new(all_collectible.keys.each_with_object({}) { |code, result| result[code] = 3 }) end |
#lookup_card(card_code) ⇒ CardMetadata
document errors if card_code doesn’t exist
Fetch card metadata for a card via its card code
44 45 46 |
# File 'lib/runeterra_cards/metadata.rb', line 44 def lookup_card(card_code) @cards.fetch(card_code) end |