diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index d74c31d..b4acba5 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -273,23 +273,23 @@ defer { return self.tournamentStore.matches.filter { $0.round == self.id && $0.disabled == false }.sorted(by: \.index) } - func displayableMatches() -> [Match] { -#if DEBUG_TIME //DEBUGING TIME - let start = Date() - defer { - let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) - print("func displayableMatches of round: ", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) - } -#endif - - if index == 0 && isUpperBracket() { - var matches : [Match?] = [playedMatches().first] - matches.append(loserRounds().first?.playedMatches().first) - return matches.compactMap({ $0 }) - } else { - return playedMatches() - } - } +// func displayableMatches() -> [Match] { +//#if DEBUG_TIME //DEBUGING TIME +// let start = Date() +// defer { +// let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) +// print("func displayableMatches of round: ", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) +// } +//#endif +// +// if index == 0 && isUpperBracket() { +// var matches : [Match?] = [playedMatches().first] +// matches.append(loserRounds().first?.playedMatches().first) +// return matches.compactMap({ $0 }) +// } else { +// return playedMatches() +// } +// } func playedMatches() -> [Match] { if parent == nil { @@ -550,7 +550,15 @@ defer { func isLoserBracket() -> Bool { return parent != nil } - + + func deleteLoserBracket() { + do { + try self.tournamentStore.rounds.delete(contentOfs: loserRounds()) + } catch { + Logger.error(error) + } + } + func buildLoserBracket() { guard loserRounds().isEmpty else { return } let currentRoundMatchCount = RoundRule.numberOfMatches(forRoundIndex: index) diff --git a/PadelClub/Views/Match/MatchSetupView.swift b/PadelClub/Views/Match/MatchSetupView.swift index 8a6a5d0..b91beb1 100644 --- a/PadelClub/Views/Match/MatchSetupView.swift +++ b/PadelClub/Views/Match/MatchSetupView.swift @@ -44,15 +44,25 @@ struct MatchSetupView: View { //todo if match.isSeededBy(team: team, inTeamPosition: teamPosition) { team.bracketPosition = nil - match.updateTeamScores() - match.enableMatch() do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } + match.updateTeamScores() + match.previousMatches().forEach { previousMatch in + if previousMatch.disabled { + previousMatch.enableMatch() + do { + try tournamentStore.matches.addOrUpdate(instance: previousMatch) + } catch { + Logger.error(error) + } + } + } + do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/LoserRoundSettingsView.swift b/PadelClub/Views/Round/LoserRoundSettingsView.swift index 21824f2..b8e9ba9 100644 --- a/PadelClub/Views/Round/LoserRoundSettingsView.swift +++ b/PadelClub/Views/Round/LoserRoundSettingsView.swift @@ -12,9 +12,25 @@ struct LoserRoundSettingsView: View { @EnvironmentObject var dataStore: DataStore @Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed @Environment(Tournament.self) var tournament: Tournament - + @State var upperBracketRound: UpperRound + var body: some View { List { + Section { + RowButtonView("Effacer les matchs de classements", role: .destructive) { + upperBracketRound.round.deleteLoserBracket() + } + } + + Section { + RowButtonView("Créer les matchs de classements", role: .destructive) { + upperBracketRound.round.buildLoserBracket() + upperBracketRound.round.disabledMatches().forEach { match in + match.disableMatch() + } + } + } + Section { RowButtonView(isEditingTournamentSeed.wrappedValue == true ? "Terminer l'édition" : "Éditer les tours joués") { isEditingTournamentSeed.wrappedValue.toggle() diff --git a/PadelClub/Views/Round/LoserRoundsView.swift b/PadelClub/Views/Round/LoserRoundsView.swift index b2c0fac..4bf7465 100644 --- a/PadelClub/Views/Round/LoserRoundsView.swift +++ b/PadelClub/Views/Round/LoserRoundsView.swift @@ -10,9 +10,9 @@ import SwiftUI class UpperRound: Identifiable, Selectable { var id: String { round.id } let round: Round - lazy var loserRounds: [LoserRound] = { + var loserRounds: [LoserRound] { LoserRound.updateDestinations(fromLoserRounds: round.loserRounds(), inUpperBracketRound: round) - }() + } let title: String let playedMatches: [Match] @@ -180,7 +180,7 @@ struct LoserRoundsView: View { case .some(let selectedRound): LoserRoundView(loserBracket: selectedRound) default: - LoserRoundSettingsView() + LoserRoundSettingsView(upperBracketRound: upperBracketRound) } } .environment(\.isEditingTournamentSeed, $isEditingTournamentSeed) diff --git a/PadelClub/Views/Round/RoundView.swift b/PadelClub/Views/Round/RoundView.swift index fba33ba..3891599 100644 --- a/PadelClub/Views/Round/RoundView.swift +++ b/PadelClub/Views/Round/RoundView.swift @@ -34,14 +34,14 @@ struct RoundView: View { } private var spaceLeft: [Match] { - let displayableMatches: [Match] = self.upperRound.round.displayableMatches() + let displayableMatches: [Match] = self.upperRound.round.playedMatches() return displayableMatches.filter { match in match.teamScores.count == 1 } } private var seedSpaceLeft: [Match] { - let displayableMatches: [Match] = self.upperRound.round.displayableMatches() + let displayableMatches: [Match] = self.upperRound.round.playedMatches() return displayableMatches.filter { match in match.teamScores.count == 0 } @@ -62,12 +62,31 @@ struct RoundView: View { var body: some View { List { - let displayableMatches = upperRound.round.displayableMatches().sorted(by: \.index) + let displayableMatches = upperRound.round.playedMatches().sorted(by: \.index) if displayableMatches.isEmpty { Section { ContentUnavailableView("Aucun match dans cette manche", systemImage: "tennisball") } - } else if isEditingTournamentSeed.wrappedValue == false { + } + + let disabledMatchesCount = BracketEditTip.matchesHidden + if disabledMatchesCount > 0 { + let bracketTip = BracketEditTip(nextRoundName: upperRound.round.nextRound()?.roundTitle()) + TipView(bracketTip).tipStyle(tint: .green, asSection: true) + + Section { + let leftToPlay = (RoundRule.numberOfMatches(forRoundIndex: upperRound.round.index) - disabledMatchesCount) + LabeledContent { + Text(leftToPlay.formatted()) + } label: { + Text("Match\(leftToPlay.pluralSuffix) à jouer \(upperRound.title)") + } + } footer: { + Text("\(disabledMatchesCount) match\(disabledMatchesCount.pluralSuffix) désactivé\(disabledMatchesCount.pluralSuffix) automatiquement") + } + } + + if isEditingTournamentSeed.wrappedValue == false { //(where: { $0.isDisabled() == false || isEditingTournamentSeed.wrappedValue }) let printTip = PrintTip() TipView(printTip) { actions in @@ -75,7 +94,7 @@ struct RoundView: View { } .tipStyle(tint: .master, asSection: true) - if upperRound.loserRounds.isEmpty == false { + if upperRound.round.index > 0 { let correspondingLoserRoundTitle = upperRound.round.correspondingLoserRoundTitle() Section { NavigationLink { @@ -94,25 +113,11 @@ struct RoundView: View { Text(correspondingLoserRoundTitle) } } + } header: { + Text("Match de classement") } } } else { - let disabledMatchesCount = BracketEditTip.matchesHidden - if disabledMatchesCount > 0 { - let bracketTip = BracketEditTip(nextRoundName: upperRound.round.nextRound()?.roundTitle()) - TipView(bracketTip).tipStyle(tint: .green, asSection: true) - - Section { - let leftToPlay = (RoundRule.numberOfMatches(forRoundIndex: upperRound.round.index) - disabledMatchesCount) - LabeledContent { - Text(leftToPlay.formatted()).font(.largeTitle) - } label: { - Text("Match\(leftToPlay.pluralSuffix) à jouer \(upperRound.title)") - Text("\(disabledMatchesCount) match\(disabledMatchesCount.pluralSuffix) désactivé\(disabledMatchesCount.pluralSuffix) automatiquement") - } - } - } - let availableSeeds = tournament.availableSeeds() let availableQualifiedTeams = tournament.availableQualifiedTeams() @@ -255,6 +260,17 @@ struct RoundView: View { Text(match.teamScores.count.formatted()) #endif } + } footer: { + if isEditingTournamentSeed.wrappedValue == true && match.followingMatch()?.disabled == true { + FooterButtonView("Désactiver", role: .destructive) { + match.disableMatch() + do { + try tournamentStore.matches.addOrUpdate(instance: match) + } catch { + Logger.error(error) + } + } + } } } } @@ -285,8 +301,10 @@ struct RoundView: View { Button(isEditingTournamentSeed.wrappedValue == true ? "Valider" : "Modifier") { if isEditingTournamentSeed.wrappedValue == true { _save() + isEditingTournamentSeed.wrappedValue = false + } else { + isEditingTournamentSeed.wrappedValue = true } - isEditingTournamentSeed.wrappedValue.toggle() } } }