From 0a6d7fe79768458148378fd65225ff89c2e2476c Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 19 Mar 2025 12:12:22 +0100 Subject: [PATCH] fix table reset --- PadelClub/Data/Tournament.swift | 11 +++++- .../Screen/TableStructureView.swift | 39 +++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index f104e3a..492386c 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -2063,10 +2063,19 @@ defer { } } + func removeWildCards() { + let wcs = unsortedTeams().filter({ $0.isWildCard() && $0.unsortedPlayers().isEmpty }) + do { + try tournamentStore.teamRegistrations.delete(contentOfs: wcs) + } catch { + Logger.error(error) + } + } + func setGroupStageTeams(randomize: Bool) { let groupStages = groupStages() let max = groupStages.map { $0.size }.reduce(0,+) - var chunks = selectedSortedTeams().suffix(max).chunked(into: groupStageCount) + var chunks = selectedSortedTeams().filter({ $0.wildCardBracket == false }).suffix(max).chunked(into: groupStageCount) for (index, _) in chunks.enumerated() { if randomize { chunks[index].shuffle() diff --git a/PadelClub/Views/Tournament/Screen/TableStructureView.swift b/PadelClub/Views/Tournament/Screen/TableStructureView.swift index 9d8f20b..58f7145 100644 --- a/PadelClub/Views/Tournament/Screen/TableStructureView.swift +++ b/PadelClub/Views/Tournament/Screen/TableStructureView.swift @@ -81,12 +81,7 @@ struct TableStructureView: View { Text(structurePreset.localizedDescriptionStructurePresetTitle()) } .onChange(of: structurePreset) { - teamCount = structurePreset.tableDimension() + structurePreset.teamsInQualifiers() - structurePreset.qualifiedPerGroupStage() * structurePreset.groupStageCount() - groupStageCount = structurePreset.groupStageCount() - teamsPerGroupStage = structurePreset.teamsPerGroupStage() - qualifiedPerGroupStage = structurePreset.qualifiedPerGroupStage() - groupStageAdditionalQualified = 0 - buildWildcards = tournament.level.wildcardArePossible() + _updatePreset() } } @@ -293,9 +288,27 @@ struct TableStructureView: View { Section { RowButtonView("Remise-à-zéro", role: .destructive) { + tournament.removeWildCards() tournament.deleteGroupStages() tournament.deleteStructure() - structurePreset = PadelTournamentStructurePreset.manual + + if structurePreset != .manual { + structurePreset = PadelTournamentStructurePreset.manual + } else { + _updatePreset() + } + + tournament.teamCount = teamCount + tournament.groupStageCount = groupStageCount + tournament.teamsPerGroupStage = teamsPerGroupStage + tournament.qualifiedPerGroupStage = qualifiedPerGroupStage + tournament.groupStageAdditionalQualified = groupStageAdditionalQualified + + do { + try dataStore.tournaments.addOrUpdate(instance: tournament) + } catch { + Logger.error(error) + } } } } @@ -444,11 +457,12 @@ struct TableStructureView: View { tournament.groupStageAdditionalQualified = groupStageAdditionalQualified if rebuildEverything { - tournament.deleteAndBuildEverything(preset: structurePreset) + tournament.removeWildCards() if structurePreset.hasWildcards(), buildWildcards { tournament.addWildCardIfNeeded(structurePreset.wildcardBrackets(), .bracket) tournament.addWildCardIfNeeded(structurePreset.wildcardQualifiers(), .groupStage) } + tournament.deleteAndBuildEverything(preset: structurePreset) } else if (rebuildEverything == false && requirements.contains(.groupStage)) { tournament.deleteGroupStages() tournament.buildGroupStages() @@ -467,6 +481,15 @@ struct TableStructureView: View { } } + private func _updatePreset() { + teamCount = structurePreset.tableDimension() + structurePreset.teamsInQualifiers() - structurePreset.qualifiedPerGroupStage() * structurePreset.groupStageCount() + groupStageCount = structurePreset.groupStageCount() + teamsPerGroupStage = structurePreset.teamsPerGroupStage() + qualifiedPerGroupStage = structurePreset.qualifiedPerGroupStage() + groupStageAdditionalQualified = 0 + buildWildcards = tournament.level.wildcardArePossible() + } + private func _verifyValueIntegrity() { if teamCount > 128 { teamCount = 128