Class: Deckstrings::Deck
- Inherits:
-
Object
- Object
- Deckstrings::Deck
- Defined in:
- lib/deckstrings/deckstrings.rb
Overview
A Hearthstone deck with metadata that is convertible to and from a deckstring.
Instance Attribute Summary collapse
-
#cards ⇒ Hash{Card => Integer}
readonly
The cards contained in this deck.
-
#format ⇒ Format
readonly
Format for this deck.
-
#heroes ⇒ Array<Hero>
readonly
The heroes associated with this deck.
Class Method Summary collapse
-
.decode(deckstring) ⇒ Deck
Decodes a Hearthstone deckstring into a Deck with basic hero and card metadata.
-
.encode(format:, heroes:, cards:) ⇒ String
Encodes a Hearthstone deck as a compact deckstring.
Instance Method Summary collapse
-
#deckstring ⇒ String
Encoded deckstring for this deck.
-
#initialize(format:, heroes:, cards:) ⇒ Deck
constructor
Create a new deck from component parts.
-
#raw ⇒ { format: Integer, heroes: Array<Integer>, cards: Hash{Integer => Integer} }
Raw deck details.
-
#standard? ⇒ Boolean
true
if the deck is Standard format,false
otherwise. -
#to_s ⇒ String
Pretty-printed deck listing.
-
#wild? ⇒ Boolean
true
if the deck is Wild format,false
otherwise.
Constructor Details
#initialize(format:, heroes:, cards:) ⇒ Deck
Create a new deck from component parts.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/deckstrings/deckstrings.rb', line 280 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
#cards ⇒ Hash{Card => Integer} (readonly)
The cards contained in this deck.
433 434 435 |
# File 'lib/deckstrings/deckstrings.rb', line 433 def cards @cards end |
#format ⇒ Format (readonly)
Returns Format for this deck.
425 426 427 |
# File 'lib/deckstrings/deckstrings.rb', line 425 def format @format end |
#heroes ⇒ Array<Hero> (readonly)
The heroes associated with this deck.
429 430 431 |
# File 'lib/deckstrings/deckstrings.rb', line 429 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.
330 331 332 333 334 335 336 337 |
# File 'lib/deckstrings/deckstrings.rb', line 330 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.
361 362 363 364 365 366 367 |
# File 'lib/deckstrings/deckstrings.rb', line 361 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
#deckstring ⇒ String
Encoded deckstring for this deck.
310 311 312 |
# File 'lib/deckstrings/deckstrings.rb', line 310 def deckstring return Deckstrings::encode(self.raw) end |
#raw ⇒ { format: Integer, heroes: Array<Integer>, cards: Hash{Integer => Integer} }
Raw deck details.
300 301 302 303 304 |
# File 'lib/deckstrings/deckstrings.rb', line 300 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.
377 378 379 |
# File 'lib/deckstrings/deckstrings.rb', line 377 def standard? format.standard? end |
#to_s ⇒ String
Pretty-printed deck listing.
409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/deckstrings/deckstrings.rb', line 409 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.
371 372 373 |
# File 'lib/deckstrings/deckstrings.rb', line 371 def wild? format.wild? end |