|
|
|
|
@ -104,6 +104,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 } |
|
|
|
|
@ -872,7 +891,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 { |
|
|
|
|
@ -880,7 +899,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] { |
|
|
|
|
@ -1698,12 +1720,14 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func initSettings(templateTournament: Tournament?, overrideTeamCount: Bool = true) { |
|
|
|
|
courtCount = eventObject()?.clubObject()?.courtCount ?? 2 |
|
|
|
|
setupDefaultPrivateSettings(templateTournament: templateTournament) |
|
|
|
|
setupUmpireSettings(defaultTournament: nil) //default is not template, default is for event sharing settings |
|
|
|
|
if let templateTournament { |
|
|
|
|
setupRegistrationSettings(templateTournament: templateTournament, overrideTeamCount: overrideTeamCount) |
|
|
|
|
} |
|
|
|
|
setupFederalSettings() |
|
|
|
|
customizeUsingPreferences() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func setupFederalSettings() { |
|
|
|
|
@ -1719,6 +1743,23 @@ defer { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func customizeUsingPreferences() { |
|
|
|
|
guard let lastTournamentWithSameBuild = DataStore.shared.tournaments.filter({ tournament in |
|
|
|
|
tournament.tournamentLevel == self.tournamentLevel |
|
|
|
|
&& tournament.tournamentCategory == self.tournamentCategory |
|
|
|
|
&& tournament.federalTournamentAge == self.federalTournamentAge |
|
|
|
|
&& tournament.hasEnded() == true |
|
|
|
|
&& tournament.isCanceled == false |
|
|
|
|
&& tournament.isDeleted == false |
|
|
|
|
}).sorted(by: \.endDate!, order: .descending).first else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.entryFee = lastTournamentWithSameBuild.entryFee |
|
|
|
|
self.clubMemberFeeDeduction = lastTournamentWithSameBuild.clubMemberFeeDeduction |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func deadline(for type: TournamentDeadlineType) -> Date? { |
|
|
|
|
guard [.p500, .p1000, .p1500, .p2000].contains(tournamentLevel) else { return nil } |
|
|
|
|
|
|
|
|
|
|