diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index 50c361d..de2babd 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -79,7 +79,7 @@ class GroupStage: ModelObject, Storable { var _matches = [Match]() for i in 0..<_numberOfMatchesToBuild() { - let newMatch = Match(groupStage: id, index: i, matchFormat: matchFormat) + let newMatch = Match(groupStage: id, index: i, matchFormat: matchFormat, name: localizedMatchUpLabel(for: i)) _matches.append(newMatch) } diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 4fd7b0b..ab0c4a3 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -12,7 +12,12 @@ import LeStorage class Match: ModelObject, Storable { static func resourceName() -> String { "matches" } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } - + + static func setServerTitle(upperRound: Round, matchIndex: Int) -> String { + if upperRound.index == 0 { return upperRound.roundTitle() } + return upperRound.roundTitle() + " #" + (matchIndex + 1).formatted() + } + var byeState: Bool = false var id: String = Store.randomId() @@ -27,7 +32,7 @@ class Match: ModelObject, Storable { var winningTeamId: String? var losingTeamId: String? //var broadcasted: Bool - private var name: String? + var name: String? //var order: Int var disabled: Bool = false private(set) var courtIndex: Int? diff --git a/PadelClub/Data/MatchScheduler.swift b/PadelClub/Data/MatchScheduler.swift index 801a988..023972e 100644 --- a/PadelClub/Data/MatchScheduler.swift +++ b/PadelClub/Data/MatchScheduler.swift @@ -88,7 +88,35 @@ class MatchScheduler : ModelObject, Storable { }) var lastDate : Date = tournament.startDate - groupStages.chunked(into: groupStageCourtCount).forEach { groups in + let times = Set(groupStages.compactMap { $0.startDate }).sorted() + + if let first = times.first { + if first.isEarlierThan(tournament.startDate) { + tournament.startDate = first + try? DataStore.shared.tournaments.addOrUpdate(instance: tournament) + } + } + + times.forEach({ time in + lastDate = time + let groups = groupStages.filter({ $0.startDate == time }) + let dispatch = groupStageDispatcher(numberOfCourtsAvailablePerRotation: numberOfCourtsAvailablePerRotation, groupStages: groups, startingDate: lastDate) + + dispatch.timedMatches.forEach { matchSchedule in + if let match = matches.first(where: { $0.id == matchSchedule.matchID }) { + let estimatedDuration = match.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration) + let timeIntervalToAdd = (Double(matchSchedule.rotationIndex)) * Double(estimatedDuration) * 60 + if let startDate = match.groupStageObject?.startDate { + let matchStartDate = startDate.addingTimeInterval(timeIntervalToAdd) + match.startDate = matchStartDate + lastDate = matchStartDate.addingTimeInterval(Double(estimatedDuration) * 60) + } + match.setCourt(matchSchedule.courtIndex) + } + } + }) + + groupStages.filter({ $0.startDate == nil || times.contains($0.startDate!) == false }).chunked(into: groupStageCourtCount).forEach { groups in groups.forEach({ $0.startDate = lastDate }) try? DataStore.shared.groupStages.addOrUpdate(contentOfs: groups) diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index f5b38e8..8b9da7a 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -431,12 +431,8 @@ class Round: ModelObject, Storable { let matches = (0.. Date { + startDate + } + + func areTeamsPublished() -> Bool { + Date() >= startDate + } + + func publishedGroupStagesDate() -> Date? { + if let first = groupStages().flatMap({ $0.playedMatches() }).compactMap({ $0.startDate }).sorted().first?.atNine() { + if first.isEarlierThan(startDate) { + return startDate + } else { + return first + } + } else { + return nil + } + } + + func areGroupStagesPublished() -> Bool { + if let publishedGroupStagesDate = publishedGroupStagesDate() { + return Date() >= publishedGroupStagesDate + } else { + return false + } + } + + func publishedBracketsDate() -> Date? { + if let first = rounds().flatMap({ $0.playedMatches() }).compactMap({ $0.startDate }).sorted().first?.atNine() { + if first.isEarlierThan(startDate) { + return startDate + } else { + return first + } + } else { + return nil + } + } + + func areBracketsPublished() -> Bool { + if let publishedBracketsDate = publishedBracketsDate() { + return Date() >= publishedBracketsDate + } else { + return false + } + } + + func shareURL() -> URL? { return URLs.main.url.appending(path: "tournament/\(id)") } @@ -1096,7 +1145,7 @@ class Tournament : ModelObject, Storable { let matches = (0..