@ -363,6 +363,7 @@ class MatchScheduler : ModelObject, Storable {
var rotationIndex = 0
var availableMatchs = flattenedMatches . filter ( { $0 . startDate = = nil } )
let courtsUnavailability = courtsUnavailability
var issueFound : Bool = false
flattenedMatches . filter { $0 . startDate != nil } . sorted ( by : \ . startDate ! ) . forEach { match in
if _startDate = = nil {
@ -388,7 +389,7 @@ class MatchScheduler : ModelObject, Storable {
var shouldStartAtDispatcherDate = rotationIndex > 0
while availableMatchs . count > 0 {
while availableMatchs . count > 0 && issueFound = = false {
freeCourtPerRotation [ rotationIndex ] = [ ]
let previousRotationSlots = slots . filter ( { $0 . rotationIndex = = rotationIndex - 1 } )
var rotationStartDate : Date = getNextStartDate ( fromPreviousRotationSlots : previousRotationSlots , includeBreakTime : false ) ? ? dispatcherStartDate
@ -431,6 +432,13 @@ class MatchScheduler : ModelObject, Storable {
rotationStartDate = rotationStartDate . addingTimeInterval ( - difference )
}
}
} else if let first = availableMatchs . first {
let duration = first . matchFormat . getEstimatedDuration ( additionalEstimationDuration )
let courtsUnavailable = courtsUnavailable ( startDate : rotationStartDate , duration : duration , courtsUnavailability : courtsUnavailability )
if courtsUnavailable = = numberOfCourtsAvailablePerRotation {
print ( " issue " )
issueFound = true
}
}
dispatchCourts ( availableCourts : numberOfCourtsAvailablePerRotation , courts : courts , availableMatchs : & availableMatchs , slots : & slots , rotationIndex : rotationIndex , rotationStartDate : rotationStartDate , freeCourtPerRotation : & freeCourtPerRotation , courtsUnavailability : courtsUnavailability )
@ -450,7 +458,7 @@ class MatchScheduler : ModelObject, Storable {
}
return MatchDispatcher ( timedMatches : slots , freeCourtPerRotation : freeCourtPerRotation , rotationCount : rotationIndex )
return MatchDispatcher ( timedMatches : slots , freeCourtPerRotation : freeCourtPerRotation , rotationCount : rotationIndex , issueFound : issueFound )
}
func dispatchCourts ( availableCourts : Int , courts : [ Int ] , availableMatchs : inout [ Match ] , slots : inout [ TimeMatch ] , rotationIndex : Int , rotationStartDate : Date , freeCourtPerRotation : inout [ Int : [ Int ] ] , courtsUnavailability : [ DateInterval ] ? ) {
@ -542,16 +550,18 @@ class MatchScheduler : ModelObject, Storable {
} . sorted ( by : \ . 1 ) . map { $0 . 0 }
}
let courtsUnavailable = courtsUnavailable ( at : minimumTargetedEndDate , courtsUnavailability : courtsUnavailability )
if let courtsUnavailable , courtsUnavailable = = availableCourts {
minimumTargetedEndDate . addTimeInterval ( 3600 )
if let first = availableMatchs . first {
let duration = first . matchFormat . getEstimatedDuration ( additionalEstimationDuration )
let courtsUnavailable = courtsUnavailable ( startDate : minimumTargetedEndDate , duration : duration , courtsUnavailability : courtsUnavailability )
if courtsUnavailable < availableCourts {
dispatchCourts ( availableCourts : availableCourts , courts : freeCourts , availableMatchs : & availableMatchs , slots : & slots , rotationIndex : rotationIndex , rotationStartDate : minimumTargetedEndDate , freeCourtPerRotation : & freeCourtPerRotation , courtsUnavailability : courtsUnavailability )
}
}
dispatchCourts ( availableCourts : availableCourts , courts : freeCourts , availableMatchs : & availableMatchs , slots : & slots , rotationIndex : rotationIndex , rotationStartDate : minimumTargetedEndDate , freeCourtPerRotation : & freeCourtPerRotation , courtsUnavailability : courtsUnavailability )
}
}
func updateBracketSchedule ( tournament : Tournament , fromRoundId roundId : String ? , fromMatchId matchId : String ? , startDate : Date ) {
@ discardableResult func updateBracketSchedule ( tournament : Tournament , fromRoundId roundId : String ? , fromMatchId matchId : String ? , startDate : Date ) -> Bool {
let upperRounds : [ Round ] = tournament . rounds ( )
let allMatches : [ Match ] = tournament . allMatches ( )
@ -643,6 +653,8 @@ class MatchScheduler : ModelObject, Storable {
} catch {
Logger . error ( error )
}
return roundDispatch . issueFound
}
@ -656,18 +668,6 @@ class MatchScheduler : ModelObject, Storable {
} . count
}
func courtsUnavailable ( at startDate : Date , courtsUnavailability : [ DateInterval ] ? ) -> Int ? {
guard let courtsUnavailability else { return nil }
let groupedBy = Dictionary ( grouping : courtsUnavailability , by : { $0 . courtIndex } )
let courts = groupedBy . keys
return courts . filter { courtIndex in
let courtLockedSchedule = courtsUnavailability . filter ( { $0 . courtIndex = = courtIndex } )
return courtLockedSchedule . anySatisfy ( { dateInterval in
dateInterval . range . contains ( startDate )
} )
} . count
}
func courtUnavailable ( courtIndex : Int , from startDate : Date , to endDate : Date , source : [ DateInterval ] ) -> Bool {
let courtLockedSchedule = source . filter ( { $0 . courtIndex = = courtIndex } )
return courtLockedSchedule . anySatisfy ( { dateInterval in
@ -676,9 +676,9 @@ class MatchScheduler : ModelObject, Storable {
} )
}
func updateSchedule ( tournament : Tournament ) {
func updateSchedule ( tournament : Tournament ) -> Bool {
let lastDate = updateGroupStageSchedule ( tournament : tournament )
updateBracketSchedule ( tournament : tournament , fromRoundId : nil , fromMatchId : nil , startDate : lastDate )
return updateBracketSchedule ( tournament : tournament , fromRoundId : nil , fromMatchId : nil , startDate : lastDate )
}
}
@ -714,6 +714,7 @@ struct MatchDispatcher {
let timedMatches : [ TimeMatch ]
let freeCourtPerRotation : [ Int : [ Int ] ]
let rotationCount : Int
let issueFound : Bool
}
extension Match {