module MtgDecksort
Constants
- Card
Public Class Methods
Private: Converts an array of Cards to an array of Strings representing the
Cards.
array - The Array of Cards to convert.
Returns an Array of Strings representing the Array of Cards passed in.
# File lib/mtgdecksort.rb, line 232 def MtgDecksort.cardArrayToString(array) return_strings = [] array.each do |card| return_strings << card.quantity.to_s + " " + card.name end return return_strings end
# File lib/mtgdecksort.rb, line 9 def MtgDecksort.execute options = {} OptionParser.new do |opts| opts.banner = "Usage: mtgdecksort [options]" opts.on("-p", "--path STR", "Decklist file") do |p| options[:file] = p end opts.on("-f", "--format STR", "Output format") do |f| options[:format] = f end end.parse! unless options == {} sortDeck(options[:file], options[:format]) end end
Private: Removes one card type Array from the deck, sorts it, and returns it
as an Array of Strings.
deck - The deck to extract the card type Array from, as a Hash.
type - The card type to extract, represented as a Symbol.
Returns a sorted Array of Strings representing the Cards in the extracted
Array.
# File lib/mtgdecksort.rb, line 217 def MtgDecksort.extractCardType(deck, type) sortList!(deck[type]) deck_strings = cardArrayToString(deck[type]) deck.delete(type) deck_strings << "\n" return deck_strings end
Private: Gets the information for a specific card using tutor (github.com/davidchambers/tutor).
cardname - The name of the card to retrieve information for, as a String.
Returns a Card
containing the relevant card information.
# File lib/mtgdecksort.rb, line 65 def MtgDecksort.getCardInfo(cardname) # ignore lines beginning with // unless /(\/\/).*$/.match(cardname) # Only do the split if the cardname has a quantity in front if /[0-9]+\s.+$/.match(cardname) quantity, cardname = cardname.split(" ", 2) else quantity = 1 end cardname = cardname.strip puts "Parsing " + cardname + "..." info = 'tutor card \'' + cardname + '\' --format json' info = %x[ #{info} ] info_parsed = JSON.parse(info) card = Card.new(cardname, info_parsed["converted_mana_cost"], info_parsed["types"], quantity) return card end end
Private: Parses a file containing a deck list.
f - The path to the file containing the deck list to be parsed, as a String.
Returns an Array of Cards containing the parsed deck.
# File lib/mtgdecksort.rb, line 49 def MtgDecksort.parseDeck(f) deck = [] File.open(f).each do |cardname| deck << getCardInfo(cardname) end return deck end
Private: builds an Array of Strings to populate a decklist file based on the
provided Hash of Cards, with Symbols representing card type as keys.
deck - The deck to build the strings from, as a Hash with Cards as values
and Symbols representing card types as keys.
format - A String representing the order to place the card types in. Card
card types should be represented by their first character. Any card type not represented will be placed in the general 'Spells' category.
Returns the array of strings.
# File lib/mtgdecksort.rb, line 164 def MtgDecksort.rebuildDeck(deck, format) deck_strings = [] format = format.split("") format.each do |type| case type when 'a' deck_strings << "// Artifacts" deck_strings << extractCardType(deck, :artifacts) when 'c' deck_strings << "// Creatures" deck_strings << extractCardType(deck, :creatures) when 'e' deck_strings << "// Enchantments" deck_strings << extractCardType(deck, :enchantments) when 'i' deck_strings << "// Instants" deck_strings << extractCardType(deck, :instants) when 'l' deck_strings << "// Lands" deck_strings << extractCardType(deck, :lands) when 'p' deck_strings << "// Planeswalkers" deck_strings << extractCardType(deck, :planeswalkers) when 's' deck_strings << "// Sorceries" deck_strings << extractCardType(deck, :sorceries) end end deck_strings << "// Spells" leftover_cards = [] deck.each do |type, list| list.each do |card| leftover_cards << card end end sortList!(leftover_cards) deck_strings << cardArrayToString(leftover_cards) return deck_strings end
Private: Separates a deck Array into a Hash, with card type represented as
a Symbol as the key.
deck - The deck to separate.
Returns a Hash with Cards as values and card types represented as Symbols as
keys.
# File lib/mtgdecksort.rb, line 97 def MtgDecksort.separateTypes(deck) cards = { :artifacts => [], :creatures => [], :enchantments => [], :instants => [], :lands => [], :planeswalkers => [], :sorceries => [] } deck.each do |card| case card.type when ["Artifact", "Creature"] cards[:creatures] << card when ["Enchantment", "Creature"] cards[:creatures] << card when ["Creature"] cards[:creatures] << card when ["Instant"] cards[:instants] << card when ["Land"] cards[:lands] << card when ["Planeswalker"] cards[:planeswalkers] << card when ["Sorcery"] cards[:sorceries] << card when ["Artifact"] cards[:artifacts]<< card when ["Enchantment", "Artifact"] cards[:artifacts] << card when ["Enchantment"] cards[:enchantments] << card end end cards.delete_if do |k, v| v.empty? end return cards end
Private: Sort a deck list.
path - The path to the file containing the deck list to be sorted, as a
String.
format - A String detailing the order of output categories.
Returns nothing.
# File lib/mtgdecksort.rb, line 34 def MtgDecksort.sortDeck(path, format) deck = parseDeck(path) separated_deck = separateTypes(deck) deck = rebuildDeck(separated_deck, format) puts deck end
Private: Sorts an Array of cards by their converted mana cost, then name.
list - The Array of cards to sort.
Returns nothing.
# File lib/mtgdecksort.rb, line 145 def MtgDecksort.sortList!(list) list.sort_by! do |card| [card.cmc, card.name] end end