From ebac3974c98461c60fb5e3b0f5bd57040037e7f6 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Sat, 13 Apr 2024 20:08:00 +0200 Subject: [PATCH] fixes --- PadelClub/Manager/PadelRule.swift | 10 +-- PadelClub/ViewModel/MatchScheduler.swift | 86 +++++-------------- .../Navigation/Agenda/EventListView.swift | 3 +- .../Views/Planning/PlanningSettingsView.swift | 80 +++++++++-------- PadelClub/Views/Planning/PlanningView.swift | 16 ++-- .../Views/Tournament/Shared/DateBoxView.swift | 11 +-- .../Shared/TournamentCellView.swift | 2 +- 7 files changed, 89 insertions(+), 119 deletions(-) diff --git a/PadelClub/Manager/PadelRule.swift b/PadelClub/Manager/PadelRule.swift index cd48f06..cd55595 100644 --- a/PadelClub/Manager/PadelRule.swift +++ b/PadelClub/Manager/PadelRule.swift @@ -939,7 +939,7 @@ enum SetFormat: Int, Hashable, Codable { var firstGameFormat: Format { switch self { case .megaTieBreak: - return .tiebreakFiveTeen + return .tiebreakFifteen case .superTieBreak: return .tiebreakTen default: @@ -1243,7 +1243,7 @@ enum Format: Int, Hashable, Codable { case normal case tiebreakSeven case tiebreakTen - case tiebreakFiveTeen + case tiebreakFifteen func localizedLabel(_ displayStyle: DisplayStyle = .wide) -> String { switch self { @@ -1253,7 +1253,7 @@ enum Format: Int, Hashable, Codable { return "tie-break en 7" case .tiebreakTen: return "tie-break en 10" - case .tiebreakFiveTeen: + case .tiebreakFifteen: return "tie-break en 15" } } @@ -1261,7 +1261,7 @@ enum Format: Int, Hashable, Codable { switch self { case .normal: return false - case .tiebreakSeven, .tiebreakTen, .tiebreakFiveTeen: + case .tiebreakSeven, .tiebreakTen, .tiebreakFifteen: return true } } @@ -1274,7 +1274,7 @@ enum Format: Int, Hashable, Codable { return 7 case .tiebreakTen: return 10 - case .tiebreakFiveTeen: + case .tiebreakFifteen: return 15 } } diff --git a/PadelClub/ViewModel/MatchScheduler.swift b/PadelClub/ViewModel/MatchScheduler.swift index 33eaa61..b1471e4 100644 --- a/PadelClub/ViewModel/MatchScheduler.swift +++ b/PadelClub/ViewModel/MatchScheduler.swift @@ -18,12 +18,10 @@ struct TimeMatch { let matchID: String let rotationIndex: Int var courtIndex: Int - let groupIndex: Int var startDate: Date var durationLeft: Int //in minutes var minimumBreakTime: Int //in minutes - var courtLocked: Bool = false - var freeCourt: Bool = false + func estimatedEndDate(includeBreakTime: Bool) -> Date { let minutesToAdd = Double(durationLeft + (includeBreakTime ? minimumBreakTime : 0)) return startDate.addingTimeInterval(minutesToAdd * 60.0) @@ -41,7 +39,6 @@ struct MatchDispatcher { let timedMatches: [TimeMatch] let freeCourtPerRotation: [Int: [Int]] let rotationCount: Int - let groupLastRotation: [Int: Int] } extension Match { @@ -59,7 +56,7 @@ class MatchScheduler { func groupStageDispatcher(numberOfCourtsAvailablePerRotation: Int, groupStages: [GroupStage], startingDate: Date?, randomizeCourts: Bool) -> GroupStageMatchDispatcher { - let _groupStages = groupStages.filter { startingDate == nil || $0.startDate == startingDate } + let _groupStages = groupStages // Get the maximum count of matches in any group let maxMatchesCount = _groupStages.map { $0._matches().count }.max() ?? 0 @@ -176,19 +173,6 @@ class MatchScheduler { } } - func getAvailableCourt(inSlots slots: [TimeMatch], nextStartDate: Date) -> [TimeMatch] { - guard let minimumDuration = slots.compactMap({ $0.durationLeft }).min() else { return [] } - var newSlots = [TimeMatch]() - slots.forEach { timeMatch in - let durationLeft = timeMatch.durationLeft - if durationLeft - minimumDuration > 0 { - let timeMatch = TimeMatch(matchID: timeMatch.matchID, rotationIndex: timeMatch.rotationIndex + 1, courtIndex: timeMatch.courtIndex, groupIndex: timeMatch.groupIndex, startDate: nextStartDate, durationLeft: durationLeft, minimumBreakTime: timeMatch.minimumBreakTime, courtLocked: true) - newSlots.append(timeMatch) - } - } - return newSlots - } - func getNextStartDate(fromPreviousRotationSlots slots: [TimeMatch], includeBreakTime: Bool) -> Date? { slots.map { $0.estimatedEndDate(includeBreakTime: includeBreakTime) }.min() } @@ -207,37 +191,21 @@ class MatchScheduler { ) } - - - func roundDispatcher(numberOfCourtsAvailablePerRotation: Int, flattenedMatches: [Match], randomizeCourts: Bool, initialOccupiedCourt: Int = 0, dispatcherStartDate: Date) -> MatchDispatcher { + func roundDispatcher(numberOfCourtsAvailablePerRotation: Int, flattenedMatches: [Match], randomizeCourts: Bool, dispatcherStartDate: Date) -> MatchDispatcher { var slots = [TimeMatch]() var availableMatchs = flattenedMatches var rotationIndex = 0 var freeCourtPerRotation = [Int: [Int]]() - var groupLastRotation = [Int: Int]() var courts = [Int]() - var timeToAdd = 0.0 + while availableMatchs.count > 0 { freeCourtPerRotation[rotationIndex] = [] var matchPerRound = [Int: Int]() var availableCourt = numberOfCourtsAvailablePerRotation - if rotationIndex == 0 { - availableCourt = availableCourt - initialOccupiedCourt - } - courts = (0.. 0 { -// let previousPreviousRotationSlots = slots.filter({ $0.rotationIndex == rotationIndex - 2 }) -// rotationStartDate = getNextStartDate(fromPreviousRotationSlots: previousPreviousRotationSlots, includeBreakTime: false) ?? dispatcherStartDate -// } else if freeCourtPreviousRotation > 0 { -// print("scenario where we are waiting for a breaktime to be over without any match to play in between or a free court was available and we need to recheck breaktime left on it") -// let previousPreviousRotationSlots = slots.filter({ $0.rotationIndex == rotationIndex - 2 }) -// if let previousEndDate = getNextStartDate(fromPreviousRotationSlots: previousPreviousRotationSlots, includeBreakTime: true) { -// rotationStartDate = previousEndDate -// courts = freeCourtPerRotation[rotationIndex - 1]! -// } -// } dispatchCourts(availableCourts: numberOfCourtsAvailablePerRotation, courts: courts, availableMatchs: &availableMatchs, slots: &slots, rotationIndex: rotationIndex, rotationStartDate: rotationStartDate, freeCourtPerRotation: &freeCourtPerRotation) rotationIndex += 1 } -// var organizedSlots = [TimeMatch]() -// for i in 0..= tournament.startDate } ) +// if times.isEmpty { +// groupStages.forEach({ $0.startDate = tournament.startDate }) +// times.insert(tournament.startDate) +// try? dataStore.groupStages.addOrUpdate(contentOfs: groupStages) +// } + + var lastDate : Date = tournament.startDate + groupStages.chunked(into: groupStageCourtCount).forEach { groups in + groups.forEach({ $0.startDate = lastDate }) + try? dataStore.groupStages.addOrUpdate(contentOfs: groups) + + let dispatch = matchScheduler.groupStageDispatcher(numberOfCourtsAvailablePerRotation: numberOfCourtsAvailablePerRotation, groupStages: groups, startingDate: lastDate, randomizeCourts: randomCourtDistribution) + + dispatch.timedMatches.forEach { matchSchedule in + if let match = matches.first(where: { $0.id == matchSchedule.matchID }) { + let timeIntervalToAdd = (Double(matchSchedule.rotationIndex)) * Double(match.matchFormat.estimatedDuration) * 60 + if let startDate = match.groupStageObject?.startDate { + let matchStartDate = startDate.addingTimeInterval(timeIntervalToAdd) + match.startDate = matchStartDate + lastDate = matchStartDate.addingTimeInterval(Double(match.matchFormat.estimatedDuration) * 60) + } + match.setCourt(matchSchedule.courtIndex + 1) + } + } + } + try? dataStore.matches.addOrUpdate(contentOfs: matches) + + matchScheduler.updateSchedule(tournament: tournament, fromRoundId: nil, fromMatchId: nil, randomizeCourts: randomCourtDistribution, startDate: lastDate) + + scheduleSetup = true + + } + private func _save() { try? dataStore.tournaments.addOrUpdate(instance: tournament) } diff --git a/PadelClub/Views/Planning/PlanningView.swift b/PadelClub/Views/Planning/PlanningView.swift index db3c14d..6214d5b 100644 --- a/PadelClub/Views/Planning/PlanningView.swift +++ b/PadelClub/Views/Planning/PlanningView.swift @@ -92,12 +92,18 @@ struct PlanningView: View { NavigationLink { MatchDetailView(match: match, matchViewStyle: .sectionedStandardStyle) } label: { - if let groupStage = match.groupStageObject { - Text(groupStage.groupStageTitle()) - } else if let round = match.roundObject { - Text(round.roundTitle()) + LabeledContent { + if let court = match.court { + Text(court) + } + } label: { + if let groupStage = match.groupStageObject { + Text(groupStage.groupStageTitle()) + } else if let round = match.roundObject { + Text(round.roundTitle()) + } + Text(match.matchTitle()) } - Text(match.matchTitle()) } } } label: { diff --git a/PadelClub/Views/Tournament/Shared/DateBoxView.swift b/PadelClub/Views/Tournament/Shared/DateBoxView.swift index f9377bf..1f03840 100644 --- a/PadelClub/Views/Tournament/Shared/DateBoxView.swift +++ b/PadelClub/Views/Tournament/Shared/DateBoxView.swift @@ -20,11 +20,12 @@ struct DateBoxView: View { .font(displayStyle == .wide ? .title : .title3) .monospacedDigit() } - Text(date.formatted(.dateTime.month(.abbreviated))) - .font(.caption2) - Text(date.formatted(.dateTime.year())) - .font(.caption2) - + if displayStyle == .wide { + Text(date.formatted(.dateTime.month(.abbreviated))) + .font(.caption2) + Text(date.formatted(.dateTime.year())) + .font(.caption2) + } } } } diff --git a/PadelClub/Views/Tournament/Shared/TournamentCellView.swift b/PadelClub/Views/Tournament/Shared/TournamentCellView.swift index b796d6b..72a4f9d 100644 --- a/PadelClub/Views/Tournament/Shared/TournamentCellView.swift +++ b/PadelClub/Views/Tournament/Shared/TournamentCellView.swift @@ -28,7 +28,7 @@ struct TournamentCellView: View { private func _buildView(_ build: any TournamentBuildHolder, existingTournament: Tournament?) -> some View { HStack { - DateBoxView(date: tournament.startDate, displayStyle: displayStyle) + DateBoxView(date: tournament.startDate, displayStyle: .short) Rectangle() .fill(color) .frame(width: 2)