fix missing teamScores

multistore
Razmig Sarkissian 1 year ago
parent 9f05ad4863
commit f8d634a76e
  1. 4
      PadelClub/Data/GroupStage.swift
  2. 37
      PadelClub/Data/Match.swift
  3. 4
      PadelClub/Data/TeamRegistration.swift
  4. 29
      PadelClub/Data/Tournament.swift
  5. 2
      PadelClub/Views/GroupStage/Components/GroupStageTeamView.swift
  6. 9
      PadelClub/Views/GroupStage/GroupStageSettingsView.swift
  7. 2
      PadelClub/Views/GroupStage/GroupStageView.swift
  8. 2
      PadelClub/Views/Match/MatchSetupView.swift
  9. 2
      PadelClub/Views/Planning/PlanningSettingsView.swift
  10. 2
      PadelClub/Views/Round/LoserRoundView.swift
  11. 5
      PadelClub/Views/Round/RoundSettingsView.swift
  12. 2
      PadelClub/Views/Round/RoundView.swift
  13. 8
      PadelClub/Views/Subscription/Guard.swift
  14. 2
      PadelClub/Views/Tournament/TournamentBuildView.swift

@ -80,6 +80,7 @@ class GroupStage: ModelObject, Storable {
var _matches = [Match]() var _matches = [Match]()
for i in 0..<_numberOfMatchesToBuild() { for i in 0..<_numberOfMatchesToBuild() {
let newMatch = Match(groupStage: id, index: i, matchFormat: matchFormat, name: localizedMatchUpLabel(for: i)) let newMatch = Match(groupStage: id, index: i, matchFormat: matchFormat, name: localizedMatchUpLabel(for: i))
newMatch.updateTeamScores()
_matches.append(newMatch) _matches.append(newMatch)
} }
@ -104,6 +105,7 @@ class GroupStage: ModelObject, Storable {
for (index, team) in teams.enumerated() { for (index, team) in teams.enumerated() {
team.qualified = index < tournament.qualifiedPerGroupStage team.qualified = index < tournament.qualifiedPerGroupStage
if team.bracketPosition != nil && team.qualified == false { if team.bracketPosition != nil && team.qualified == false {
tournamentObject()?.resetTeamScores(in: team.bracketPosition)
team.bracketPosition = nil team.bracketPosition = nil
} }
} }
@ -224,7 +226,7 @@ class GroupStage: ModelObject, Storable {
} }
private func _removeMatches() { private func _removeMatches() {
try? deleteDependencies() try? DataStore.shared.matches.delete(contentOfs: _matches())
} }
private func _numberOfMatchesToBuild() -> Int { private func _numberOfMatchesToBuild() -> Int {

@ -58,7 +58,7 @@ class Match: ModelObject, Storable {
func indexInRound() -> Int { func indexInRound() -> Int {
if groupStage != nil { if groupStage != nil {
return index return index
} else if let index = roundObject?.playedMatches().firstIndex(where: { $0.id == id }) { } else if let index = roundObject?.playedMatches().sorted(by: \.index).firstIndex(where: { $0.id == id }) {
return index return index
} }
return RoundRule.matchIndexWithinRound(fromMatchIndex: index) return RoundRule.matchIndexWithinRound(fromMatchIndex: index)
@ -400,6 +400,7 @@ class Match: ModelObject, Storable {
losingTeamId = teamScoreWalkout.teamRegistration losingTeamId = teamScoreWalkout.teamRegistration
groupStageObject?.updateGroupStageState() groupStageObject?.updateGroupStageState()
roundObject?.updateTournamentState() roundObject?.updateTournamentState()
updateFollowingMatchTeamScore()
} }
func setScore(fromMatchDescriptor matchDescriptor: MatchDescriptor) { func setScore(fromMatchDescriptor matchDescriptor: MatchDescriptor) {
@ -411,6 +412,7 @@ class Match: ModelObject, Storable {
losingTeamId = team(matchDescriptor.winner.otherTeam)?.id losingTeamId = team(matchDescriptor.winner.otherTeam)?.id
groupStageObject?.updateGroupStageState() groupStageObject?.updateGroupStageState()
roundObject?.updateTournamentState() roundObject?.updateTournamentState()
updateFollowingMatchTeamScore()
} }
func updateScore(fromMatchDescriptor matchDescriptor: MatchDescriptor) { func updateScore(fromMatchDescriptor matchDescriptor: MatchDescriptor) {
@ -422,6 +424,39 @@ class Match: ModelObject, Storable {
matchFormat = matchDescriptor.matchFormat matchFormat = matchDescriptor.matchFormat
} }
func updateFollowingMatchTeamScore() {
followingMatch()?.updateTeamScores()
_loserMatch()?.updateTeamScores()
}
func resetTeamScores() {
let teamScores = teamScores
if teamScores.isEmpty == false {
do {
try DataStore.shared.teamScores.delete(contentOfs: teamScores)
} catch {
Logger.error(error)
}
}
followingMatch()?.resetTeamScores()
_loserMatch()?.resetTeamScores()
}
func updateTeamScores() {
resetTeamScores()
let teamOne = team(.one)
let teamTwo = team(.two)
let teams = [teamOne, teamTwo].compactMap({ $0 }).map { TeamScore(match: id, team: $0) }
do {
try DataStore.shared.teamScores.addOrUpdate(contentOfs: teams)
} catch {
Logger.error(error)
}
if teams.isEmpty == false {
updateFollowingMatchTeamScore()
}
}
func validateMatch(fromStartDate: Date, toEndDate: Date, fieldSetup: MatchFieldSetup) { func validateMatch(fromStartDate: Date, toEndDate: Date, fieldSetup: MatchFieldSetup) {
if hasEnded() == false { if hasEnded() == false {
startDate = fromStartDate startDate = fromStartDate

@ -61,7 +61,9 @@ class TeamRegistration: ModelObject, Storable {
func setSeedPosition(inSpot match: Match, slot: TeamPosition?, opposingSeeding: Bool) { func setSeedPosition(inSpot match: Match, slot: TeamPosition?, opposingSeeding: Bool) {
let seedPosition = match.lockAndGetSeedPosition(atTeamPosition: slot, opposingSeeding: opposingSeeding) let seedPosition = match.lockAndGetSeedPosition(atTeamPosition: slot, opposingSeeding: opposingSeeding)
tournamentObject()?.resetTeamScores(in: bracketPosition)
bracketPosition = seedPosition bracketPosition = seedPosition
tournamentObject()?.updateTeamScores(in: bracketPosition)
} }
func expectedSummonDate() -> Date? { func expectedSummonDate() -> Date? {
@ -183,8 +185,10 @@ class TeamRegistration: ModelObject, Storable {
} }
func resetPositions() { func resetPositions() {
groupStageObject()?._matches().forEach({ $0.updateTeamScores() })
groupStage = nil groupStage = nil
groupStagePosition = nil groupStagePosition = nil
tournamentObject()?.resetTeamScores(in: bracketPosition)
bracketPosition = nil bracketPosition = nil
} }

@ -315,7 +315,7 @@ class Tournament : ModelObject, Storable {
return first return first
} }
} else { } else {
return nil return startDate
} }
} }
@ -336,7 +336,7 @@ class Tournament : ModelObject, Storable {
return first return first
} }
} else { } else {
return nil return startDate
} }
} }
@ -1137,7 +1137,6 @@ class Tournament : ModelObject, Storable {
try? DataStore.shared.groupStages.addOrUpdate(contentOfs: _groupStages) try? DataStore.shared.groupStages.addOrUpdate(contentOfs: _groupStages)
groupStages().forEach { $0.buildMatches() }
refreshGroupStages() refreshGroupStages()
} }
@ -1173,6 +1172,26 @@ class Tournament : ModelObject, Storable {
round.buildLoserBracket() round.buildLoserBracket()
} }
} }
func match(for bracketPosition: Int?) -> Match? {
guard let bracketPosition else { return nil }
let matchIndex = bracketPosition / 2
let roundIndex = RoundRule.roundIndex(fromMatchIndex: matchIndex)
if let round : Round = Store.main.filter(isIncluded: { $0.tournament == id && $0.index == roundIndex }).first {
return Store.main.filter(isIncluded: { $0.round == round.id && $0.index == matchIndex }).first
}
return nil
}
func resetTeamScores(in matchOfBracketPosition: Int?) {
guard let match = match(for: matchOfBracketPosition) else { return }
match.resetTeamScores()
}
func updateTeamScores(in matchOfBracketPosition: Int?) {
guard let match = match(for: matchOfBracketPosition) else { return }
match.updateTeamScores()
}
func deleteStructure() { func deleteStructure() {
do { do {
@ -1234,7 +1253,7 @@ class Tournament : ModelObject, Storable {
} }
let max = groupStages.map { $0.size }.reduce(0,+) let max = groupStages.map { $0.size }.reduce(0,+)
var chunks = selectedSortedTeams().suffix(max).chunked(into: numberOfBracketsAsInt) var chunks = selectedSortedTeams().suffix(max).chunked(into: groupStageCount)
for (index, _) in chunks.enumerated() { for (index, _) in chunks.enumerated() {
if randomize { if randomize {
chunks[index].shuffle() chunks[index].shuffle()
@ -1251,6 +1270,8 @@ class Tournament : ModelObject, Storable {
try? DataStore.shared.teamRegistrations.addOrUpdate(instance: chunks[index][jIndex]) try? DataStore.shared.teamRegistrations.addOrUpdate(instance: chunks[index][jIndex])
} }
} }
groupStages.forEach { $0.buildMatches() }
} }

@ -59,6 +59,7 @@ struct GroupStageTeamView: View {
} else { } else {
RowButtonView("Annuler la qualification", role: .destructive) { RowButtonView("Annuler la qualification", role: .destructive) {
team.qualified = false team.qualified = false
groupStage.tournamentObject()?.resetTeamScores(in: team.bracketPosition)
team.bracketPosition = nil team.bracketPosition = nil
_save() _save()
} }
@ -71,6 +72,7 @@ struct GroupStageTeamView: View {
team.groupStagePosition = nil team.groupStagePosition = nil
team.groupStage = nil team.groupStage = nil
_save() _save()
groupStage._matches().forEach({ $0.updateTeamScores() })
} }
} }
} }

