From 348aa591f3140e86e99c39d6fa2667940d845249 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Wed, 24 Apr 2024 12:29:21 +0200 Subject: [PATCH] fix round to loser and court Index modification --- PadelClub/Data/Match.swift | 4 +-- PadelClub/Data/Round.swift | 36 +++++++++---------- PadelClub/Data/Tournament.swift | 2 +- PadelClub/ViewModel/MatchScheduler.swift | 11 +++--- .../Views/Planning/PlanningSettingsView.swift | 7 ++-- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index dce13d2..5a9eac9 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -195,7 +195,7 @@ class Match: ModelObject, Storable { func _toggleForwardMatchDisableState(_ state: Bool) { guard let roundObject else { return } - guard roundObject.loser != nil else { return } + guard roundObject.parent != nil else { return } guard let forwardMatch = _forwardMatch(inRound: roundObject) else { return } guard let next = _otherMatch() else { return } if next.disabled && byeState == false && next.byeState == false { @@ -590,7 +590,7 @@ class Match: ModelObject, Storable { } var isLoserBracket: Bool { - roundObject?.loser != nil + roundObject?.parent != nil } var teamScores: [TeamScore] { diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index e88639f..0756e06 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -15,14 +15,14 @@ class Round: ModelObject, Storable { var id: String = Store.randomId() var tournament: String var index: Int - var loser: String? + var parent: String? var format: Int? var startDate: Date? - internal init(tournament: String, index: Int, loser: String? = nil, matchFormat: MatchFormat? = nil) { + internal init(tournament: String, index: Int, parent: String? = nil, matchFormat: MatchFormat? = nil) { self.tournament = tournament self.index = index - self.loser = loser + self.parent = parent self.format = matchFormat?.rawValue } @@ -53,7 +53,7 @@ class Round: ModelObject, Storable { } func upperMatches(ofMatch match: Match) -> [Match] { - if loser != nil, previousRound() == nil, let parentRound { + if parent != nil, previousRound() == nil, let parentRound { let matchIndex = match.index let indexInRound = RoundRule.matchIndexWithinRound(fromMatchIndex: matchIndex) return [parentRound.getMatch(atMatchIndexInRound: indexInRound * 2), parentRound.getMatch(atMatchIndexInRound: indexInRound * 2 + 1)].compactMap({ $0 }) @@ -131,8 +131,8 @@ class Round: ModelObject, Storable { case .one: if let luckyLoser = match.teamScores.first(where: { $0.luckyLoser == match.index * 2 }) { return luckyLoser.team - } else if let loser = upperBracketTopMatch(ofMatchIndex: match.index)?.losingTeamId { - return Store.main.findById(loser) + } else if let parent = upperBracketTopMatch(ofMatchIndex: match.index)?.losingTeamId { + return Store.main.findById(parent) } else if let previousMatch = topPreviousRoundMatch(ofMatch: match) { if let teamId = previousMatch.winningTeamId { return Store.main.findById(teamId) @@ -143,8 +143,8 @@ class Round: ModelObject, Storable { case .two: if let luckyLoser = match.teamScores.first(where: { $0.luckyLoser == match.index * 2 + 1 }) { return luckyLoser.team - } else if let loser = upperBracketBottomMatch(ofMatchIndex: match.index)?.losingTeamId { - return Store.main.findById(loser) + } else if let parent = upperBracketBottomMatch(ofMatchIndex: match.index)?.losingTeamId { + return Store.main.findById(parent) } else if let previousMatch = bottomPreviousRoundMatch(ofMatch: match) { if let teamId = previousMatch.winningTeamId { return Store.main.findById(teamId) @@ -196,7 +196,7 @@ class Round: ModelObject, Storable { } func playedMatches() -> [Match] { - if loser == nil { + if parent == nil { Store.main.filter { $0.round == self.id && $0.disabled == false } } else { _matches() @@ -204,11 +204,11 @@ class Round: ModelObject, Storable { } func previousRound() -> Round? { - Store.main.filter(isIncluded: { $0.tournament == tournament && $0.loser == loser && $0.index == index + 1 }).first + Store.main.filter(isIncluded: { $0.tournament == tournament && $0.parent == parent && $0.index == index + 1 }).first } func nextRound() -> Round? { - Store.main.filter(isIncluded: { $0.tournament == tournament && $0.loser == loser && $0.index == index - 1 }).first + Store.main.filter(isIncluded: { $0.tournament == tournament && $0.parent == parent && $0.index == index - 1 }).first } func loserRounds(forRoundIndex roundIndex: Int) -> [Round] { @@ -314,7 +314,7 @@ class Round: ModelObject, Storable { } func seedInterval() -> SeedInterval? { - if loser == nil { + if parent == nil { let numberOfMatches = RoundRule.numberOfMatches(forRoundIndex: index + 1) let initialMatchIndexFromRoundIndex = RoundRule.matchIndex(fromRoundIndex: index) let seedsAfterThisRound : [TeamRegistration] = Store.main.filter(isIncluded: { @@ -337,7 +337,7 @@ class Round: ModelObject, Storable { } func roundTitle(_ displayStyle: DisplayStyle = .wide) -> String { - if loser != nil { + if parent != nil { return seedInterval()?.localizedLabel(displayStyle) ?? "Pas trouvé" } return RoundRule.roundName(fromRoundIndex: index) @@ -352,7 +352,7 @@ class Round: ModelObject, Storable { } func loserRounds() -> [Round] { - return Store.main.filter(isIncluded: { $0.loser == id }).sorted(by: \.index).reversed() + return Store.main.filter(isIncluded: { $0.parent == id }).sorted(by: \.index).reversed() } func loserRoundsAndChildren() -> [Round] { @@ -361,7 +361,7 @@ class Round: ModelObject, Storable { } func isLoserBracket() -> Bool { - loser != nil + parent != nil } func buildLoserBracket() { @@ -373,7 +373,7 @@ class Round: ModelObject, Storable { let rounds = (0.. [Round] { - Store.main.filter { $0.tournament == self.id && $0.loser == nil }.sorted(by: \.index).reversed() + Store.main.filter { $0.tournament == self.id && $0.parent == nil }.sorted(by: \.index).reversed() } func sortedTeams() -> [TeamRegistration] { diff --git a/PadelClub/ViewModel/MatchScheduler.swift b/PadelClub/ViewModel/MatchScheduler.swift index d40cd76..6ecb68a 100644 --- a/PadelClub/ViewModel/MatchScheduler.swift +++ b/PadelClub/ViewModel/MatchScheduler.swift @@ -391,7 +391,7 @@ class MatchScheduler { if shouldHandleUpperRoundSlice() { let roundMatchesCount = roundObject.playedMatches().count print("shouldHandleUpperRoundSlice \(roundMatchesCount)") - if roundObject.loser == nil && roundMatchesCount > courts.count { + if roundObject.parent == nil && roundMatchesCount > courts.count { print("roundMatchesCount \(roundMatchesCount) > \(courts.count)") if currentRotationSameRoundMatches >= min(roundMatchesCount / 2, courts.count) { print("return false, \(currentRotationSameRoundMatches) >= \(min(roundMatchesCount / 2, courts.count))") @@ -403,7 +403,7 @@ class MatchScheduler { let indexInRound = match.indexInRound() print("Upper Round, index > 0, first Match of round \(indexInRound) and more than one court available; looking for next match (same round) \(indexInRound + 1)") - if roundObject.loser == nil && roundObject.index > 0, indexInRound == 0, courts.count > 1, let nextMatch = match.next() { + if roundObject.parent == nil && roundObject.index > 0, indexInRound == 0, courts.count > 1, let nextMatch = match.next() { if canBePlayed && roundMatchCanBePlayed(nextMatch, roundObject: roundObject, slots: slots, rotationIndex: rotationIndex, targetedStartDate: rotationStartDate, minimumTargetedEndDate: &minimumTargetedEndDate) { print("next match and this match can be played, returning true") @@ -417,7 +417,7 @@ class MatchScheduler { print("\(currentRotationSameRoundMatches) modulo \(currentRotationSameRoundMatches%2) same round match is even, index of round is not 0 and upper bracket. If it's not the last court available \(courtIndex) == \(courts.count - 1)") - if currentRotationSameRoundMatches%2 == 0 && roundObject.index != 0 && roundObject.loser == nil && courtIndex == courts.count - 1 { + if currentRotationSameRoundMatches%2 == 0 && roundObject.index != 0 && roundObject.parent == nil && courtIndex == courts.count - 1 { print("we return false") return false } @@ -427,7 +427,7 @@ class MatchScheduler { }) { print(first.roundObject!.roundTitle(), first.matchTitle(), courtIndex, rotationStartDate) - if first.roundObject!.loser == nil { + if first.roundObject!.parent == nil { if let roundIndex = matchPerRound[first.roundObject!.index] { matchPerRound[first.roundObject!.index] = roundIndex + 1 } else { @@ -477,6 +477,7 @@ class MatchScheduler { flattenedMatches.forEach({ if (roundId == nil && matchId == nil) || $0.startDate?.isEarlierThan(startDate) == false { $0.startDate = nil + $0.removeCourt() } }) @@ -510,7 +511,7 @@ class MatchScheduler { roundDispatch.timedMatches.forEach { matchSchedule in if let match = flattenedMatches.first(where: { $0.id == matchSchedule.matchID }) { match.startDate = matchSchedule.startDate - match.setCourt(matchSchedule.courtIndex + 1) + match.setCourt(matchSchedule.courtIndex) } } diff --git a/PadelClub/Views/Planning/PlanningSettingsView.swift b/PadelClub/Views/Planning/PlanningSettingsView.swift index 9a00e3a..2bc1577 100644 --- a/PadelClub/Views/Planning/PlanningSettingsView.swift +++ b/PadelClub/Views/Planning/PlanningSettingsView.swift @@ -204,7 +204,10 @@ struct PlanningSettingsView: View { matchScheduler.timeDifferenceLimit = timeDifferenceLimit let matches = tournament.groupStages().flatMap({ $0._matches() }) - matches.forEach({ $0.startDate = nil }) + matches.forEach({ + $0.removeCourt() + $0.startDate = nil + }) var lastDate : Date = tournament.startDate groupStages.chunked(into: groupStageCourtCount).forEach { groups in @@ -222,7 +225,7 @@ struct PlanningSettingsView: View { match.startDate = matchStartDate lastDate = matchStartDate.addingTimeInterval(Double(estimatedDuration) * 60) } - match.setCourt(matchSchedule.courtIndex + 1) + match.setCourt(matchSchedule.courtIndex) } } }