From ecf14eaa32f4e4bd9bd8e93de9c99c6bfa2e7433 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Thu, 9 Oct 2025 15:57:53 +0200 Subject: [PATCH] couple of fixes add the ability to set a planning from a previous tournament --- PadelClubData/Data/GroupStage.swift | 16 ++++++++++++++++ PadelClubData/Data/Match.swift | 12 ++++++++++++ PadelClubData/Data/Round.swift | 24 ++++++++++++++++++++++++ PadelClubData/ViewModel/PadelRule.swift | 4 +++- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/PadelClubData/Data/GroupStage.swift b/PadelClubData/Data/GroupStage.swift index 3c00d7e..cbee440 100644 --- a/PadelClubData/Data/GroupStage.swift +++ b/PadelClubData/Data/GroupStage.swift @@ -628,6 +628,22 @@ final public class GroupStage: BaseGroupStage, SideStorable { } tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } + + public func setData(from correspondingGroupStage: GroupStage, tournamentStartDate: Date, previousTournamentStartDate: Date) { + + self.matchFormat = correspondingGroupStage.matchFormat + if let correspondingPlannedStartDate = correspondingGroupStage.plannedStartDate { + let offset = correspondingPlannedStartDate.timeIntervalSince(previousTournamentStartDate) + self.startDate = tournamentStartDate.addingTimeInterval(offset) + } + self.size = correspondingGroupStage.size + self.name = correspondingGroupStage.name + + let matches = correspondingGroupStage._matches() + for (index, match) in self._matches().enumerated() { + match.setData(from: matches[index], tournamentStartDate: tournamentStartDate, previousTournamentStartDate: previousTournamentStartDate) + } + } public override func deleteDependencies(store: Store, actionOption: ActionOption) { store.deleteDependencies(type: Match.self, actionOption: actionOption) { $0.groupStage == self.id } diff --git a/PadelClubData/Data/Match.swift b/PadelClubData/Data/Match.swift index a86d7a1..2b39159 100644 --- a/PadelClubData/Data/Match.swift +++ b/PadelClubData/Data/Match.swift @@ -1139,6 +1139,18 @@ defer { public func initialStartDate() -> Date? { plannedStartDate ?? startDate } + + public func setData(from correspondingMatch: Match, tournamentStartDate: Date, previousTournamentStartDate: Date) { + if let correspondingMatchPlannedStartDate = correspondingMatch.plannedStartDate { + let offset = correspondingMatchPlannedStartDate.timeIntervalSince(previousTournamentStartDate) + self.startDate = tournamentStartDate.addingTimeInterval(offset) + } + + self.disabled = correspondingMatch.disabled + self.matchFormat = correspondingMatch.matchFormat + self.courtIndex = correspondingMatch.courtIndex + self.name = correspondingMatch.name + } func insertOnServer() { self.tournamentStore?.matches.writeChangeAndInsertOnServer(instance: self) diff --git a/PadelClubData/Data/Round.swift b/PadelClubData/Data/Round.swift index 4b1feef..753d668 100644 --- a/PadelClubData/Data/Round.swift +++ b/PadelClubData/Data/Round.swift @@ -56,6 +56,30 @@ final public class Round: BaseRound, SideStorable { return tournamentStore.matches.filter { $0.round == self.id && $0.disabled == true } } + public func setData(from correspondingRound: Round, tournamentStartDate: Date, previousTournamentStartDate: Date) { + let matches = correspondingRound._matches() + for (index, match) in self._matches().enumerated() { + match.setData(from: matches[index], tournamentStartDate: tournamentStartDate, previousTournamentStartDate: previousTournamentStartDate) + } + + self.matchFormat = correspondingRound.matchFormat + if let correspondingPlannedStartDate = correspondingRound.plannedStartDate { + let offset = correspondingPlannedStartDate.timeIntervalSince(previousTournamentStartDate) + self.startDate = tournamentStartDate.addingTimeInterval(offset) + } + self.loserBracketMode = correspondingRound.loserBracketMode + self.groupStageLoserBracket = correspondingRound.groupStageLoserBracket + + loserRounds().forEach { round in + + if let pRound = correspondingRound.loserRounds().first(where: { r in + r.index == round.index + }) { + round.setData(from: pRound, tournamentStartDate: tournamentStartDate, previousTournamentStartDate: previousTournamentStartDate) + } + } + } + // MARK: - public var matchFormat: MatchFormat { diff --git a/PadelClubData/ViewModel/PadelRule.swift b/PadelClubData/ViewModel/PadelRule.swift index 9ec61aa..5c9b033 100644 --- a/PadelClubData/ViewModel/PadelRule.swift +++ b/PadelClubData/ViewModel/PadelRule.swift @@ -349,7 +349,9 @@ public enum FederalTournamentAge: Int, Hashable, Codable, CaseIterable, Identifi return 4 } else { switch level { - case .p25, .p100, .p250: + case .p25: + return 4 + case .p100, .p250: if category == .women { return 4 }