xcode16
Razmig Sarkissian 1 year ago
parent 8e301a4f24
commit 8fc6f09908
  1. 12
      PadelClub/Data/Tournament.swift
  2. 102
      PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift
  3. 3
      PadelClub/Views/Round/RoundSettingsView.swift

@ -348,19 +348,19 @@ final class Tournament : ModelObject, Storable {
override func deleteDependencies() throws { override func deleteDependencies() throws {
let store = self.tournamentStore let store = self.tournamentStore
let teams = self.unsortedTeams() let teams = self.tournamentStore.teamRegistrations
for team in teams { for team in teams {
try team.deleteDependencies() try team.deleteDependencies()
} }
store.teamRegistrations.deleteDependencies(teams) store.teamRegistrations.deleteDependencies(teams)
let groups = self.groupStages() let groups = self.tournamentStore.groupStages
for group in groups { for group in groups {
try group.deleteDependencies() try group.deleteDependencies()
} }
store.groupStages.deleteDependencies(groups) store.groupStages.deleteDependencies(groups)
let rounds = self.rounds() let rounds = self.self.tournamentStore.rounds
for round in rounds { for round in rounds {
try round.deleteDependencies() try round.deleteDependencies()
} }
@ -2019,13 +2019,13 @@ defer {
DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self)
for teamRegistration in self.unsortedTeams() { for teamRegistration in self.tournamentStore.teamRegistrations {
teamRegistration.insertOnServer() teamRegistration.insertOnServer()
} }
for groupStage in self.groupStages() { for groupStage in self.tournamentStore.groupStages {
groupStage.insertOnServer() groupStage.insertOnServer()
} }
for round in self.allRounds() { for round in self.tournamentStore.rounds {
round.insertOnServer() round.insertOnServer()
} }

@ -54,31 +54,10 @@ struct LoserBracketFromGroupStageView: View {
} }
} footer: { } footer: {
if isEditingLoserBracketGroupStage == true { if isEditingLoserBracketGroupStage == true {
HStack { GroupStageLoserBracketMatchFooterView(match: match, samePlaceThanAboveOption: displayableMatches.count > 1)
if match.index > tournament.groupStageLoserBracketsInitialPlace() {
FooterButtonView("même place qu'au-dessus") {
match.index -= 2
do {
try tournamentStore.matches.addOrUpdate(instance: match)
} catch {
Logger.error(error)
}
}
Spacer()
}
FooterButtonView("effacer", role: .destructive) {
do {
try match.deleteDependencies()
try tournamentStore.matches.delete(instance: match)
} catch {
Logger.error(error)
}
}
} }
} }
} }
}
Section { Section {
if displayableMatches.count > 1 && isEditingLoserBracketGroupStage == true { if displayableMatches.count > 1 && isEditingLoserBracketGroupStage == true {
Section { Section {
@ -124,7 +103,8 @@ struct LoserBracketFromGroupStageView: View {
} }
private func _addNewMatch() { private func _addNewMatch() {
let placeCount = tournament.groupStageLoserBracketsInitialPlace() + displayableMatches.count * 2 let currentGroupStageLoserBracketsInitialPlace = tournament.groupStageLoserBracketsInitialPlace()
let placeCount = displayableMatches.isEmpty ? currentGroupStageLoserBracketsInitialPlace : max(currentGroupStageLoserBracketsInitialPlace, displayableMatches.map({ $0.index }).max()! + 2)
let match = Match(round: loserBracket.id, index: placeCount, matchFormat: loserBracket.matchFormat) let match = Match(round: loserBracket.id, index: placeCount, matchFormat: loserBracket.matchFormat)
match.name = "\(placeCount)\(placeCount.ordinalFormattedSuffix()) place" match.name = "\(placeCount)\(placeCount.ordinalFormattedSuffix()) place"
do { do {
@ -148,3 +128,79 @@ struct LoserBracketFromGroupStageView: View {
} }
} }
struct GroupStageLoserBracketMatchFooterView: View {
@Environment(Tournament.self) var tournament: Tournament
@Bindable var match: Match
let samePlaceThanAboveOption: Bool
@State private var selectPlacePlayed: Bool = false
@State private var index: Int = 0
var body: some View {
HStack {
Menu {
if samePlaceThanAboveOption {
Button("Même place qu'au-dessus") {
_updateIndex(match.index-2)
}
}
Button("Choisir la place") {
index = match.index
selectPlacePlayed = true
}
} label: {
Text("Éditer la place jouée")
.underline()
}
Spacer()
FooterButtonView("Effacer", role: .destructive) {
do {
try match.deleteDependencies()
try match.tournamentStore.matches.delete(instance: match)
} catch {
Logger.error(error)
}
}
}
.alert("Place jouée", isPresented: $selectPlacePlayed) {
TextField("Place jouée", value: $index, format: .number)
.keyboardType(.numberPad)
.multilineTextAlignment(.trailing)
.onSubmit {
_updateIndex(index)
}
Button("Confirmer") {
_updateIndex(index)
}
Button("Annuler", role: .cancel) {
}
}
}
private func _updateIndex(_ newIndex: Int) {
let newIndexValidated = max(1,abs(newIndex))
let teamScores = match.teamScores
teamScores.forEach { ts in
if let luckyLoser = ts.luckyLoser {
ts.luckyLoser = (luckyLoser - match.index * 2) % 2 + newIndexValidated * 2
}
}
match.index = newIndexValidated
match.name = "\(newIndexValidated)\(newIndexValidated.ordinalFormattedSuffix()) place"
do {
try match.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores)
} catch {
Logger.error(error)
}
do {
try match.tournamentStore.matches.addOrUpdate(instance: match)
} catch {
Logger.error(error)
}
}
}

@ -132,7 +132,8 @@ struct RoundSettingsView: View {
Logger.error(error) Logger.error(error)
} }
round.buildLoserBracket() round.buildLoserBracket()
matches.filter { $0.disabled }.forEach { $0._toggleLoserMatchDisableState(true) matches.filter { $0.disabled }.forEach {
$0._toggleLoserMatchDisableState(true)
} }
} }
} }

Loading…
Cancel
Save