From ad5b2f06c45255a10fdd6112539a50535adab164 Mon Sep 17 00:00:00 2001 From: Raz Date: Sat, 12 Oct 2024 07:53:58 +0200 Subject: [PATCH] fix round title issues --- PadelClub/Data/Round.swift | 29 ++++++++----- PadelClub/Data/Tournament.swift | 4 +- PadelClub/Views/Round/LoserRoundView.swift | 25 +++++++---- PadelClub/Views/Round/RoundView.swift | 50 ++++++++++++---------- 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index c618811..d7cf727 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -503,17 +503,26 @@ defer { if isUpperBracket() { if index == 0 { return SeedInterval(first: 1, last: 2) } let initialMatchIndexFromRoundIndex = RoundRule.matchIndex(fromRoundIndex: index) - let seedsAfterThisRound : [TeamRegistration] = self.tournamentStore.teamRegistrations.filter { - $0.bracketPosition != nil - && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex - } - let playedMatches = playedMatches().count - let minimumMatches = initialMode ? RoundRule.numberOfMatches(forRoundIndex: index) : playedMatches * 2 - //print("playedMatches \(playedMatches)", initialMode, parent, parentRound?.roundTitle(), seedsAfterThisRound.count) - let seedInterval = SeedInterval(first: playedMatches + seedsAfterThisRound.count + 1, last: minimumMatches + seedsAfterThisRound.count) - //print(seedInterval.localizedLabel()) - return seedInterval + if initialMode { + let playedMatches = RoundRule.numberOfMatches(forRoundIndex: index) + let seedInterval = SeedInterval(first: playedMatches + 1, last: playedMatches * 2) + //print(seedInterval.localizedLabel()) + return seedInterval + } else { + let seedsAfterThisRound : [TeamRegistration] = self.tournamentStore.teamRegistrations.filter { + $0.bracketPosition != nil + && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex + } + + let playedMatches = playedMatches().count + let minimumMatches = playedMatches * 2 + //print("playedMatches \(playedMatches)", initialMode, parent, parentRound?.roundTitle(), seedsAfterThisRound.count) + let seedInterval = SeedInterval(first: playedMatches + seedsAfterThisRound.count + 1, last: minimumMatches + seedsAfterThisRound.count) + //print(seedInterval.localizedLabel()) + return seedInterval + + } } if let previousRound = previousRound() { diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index a1feebc..3c70565 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -800,13 +800,13 @@ defer { func selectedSortedTeams() -> [TeamRegistration] { -// #if _DEBUG_TIME //DEBUGING TIME + #if _DEBUG_TIME //DEBUGING TIME let start = Date() defer { let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) print("func selectedSortedTeams", id, tournamentTitle(), duration.formatted(.units(allowed: [.seconds, .milliseconds]))) } -// #endif + #endif var _sortedTeams : [TeamRegistration] = [] var _teams = unsortedTeams().filter({ $0.walkOut == false }) diff --git a/PadelClub/Views/Round/LoserRoundView.swift b/PadelClub/Views/Round/LoserRoundView.swift index eb7b2fa..1707e75 100644 --- a/PadelClub/Views/Round/LoserRoundView.swift +++ b/PadelClub/Views/Round/LoserRoundView.swift @@ -70,13 +70,13 @@ struct LoserRoundView: View { if let seedInterval = previousRound.seedInterval(initialMode: isEditingTournamentSeed.wrappedValue == true) { Text(seedInterval.localizedLabel()) } else { - Text("no previous round") + Text("seedInterval is missing (previous round)") } } else if let parentRound = loserRound.parentRound { if let seedInterval = parentRound.seedInterval(initialMode: isEditingTournamentSeed.wrappedValue == true) { Text(seedInterval.localizedLabel()) } else { - Text("no parent round") + Text("seedInterval is missing (parent round)") } } } @@ -103,13 +103,7 @@ struct LoserRoundView: View { isEditingTournamentSeed.wrappedValue.toggle() if isEditingTournamentSeed.wrappedValue == false { - let allRoundMatches = loserBracket.allMatches - allRoundMatches.forEach({ $0.name = $0.roundTitle() }) - do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) - } catch { - Logger.error(error) - } + _refreshNames() } } } @@ -131,4 +125,17 @@ struct LoserRoundView: View { } } } + + private func _refreshNames() { + DispatchQueue.global(qos: .background).async { + + let allRoundMatches = loserBracket.allMatches + allRoundMatches.forEach({ $0.name = $0.roundTitle() }) + do { + try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) + } catch { + Logger.error(error) + } + } + } } diff --git a/PadelClub/Views/Round/RoundView.swift b/PadelClub/Views/Round/RoundView.swift index 540b94a..3b31959 100644 --- a/PadelClub/Views/Round/RoundView.swift +++ b/PadelClub/Views/Round/RoundView.swift @@ -338,31 +338,37 @@ struct RoundView: View { Logger.error(error) } - //todo should be done server side - let rounds = tournament.rounds() - rounds.forEach { round in - let matches = round.playedMatches() - matches.forEach { match in - match.name = Match.setServerTitle(upperRound: round, matchIndex: match.indexInRound(in: matches)) - } - } - - let loserMatches = self.upperRound.loserMatches() - loserMatches.forEach { match in - match.name = match.roundTitle() - } - - let allRoundMatches = tournament.allRoundMatches() - - do { - try tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) - } catch { - Logger.error(error) - } - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { self.isEditingTournamentSeed.wrappedValue = false } + + _refreshNames() + } + + private func _refreshNames() { + DispatchQueue.global(qos: .background).async { + //todo should be done server side + let rounds = tournament.rounds() + rounds.forEach { round in + let matches = round.playedMatches() + matches.forEach { match in + match.name = Match.setServerTitle(upperRound: round, matchIndex: match.indexInRound(in: matches)) + } + } + + let loserMatches = self.upperRound.loserMatches() + loserMatches.forEach { match in + match.name = match.roundTitle() + } + + let allRoundMatches = tournament.allRoundMatches() + + do { + try tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) + } catch { + Logger.error(error) + } + } } }