diff --git a/PadelClubData/Data/AppSettings.swift b/PadelClubData/Data/AppSettings.swift index 1703b03..fcb1fd5 100644 --- a/PadelClubData/Data/AppSettings.swift +++ b/PadelClubData/Data/AppSettings.swift @@ -29,6 +29,7 @@ final public class AppSettings: MicroStorable { public var nationalCup: Bool public var dayDuration: Int? public var dayPeriod: DayPeriod + public var weekdays: Set public func lastDataSourceDate() -> Date? { guard let lastDataSource else { return nil } @@ -55,6 +56,7 @@ final public class AppSettings: MicroStorable { nationalCup = false dayDuration = nil dayPeriod = .all + weekdays = Set() } public required init() { @@ -70,6 +72,7 @@ final public class AppSettings: MicroStorable { nationalCup = false dayDuration = nil dayPeriod = .all + weekdays = Set() } public required init(from decoder: Decoder) throws { @@ -89,6 +92,7 @@ final public class AppSettings: MicroStorable { nationalCup = try container.decodeIfPresent(Bool.self, forKey: ._nationalCup) ?? false dayDuration = try container.decodeIfPresent(Int.self, forKey: ._dayDuration) dayPeriod = try container.decodeIfPresent(DayPeriod.self, forKey: ._dayPeriod) ?? .all + weekdays = try container.decodeIfPresent(Set.self, forKey: ._weekdays) ?? Set() } enum CodingKeys: String, CodingKey { @@ -107,5 +111,6 @@ final public class AppSettings: MicroStorable { case _nationalCup = "nationalCup" case _dayDuration = "dayDuration" case _dayPeriod = "dayPeriod" + case _weekdays = "weekdays" } } diff --git a/PadelClubData/Data/GroupStage.swift b/PadelClubData/Data/GroupStage.swift index bd86494..2cb9df1 100644 --- a/PadelClubData/Data/GroupStage.swift +++ b/PadelClubData/Data/GroupStage.swift @@ -298,7 +298,7 @@ final public class GroupStage: BaseGroupStage, SideStorable { return playedMatches.filter({ $0.isRunning() }).sorted(by: \.computedStartDateForSorting) } - public func readyMatches(playedMatches: [Match]) -> [Match] { + public func readyMatches(playedMatches: [Match], runningMatches: [Match]) -> [Match] { #if _DEBUG_TIME //DEBUGING TIME let start = Date() defer { @@ -306,7 +306,9 @@ final public class GroupStage: BaseGroupStage, SideStorable { print("func group stage readyMatches", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) } #endif - return playedMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false }) + let playingTeams = runningMatches.flatMap({ $0.teams() }).map({ $0.id }) + + return playedMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false && $0.containsTeamIds(playingTeams) == false }) } public func finishedMatches(playedMatches: [Match]) -> [Match] { @@ -616,6 +618,16 @@ final public class GroupStage: BaseGroupStage, SideStorable { public func computedStartDate() -> Date? { return _matches().sorted(by: \.computedStartDateForSorting).first?.startDate } + + public func removeAllTeams() { + let teams = teams() + teams.forEach { team in + team.groupStagePosition = nil + team.groupStage = nil + self._matches().forEach({ $0.updateTeamScores() }) + } + tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) + } public override func deleteDependencies(store: Store, shouldBeSynchronized: Bool) { diff --git a/PadelClubData/Data/MatchScheduler.swift b/PadelClubData/Data/MatchScheduler.swift index 39ed5de..637d21f 100644 --- a/PadelClubData/Data/MatchScheduler.swift +++ b/PadelClubData/Data/MatchScheduler.swift @@ -896,6 +896,11 @@ extension Match { return teamIds().contains(id) } + public func containsTeamIds(_ ids: [String]) -> Bool { + let teamIds = teamIds() + return !Set(ids).isDisjoint(with: teamIds) + } + public func containsTeamIndex(_ id: String) -> Bool { matchUp().contains(id) } diff --git a/PadelClubData/Data/Tournament.swift b/PadelClubData/Data/Tournament.swift index 9c16560..f323fbc 100644 --- a/PadelClubData/Data/Tournament.swift +++ b/PadelClubData/Data/Tournament.swift @@ -88,6 +88,25 @@ final public class Tournament: BaseTournament { return self.tournamentStore?.teamRegistrations.count ?? 0 } + public func deleteGroupStage(_ groupStage: GroupStage) { + groupStage.removeAllTeams() + let index = groupStage.index + self.tournamentStore?.groupStages.delete(instance: groupStage) + self.groupStageCount -= 1 + let groupStages = self.groupStages() + groupStages.filter({ $0.index > index }).forEach { gs in + gs.index -= 1 + } + self.tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) + } + + public func addGroupStage() { + let groupStage = GroupStage(tournament: id, index: groupStageCount, size: teamsPerGroupStage, format: groupStageFormat) + self.tournamentStore?.groupStages.addOrUpdate(instance: groupStage) + groupStage.buildMatches(keepExistingMatches: false) + self.groupStageCount += 1 + } + public func groupStages(atStep step: Int = 0) -> [GroupStage] { guard let tournamentStore = self.tournamentStore else { return [] } let groupStages: [GroupStage] = tournamentStore.groupStages.filter { $0.tournament == self.id && $0.step == step } @@ -856,7 +875,7 @@ defer { return allMatches.filter({ $0.isRunning() && $0.isReady() }).sorted(using: defaultSorting, order: .ascending) } - public static func readyMatches(_ allMatches: [Match]) -> [Match] { + public static func readyMatches(_ allMatches: [Match], runningMatches: [Match]) -> [Match] { #if _DEBUG_TIME //DEBUGING TIME let start = Date() defer { @@ -864,7 +883,10 @@ defer { print("func tournament readyMatches", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) } #endif - return allMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false }).sorted(using: defaultSorting, order: .ascending) + + let playingTeams = runningMatches.flatMap({ $0.teams() }).map({ $0.id }) + + return allMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false && $0.containsTeamIds(playingTeams) == false }).sorted(using: defaultSorting, order: .ascending) } public static func matchesLeft(_ allMatches: [Match]) -> [Match] { diff --git a/PadelClubData/Extensions/Date+Extensions.swift b/PadelClubData/Extensions/Date+Extensions.swift index a9878f0..aa681c4 100644 --- a/PadelClubData/Extensions/Date+Extensions.swift +++ b/PadelClubData/Extensions/Date+Extensions.swift @@ -118,6 +118,14 @@ public extension Date { } } + var nextDay: Date { + return Calendar.current.date(byAdding: .day, value: 1, to: self)! + } + + var weekDay: Int { + Calendar.current.component(.weekday, from: self) + } + func atBeginningOfDay(hourInt: Int = 9) -> Date { Calendar.current.date(byAdding: .hour, value: hourInt, to: self.startOfDay)! } @@ -144,6 +152,28 @@ public extension Date { return weekdays.map { $0.capitalized } }() + static var weekdays: [String] = { + let calendar = Calendar.current + // let weekdays = calendar.shortWeekdaySymbols + + // return weekdays.map { weekday in + // guard let firstLetter = weekday.first else { return "" } + // return String(firstLetter).capitalized + // } + // Adjusted for the different weekday starts + var weekdays = calendar.weekdaySymbols + if firstDayOfWeek > 1 { + for _ in 1..