Class: RuneterraCards::CardSet

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

Overview

TODO:

The API to this class is very unstable and will change a lot in a coming release.

Represents a collection of cards.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ CardSet

Returns a new instance of CardSet.

Parameters:

  • cards (Hash{String => Number}, Enumerable{String => Number})

    A Hash of card codes mapping to card counts


19
20
21
# File 'lib/runeterra_cards/card_set.rb', line 19

def initialize(cards)
  @cards = cards
end

Instance Attribute Details

#cardsHash{String => Number} (readonly)

Deprecated.

Returns:

  • (Hash{String => Number})

16
17
18
# File 'lib/runeterra_cards/card_set.rb', line 16

def cards
  @cards
end

Class Method Details

.from_deck_code(deck_code) ⇒ CardSet

Parse a Deck Code.

Parameters:

  • deck_code (String)

Returns:

Raises:


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/runeterra_cards/card_set.rb', line 64

def self.from_deck_code(deck_code)
  binary_data = decode_base32(deck_code)
  format, version = decode_format_and_version(binary_data[0])

  raise UnrecognizedVersionError.new(SUPPORTED_VERSION, version) unless version <= SUPPORTED_VERSION

  raise unless format.equal? 1

  int_array = unpack_big_endian_varint(binary_data[1..])
  cards = assemble_card_list(int_array)

  new(cards.to_h)
end

Instance Method Details

#-(other) ⇒ CardSet

Subtract another CardSet from this one. Items with count 0 are not represented in the returned CardSet, they are removed altogether.

Parameters:

Returns:


27
28
29
30
31
32
33
34
35
# File 'lib/runeterra_cards/card_set.rb', line 27

def -(other)
  remaining_cards =
    cards.each_with_object({}) do |(code, count), result|
      new_count = count - other.count_for_card_code(code)
      result[code] = new_count unless new_count.equal?(0)
    end

  CardSet.new(remaining_cards)
end

#as_card_codesEnumerable<String => Number>

Return all cards in the card set as a map of card codes to counts.

Examples:

set.as_card_codes #=> { '01DE044' => 1, '02NX003' => 2 }

Returns:

  • (Enumerable<String => Number>)

46
47
48
# File 'lib/runeterra_cards/card_set.rb', line 46

def as_card_codes
  cards
end

#as_cardsEnumerable<Card => Number>

Returns:

  • (Enumerable<Card => Number>)

38
39
40
# File 'lib/runeterra_cards/card_set.rb', line 38

def as_cards
  cards.transform_keys { |code| Card.new(code: code) }
end

#count_for_card_code(code) ⇒ Integer

Returns how many of the given card are in this CardSet.

Parameters:

  • code (String)

    Card code, e.g. “01DE031”

Returns:

  • (Integer)

    How many of the card are in this CardSet, or 0 if it isn't present.


53
54
55
# File 'lib/runeterra_cards/card_set.rb', line 53

def count_for_card_code(code)
  cards[code] || 0
end