Class: SportsManager::TournamentGenerator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sports_manager/tournament_generator.rb

Overview

Public:Tournament Solver

         _TEAM A______
MATCH 1               \_TEAM B______
         _TEAM B______/             \
                          MATCH 5    \_TEAM B___
         _TEAM C______               /          \
MATCH 2               \_TEAM C______/            \
         _TEAM D______/                           \
                                       MATCH 7     \__TEAM E__
         _TEAM E______                             /
MATCH 3               \_TEAM E______              /
         _TEAM F______/             \            /
                          MATCH 6    \_TEAM E___/
         _TEAM G______               /
MATCH 4               \_TEAM G______/
         _TEAM H______/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: :cli, timeout: nil) ⇒ TournamentGenerator

Returns a new instance of TournamentGenerator.



48
49
50
51
52
53
54
55
56
57
# File 'lib/sports_manager/tournament_generator.rb', line 48

def initialize(format: :cli, timeout: nil)
  @format = format
  @timeout = timeout
  @days = {}
  @subscriptions = {}
  @matches = {}
  @ordering = Algorithms::Ordering::MultipleMatchesParticipant
  @filtering = Algorithms::Filtering::NoOverlap
  @lookahead = CSP::Algorithms::Lookahead::Ac3
end

Instance Attribute Details

#courtsObject

Returns the value of attribute courts.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def courts
  @courts
end

#cspObject (readonly)

Returns the value of attribute csp.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def csp
  @csp
end

#daysObject

Returns the value of attribute days.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def days
  @days
end

#domainsObject (readonly)

Returns the value of attribute domains.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def domains
  @domains
end

#filteringObject (readonly)

Returns the value of attribute filtering.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def filtering
  @filtering
end

#formatObject (readonly)

Returns the value of attribute format.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def format
  @format
end

#game_lengthObject

Returns the value of attribute game_length.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def game_length
  @game_length
end

#lookaheadObject (readonly)

Returns the value of attribute lookahead.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def lookahead
  @lookahead
end

#matchesObject

Returns the value of attribute matches.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def matches
  @matches
end

#orderingObject (readonly)

Returns the value of attribute ordering.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def ordering
  @ordering
end

#rest_breakObject

Returns the value of attribute rest_break.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def rest_break
  @rest_break
end

#single_day_matchesObject

Returns the value of attribute single_day_matches.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def single_day_matches
  @single_day_matches
end

#subscriptionsObject

Returns the value of attribute subscriptions.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def subscriptions
  @subscriptions
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def timeout
  @timeout
end

#tournamentObject (readonly)

Returns the value of attribute tournament.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def tournament
  @tournament
end

#tournament_typeObject

Returns the value of attribute tournament_type.



29
30
31
# File 'lib/sports_manager/tournament_generator.rb', line 29

def tournament_type
  @tournament_type
end

#variablesObject (readonly)

Returns the value of attribute variables.



26
27
28
# File 'lib/sports_manager/tournament_generator.rb', line 26

def variables
  @variables
end

Class Method Details

.example(type = :simple, format: :cli) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sports_manager/tournament_generator.rb', line 34

def self.example(type = :simple, format: :cli)
  params = Helper.public_send(type)

  new(format: format)
    .add_days(params[:when])
    .add_subscriptions(params[:subscriptions])
    .add_matches(params[:matches])
    .add_courts(params[:courts])
    .add_game_length(params[:game_length])
    .add_rest_break(params[:rest_break])
    .enable_single_day_matches(params[:single_day_matches])
    .call
end

Instance Method Details

#add_courts(number_of_courts) ⇒ Object



107
108
109
110
# File 'lib/sports_manager/tournament_generator.rb', line 107

def add_courts(number_of_courts)
  @courts = number_of_courts
  self
end

#add_day(day, start, finish) ⇒ Object



59
60
61
62
# File 'lib/sports_manager/tournament_generator.rb', line 59

def add_day(day, start, finish)
  days[day.to_sym] = { start: start, end: finish }
  self
end

#add_days(days) ⇒ Object



64
65
66
67
68
69
# File 'lib/sports_manager/tournament_generator.rb', line 64

def add_days(days)
  days.each do |day, time_hash|
    add_day(day, time_hash[:start], time_hash[:end])
  end
  self
end

#add_game_length(game_length) ⇒ Object



112
113
114
115
# File 'lib/sports_manager/tournament_generator.rb', line 112

def add_game_length(game_length)
  @game_length = game_length
  self
end

#add_match(category, match) ⇒ Object



89
90
91
92
93
# File 'lib/sports_manager/tournament_generator.rb', line 89

def add_match(category, match)
  matches[category] ||= []
  matches[category] << match
  self
end

#add_matches(matches) ⇒ Object



100
101
102
103
104
105
# File 'lib/sports_manager/tournament_generator.rb', line 100

def add_matches(matches)
  matches.each do |category, matches_per_category|
    add_matches_per_category(category, matches_per_category)
  end
  self
end

#add_matches_per_category(category, matches) ⇒ Object



95
96
97
98
# File 'lib/sports_manager/tournament_generator.rb', line 95

def add_matches_per_category(category, matches)
  matches.each { |match| add_match(category, match) }
  self
end

#add_rest_break(rest_break) ⇒ Object



117
118
119
120
# File 'lib/sports_manager/tournament_generator.rb', line 117

def add_rest_break(rest_break)
  @rest_break = rest_break
  self
end

#add_subscription(category, subscription) ⇒ Object



71
72
73
74
75
# File 'lib/sports_manager/tournament_generator.rb', line 71

def add_subscription(category, subscription)
  subscriptions[category] ||= []
  subscriptions[category] << subscription
  self
end

#add_subscriptions(subscriptions) ⇒ Object



82
83
84
85
86
87
# File 'lib/sports_manager/tournament_generator.rb', line 82

def add_subscriptions(subscriptions)
  subscriptions.each do |category, subscriptions_per_category|
    add_subscriptions_per_category(category, subscriptions_per_category)
  end
  self
end

#add_subscriptions_per_category(category, subscriptions) ⇒ Object



77
78
79
80
# File 'lib/sports_manager/tournament_generator.rb', line 77

def add_subscriptions_per_category(category, subscriptions)
  subscriptions.each { |subscription| add_subscription(category, subscription) }
  self
end

#callObject



132
133
134
135
136
137
138
139
140
# File 'lib/sports_manager/tournament_generator.rb', line 132

def call
  setup

  solutions = Timeout.timeout(timeout) { csp.solve }

  TournamentSolution
    .new(tournament: tournament, solutions: solutions)
    .tap(&method(:print_solution))
end

#enable_single_day_matches(single_day_matches) ⇒ Object



122
123
124
125
# File 'lib/sports_manager/tournament_generator.rb', line 122

def enable_single_day_matches(single_day_matches)
  @single_day_matches = single_day_matches
  self
end

#single_elimination_algorithmObject



127
128
129
130
# File 'lib/sports_manager/tournament_generator.rb', line 127

def single_elimination_algorithm
  @tournament_type = Matches::Algorithms::SingleEliminationAlgorithm
  self
end