/
Rakefile
202 lines (168 loc) · 4.9 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# for editing the collection
require 'mini_magick'
require 'open-uri'
require 'json'
# for tests
require 'jasmine'
load 'jasmine/tasks/jasmine.rake'
@raw_filepath = File.expand_path("./../data/cards_raw.json", __FILE__)
@dict_filepath = File.expand_path("./../data/cards_dictionary.json", __FILE__)
@collection_filepath = File.expand_path("./../data/collection.json", __FILE__)
@data_points = ["id", "text", "type", "rarity", "cost", "attack", "health", "race", "mechanics"]
@spinner = {
index: 0,
chars: ["/", "-", "\\"]
}
def spinner
i = (@spinner[:index] + 1) % @spinner[:chars].size
@spinner[:index] = i
@spinner[:chars][i]
end
def load_collection
print "Loading collection... "
infile = open(@collection_filepath)
collection = JSON.parse infile.read
infile.close
print "Done.\n"
return collection
end
def write_collection hash
print "Writing collection... "
open(@collection_filepath, "w") do |f|
f << JSON.pretty_generate(hash)
end
print "Done.\n"
end
def load_dictionary
print "Loading dictionary... "
infile = open(@dict_filepath)
dict = JSON.parse infile.read
infile.close
print "Done.\n"
return dict
end
def ask_user
STDIN.gets.strip
end
def ask_card_name dict
loop do
print "Which card would you like to modify in your collection? "
name = ask_user
return [name, dict[name]] if dict.assoc(name)
print "Couldn't find the card '#{name}'.\n"
end
end
def download_and_crop_image filename
url = "http://wow.zamimg.com/images/hearthstone/cards/enus/original/#{filename}"
path = File.expand_path("./../source/images/cards/#{filename}", __FILE__)
open(url) do |image|
File.open(path, "w") do |file|
file.puts image.read
end
end
image = MiniMagick::Image.open(path)
image.crop("100x100+100+120") #hardcodez ftw
image.write path
end
namespace :cards do
desc "Updates cards data from hearthstonejson.com"
task :update do
print "Updating cards_raw.json... "
url = "https://hearthstonejson.com/json/AllSets.json"
open(@raw_filepath, "w") do |f|
f << open(url).read
end
print "Done.\n"
Rake::Task["cards:dict"].invoke
end
desc "Creates dictionary of cards based on name"
task :dict do
print "Updating cards_dictionary.json... "
infile = open(@raw_filepath, "r")
raw_hash = JSON.parse infile.read
infile.close
working_hash = {}
raw_hash.each do |set_name, set|
set.each do |card|
if card["collectible"] and card["type"] != "Hero"
name = card.delete "name"
working_hash[name] = card # NOTE: dangerous
end
end
end
open(@dict_filepath, "w") do |f|
f << working_hash.to_json
end
print "Done.\n"
end
desc "Download collection cards from Hearthhead"
task :grab_images do
collection = load_collection
images = Dir.entries(File.expand_path("./../source/images/cards", __FILE__))
collection.each do |name, data|
id = data["id"]
print "\rDownloading collection card images... #{spinner}"
if data["amount"][0] + data["amount"][1] > 0
filename = "#{id}.png"
download_and_crop_image(filename) unless images.include? filename
end
end
print "\rDownloading collection card images... Done.\n"
end
end
namespace :collection do
desc "add or remove cards from collection"
task :edit do
dict = load_dictionary
collection = load_collection
loop do
card = ask_card_name dict
name = card[0]
if collection[name]
card_owned = collection[name]["amount"]
print "You have #{card_owned[0]} regular card(s) and #{card_owned[1]} golden " +
"cards currently in your collection.\n"
else
print "You don't have that card yet.\n"
collection[name] = {}
collection[name]["amount"] = [0, 0]
@data_points.each do |pt|
collection[name][pt] = card[1][pt]
end
end
print "How many regular cards do you have? "
amt_reg = ask_user.to_i
print "How many golden cards do you have? "
amt_gold = ask_user.to_i
collection[name]["amount"] = [amt_reg, amt_gold]
print "Add another card? (y/n) "
yes_or_no = ask_user.downcase
break if yes_or_no == 'n'
end
write_collection collection
Rake::Task["cards:grab_images"].invoke
end
desc "Rebuilds collection with new data points"
task :rebuild do
collection = load_collection
dict = load_dictionary
print "Rebuilding collection... "
collection.each do |name, data|
@data_points.each do |pt|
unless data.has_key? pt
data[pt] = dict[name][pt]
end
end
end
print "Done\n"
write_collection collection
end
end
task :travis do
["rake jasmine:ci"].each do |cmd|
puts "Starting to run #{cmd}..."
system("export DISPLAY=:99.0 && bundle exec #{cmd}")
raise "#{cmd} failed!" unless $?.exitstatus == 0
end
end
task default: [:travis]