ucl_last_16_draw 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ucl_last_16_draw.rb +112 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 63e57136b696e3f75335b6517418800183194d957cf1ee67edbc569923960b8f
|
4
|
+
data.tar.gz: ab7d4b4fbd5b9c03eda627576b6d6b9515238ea330e2b62ad2297ee29a1a38fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7820d370041c73a9824e826e4a3dbbae6da17d14142c3ea05e0d156f6fbeff73256b5e8297d2386b4cc81761f7fbb43874d0f1f78400a636a447e3da9e71ef7b
|
7
|
+
data.tar.gz: a565c3956a4828dc348293777bf01ee791d37f7842c7074161ca34c5f0307e3075616c6793ea5d449cfc444cb81bbe45578c180250e31213986bd37c89a9dc4c
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module UCLLast16Draw
|
2
|
+
class Participants
|
3
|
+
def initialize(participants = {})
|
4
|
+
check_participants(participants)
|
5
|
+
|
6
|
+
@group_winners = participants[:winners] || []
|
7
|
+
@runners_up = participants[:runners_up] || []
|
8
|
+
|
9
|
+
check_hash_key
|
10
|
+
check_participants_number
|
11
|
+
|
12
|
+
@draw_header_label = "UEFA Champions League Sample Draw (last 16 Round)"
|
13
|
+
@draw_header_width = 90
|
14
|
+
@max_center_position = 40
|
15
|
+
@draw_result = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate_draw
|
19
|
+
winners = @group_winners.clone
|
20
|
+
loop_count = 0
|
21
|
+
|
22
|
+
while @draw_result.empty?
|
23
|
+
loop_count += 1
|
24
|
+
|
25
|
+
if loop_count > 22 # rude assusmtion number too many attempt for creating a draw.
|
26
|
+
abort("Please check your participants! It doesn't seem possible.")
|
27
|
+
end
|
28
|
+
|
29
|
+
available_runner_up = @runners_up.clone
|
30
|
+
|
31
|
+
winners.shuffle.each do |item|
|
32
|
+
candidate = opponent_candidate(available_runner_up, item).sample
|
33
|
+
|
34
|
+
if candidate.nil?
|
35
|
+
@draw_result = []
|
36
|
+
break
|
37
|
+
end
|
38
|
+
|
39
|
+
@draw_result.push([item, candidate])
|
40
|
+
available_runner_up.delete(candidate)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@draw_result
|
45
|
+
end
|
46
|
+
|
47
|
+
def print_draw
|
48
|
+
abort("Please doing 'generate_draw' first!") if @draw_result.empty?
|
49
|
+
|
50
|
+
format_output(@draw_result)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def check_participants(participants)
|
56
|
+
abort("Please fill group winners.") unless participants.key?(:winners)
|
57
|
+
abort("Please fill group runners up.") unless participants.key?(:runners_up)
|
58
|
+
end
|
59
|
+
|
60
|
+
def check_participants_number
|
61
|
+
abort("Group winners number must be 8 teams.") unless @group_winners.length == 8
|
62
|
+
abort("Runners up number must be 8 teams.") unless @runners_up.length == 8
|
63
|
+
end
|
64
|
+
|
65
|
+
def check_hash_key
|
66
|
+
keys = [:group, :name, :country]
|
67
|
+
all_team = convert_string_key_into_symbol(@group_winners + @runners_up)
|
68
|
+
result = all_team.select { |x| x.keys.sort != keys.sort }
|
69
|
+
|
70
|
+
abort("Every team must have 'group', 'name', and 'country' value.") if result.length > 0
|
71
|
+
end
|
72
|
+
|
73
|
+
def opponent_candidate(participants, candidate)
|
74
|
+
participants.select{ |x| x[:country] != candidate[:country] && x[:group] != candidate[:group]}
|
75
|
+
end
|
76
|
+
|
77
|
+
def header(title, area_width)
|
78
|
+
header = "\n\t" + title.center(area_width)
|
79
|
+
header += "\n\t" + ("_" * area_width).center(area_width) + "\n\n"
|
80
|
+
|
81
|
+
header
|
82
|
+
end
|
83
|
+
|
84
|
+
def format_output(draw)
|
85
|
+
text = header(@draw_header_label, @draw_header_width)
|
86
|
+
|
87
|
+
draw.each do |x|
|
88
|
+
winner = x.first
|
89
|
+
runner_up = x.last
|
90
|
+
winner_text = "#{winner[:name]} (#{winner[:country]})/Group #{winner[:group]}"
|
91
|
+
|
92
|
+
text += "\t".ljust(@max_center_position-winner_text.length) + winner_text
|
93
|
+
text += " vs "
|
94
|
+
text += "#{runner_up[:name]} (#{runner_up[:country]})/Group #{runner_up[:group]}\n\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
text
|
98
|
+
end
|
99
|
+
|
100
|
+
def convert_string_key_into_symbol(previous_hash)
|
101
|
+
new_array = Array.new
|
102
|
+
|
103
|
+
previous_hash.each do |item|
|
104
|
+
new_hash = Hash.new
|
105
|
+
item.each { |k, v| new_hash[k.to_sym] = v }
|
106
|
+
new_array.push(new_hash)
|
107
|
+
end
|
108
|
+
|
109
|
+
new_array
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ucl_last_16_draw
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rido Atmanto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem to help randomize UCL match drawing
|
14
|
+
email: ridoatmanto@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/ucl_last_16_draw.rb
|
20
|
+
homepage: https://rubygems.org/gems/ucl_last_16_draw
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.7.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Doing UEFA Champions League last 16 draw with ruby gem
|
44
|
+
test_files: []
|