From a01dfcea59500ca8175010e811d0e1879e9762d6 Mon Sep 17 00:00:00 2001 From: Raz Date: Sun, 29 Sep 2024 19:43:21 +0200 Subject: [PATCH] fix bugs double groupstage mode --- PadelClub.xcodeproj/project.pbxproj | 4 ++-- PadelClub/Data/GroupStage.swift | 12 ++++++++---- PadelClub/Data/Tournament.swift | 9 +++++---- PadelClub/Views/GroupStage/GroupStageView.swift | 14 +++++++++++++- .../Views/Tournament/TournamentBuildView.swift | 13 ++++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 2bc0a9a..cb7b062 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -3134,7 +3134,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 3; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; @@ -3179,7 +3179,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 3; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; DEVELOPMENT_TEAM = BQ3Y44M3Q6; diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index 8ec0c09..00aff51 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -163,7 +163,11 @@ final class GroupStage: ModelObject, Storable { Logger.error(error) } - if tournament.groupStagesAreOver(), tournament.groupStageLoserBracketAreOver(), tournament.rounds().isEmpty { + let groupStagesAreOverAtFirstStep = tournament.groupStagesAreOver(atStep: 0) + let nextStepGroupStages = tournament.groupStages(atStep: 1) + let groupStagesAreOverAtSecondStep = tournament.groupStagesAreOver(atStep: 1) + + if groupStagesAreOverAtFirstStep, nextStepGroupStages.isEmpty || groupStagesAreOverAtSecondStep == true, tournament.groupStageLoserBracketAreOver(), tournament.rounds().isEmpty { tournament.endDate = Date() do { try DataStore.shared.tournaments.addOrUpdate(instance: tournament) @@ -365,17 +369,17 @@ final class GroupStage: ModelObject, Storable { func teams(_ sortedByScore: Bool = false, scores: [TeamGroupStageScore]? = nil) -> [TeamRegistration] { if sortedByScore { return unsortedTeams().compactMap({ team in - scores?.first(where: { $0.team.id == team.id }) ?? _score(forGroupStagePosition: team.groupStagePosition!) + scores?.first(where: { $0.team.id == team.id }) ?? _score(forGroupStagePosition: team.groupStagePositionAtStep(step)!) }).sorted { (lhs, rhs) in let predicates: [TeamScoreAreInIncreasingOrder] = [ { $0.wins < $1.wins }, { $0.setDifference < $1.setDifference }, { $0.gameDifference < $1.gameDifference}, { self._headToHead($0.team, $1.team) }, - { $0.team.groupStagePosition! > $1.team.groupStagePosition! } + { [self] in $0.team.groupStagePositionAtStep(self.step)! > $1.team.groupStagePositionAtStep(self.step)! } ] - for predicate in predicates { + for predicate in predicates { if !predicate(lhs, rhs) && !predicate(rhs, lhs) { continue } diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 9edcef5..07f7d0b 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -1225,8 +1225,9 @@ defer { let groupStages = groupStages(atStep: lastStep) for groupStage in groupStages { - for (teamIndex, team) in groupStage.teams(true).enumerated() { - teams[groupStage.index + 1 + teamIndex] = [team.id] + let groupStageTeams = groupStage.teams(true) + for teamIndex in 0.. Bool { - let groupStages = groupStages() + func groupStagesAreOver(atStep: Int = 0) -> Bool { + let groupStages = groupStages(atStep: atStep) guard groupStages.isEmpty == false else { return true } diff --git a/PadelClub/Views/GroupStage/GroupStageView.swift b/PadelClub/Views/GroupStage/GroupStageView.swift index cf13eff..8a2e134 100644 --- a/PadelClub/Views/GroupStage/GroupStageView.swift +++ b/PadelClub/Views/GroupStage/GroupStageView.swift @@ -57,12 +57,24 @@ struct GroupStageView: View { MatchListView(section: "prêt à démarrer", matches: availableToStart, hideWhenEmpty: true) .listRowView(isActive: availableToStart.isEmpty == false, color: .green, hideColorVariation: true) MatchListView(section: "à lancer", matches: groupStage.readyMatches(playedMatches: playedMatches), hideWhenEmpty: true) - MatchListView(section: "terminés", matches: groupStage.finishedMatches(playedMatches: playedMatches), isExpanded: false) + MatchListView(section: "terminés", matches: groupStage.finishedMatches(playedMatches: playedMatches), hideWhenEmpty: playedMatches.isEmpty || playedMatches.flatMap({ $0.teamScores }).isEmpty, isExpanded: false) if playedMatches.isEmpty { RowButtonView("Créer les matchs de poules") { groupStage.buildMatches() } + } else if groupStage.step > 0, playedMatches.flatMap({ $0.teamScores }).isEmpty { + Section { + RowButtonView("Préparer les matchs") { + playedMatches.forEach { match in + match.updateTeamScores() + } + } + .disabled(tournament.groupStagesAreOver(atStep: 0) == false) + } footer: { + Text("La première phase doit être terminée avant de pouvoir préparer les matchs de la deuxième phase de poule.") + } + } } .toolbar { diff --git a/PadelClub/Views/Tournament/TournamentBuildView.swift b/PadelClub/Views/Tournament/TournamentBuildView.swift index 0266797..fcb5b0e 100644 --- a/PadelClub/Views/Tournament/TournamentBuildView.swift +++ b/PadelClub/Views/Tournament/TournamentBuildView.swift @@ -60,8 +60,19 @@ struct TournamentBuildView: View { } if tournament.groupStages(atStep: 1).isEmpty == false { - NavigationLink("2ème phase de poules") { + NavigationLink { GroupStagesView(tournament: tournament, step: 1) + } label: { + LabeledContent { + if tournament.groupStagesAreOver(atStep: 1) { + Text("terminées") + } else { + Text("") + } + } label: { + Text("2ème phase de poules") + } + } }