Class: SportsManager::Constraints::MatchConstraint

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

Overview

Public: Restrict match to be set only after the previous matches it depends on are scheduled.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_match:, matches:) ⇒ MatchConstraint



22
23
24
25
26
# File 'lib/sports_manager/constraints/match_constraint.rb', line 22

def initialize(target_match:, matches:)
  super([target_match] + matches)
  @target_match = target_match
  @matches = matches
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



8
9
10
# File 'lib/sports_manager/constraints/match_constraint.rb', line 8

def matches
  @matches
end

#target_matchObject (readonly)

Returns the value of attribute target_match.



8
9
10
# File 'lib/sports_manager/constraints/match_constraint.rb', line 8

def target_match
  @target_match
end

Class Method Details

.for_tournament(tournament:, csp:) ⇒ Object



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

def self.for_tournament(tournament:, csp:)
  tournament.matches.each do |(_category, matches)|
    matches.select(&:previous_matches?).each do |match|
      match.previous_matches.each do |previous_match|
        csp.add_constraint(
          new(target_match: match, matches: [previous_match])
        )
      end
    end
  end
end

Instance Method Details

#satisfies?(assignment) ⇒ Boolean



28
29
30
31
32
33
34
35
36
# File 'lib/sports_manager/constraints/match_constraint.rb', line 28

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

  target_time = assignment[target_match]

  matches
    .map { |match| assignment[match] }
    .all? { |time| time < target_time }
end