Class: Deckstrings::Deck

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

Overview

A Hearthstone deck with metadata that is convertible to and from a deckstring.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format:, heroes:, cards:) ⇒ Deck

Create a new deck from component parts.

Parameters:

  • format (Integer, Deckstrings::Format)

    Format for this deck: wild or standard.

  • heroes (Array<Integer, Deckstrings::Hero>)

    Heroes for this deck. Multiple heroes are supported, but typically this array will contain one element.

  • cards (Hash{Integer, Deckstrings::Card => Integer})

    Cards in the deck. A Hash from card ID to its instance count in the deck.

Raises:

  • (ArgumentError)

    If format, heroes, or any cards are unknown.


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/deckstrings/deckstrings.rb', line 275

def initialize(format:, heroes:, cards:)
  @format = Format.parse(format) if !format.is_a?(Format)
  raise ArgumentError, "Unknown format: #{format}." if !@format

  @heroes = heroes.map do |id|
    hero = Database.instance.heroes[id]
    raise ArgumentError, "Unknown hero: #{id}." if hero.nil?
    Hero.new(id, hero['name'], hero['class'])
  end

  @cards = cards.map do |id, count|
    card = Database.instance.cards[id]
    raise ArgumentError, "Unknown card: #{id}." if card.nil?
    [Card.new(id, card['name'], card['cost']), count]
  end.sort_by { |card, _| card.cost }.to_h
end

Instance Attribute Details

#cardsHash{Card => Integer} (readonly)

The cards contained in this deck.

Returns:

  • (Hash{Card => Integer})

    A Hash from Card to its instance count in the deck.


428
429
430
# File 'lib/deckstrings/deckstrings.rb', line 428

def cards
  @cards
end

#formatFormat (readonly)

Returns Format for this deck.

Returns:

  • (Format)

    Format for this deck.


420
421
422
# File 'lib/deckstrings/deckstrings.rb', line 420

def format
  @format
end

#heroesArray<Hero> (readonly)

The heroes associated with this deck.

Returns:

  • (Array<Hero>)

    Typically, this array will contain one element.


424
425
426
# File 'lib/deckstrings/deckstrings.rb', line 424

def heroes
  @heroes
end

Class Method Details

.decode(deckstring) ⇒ Deck

Decodes a Hearthstone deckstring into a Deckstrings::Deck with basic hero and card metadata.

This method validates the well-formedness of the deckstring, the embedded version, the format, card counts, and each hero/card ID.

All IDs refer to unique Hearthstone DBF IDs which can be used in conjunction with HearthstoneJSON metadata.

Examples:

deck = Deckstrings::Deck.decode('AAEBAf0GAA/yAaIC3ALgBPcE+wWKBs4H2QexCMII2Q31DfoN9g4A')
deck = Deckstrings::Deck.decode('AAECAZICCPIF+Az5DK6rAuC7ApS9AsnHApnTAgtAX/4BxAbkCLS7Asu8As+8At2+AqDNAofOAgA=')

Parameters:

  • deckstring (String)

    Base64-encoded Hearthstone deckstring.

Returns:

  • (Deck)

    Decoded Hearthstone deck.

Raises:

  • (FormatError)

    If the deckstring is malformed or contains invalid deck data.

See Also:


325
326
327
328
329
330
331
332
# File 'lib/deckstrings/deckstrings.rb', line 325

def self.decode(deckstring)
  parts = Deckstrings::decode(deckstring)
  begin
    Deck.new(parts)
  rescue ArgumentError => e
    raise FormatError, e.to_s
  end
end

.encode(format:, heroes:, cards:) ⇒ String

Encodes a Hearthstone deck as a compact deckstring.

This method validates card counts, format, and each hero/card ID.

All IDs refer to unique Hearthstone DBF IDs which can be seen in HearthstoneJSON metadata.

Examples:

