Class: RuneterraCards::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/runeterra_cards/metadata.rb

Overview

Note:

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.

Examples:

Load data and retrieve data for a card

m = RuneterraCards::Metadata.new
m.add_set_file('./set1-en_us.json')
m.lookup_card('01DE031') #=> CardMetadata

Load data from multiple sets

m = RuneterraCards::Metadata.new
m.add_set_file('./set1-en_us.json')
m.add_set_file('./set2-en_us.json')

Instance Method Summary collapse

Constructor Details

#initializeMetadata

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

TODO:

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”

Parameters:

  • file_path (String)

    The path to the metadata file to read


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
  end
end

#all_collectibleHash<String,CardMetadata>

Returns all cards in the metadata set that are collectible

Returns:

See Also:


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.

Parameters:

Returns:

  • (Cost)

    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_setCardSet

Returns a [CardSet] that represents a complete card collection. That is: 3x of every card that is collectible.

Returns:


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

TODO:

document errors if card_code doesn't exist

Fetch card metadata for a card via its card code

Parameters:

  • card_code (String)

    card code, e.g. 01DE031

Returns:


44
45
46
# File 'lib/runeterra_cards/metadata.rb', line 44

def lookup_card(card_code)
  @cards.fetch(card_code)
end