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) setCourt(_courtIndex)
} }
case .random: case .random:
if let _courtIndex = availableCourts().randomElement() { let runningMatches: [Match] = DataStore.shared.runningMatches()
if let _courtIndex = availableCourts(runningMatches: runningMatches).randomElement() {
setCourt(_courtIndex) setCourt(_courtIndex)
} }
case .field(let _courtIndex): case .field(let _courtIndex):
@ -707,8 +708,8 @@ defer {
return currentTournament()?.courtCount ?? 1 return currentTournament()?.courtCount ?? 1
} }
func courtIsAvailable(_ courtIndex: Int) -> Bool { func courtIsAvailable(_ courtIndex: Int, in runningMatches: [Match]) -> Bool {
let courtUsed = currentTournament()?.courtUsed() ?? [] let courtUsed = currentTournament()?.courtUsed(runningMatches: runningMatches) ?? []
return courtUsed.contains(courtIndex) == false return courtUsed.contains(courtIndex) == false
} }
@ -721,8 +722,8 @@ defer {
return availableCourts return availableCourts
} }
func availableCourts() -> [Int] { func availableCourts(runningMatches: [Match]) -> [Int] {
let courtUsed = currentTournament()?.courtUsed() ?? [] let courtUsed = currentTournament()?.courtUsed(runningMatches: runningMatches) ?? []
return Set(allCourts().map { $0 }).subtracting(Set(courtUsed)).sorted() return Set(allCourts().map { $0 }).subtracting(Set(courtUsed)).sorted()
} }
@ -1002,10 +1003,10 @@ defer {
return " depuis " + startDate.timeElapsedString() return " depuis " + startDate.timeElapsedString()
} }
func canBePlayedInSpecifiedCourt() -> Bool { func canBePlayedInSpecifiedCourt(runningMatches: [Match]) -> Bool {
guard let courtIndex else { return false } guard let courtIndex else { return false }
if expectedToBeRunning() { if expectedToBeRunning() {
return courtIsAvailable(courtIndex) return courtIsAvailable(courtIndex, in: runningMatches)
} else { } else {
return true return true
} }

@ -513,7 +513,7 @@ final class Tournament : ModelObject, Storable {
return URLs.main.url.appending(path: "tournament/\(id)").appending(path: pageLink.path) 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 #if _DEBUGING_TIME //DEBUGING TIME
let start = Date() let start = Date()
defer { defer {
@ -521,8 +521,6 @@ defer {
print("func courtUsed()", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) print("func courtUsed()", id, duration.formatted(.units(allowed: [.seconds, .milliseconds])))
} }
#endif #endif
let runningMatches: [Match] = DataStore.shared.runningMatches()
return Set(runningMatches.compactMap { $0.courtIndex }).sorted() return Set(runningMatches.compactMap { $0.courtIndex }).sorted()
} }

@ -22,7 +22,10 @@ struct MatchSummaryView: View {
init(match: Match, title: String? = nil, updatedField: Int? = nil) { init(match: Match, title: String? = nil, updatedField: Int? = nil) {
self.match = match self.match = match
self.updatedField = updatedField self.updatedField = updatedField
let currentAvailableCourts = match.availableCourts()
let runningMatches = DataStore.shared.runningMatches()
let currentAvailableCourts = match.availableCourts(runningMatches: runningMatches)
self.availableCourts = currentAvailableCourts self.availableCourts = currentAvailableCourts
if let groupStage = match.groupStageObject { if let groupStage = match.groupStageObject {
@ -40,9 +43,8 @@ struct MatchSummaryView: View {
} else { } else {
self.courtName = nil self.courtName = nil
} }
let runningMatches = Tournament.runningMatches(match.currentTournament()?.allMatches() ?? [])
self.estimatedStartDate = match.estimatedStartDate(availableCourts: currentAvailableCourts, runningMatches: runningMatches) self.estimatedStartDate = match.estimatedStartDate(availableCourts: currentAvailableCourts, runningMatches: runningMatches)
self.canBePlayedInSpecifiedCourt = match.canBePlayedInSpecifiedCourt() self.canBePlayedInSpecifiedCourt = match.canBePlayedInSpecifiedCourt(runningMatches: runningMatches)
} }
var spacing: CGFloat { var spacing: CGFloat {

Loading…
Cancel
Save