fix stuff with loser bracket and ts removing

club_update
Razmig Sarkissian 1 year ago
parent 39c76a7260
commit 23007c6087
  1. 42
      PadelClub/Data/Round.swift
  2. 18
      PadelClub/Views/Match/MatchSetupView.swift
  3. 16
      PadelClub/Views/Round/LoserRoundSettingsView.swift
  4. 6
      PadelClub/Views/Round/LoserRoundsView.swift
  5. 62
      PadelClub/Views/Round/RoundView.swift

@ -273,23 +273,23 @@ defer {
return self.tournamentStore.matches.filter { $0.round == self.id && $0.disabled == false }.sorted(by: \.index) return self.tournamentStore.matches.filter { $0.round == self.id && $0.disabled == false }.sorted(by: \.index)
} }
func displayableMatches() -> [Match] { // func displayableMatches() -> [Match] {
#if DEBUG_TIME //DEBUGING TIME //#if DEBUG_TIME //DEBUGING TIME
let start = Date() // let start = Date()
defer { // defer {
let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) // let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000)
print("func displayableMatches of round: ", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) // print("func displayableMatches of round: ", id, duration.formatted(.units(allowed: [.seconds, .milliseconds])))
} // }
#endif //#endif
//
if index == 0 && isUpperBracket() { // if index == 0 && isUpperBracket() {
var matches : [Match?] = [playedMatches().first] // var matches : [Match?] = [playedMatches().first]
matches.append(loserRounds().first?.playedMatches().first) // matches.append(loserRounds().first?.playedMatches().first)
return matches.compactMap({ $0 }) // return matches.compactMap({ $0 })
} else { // } else {
return playedMatches() // return playedMatches()
} // }
} // }
func playedMatches() -> [Match] { func playedMatches() -> [Match] {
if parent == nil { if parent == nil {
@ -551,6 +551,14 @@ defer {
return parent != nil return parent != nil
} }
func deleteLoserBracket() {
do {
try self.tournamentStore.rounds.delete(contentOfs: loserRounds())
} catch {
Logger.error(error)
}
}
func buildLoserBracket() { func buildLoserBracket() {
guard loserRounds().isEmpty else { return } guard loserRounds().isEmpty else { return }
let currentRoundMatchCount = RoundRule.numberOfMatches(forRoundIndex: index) let currentRoundMatchCount = RoundRule.numberOfMatches(forRoundIndex: index)

@ -44,15 +44,25 @@ struct MatchSetupView: View {
//todo //todo
if match.isSeededBy(team: team, inTeamPosition: teamPosition) { if match.isSeededBy(team: team, inTeamPosition: teamPosition) {
team.bracketPosition = nil team.bracketPosition = nil
match.updateTeamScores()
match.enableMatch()
do { do {
try tournamentStore.matches.addOrUpdate(instance: match) try tournamentStore.teamRegistrations.addOrUpdate(instance: team)
} catch { } catch {
Logger.error(error) 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 { do {
try tournamentStore.teamRegistrations.addOrUpdate(instance: team) try tournamentStore.matches.addOrUpdate(instance: match)
} catch { } catch {
Logger.error(error) Logger.error(error)
} }

@ -12,9 +12,25 @@ struct LoserRoundSettingsView: View {
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed @Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed
@Environment(Tournament.self) var tournament: Tournament @Environment(Tournament.self) var tournament: Tournament
@State var upperBracketRound: UpperRound
var body: some View { var body: some View {
List { 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 { Section {
RowButtonView(isEditingTournamentSeed.wrappedValue == true ? "Terminer l'édition" : "Éditer les tours joués") { RowButtonView(isEditingTournamentSeed.wrappedValue == true ? "Terminer l'édition" : "Éditer les tours joués") {
isEditingTournamentSeed.wrappedValue.toggle() isEditingTournamentSeed.wrappedValue.toggle()

@ -10,9 +10,9 @@ import SwiftUI
class UpperRound: Identifiable, Selectable { class UpperRound: Identifiable, Selectable {
var id: String { round.id } var id: String { round.id }
let round: Round let round: Round
lazy var loserRounds: [LoserRound] = { var loserRounds: [LoserRound] {
LoserRound.updateDestinations(fromLoserRounds: round.loserRounds(), inUpperBracketRound: round) LoserRound.updateDestinations(fromLoserRounds: round.loserRounds(), inUpperBracketRound: round)
}() }
let title: String let title: String
let playedMatches: [Match] let playedMatches: [Match]
@ -180,7 +180,7 @@ struct LoserRoundsView: View {
case .some(let selectedRound): case .some(let selectedRound):
LoserRoundView(loserBracket: selectedRound) LoserRoundView(loserBracket: selectedRound)
default: default:
LoserRoundSettingsView() LoserRoundSettingsView(upperBracketRound: upperBracketRound)
} }
} }
.environment(\.isEditingTournamentSeed, $isEditingTournamentSeed) .environment(\.isEditingTournamentSeed, $isEditingTournamentSeed)

@ -34,14 +34,14 @@ struct RoundView: View {
} }
private var spaceLeft: [Match] { private var spaceLeft: [Match] {
let displayableMatches: [Match] = self.upperRound.round.displayableMatches() let displayableMatches: [Match] = self.upperRound.round.playedMatches()
return displayableMatches.filter { match in return displayableMatches.filter { match in
match.teamScores.count == 1 match.teamScores.count == 1
} }
} }
private var seedSpaceLeft: [Match] { private var seedSpaceLeft: [Match] {
let displayableMatches: [Match] = self.upperRound.round.displayableMatches() let displayableMatches: [Match] = self.upperRound.round.playedMatches()
return displayableMatches.filter { match in return displayableMatches.filter { match in
match.teamScores.count == 0 match.teamScores.count == 0
} }
@ -62,12 +62,31 @@ struct RoundView: View {
var body: some View { var body: some View {
List { List {
let displayableMatches = upperRound.round.displayableMatches().sorted(by: \.index) let displayableMatches = upperRound.round.playedMatches().sorted(by: \.index)
if displayableMatches.isEmpty { if displayableMatches.isEmpty {
Section { Section {
ContentUnavailableView("Aucun match dans cette manche", systemImage: "tennisball") 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 }) //(where: { $0.isDisabled() == false || isEditingTournamentSeed.wrappedValue })
let printTip = PrintTip() let printTip = PrintTip()
TipView(printTip) { actions in TipView(printTip) { actions in
@ -75,7 +94,7 @@ struct RoundView: View {
} }
.tipStyle(tint: .master, asSection: true) .tipStyle(tint: .master, asSection: true)
if upperRound.loserRounds.isEmpty == false { if upperRound.round.index > 0 {
let correspondingLoserRoundTitle = upperRound.round.correspondingLoserRoundTitle() let correspondingLoserRoundTitle = upperRound.round.correspondingLoserRoundTitle()
Section { Section {
NavigationLink { NavigationLink {
@ -94,25 +113,11 @@ struct RoundView: View {
Text(correspondingLoserRoundTitle) Text(correspondingLoserRoundTitle)
} }
} }
} header: {
Text("Match de classement")
} }
} }
} else { } 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 availableSeeds = tournament.availableSeeds()
let availableQualifiedTeams = tournament.availableQualifiedTeams() let availableQualifiedTeams = tournament.availableQualifiedTeams()
@ -255,6 +260,17 @@ struct RoundView: View {
Text(match.teamScores.count.formatted()) Text(match.teamScores.count.formatted())
#endif #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") { Button(isEditingTournamentSeed.wrappedValue == true ? "Valider" : "Modifier") {
if isEditingTournamentSeed.wrappedValue == true { if isEditingTournamentSeed.wrappedValue == true {
_save() _save()
isEditingTournamentSeed.wrappedValue = false
} else {
isEditingTournamentSeed.wrappedValue = true
} }
isEditingTournamentSeed.wrappedValue.toggle()
} }
} }
} }

Loading…
Cancel
Save