fix some stuff

multistore
Razmig Sarkissian 1 year ago
parent 645beb7171
commit 71f4377f37
  1. 19
      PadelClub/Data/GroupStage.swift
  2. 10
      PadelClub/Data/Match.swift
  3. 11
      PadelClub/Data/Tournament.swift
  4. 8
      PadelClub/Utils/HtmlService.swift
  5. 2
      PadelClub/Utils/Tips.swift
  6. 70
      PadelClub/Views/GroupStage/GroupStageView.swift
  7. 13
      PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift

@ -170,21 +170,20 @@ class GroupStage: ModelObject, Storable {
return _matches().first(where: { matchIndexes.contains($0.index) }) return _matches().first(where: { matchIndexes.contains($0.index) })
} }
func availableToStart() -> [Match] { func availableToStart(playedMatches: [Match], in runningMatches: [Match]) -> [Match] {
let runningMatches = runningMatches() return playedMatches.filter({ $0.canBeStarted(inMatches: runningMatches) && $0.isRunning() == false })
return playedMatches().filter({ $0.canBeStarted(inMatches: runningMatches) && $0.isRunning() == false })
} }
func runningMatches() -> [Match] { func runningMatches(playedMatches: [Match]) -> [Match] {
playedMatches().filter({ $0.isRunning() }) playedMatches.filter({ $0.isRunning() })
} }
func readyMatches() -> [Match] { func readyMatches(playedMatches: [Match]) -> [Match] {
playedMatches().filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false }) playedMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false })
} }
func finishedMatches() -> [Match] { func finishedMatches(playedMatches: [Match]) -> [Match] {
playedMatches().filter({ $0.hasEnded() }) playedMatches.filter({ $0.hasEnded() })
} }
private func _matchOrder() -> [Int] { private func _matchOrder() -> [Int] {
@ -366,7 +365,7 @@ extension GroupStage: Selectable {
} }
func badgeValue() -> Int? { func badgeValue() -> Int? {
runningMatches().count runningMatches(playedMatches: playedMatches()).count
} }
func badgeValueColor() -> Color? { func badgeValueColor() -> Color? {

@ -486,9 +486,9 @@ class Match: ModelObject, Storable {
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
followingMatch()?.resetTeamScores()
_loserMatch()?.resetTeamScores()
} }
followingMatch()?.resetTeamScores()
_loserMatch()?.resetTeamScores()
} }
func createTeamScores() -> [TeamScore] { func createTeamScores() -> [TeamScore] {
@ -568,15 +568,15 @@ class Match: ModelObject, Storable {
} }
func canBeStarted(inMatches matches: [Match]) -> Bool { func canBeStarted(inMatches matches: [Match]) -> Bool {
let teams = teams() let teams = teamScores
guard teams.count == 2 else { return false } guard teams.count == 2 else { return false }
guard hasEnded() == false else { return false } guard hasEnded() == false else { return false }
guard hasStarted() == false else { return false } guard hasStarted() == false else { return false }
return teams.allSatisfy({ $0.canPlay() && isTeamPlaying($0, inMatches: matches) == false }) return teams.compactMap({ $0.team }).allSatisfy({ $0.canPlay() && isTeamPlaying($0, inMatches: matches) == false })
} }
func isTeamPlaying(_ team: TeamRegistration, inMatches matches: [Match]) -> Bool { func isTeamPlaying(_ team: TeamRegistration, inMatches matches: [Match]) -> Bool {
matches.filter({ $0.teams().contains(team) }).isEmpty == false matches.filter({ $0.teamScores.compactMap { $0.teamRegistration }.contains(team.id) }).isEmpty == false
} }
var computedStartDateForSorting: Date { var computedStartDateForSorting: Date {

@ -1399,15 +1399,14 @@ class Tournament : ModelObject, Storable {
print("Position \(index+1) Poule \(groupStages[jIndex].index)") print("Position \(index+1) Poule \(groupStages[jIndex].index)")
chunks[index][jIndex].groupStage = groupStages[jIndex].id chunks[index][jIndex].groupStage = groupStages[jIndex].id
chunks[index][jIndex].groupStagePosition = index chunks[index][jIndex].groupStagePosition = index
do {
try DataStore.shared.teamRegistrations.addOrUpdate(instance: chunks[index][jIndex])
} catch {
Logger.error(error)
}
} }
} }
do {
try DataStore.shared.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams())
} catch {
Logger.error(error)
}
groupStages.forEach { $0.buildMatches() } groupStages.forEach { $0.buildMatches() }
} }

