optimize running matches gathering

paca_championship
Raz 12 months ago
parent e852999739
commit 16de4ee012
  1. 15
      PadelClub/Data/Match.swift
  2. 4
      PadelClub/Data/Tournament.swift
  3. 8
      PadelClub/Views/Match/MatchSummaryView.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
}

@ -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()
}

@ -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 {

Loading…
Cancel
Save