@ -27,6 +27,15 @@ struct GroupStageSettingsView: View {
var body: some View { var body: some View {
List { List {
#if DEBUG
Section {
RowButtonView("delete all group stages") {
tournament.deleteGroupStages()
}
}
#endif
if tournament.unsortedTeams().filter({ $0.groupStagePosition != nil }).isEmpty == false { if tournament.unsortedTeams().filter({ $0.groupStagePosition != nil }).isEmpty == false {
Section { Section {
menuBuildAllGroupStages menuBuildAllGroupStages

@ -163,6 +163,7 @@ struct GroupStageView: View {
print(team.pasteData()) print(team.pasteData())
team.groupStage = groupStage.id team.groupStage = groupStage.id
team.groupStagePosition = index team.groupStagePosition = index
groupStage._matches().forEach({ $0.updateTeamScores() })
do { do {
try dataStore.teamRegistrations.addOrUpdate(instance: team) try dataStore.teamRegistrations.addOrUpdate(instance: team)
} catch { } catch {
@ -199,6 +200,7 @@ struct GroupStageView: View {
teams.forEach { team in teams.forEach { team in
team.groupStagePosition = nil team.groupStagePosition = nil
team.groupStage = nil team.groupStage = nil
groupStage._matches().forEach({ $0.updateTeamScores() })
} }
do { do {
try dataStore.teamRegistrations.addOrUpdate(contentOfs: teams) try dataStore.teamRegistrations.addOrUpdate(contentOfs: teams)

@ -31,8 +31,10 @@ struct MatchSetupView: View {
TeamRowView(team: team, teamPosition: teamPosition) TeamRowView(team: team, teamPosition: teamPosition)
.swipeActions(edge: .trailing, allowsFullSwipe: false) { .swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button(role: .cancel) { Button(role: .cancel) {
//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() match.enableMatch()
try? dataStore.matches.addOrUpdate(instance: match) try? dataStore.matches.addOrUpdate(instance: match)
try? dataStore.teamRegistrations.addOrUpdate(instance: team) try? dataStore.teamRegistrations.addOrUpdate(instance: team)

@ -153,7 +153,7 @@ struct PlanningSettingsView: View {
@ViewBuilder @ViewBuilder
private func _optionsView() -> some View { private func _optionsView() -> some View {
if tournament.groupStages().isEmpty == false { if tournament.groupStageCount > 0 {
Section { Section {
TournamentFieldsManagerView(localizedStringKey: "Poule en parallèle", count: $groupStageChunkCount, max: tournament.groupStageCount) TournamentFieldsManagerView(localizedStringKey: "Poule en parallèle", count: $groupStageChunkCount, max: tournament.groupStageCount)
.onChange(of: groupStageChunkCount) { .onChange(of: groupStageChunkCount) {

@ -27,7 +27,7 @@ struct LoserRoundView: View {
ForEach(loserRounds) { loserRound in ForEach(loserRounds) { loserRound in
if isEditingTournamentSeed || loserRound.isDisabled() == false { if isEditingTournamentSeed || loserRound.isDisabled() == false {
Section { Section {
let matches = isEditingTournamentSeed ? loserRound.playedMatches() : loserRound.playedMatches().filter({ $0.disabled == false }) let matches = (isEditingTournamentSeed ? loserRound.playedMatches() : loserRound.playedMatches().filter({ $0.disabled == false })).sorted(by: \.index)
ForEach(matches) { match in ForEach(matches) { match in
MatchRowView(match: match, matchViewStyle: .sectionedStandardStyle) MatchRowView(match: match, matchViewStyle: .sectionedStandardStyle)
.overlay { .overlay {

@ -26,7 +26,10 @@ struct RoundSettingsView: View {
// } // }
Section { Section {
RowButtonView("Retirer toutes les têtes de séries", role: .destructive) { RowButtonView("Retirer toutes les têtes de séries", role: .destructive) {
tournament.unsortedTeams().forEach({ $0.bracketPosition = nil }) tournament.unsortedTeams().forEach({ team in
tournament.resetTeamScores(in: team.bracketPosition)
team.bracketPosition = nil
})
try? dataStore.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams()) try? dataStore.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams())
tournament.allRounds().forEach({ round in tournament.allRounds().forEach({ round in
round.enableRound() round.enableRound()

@ -28,7 +28,7 @@ struct RoundView: View {
let loserRounds = round.loserRounds() let loserRounds = round.loserRounds()
let availableQualifiedTeams = tournament.availableQualifiedTeams() let availableQualifiedTeams = tournament.availableQualifiedTeams()
let displayableMatches = round.displayableMatches() let displayableMatches = round.displayableMatches().sorted(by: \.index)
let spaceLeft = displayableMatches.filter({ $0.hasSpaceLeft() }) let spaceLeft = displayableMatches.filter({ $0.hasSpaceLeft() })
if isEditingTournamentSeed.wrappedValue == false { if isEditingTournamentSeed.wrappedValue == false {
//(where: { $0.isDisabled() == false || isEditingTournamentSeed.wrappedValue }) //(where: { $0.isDisabled() == false || isEditingTournamentSeed.wrappedValue })

@ -145,14 +145,14 @@ import LeStorage
} }
var currentPlan: StoreItem? { var currentPlan: StoreItem? {
// #if DEBUG #if DEBUG
// return nil return .monthlyUnlimited
// #else #else
if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) { if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) {
return plan return plan
} }
return nil return nil
// #endif #endif
} }
func userFilteredPurchases() -> [StoreKit.Transaction] { func userFilteredPurchases() -> [StoreKit.Transaction] {

@ -57,7 +57,7 @@ struct TournamentBuildView: View {
} }
Section { Section {
if tournament.groupStages().isEmpty == false { if tournament.groupStageCount > 0 {
NavigationLink(value: Screen.groupStage) { NavigationLink(value: Screen.groupStage) {
LabeledContent { LabeledContent {
Text(tournament.groupStageStatus()) Text(tournament.groupStageStatus())

Loading…
Cancel
Save