@ -84,7 +84,7 @@ enum HtmlService {
if let playerOne = entrant.players()[safe: 0] { if let playerOne = entrant.players()[safe: 0] {
template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel()) template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel())
if withRank { if withRank {
template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank())") template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank()))")
} else { } else {
template = template.replacingOccurrences(of: "{{weightOne}}", with: "") template = template.replacingOccurrences(of: "{{weightOne}}", with: "")
} }
@ -93,7 +93,7 @@ enum HtmlService {
if let playerTwo = entrant.players()[safe: 1] { if let playerTwo = entrant.players()[safe: 1] {
template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel()) template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel())
if withRank { if withRank {
template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank())") template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank()))")
} else { } else {
template = template.replacingOccurrences(of: "{{weightTwo}}", with: "") template = template.replacingOccurrences(of: "{{weightTwo}}", with: "")
} }
@ -135,7 +135,7 @@ enum HtmlService {
if let playerOne = entrant.players()[safe: 0] { if let playerOne = entrant.players()[safe: 0] {
template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel(.short)) template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel(.short))
if withRank { if withRank {
template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank())") template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank()))")
} else { } else {
template = template.replacingOccurrences(of: "{{weightOne}}", with: "") template = template.replacingOccurrences(of: "{{weightOne}}", with: "")
} }
@ -144,7 +144,7 @@ enum HtmlService {
if let playerTwo = entrant.players()[safe: 1] { if let playerTwo = entrant.players()[safe: 1] {
template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel(.short)) template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel(.short))
if withRank { if withRank {
template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank())") template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank()))")
} else { } else {
template = template.replacingOccurrences(of: "{{weightTwo}}", with: "") template = template.replacingOccurrences(of: "{{weightTwo}}", with: "")
} }

