Sha256: 562a85ffa021f0cbd3e305ba0b94f42c14628df86db6c53139b10761eef5ea1d

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module SportsManager
  module Constraints
    # Public: Restrict match to be set only after the previous matches
    # it depends on are scheduled.
    class MatchConstraint < ::CSP::Constraint
      attr_reader :target_match, :matches

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

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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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