Sha256: e9c23cf838b2866f8b82f3b9ca741406cc8eb4e115286654c444875acb1c5d3a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module SportsManager
  module Constraints
    class MultiCategoryConstraint < ::CSP::Constraint
      attr_reader :target_participant, :matches,
                  :match_time, :break_time, :minimum_match_gap

      MINUTE = 60

      def self.for_tournament(tournament:, csp:)
        tournament.multi_tournament_participants.each do |participant|
          matches = tournament.find_participant_matches(participant)

          constraint = new(
            target_participant: participant,
            matches: matches,
            match_time: tournament.match_time,
            break_time: tournament.break_time
          )

          csp.add_constraint(constraint)
        end
      end

      def initialize(target_participant:, matches:, match_time:, break_time:)
        super(matches)
        @target_participant = target_participant
        @matches = matches
        @match_time = match_time
        @break_time = break_time
        @minimum_match_gap = match_time + break_time
      end

      # TODO: See if needs review in case of solution of split timeslots under match_time
      def satisfies?(assignment)
        return true unless variables.all? { |variable| assignment.key?(variable) }

        timeslots = assignment.slice(*variables).values.map(&:slot)

        timeslots.combination(2).all? do |timeslot1, timeslot2|
          diff_in_minutes = (timeslot1 - timeslot2).abs / MINUTE

          diff_in_minutes >= minimum_match_gap
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sports-manager-0.0.1 lib/sports_manager/constraints/multi_category_constraint.rb