scheduler v1 ok

multistore
Razmig Sarkissian 2 years ago
parent 3732d636f7
commit 10df57a72e
  1. 22
      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..<availableCourts).map { $0 }
let courtsUsed = getNextEarliestAvailableDate(from: slots)
print(courtsUsed)
let freeCourts = courtsUsed.filter { (courtIndex, availableDate) in
availableDate <= minimumTargetedEndDate
}.sorted(by: \.1).map { $0.0 }
dispatchCourts(availableCourts: availableCourts, courts: freeCourts, availableMatchs: &availableMatchs, slots: &slots, rotationIndex: rotationIndex, rotationStartDate: minimumTargetedEndDate, freeCourtPerRotation: &freeCourtPerRotation)
}

Loading…
Cancel
Save