Class: SportsManager::Constraints::NoOverlappingConstraint

Inherits:
CSP::Constraint
  • Object
show all
Defined in:
lib/sports_manager/constraints/no_overlapping_constraint.rb

Constant Summary collapse

IN_PAIRS =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matches:, match_time:) ⇒ NoOverlappingConstraint

Returns a new instance of NoOverlappingConstraint.



21
22
23
24
25
# File 'lib/sports_manager/constraints/no_overlapping_constraint.rb', line 21

def initialize(matches:, match_time:)
  super(matches)
  @matches = matches
  @match_time = match_time
end

Instance Attribute Details

#match_timeObject (readonly)

Returns the value of attribute match_time.



6
7
8
# File 'lib/sports_manager/constraints/no_overlapping_constraint.rb', line 6

def match_time
  @match_time
end

#matchesObject (readonly)

Returns the value of attribute matches.



6
7
8
# File 'lib/sports_manager/constraints/no_overlapping_constraint.rb', line 6

def matches
  @matches
end

Class Method Details

.for_tournament(tournament:, csp:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/sports_manager/constraints/no_overlapping_constraint.rb', line 10

def self.for_tournament(tournament:, csp:)
  matches = tournament.matches.values.flatten
  match_time = tournament.match_time

  matches.combination(IN_PAIRS) do |match1, match2|
    csp.add_constraint(
      new(matches: [match1, match2], match_time: match_time)
    )
  end
end

Instance Method Details

#satisfies?(assignment) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/sports_manager/constraints/no_overlapping_constraint.rb', line 27

def satisfies?(assignment)
  return true unless variables.all? { |variable| assignment.key?(variable) }

  variables
    .map { |variable| assignment[variable] }
    .group_by(&:court)
    .none? { |_, timeslots| timeslots_overlap?(timeslots: timeslots) }
end