/
app.rb
340 lines (319 loc) · 11 KB
/
app.rb
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
require 'boot.rb'
require 'faster_csv'
require 'highline/import'
require 'buyer'
require 'entry'
require 'game'
require 'season'
say "Welcome to <%= color('divvyr', RED) %>."
@season = Season.new
loop do
choose do |menu|
menu.index = :letter
menu.index_suffix = ') '
menu.prompt = "Choose an option: "
menu.choice 'load season' do
@season = File.open(".divvyr_season.yaml", "r") {|file| YAML::load(file)}
say "Loaded season with #{@season.games.size} games and #{@season.buyers.size} buyers."
end
menu.choice 'save season' do
File.open(".divvyr_season.yaml", "w") {|file| YAML.dump(@season, file)}
say "Season saved."
end
menu.choice 'import games' do
path = ask("Where's your spreadsheet: ")
FasterCSV.foreach(path, :headers => true) do |line|
id = line.fields[0]
data = []
for datum_index in 1..(line.fields.size - 1) do
data << line.fields[datum_index]
end
@season.games << Game.new(id, data)
end
say "Import complete (season now has #{@season.games.size} games)"
end
if !@season.games.empty?
menu.choice 'view games' do
say("Displaying Games")
@season.list_games
end
menu.choice 'delete game' do
selected_game = nil
choose do |game|
game.index = :number
game.index_suffix = ') '
game.prompt = 'Which game would you like to delete?'
game.choice 'cancel' do
selected_game = nil
end
@season.games.each do |season_game|
game.choice season_game do
selected_game = season_game
end
end
end
if !selected_game.nil?
@season.games.delete(selected_game)
end
end
end
menu.choice 'add a buyer' do
name = ask("Buyer name: ")
num_games = ask("Number of games: ", Integer)
@season.buyers << Buyer.new(name, num_games)
end
if !@season.buyers.empty?
menu.choice 'list buyers' do
say "Listing buyers"
@season.list_buyers
end
menu.choice 'edit a buyer' do
selected_buyer = nil
choose do |buyer|
buyer.index = :number
buyer.index_suffix = ') '
buyer.prompt = 'Which buyer would you like to edit?'
buyer.choice 'cancel' do
selected_buyer = nil
end
@season.buyers.each do |season_buyer|
buyer.choice season_buyer do
selected_buyer = season_buyer
end
end
end
if !selected_buyer.nil?
puts "Editing #{selected_buyer}..."
new_name = ask("New name: ")
new_num_games = ask("New number of games: ")
selected_buyer.name = new_name unless new_name.length < 1
selected_buyer.number_of_games = new_num_games unless new_num_games.length < 1
end
end
menu.choice 'remove a buyer' do
selected_buyer = nil
choose do |buyer|
buyer.index = :number
buyer.index_suffix = ') '
buyer.prompt = 'Which buyer would you like to remove?'
buyer.choice 'cancel' do
selected_game = nil
end
@season.buyers.each do |season_buyer|
buyer.choice season_buyer do
selected_buyer = season_buyer
end
end
end
if !selected_buyer.nil?
@season.buyers.delete(selected_buyer)
end
end
menu.choice 'input buyer game preferences' do
selected_buyer = nil
choose do |buyer|
buyer.index = :number
buyer.index_suffix = ') '
buyer.prompt = 'Buyer to enter preferences for: '
buyer.choice 'cancel' do
selected_buyer = nil
end
@season.buyers.each do |season_buyer|
buyer.choice season_buyer do
selected_buyer = season_buyer
end
end
end
if !selected_buyer.nil?
selected_game = 1
while selected_buyer.games_priority.size < @season.games.size && selected_game != nil do
selected_game = nil
choose do |game|
game.index = :letter
game.index_suffix = ') '
game.prompt = "Enter choice ##{selected_buyer.games_priority.size + 1}: "
game.choice 'finish later' do
selected_game = nil
end
@season.list_games_without_buyer_preference(selected_buyer).each do |game_remaining|
game.choice game_remaining do
selected_game = game_remaining
end
end
end
if !selected_game.nil?
selected_buyer.games_priority << selected_game.identifier
end
end
end
end
menu.choice 'view buyer game preferences' do
selected_buyer = 1
while !selected_buyer.nil?
choose do |buyer|
buyer.index = :letter
buyer.index_suffix = ') '
buyer.prompt = "Player's game selections to view: "
buyer.choice 'go back' do
selected_buyer = nil
end
@season.buyers.each do |season_buyer|
buyer.choice season_buyer do
selected_buyer = season_buyer
end
end
end
if !selected_buyer.nil?
puts "Displaying choices for #{selected_buyer}:"
for index in 1..@season.games.size do
puts "#{index}. #{@season.game_by_identifier(selected_buyer.games_priority[index - 1])}"
end
end
end
end
end
if !@season.games.empty? && !@season.buyers.empty?
menu.choice 'prepare draft' do
@season.buyers.each do |buyer|
puts "#{buyer.name} gets #{@season.num_draft_entries_for(buyer, 5)} entr#{@season.num_draft_entries_for(buyer, 5) == 1 ? 'y' : 'ies'}"
end
@season.generate_entries
puts "Entries are as follows:"
counter = 1
@season.entries.each do |e|
puts "#{counter}. #{e}"
counter += 1
end
puts "Randomized entries:"
@season.randomize_entries
counter = 1
@season.entries.each do |e|
puts "#{counter}. #{e}"
counter += 1
end
end
if !@season.entries.empty?
menu.choice 'view draft entries in order' do
counter = 1
@season.entries.each do |e|
puts "#{counter}. #{e}"
counter += 1
end
end
menu.choice 'edit draft entries' do
selected_entry = 1
while !selected_entry.nil?
choose do |draft_entry|
draft_entry.index = :letter
draft_entry.index_suffix = ') '
draft_entry.prompt = 'Which draft entry to modfy?: '
draft_entry.choice 'go back' do
selected_entry = nil
end
@season.entries.each do |entry|
draft_entry.choice entry do
selected_entry = entry
end
end
end
if !selected_entry.nil?
length = selected_entry.round_data.size
puts "Current entry: #{selected_entry}"
new_entry = []
for i in 1..length do
new_entry.push(ask("Entry value at position #{i}: "))
end
selected_entry.round_data = new_entry
end
end
end
menu.choice 'random order draft entries' do
puts "Randomized entries:"
@season.randomize_entries
counter = 1
@season.entries.each do |e|
puts "#{counter}. #{e}"
counter += 1
end
end
menu.choice 'manual order draft entries' do
new_entries = []
while @season.entries.size > 0
choose do |next_entry|
next_entry.index = :letter
next_entry.index_suffix = ') '
next_entry.prompt = 'Choose next entry: '
@season.entries.each do |entry|
next_entry.choice entry do
new_entries.push(entry)
@season.entries.delete(entry)
end
end
end
end
@season.entries = new_entries
end
menu.choice 'validate draft entries' do
puts "Season has #{@season.games.size} games"
count = 0
@season.entries.each do |entry|
entry.round_data.each {|l| count +=1 if l == 'P'}
end
puts "Entries will draft #{count} games"
if @season.games.size == count
say "Current state of entries is <%= color('valid', GREEN) %>."
else
say "Current state of entries is <%= color('invalid', RED) %>."
end
end
menu.choice 'run draft' do
say "Running draft..."
num_rounds = @season.entries.first.round_data.size
remaining_game_ids = []
@season.games.each {|g| remaining_game_ids << g.identifier}
for round_index in 1..num_rounds do
say "Beginning draft round #{round_index}"
counter = 1
if round_index % 2 == 1
@season.entries.each do |entry|
buyer = @season.buyer_by_name(entry.buyer_name)
if entry.round_data[round_index - 1] == 'P'
buyer.games_priority.each do |game_id|
if !remaining_game_ids.index(game_id).nil?
say "Round #{round_index}, pick #{counter} belongs to #{buyer.name}: #{@season.game_by_identifier(game_id)}\n"
remaining_game_ids.delete(game_id)
break
end
end
counter += 1
else
say "Skipping #{buyer}; no pick this round."
end
end
else
@season.entries.reverse_each do |entry|
buyer = @season.buyer_by_name(entry.buyer_name)
if entry.round_data[round_index - 1] == 'P'
buyer.games_priority.each do |game_id|
if !remaining_game_ids.index(game_id).nil?
say "Round #{round_index}, pick #{counter} belongs to #{buyer.name}: #{@season.game_by_identifier(game_id)}\n"
remaining_game_ids.delete(game_id)
break
end
end
counter += 1
else
say "Skipping #{buyer}; no pick this round."
end
end
end
end
end
end
end
menu.choice 'exit' do
File.open(".divvyr_season.yaml", "w") {|file| YAML.dump(@season, file)} if agree("Save before exit?: ")
exit
end
end
end