@ -336,7 +336,7 @@ struct TournamentPublishingTip: Tip {
} }
var message: Text? { var message: Text? {
Text("Padel Club vous permet de publier votre tournoi et rendre accessible à tous les résultats des matchs et l'évolution de l'événement. Les informations seront accessible sur le site Padel Club.") Text("Padel Club vous permet de publier votre tournoi et rendre accessible à tous les résultats des matchs et l'évolution de l'événement. Les informations seront accessibles sur le site Padel Club.")
} }
var image: Image? { var image: Image? {

@ -16,9 +16,11 @@ struct GroupStageView: View {
@State private var confirmRemoveAll: Bool = false @State private var confirmRemoveAll: Bool = false
@State private var confirmResetMatch: Bool = false @State private var confirmResetMatch: Bool = false
@State private var groupStageName: String = "" @State private var groupStageName: String = ""
let playedMatches: [Match]
init(groupStage: GroupStage) { init(groupStage: GroupStage) {
self.groupStage = groupStage self.groupStage = groupStage
self.playedMatches = groupStage.playedMatches()
_groupStageName = State(wrappedValue: groupStage.groupStageTitle()) _groupStageName = State(wrappedValue: groupStage.groupStageTitle())
} }
@ -44,21 +46,18 @@ struct GroupStageView: View {
} }
.headerProminence(.increased) .headerProminence(.increased)
MatchListView(section: "disponible", matches: groupStage.availableToStart()).id(UUID()) let runningMatches = groupStage.runningMatches(playedMatches: playedMatches)
MatchListView(section: "en cours", matches: groupStage.runningMatches()).id(UUID()) MatchListView(section: "disponible", matches: groupStage.availableToStart(playedMatches: playedMatches, in: runningMatches))
MatchListView(section: "à lancer", matches: groupStage.readyMatches()).id(UUID()) MatchListView(section: "en cours", matches: runningMatches)
MatchListView(section: "terminés", matches: groupStage.finishedMatches(), isExpanded: false).id(UUID()) MatchListView(section: "à lancer", matches: groupStage.readyMatches(playedMatches: playedMatches))
} MatchListView(section: "terminés", matches: groupStage.finishedMatches(playedMatches: playedMatches), isExpanded: false)
.onChange(of: groupStageName) {
groupStage.name = groupStageName
_save()
} }
.toolbar { .toolbar {
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
_groupStageMenuView() _groupStageMenuView()
} }
} }
.navigationTitle($groupStageName) .navigationTitle(groupStage.groupStageTitle())
} }
private enum GroupStageSortingMode { private enum GroupStageSortingMode {
@ -178,12 +177,10 @@ struct GroupStageView: View {
private func _groupStageMenuView() -> some View { private func _groupStageMenuView() -> some View {
Menu { Menu {
if groupStage.name != nil { NavigationLink {
Button("Retirer le nom") { GroupStageNameEditionView(groupStage: groupStage)
groupStage.name = nil } label: {
groupStageName = groupStage.groupStageTitle() Label("Renommer", systemImage: "pencil")
_save()
}
} }
Button("Retirer tout le monde", role: .destructive) { Button("Retirer tout le monde", role: .destructive) {
confirmRemoveAll = true confirmRemoveAll = true
@ -225,3 +222,46 @@ struct GroupStageView: View {
} }
} }
} }
struct GroupStageNameEditionView: View {
@EnvironmentObject var dataStore: DataStore
let groupStage: GroupStage
@State private var groupStageName: String = ""
var body: some View {
Form {
Section {
TextField("Nom de la poule", text: $groupStageName)
.keyboardType(.alphabet)
.frame(maxWidth: .infinity)
.onAppear(perform: {
groupStageName = groupStage.name ?? ""
})
.onSubmit {
groupStageName = groupStageName.trimmed
groupStage.name = groupStageName
_save()
}
} footer: {
HStack {
Spacer()
FooterButtonView("retirer le nom") {
groupStage.name = nil
groupStageName = groupStage.groupStageTitle()
_save()
}
}
}
}
.navigationTitle(groupStage.groupStageTitle())
.toolbarBackground(.visible, for: .navigationBar)
}
private func _save() {
do {
try dataStore.groupStages.addOrUpdate(instance: groupStage)
} catch {
Logger.error(error)
}
}
}

@ -608,15 +608,20 @@ struct InscriptionManagerView: View {
Section { Section {
let unsortedTeams = tournament.unsortedTeams() let unsortedTeams = tournament.unsortedTeams()
let walkoutTeams = tournament.walkoutTeams() let walkoutTeams = tournament.walkoutTeams()
let unsortedTeamsWithoutWO = tournament.unsortedTeamsWithoutWO()
LabeledContent { LabeledContent {
Text(unsortedTeams.count.formatted() + "/" + tournament.teamCount.formatted()).font(.largeTitle) Text(unsortedTeamsWithoutWO.count.formatted() + "/" + tournament.teamCount.formatted()).font(.largeTitle)
} label: { } label: {
Text("Paire\(unsortedTeams.count.pluralSuffix) inscrite\(unsortedTeams.count.pluralSuffix)") Text("Paire\(unsortedTeamsWithoutWO.count.pluralSuffix) inscrite\(unsortedTeamsWithoutWO.count.pluralSuffix)")
Text("dont \(walkoutTeams.count) forfait\(walkoutTeams.count.pluralSuffix)") }
LabeledContent {
Text(walkoutTeams.count.formatted()).font(.largeTitle)
} label: {
Text("Forfait\(walkoutTeams.count.pluralSuffix)")
} }
let unsortedTeamsWithoutWO = tournament.unsortedTeamsWithoutWO()
LabeledContent { LabeledContent {
Text(max(0, unsortedTeamsWithoutWO.count - tournament.teamCount).formatted()).font(.largeTitle) Text(max(0, unsortedTeamsWithoutWO.count - tournament.teamCount).formatted()).font(.largeTitle)
} label: { } label: {

Loading…
Cancel
Save