deckstring = Deckstrings::Deck.encode(format: 2, heroes: [637], cards: { 1004 => 2, 315 => 2 })
deckstring = Deckstrings::Deck.encode(
  format: Deckstrings::Format.standard,
  heroes: [Deckstrings::Hero.mage],
  cards: { 1004 => 2, 315 => 2 }
)

Parameters:

  • format (Integer, Deckstrings::Format)

    Format for this deck: wild or standard.

  • heroes (Array<Integer, Deckstrings::Hero>)

    Heroes for this deck. Multiple heroes are supported, but typically this array will contain one element.

  • cards (Hash{Integer, Deckstrings::Card => Integer})

    Cards in the deck. A Hash from card ID to its instance count in the deck.

Returns:

  • (String)

    Base64-encoded compact byte string representing the deck.

Raises:

  • (FormatError)

    If any card counts are less than 1, or if any IDs do not refer to valid Hearthstone entities.

See Also:


356
357
358
359
360
361
362
# File 'lib/deckstrings/deckstrings.rb', line 356

def self.encode(format:, heroes:, cards:)
  begin
    Deck.new(format: format, heroes: heroes, cards: cards).deckstring
  rescue ArgumentError => e
    raise FormatError, e.to_s
  end
end

Instance Method Details

#deckstringString

Encoded deckstring for this deck.

Returns:

  • (String)

    Base64-encoded compact byte string representing the deck.

See Also:


305
306
307
# File 'lib/deckstrings/deckstrings.rb', line 305

def deckstring
  return Deckstrings::encode(self.raw)
end

#raw{ format: Integer, heroes: Array<Integer>, cards: Hash{Integer => Integer} }

Raw deck details.

Returns:

  • ({ format: Integer, heroes: Array<Integer>, cards: Hash{Integer => Integer} })

    See Deckstrings.decode for details.

See Also:


295
296
297
298
299
# File 'lib/deckstrings/deckstrings.rb', line 295

def raw
  heroes = @heroes.map(&:id)
  cards = @cards.map { |card, count| [card.id, count] }.to_h
  { format: @format.value, heroes: heroes, cards: cards }
end

#standard?Boolean

Returns true if the deck is Standard format, false otherwise.

Returns:

  • (Boolean)

    true if the deck is Standard format, false otherwise.

See Also:


372
373
374
# File 'lib/deckstrings/deckstrings.rb', line 372

def standard?
  format.standard?
end

#to_sString

Pretty-printed deck listing.

Examples:

puts Deckstrings::Deck.decode('AAECAZICCPIF+Az5DK6rAuC7ApS9AsnHApnTAgtAX/4BxAbkCLS7Asu8As+8At2+AqDNAofOAgA=')
Format: Standard
Class: Druid
Hero: Malfurion Stormrage

2× Innervate
2× Jade Idol
2× Wild Growth
2× Wrath
2× Jade Blossom
2× Swipe
2× Jade Spirit
1× Fandral Staghelm
1× Spellbreaker
2× Nourish
1× Big Game Hunter
2× Spreading Plague
1× The Black Knight
1× Aya Blackpaw
2× Jade Behemoth
1× Malfurion the Pestilent
1× Primordial Drake
2× Ultimate Infestation
1× Kun the Forgotten King

Returns:

  • (String)

    A pretty-printed listing of deck details.


404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/deckstrings/deckstrings.rb', line 404

def to_s
  listing = "Format: #{format}"

  hero = @heroes.first
  if hero
    listing += "\nClass: #{hero.hero_class}\nHero: #{hero.name}"
  end

  if !cards.empty?
    listing += "\n\n" + cards.map { |card, count| "#{count}× #{card.name}" }.join("\n")
  end

  listing
end

#wild?Boolean

Returns true if the deck is Wild format, false otherwise.

Returns:

  • (Boolean)

    true if the deck is Wild format, false otherwise.

See Also:


366
367
368
# File 'lib/deckstrings/deckstrings.rb', line 366

def wild?
  format.wild?
end