@ -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 ] ? ) {
@ -463,7 +471,7 @@ class MatchScheduler : ModelObject, Storable {
let roundObject = match . roundObject !
let courtsUnavailable = courtsUnavailable ( startDate : rotationStartDate , duration : match . matchFormat . getEstimatedDuration ( additionalEstimationDuration ) , courtsUnavailability : courtsUnavailability )
print ( " courtsUnavailable \( courtsUnavailable ) " )
if courtIndex >= availableCourts - courtsUnavailable {
if courtPosition >= availableCourts - courtsUnavailable {
return false
}
@ -542,11 +550,18 @@ class MatchScheduler : ModelObject, Storable {
} . sorted ( by : \ . 1 ) . map { $0 . 0 }
}
dispatchCourts ( availableCourts : availableCourts , courts : freeCourts , availableMatchs : & availableMatchs , slots : & slots , rotationIndex : rotationIndex , rotationStartDate : minimumTargetedEndDate , freeCourtPerRotation : & freeCourtPerRotation , courtsUnavailability : courtsUnavailability )
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 )
}
}
}
}
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 ( )
@ -638,6 +653,8 @@ class MatchScheduler : ModelObject, Storable {
} catch {
Logger . error ( error )
}
return roundDispatch . issueFound
}
@ -650,17 +667,18 @@ class MatchScheduler : ModelObject, Storable {
courtUnavailable ( courtIndex : $0 , from : startDate , to : endDate , source : courtsUnavailability )
} . 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
dateInterval . isDateInside ( startDate ) || dateInterval . isDateInside ( endDate )
let range = startDate . . < endDate
return dateInterval . range . overlaps ( range )
} )
}
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 )
}
}
@ -696,6 +714,7 @@ struct MatchDispatcher {
let timedMatches : [ TimeMatch ]
let freeCourtPerRotation : [ Int : [ Int ] ]
let rotationCount : Int
let issueFound : Bool
}
extension Match {