From ca6b56f2f9dfa1ec11399f1c5c3fcc8984bd314d Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 22 Jan 2025 11:32:09 +0100 Subject: [PATCH] enable realtime final ranking update --- PadelClub/Data/GroupStage.swift | 2 ++ PadelClub/Data/Match.swift | 3 --- PadelClub/Data/Round.swift | 5 +++- PadelClub/Data/Tournament.swift | 24 +++++++++---------- .../Tournament/TournamentBuildView.swift | 2 +- .../Views/Tournament/TournamentView.swift | 17 +++++++++++++ 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index 75e6946..c84ab31 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -239,6 +239,8 @@ final class GroupStage: ModelObject, Storable { Logger.error(error) } } + + tournament.updateTournamentState() } } diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 7b89728..a71580d 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -232,7 +232,6 @@ defer { servingTeamId = nil groupStageObject?.updateGroupStageState() roundObject?.updateTournamentState() - currentTournament()?.updateTournamentState() teams().forEach({ $0.resetRestingTime() }) } @@ -554,7 +553,6 @@ defer { losingTeamId = teamScoreWalkout.teamRegistration groupStageObject?.updateGroupStageState() roundObject?.updateTournamentState() - currentTournament()?.updateTournamentState() updateFollowingMatchTeamScore() } @@ -593,7 +591,6 @@ defer { } catch { Logger.error(error) } - tournament.updateTournamentState() } updateFollowingMatchTeamScore() } diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index c9a8669..38355c2 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -584,7 +584,8 @@ defer { } func updateTournamentState() { - if let tournamentObject = tournamentObject(), index == 0, isUpperBracket(), hasEnded() { + let tournamentObject = tournamentObject() + if let tournamentObject, index == 0, isUpperBracket(), hasEnded() { tournamentObject.endDate = Date() do { try DataStore.shared.tournaments.addOrUpdate(instance: tournamentObject) @@ -592,6 +593,8 @@ defer { Logger.error(error) } } + + tournamentObject?.updateTournamentState() } func roundStatus() -> String { diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index cafe5b5..f4e2347 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -1403,14 +1403,16 @@ defer { groupStages.forEach { groupStage in let groupStageTeams = groupStage.teams(true) for (index, team) in groupStageTeams.enumerated() { - if team.qualified == false && alreadyPlaceTeams.contains(team.id) == false { - let groupStageWidth = max(((index == qualifiedPerGroupStage) ? groupStageCount - groupStageAdditionalQualified : groupStageCount) * (index - qualifiedPerGroupStage), 0) - - let _index = baseRank + groupStageWidth + 1 - (index > qualifiedPerGroupStage ? groupStageAdditionalQualified : 0) - if let existingTeams = teams[_index] { - teams[_index] = existingTeams + [team.id] - } else { - teams[_index] = [team.id] + if groupStage.hasEnded() { + if team.qualified == false && alreadyPlaceTeams.contains(team.id) == false { + let groupStageWidth = max(((index == qualifiedPerGroupStage) ? groupStageCount - groupStageAdditionalQualified : groupStageCount) * (index - qualifiedPerGroupStage), 0) + + let _index = baseRank + groupStageWidth + 1 - (index > qualifiedPerGroupStage ? groupStageAdditionalQualified : 0) + if let existingTeams = teams[_index] { + teams[_index] = existingTeams + [team.id] + } else { + teams[_index] = [team.id] + } } } } @@ -2347,10 +2349,8 @@ defer { func updateTournamentState() { Task { - if hasEnded() { - let fr = await finalRanking() - _ = await setRankings(finalRanks: fr) - } + let fr = await finalRanking() + _ = await setRankings(finalRanks: fr) } } diff --git a/PadelClub/Views/Tournament/TournamentBuildView.swift b/PadelClub/Views/Tournament/TournamentBuildView.swift index b76002c..92e23b0 100644 --- a/PadelClub/Views/Tournament/TournamentBuildView.swift +++ b/PadelClub/Views/Tournament/TournamentBuildView.swift @@ -113,7 +113,7 @@ struct TournamentBuildView: View { Section { - if tournament.hasEnded() { + if true { NavigationLink(value: Screen.rankings) { LabeledContent { if tournament.publishRankings == false { diff --git a/PadelClub/Views/Tournament/TournamentView.swift b/PadelClub/Views/Tournament/TournamentView.swift index 2ee9907..f2a3ec4 100644 --- a/PadelClub/Views/Tournament/TournamentView.swift +++ b/PadelClub/Views/Tournament/TournamentView.swift @@ -236,6 +236,23 @@ struct TournamentView: View { NavigationLink(value: Screen.structure) { LabelStructure() } + + NavigationLink(value: Screen.rankings) { + LabeledContent { + if tournament.publishRankings == false { + Image(systemName: "exclamationmark.circle.fill") + .foregroundStyle(.logoYellow) + } else { + Image(systemName: "checkmark") + .foregroundStyle(.green) + } + } label: { + Text("Classement final des équipes") + if tournament.publishRankings == false { + Text("Vérifiez le classement avant de publier").foregroundStyle(.logoRed) + } + } + } NavigationLink(value: Screen.broadcast) { Label("Publication", systemImage: "airplayvideo")