Class: SportsManager::Constraints::NextRoundConstraint

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

Constant Summary collapse

MINUTE =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_match:, matches:, match_time:, break_time:) ⇒ NextRoundConstraint

Returns a new instance of NextRoundConstraint.



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

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

Instance Attribute Details

#break_timeObject (readonly)

Returns the value of attribute break_time.



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

def break_time
  @break_time
end

#match_timeObject (readonly)

Returns the value of attribute match_time.



6
7
8
# File 'lib/sports_manager/constraints/next_round_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/next_round_constraint.rb', line 6

def matches
  @matches
end

#minimum_match_gapObject (readonly)

Returns the value of attribute minimum_match_gap.



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

def minimum_match_gap
  @minimum_match_gap
end

#target_matchObject (readonly)

Returns the value of attribute target_match.



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

def target_match
  @target_match
end

Class Method Details

.for_tournament(tournament:, csp:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sports_manager/constraints/next_round_constraint.rb', line 11

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

Instance Method Details

#satisfies?(assignment) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sports_manager/constraints/next_round_constraint.rb', line 36

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

  match_time = assignment[target_match].slot
  matches_timeslots = assignment.slice(*matches).values.map(&:slot)

  matches_timeslots.all? do |timeslot|
    diff_in_minutes = (timeslot - match_time).abs / MINUTE

    diff_in_minutes >= minimum_match_gap
  end
end