diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 9084607..a2338b4 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -667,7 +667,8 @@ defer { setCourt(_courtIndex) } case .random: - if let _courtIndex = availableCourts().randomElement() { + let runningMatches: [Match] = DataStore.shared.runningMatches() + if let _courtIndex = availableCourts(runningMatches: runningMatches).randomElement() { setCourt(_courtIndex) } case .field(let _courtIndex): @@ -707,8 +708,8 @@ defer { return currentTournament()?.courtCount ?? 1 } - func courtIsAvailable(_ courtIndex: Int) -> Bool { - let courtUsed = currentTournament()?.courtUsed() ?? [] + func courtIsAvailable(_ courtIndex: Int, in runningMatches: [Match]) -> Bool { + let courtUsed = currentTournament()?.courtUsed(runningMatches: runningMatches) ?? [] return courtUsed.contains(courtIndex) == false } @@ -721,8 +722,8 @@ defer { return availableCourts } - func availableCourts() -> [Int] { - let courtUsed = currentTournament()?.courtUsed() ?? [] + func availableCourts(runningMatches: [Match]) -> [Int] { + let courtUsed = currentTournament()?.courtUsed(runningMatches: runningMatches) ?? [] return Set(allCourts().map { $0 }).subtracting(Set(courtUsed)).sorted() } @@ -1002,10 +1003,10 @@ defer { return " depuis " + startDate.timeElapsedString() } - func canBePlayedInSpecifiedCourt() -> Bool { + func canBePlayedInSpecifiedCourt(runningMatches: [Match]) -> Bool { guard let courtIndex else { return false } if expectedToBeRunning() { - return courtIsAvailable(courtIndex) + return courtIsAvailable(courtIndex, in: runningMatches) } else { return true } diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 0899530..132db89 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -513,7 +513,7 @@ final class Tournament : ModelObject, Storable { return URLs.main.url.appending(path: "tournament/\(id)").appending(path: pageLink.path) } - func courtUsed() -> [Int] { + func courtUsed(runningMatches: [Match]) -> [Int] { #if _DEBUGING_TIME //DEBUGING TIME let start = Date() defer { @@ -521,8 +521,6 @@ defer { print("func courtUsed()", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) } #endif - - let runningMatches: [Match] = DataStore.shared.runningMatches() return Set(runningMatches.compactMap { $0.courtIndex }).sorted() } diff --git a/PadelClub/Views/Match/MatchSummaryView.swift b/PadelClub/Views/Match/MatchSummaryView.swift index cdb7471..51942d3 100644 --- a/PadelClub/Views/Match/MatchSummaryView.swift +++ b/PadelClub/Views/Match/MatchSummaryView.swift @@ -22,7 +22,10 @@ struct MatchSummaryView: View { init(match: Match, title: String? = nil, updatedField: Int? = nil) { self.match = match self.updatedField = updatedField - let currentAvailableCourts = match.availableCourts() + + let runningMatches = DataStore.shared.runningMatches() + + let currentAvailableCourts = match.availableCourts(runningMatches: runningMatches) self.availableCourts = currentAvailableCourts if let groupStage = match.groupStageObject { @@ -40,9 +43,8 @@ struct MatchSummaryView: View { } else { self.courtName = nil } - let runningMatches = Tournament.runningMatches(match.currentTournament()?.allMatches() ?? []) self.estimatedStartDate = match.estimatedStartDate(availableCourts: currentAvailableCourts, runningMatches: runningMatches) - self.canBePlayedInSpecifiedCourt = match.canBePlayedInSpecifiedCourt() + self.canBePlayedInSpecifiedCourt = match.canBePlayedInSpecifiedCourt(runningMatches: runningMatches) } var spacing: CGFloat {