From 10df57a72ee07326721ca14349b1e8bd3106b2e9 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Sat, 13 Apr 2024 09:25:44 +0200 Subject: [PATCH] scheduler v1 ok --- PadelClub/ViewModel/MatchScheduler.swift | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/PadelClub/ViewModel/MatchScheduler.swift b/PadelClub/ViewModel/MatchScheduler.swift index 61a763c..33eaa61 100644 --- a/PadelClub/ViewModel/MatchScheduler.swift +++ b/PadelClub/ViewModel/MatchScheduler.swift @@ -193,6 +193,22 @@ class MatchScheduler { slots.map { $0.estimatedEndDate(includeBreakTime: includeBreakTime) }.min() } + func getNextEarliestAvailableDate(from slots: [TimeMatch]) -> [(Int, Date)] { + let byCourt = Dictionary(grouping: slots, by: { $0.courtIndex }) + return (byCourt.keys.flatMap { courtIndex in + let matchesByCourt = byCourt[courtIndex]?.sorted(by: \.startDate) + let lastMatch = matchesByCourt?.last + var results = [(Int, Date)]() + if let courtFreeDate = lastMatch?.estimatedEndDate(includeBreakTime: false) { + results.append((courtIndex, courtFreeDate)) + } + return results + } + ) + } + + + func roundDispatcher(numberOfCourtsAvailablePerRotation: Int, flattenedMatches: [Match], randomizeCourts: Bool, initialOccupiedCourt: Int = 0, dispatcherStartDate: Date) -> MatchDispatcher { var slots = [TimeMatch]() @@ -327,9 +343,13 @@ class MatchScheduler { if freeCourtPerRotation[rotationIndex]!.count == availableCourts { freeCourtPerRotation[rotationIndex] = [] - let freeCourts = (0..