From 93036ecdd5c2eb69440e077e3c6f9f7797a6ba50 Mon Sep 17 00:00:00 2001 From: Raz Date: Tue, 15 Oct 2024 11:47:54 +0200 Subject: [PATCH] fix scheduler --- PadelClub.xcodeproj/project.pbxproj | 8 ++++---- PadelClub/Data/MatchScheduler.swift | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 0635e0e..74d334d 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -3158,7 +3158,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 6; + CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; @@ -3182,7 +3182,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0.21; + MARKETING_VERSION = 1.0.22; PRODUCT_BUNDLE_IDENTIFIER = app.padelclub; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -3203,7 +3203,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 6; + CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; DEVELOPMENT_TEAM = BQ3Y44M3Q6; @@ -3226,7 +3226,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0.21; + MARKETING_VERSION = 1.0.22; PRODUCT_BUNDLE_IDENTIFIER = app.padelclub; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/PadelClub/Data/MatchScheduler.swift b/PadelClub/Data/MatchScheduler.swift index 5121c1f..21f0936 100644 --- a/PadelClub/Data/MatchScheduler.swift +++ b/PadelClub/Data/MatchScheduler.swift @@ -393,7 +393,7 @@ final class MatchScheduler : ModelObject, Storable { print("Setting minimumTargetedEndDate to the earlier of \(minimumPossibleEndDate) and \(minimumTargetedEndDate)") minimumTargetedEndDate = min(minimumPossibleEndDate, minimumTargetedEndDate) } - print("Targeted start date is before the minimum possible end date, returning false.") + print("Targeted start date \(targetedStartDate) is before the minimum possible end date, returning false. \(minimumTargetedEndDate)") return false } } @@ -462,11 +462,20 @@ final class MatchScheduler : ModelObject, Storable { var freeCourtPerRotation = [Int: [Int]]() var courts = initialCourts ?? Array(courtsAvailable) var shouldStartAtDispatcherDate = rotationIndex > 0 - + var suitableDate: Date? + while !availableMatchs.isEmpty && !issueFound && rotationIndex < 50 { freeCourtPerRotation[rotationIndex] = [] let previousRotationSlots = slots.filter({ $0.rotationIndex == rotationIndex - 1 }) - var rotationStartDate: Date = getNextStartDate(fromPreviousRotationSlots: previousRotationSlots, includeBreakTime: false) ?? dispatcherStartDate + + var rotationStartDate: Date + if previousRotationSlots.isEmpty && rotationIndex > 0 { + let computedSuitableDate = slots.sorted(by: \.computedEndDateForSorting).last?.computedEndDateForSorting + print("Previous rotation was empty, find a suitable rotationStartDate \(suitableDate)") + rotationStartDate = suitableDate ?? computedSuitableDate ?? dispatcherStartDate + } else { + rotationStartDate = getNextStartDate(fromPreviousRotationSlots: previousRotationSlots, includeBreakTime: false) ?? dispatcherStartDate + } if shouldStartAtDispatcherDate { rotationStartDate = dispatcherStartDate @@ -539,7 +548,7 @@ final class MatchScheduler : ModelObject, Storable { } // Dispatch courts and schedule matches - dispatchCourts(courts: courts, availableMatchs: &availableMatchs, slots: &slots, rotationIndex: rotationIndex, rotationStartDate: rotationStartDate, freeCourtPerRotation: &freeCourtPerRotation, courtsUnavailability: courtsUnavailability) + suitableDate = dispatchCourts(courts: courts, availableMatchs: &availableMatchs, slots: &slots, rotationIndex: rotationIndex, rotationStartDate: rotationStartDate, freeCourtPerRotation: &freeCourtPerRotation, courtsUnavailability: courtsUnavailability) rotationIndex += 1 } @@ -561,7 +570,7 @@ final class MatchScheduler : ModelObject, Storable { return MatchDispatcher(timedMatches: slots, freeCourtPerRotation: freeCourtPerRotation, rotationCount: rotationIndex, issueFound: issueFound) } - func dispatchCourts(courts: [Int], availableMatchs: inout [Match], slots: inout [TimeMatch], rotationIndex: Int, rotationStartDate: Date, freeCourtPerRotation: inout [Int: [Int]], courtsUnavailability: [DateInterval]?) { + func dispatchCourts(courts: [Int], availableMatchs: inout [Match], slots: inout [TimeMatch], rotationIndex: Int, rotationStartDate: Date, freeCourtPerRotation: inout [Int: [Int]], courtsUnavailability: [DateInterval]?) -> Date { var matchPerRound = [String: Int]() var minimumTargetedEndDate = rotationStartDate @@ -644,6 +653,8 @@ final class MatchScheduler : ModelObject, Storable { if freeCourtPerRotation[rotationIndex]?.count == courtsAvailable.count { print("All courts in rotation \(rotationIndex) are free") } + + return minimumTargetedEndDate } @discardableResult func updateBracketSchedule(tournament: Tournament, fromRoundId roundId: String?, fromMatchId matchId: String?, startDate: Date) -> Bool { @@ -839,6 +850,10 @@ struct TimeMatch { let minutesToAdd = Double(durationLeft + (includeBreakTime ? minimumBreakTime : 0)) return startDate.addingTimeInterval(minutesToAdd * 60.0) } + + var computedEndDateForSorting: Date { + estimatedEndDate(includeBreakTime: false) + } } struct GroupStageMatchDispatcher {