mtgdecksort 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mtgdecksort +8 -0
  3. data/lib/mtgdecksort.rb +240 -0
  4. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d44b74ed7c8516f48b9f4581149eb81dbd53101
4
+ data.tar.gz: d014fcbf79f252ded342441394f88b4df0d65a11
5
+ SHA512:
6
+ metadata.gz: 09f0c9d44408ef524fd901ed66d8606e4ae30a921eb7d08623ffc42b7ac1ad0aa9be7706c0af9124beab1dd1d8274e0b160707de2fec67102806e7c7312eab9b
7
+ data.tar.gz: 3ee170435d16e2f0f48213b16f13ecd7c568ee03278f05121f15f1485f0273fc245b90f66083a5d1353ac561a47acbcbd08dbcca988c368ffb9815acfaff3b10
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5
+
6
+ require 'mtgdecksort'
7
+
8
+ MtgDecksort.execute
@@ -0,0 +1,240 @@
1
+ # coding:utf-8
2
+
3
+ require 'optparse'
4
+ require 'json'
5
+
6
+ module MtgDecksort
7
+ Card = Struct.new(:name, :cmc, :type, :quantity)
8
+
9
+ def MtgDecksort.execute
10
+ options = {}
11
+
12
+ OptionParser.new do |opts|
13
+ opts.banner = "Usage: mtgdecksort [options]"
14
+ opts.on("-p", "--path STR", "Decklist file") do |p|
15
+ options[:file] = p
16
+ end
17
+ opts.on("-f", "--format STR", "Output format") do |f|
18
+ options[:format] = f
19
+ end
20
+ end.parse!
21
+
22
+ unless options == {}
23
+ sortDeck(options[:file], options[:format])
24
+ end
25
+ end
26
+
27
+ # Private: Sort a deck list.
28
+ #
29
+ # path - The path to the file containing the deck list to be sorted, as a
30
+ # String.
31
+ # format - A String detailing the order of output categories.
32
+ #
33
+ # Returns nothing.
34
+ def MtgDecksort.sortDeck(path, format)
35
+ deck = parseDeck(path)
36
+
37
+ separated_deck = separateTypes(deck)
38
+
39
+ deck = rebuildDeck(separated_deck, format)
40
+
41
+ puts deck
42
+ end
43
+
44
+ # Private: Parses a file containing a deck list.
45
+ #
46
+ # f - The path to the file containing the deck list to be parsed, as a String.
47
+ #
48
+ # Returns an Array of Cards containing the parsed deck.
49
+ def MtgDecksort.parseDeck(f)
50
+ deck = []
51
+
52
+ File.open(f).each do |cardname|
53
+ deck << getCardInfo(cardname)
54
+ end
55
+
56
+ return deck
57
+ end
58
+
59
+ # Private: Gets the information for a specific card
60
+ # using tutor (https://github.com/davidchambers/tutor).
61
+ #
62
+ # cardname - The name of the card to retrieve information for, as a String.
63
+ #
64
+ # Returns a Card containing the relevant card information.
65
+ def MtgDecksort.getCardInfo(cardname)
66
+ # ignore lines beginning with //
67
+ unless /(\/\/).*$/.match(cardname)
68
+ # Only do the split if the cardname has a quantity in front
69
+ if /[0-9]+\s.+$/.match(cardname)
70
+ quantity, cardname = cardname.split(" ", 2)
71
+ else
72
+ quantity = 1
73
+ end
74
+
75
+ cardname = cardname.strip
76
+
77
+ puts "Parsing " + cardname + "..."
78
+ info = 'tutor card \'' + cardname + '\' --format json'
79
+ info = %x[ #{info} ]
80
+
81
+ info_parsed = JSON.parse(info)
82
+
83
+ card = Card.new(cardname, info_parsed["converted_mana_cost"],
84
+ info_parsed["types"], quantity)
85
+
86
+ return card
87
+ end
88
+ end
89
+
90
+ # Private: Separates a deck Array into a Hash, with card type represented as
91
+ # a Symbol as the key.
92
+ #
93
+ # deck - The deck to separate.
94
+ #
95
+ # Returns a Hash with Cards as values and card types represented as Symbols as
96
+ # keys.
97
+ def MtgDecksort.separateTypes(deck)
98
+ cards = {
99
+ :artifacts => [],
100
+ :creatures => [],
101
+ :enchantments => [],
102
+ :instants => [],
103
+ :lands => [],
104
+ :planeswalkers => [],
105
+ :sorceries => []
106
+ }
107
+
108
+ deck.each do |card|
109
+ case card.type
110
+ when ["Artifact", "Creature"]
111
+ cards[:creatures] << card
112
+ when ["Enchantment", "Creature"]
113
+ cards[:creatures] << card
114
+ when ["Creature"]
115
+ cards[:creatures] << card
116
+ when ["Instant"]
117
+ cards[:instants] << card
118
+ when ["Land"]
119
+ cards[:lands] << card
120
+ when ["Planeswalker"]
121
+ cards[:planeswalkers] << card
122
+ when ["Sorcery"]
123
+ cards[:sorceries] << card
124
+ when ["Artifact"]
125
+ cards[:artifacts]<< card
126
+ when ["Enchantment", "Artifact"]
127
+ cards[:artifacts] << card
128
+ when ["Enchantment"]
129
+ cards[:enchantments] << card
130
+ end
131
+ end
132
+
133
+ cards.delete_if do |k, v|
134
+ v.empty?
135
+ end
136
+
137
+ return cards
138
+ end
139
+
140
+ # Private: Sorts an Array of cards by their converted mana cost, then name.
141
+ #
142
+ # list - The Array of cards to sort.
143
+ #
144
+ # Returns nothing.
145
+ def MtgDecksort.sortList!(list)
146
+ list.sort_by! do |card|
147
+ [card.cmc, card.name]
148
+ end
149
+ end
150
+
151
+ # Private: builds an Array of Strings to populate a decklist file based on the
152
+ # provided Hash of Cards, with Symbols representing card type as
153
+ # keys.
154
+ #
155
+ # deck - The deck to build the strings from, as a Hash with Cards as values
156
+ # and Symbols representing card types as keys.
157
+ #
158
+ # format - A String representing the order to place the card types in. Card
159
+ # card types should be represented by their first character. Any card
160
+ # type not represented will be placed in the general 'Spells'
161
+ # category.
162
+ #
163
+ # Returns the array of strings.
164
+ def MtgDecksort.rebuildDeck(deck, format)
165
+ deck_strings = []
166
+
167
+ format = format.split("")
168
+
169
+ format.each do |type|
170
+ case type
171
+ when 'a'
172
+ deck_strings << "// Artifacts"
173
+ deck_strings << extractCardType(deck, :artifacts)
174
+ when 'c'
175
+ deck_strings << "// Creatures"
176
+ deck_strings << extractCardType(deck, :creatures)
177
+ when 'e'
178
+ deck_strings << "// Enchantments"
179
+ deck_strings << extractCardType(deck, :enchantments)
180
+ when 'i'
181
+ deck_strings << "// Instants"
182
+ deck_strings << extractCardType(deck, :instants)
183
+ when 'l'
184
+ deck_strings << "// Lands"
185
+ deck_strings << extractCardType(deck, :lands)
186
+ when 'p'
187
+ deck_strings << "// Planeswalkers"
188
+ deck_strings << extractCardType(deck, :planeswalkers)
189
+ when 's'
190
+ deck_strings << "// Sorceries"
191
+ deck_strings << extractCardType(deck, :sorceries)
192
+ end
193
+ end
194
+
195
+ deck_strings << "// Spells"
196
+ leftover_cards = []
197
+ deck.each do |type, list|
198
+ list.each do |card|
199
+ leftover_cards << card
200
+ end
201
+ end
202
+
203
+ sortList!(leftover_cards)
204
+ deck_strings << cardArrayToString(leftover_cards)
205
+ return deck_strings
206
+ end
207
+
208
+ # Private: Removes one card type Array from the deck, sorts it, and returns it
209
+ # as an Array of Strings.
210
+ #
211
+ # deck - The deck to extract the card type Array from, as a Hash.
212
+ #
213
+ # type - The card type to extract, represented as a Symbol.
214
+ #
215
+ # Returns a sorted Array of Strings representing the Cards in the extracted
216
+ # Array.
217
+ def MtgDecksort.extractCardType(deck, type)
218
+ sortList!(deck[type])
219
+ deck_strings = cardArrayToString(deck[type])
220
+ deck.delete(type)
221
+ deck_strings << "\n"
222
+
223
+ return deck_strings
224
+ end
225
+
226
+ # Private: Converts an array of Cards to an array of Strings representing the
227
+ # Cards.
228
+ #
229
+ # array - The Array of Cards to convert.
230
+ #
231
+ # Returns an Array of Strings representing the Array of Cards passed in.
232
+ def MtgDecksort.cardArrayToString(array)
233
+ return_strings = []
234
+ array.each do |card|
235
+ return_strings << card.quantity.to_s + " " + card.name
236
+ end
237
+
238
+ return return_strings
239
+ end
240
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtgdecksort
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - James Noble
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'A utility for sorting Magic: The Gathering decklists'
14
+ email: james@jamwillinob.com
15
+ executables:
16
+ - mtgdecksort
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/mtgdecksort
21
+ - lib/mtgdecksort.rb
22
+ homepage: http://jamwillinob.com
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: 'Magic: The Gathering deck sorting utility'
46
+ test_files: []