fix player reg in team score stuff

multistore
Razmig Sarkissian 1 year ago
parent f72ed21afa
commit 4f63a2ae34
  1. 22
      PadelClub/Data/Match.swift
  2. 2
      PadelClub/Data/TeamScore.swift
  3. 2
      PadelClub/Data/Tournament.swift
  4. 22
      PadelClub/Utils/FileImportManager.swift
  5. 13
      PadelClub/Views/Calling/Components/MenuWarningView.swift
  6. 10
      PadelClub/Views/Player/Components/EditablePlayerView.swift
  7. 10
      PadelClub/Views/Tournament/FileImportView.swift

@ -344,7 +344,7 @@ class Match: ModelObject, Storable {
func followingMatch() -> Match? { func followingMatch() -> Match? {
guard let nextRoundId = roundObject?.nextRound()?.id else { return nil } guard let nextRoundId = roundObject?.nextRound()?.id else { return nil }
return Store.main.filter(isIncluded: { $0.round == nextRoundId && $0.index == index / 2 }).first return Store.main.filter(isIncluded: { $0.round == nextRoundId && $0.index == (index - 1) / 2 }).first
} }
func getDuration() -> Int { func getDuration() -> Int {
@ -478,16 +478,17 @@ class Match: ModelObject, Storable {
_loserMatch()?.updateTeamScores() _loserMatch()?.updateTeamScores()
} }
func resetTeamScores() { func resetTeamScores(outsideOf newTeamScores: [TeamScore]) {
let teamScores = teamScores let ids = newTeamScores.map { $0.id }
let teamScores = teamScores.filter({ ids.contains($0.id) == false })
if teamScores.isEmpty == false { if teamScores.isEmpty == false {
do { do {
try DataStore.shared.teamScores.delete(contentOfs: teamScores) try DataStore.shared.teamScores.delete(contentOfs: teamScores)
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
followingMatch()?.resetTeamScores() followingMatch()?.resetTeamScores(outsideOf: [])
_loserMatch()?.resetTeamScores() _loserMatch()?.resetTeamScores(outsideOf: [])
} }
} }
@ -498,14 +499,21 @@ class Match: ModelObject, Storable {
return teams return teams
} }
func getOrCreateTeamScores() -> [TeamScore] {
let teamOne = team(.one)
let teamTwo = team(.two)
let teams = [teamOne, teamTwo].compactMap({ $0 }).map { teamScore(ofTeam: $0) ?? TeamScore(match: id, team: $0) }
return teams
}
func updateTeamScores() { func updateTeamScores() {
resetTeamScores() let teams = getOrCreateTeamScores()
let teams = createTeamScores()
do { do {
try DataStore.shared.teamScores.addOrUpdate(contentOfs: teams) try DataStore.shared.teamScores.addOrUpdate(contentOfs: teams)
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
resetTeamScores(outsideOf: teams)
if teams.isEmpty == false { if teams.isEmpty == false {
updateFollowingMatchTeamScore() updateFollowingMatchTeamScore()
} }

@ -35,7 +35,7 @@ class TeamScore: ModelObject, Storable {
self.match = match self.match = match
if let team { if let team {
self.teamRegistration = team.id self.teamRegistration = team.id
self.playerRegistrations = team.players().map { $0.id } //self.playerRegistrations = team.players().map { $0.id }
} }
self.score = nil self.score = nil
self.walkOut = nil self.walkOut = nil

@ -1317,7 +1317,7 @@ class Tournament : ModelObject, Storable {
func resetTeamScores(in matchOfBracketPosition: Int?) { func resetTeamScores(in matchOfBracketPosition: Int?) {
guard let match = match(for: matchOfBracketPosition) else { return } guard let match = match(for: matchOfBracketPosition) else { return }
match.resetTeamScores() match.resetTeamScores(outsideOf: [])
} }
func updateTeamScores(in matchOfBracketPosition: Int?) { func updateTeamScores(in matchOfBracketPosition: Int?) {

@ -8,6 +8,17 @@
import Foundation import Foundation
import LeStorage import LeStorage
enum FileImportManagerError: LocalizedError {
case unknownFormat
var errorDescription: String? {
switch self {
case .unknownFormat:
return "Format non reconnu"
}
}
}
class FileImportManager { class FileImportManager {
static let shared = FileImportManager() static let shared = FileImportManager()
@ -138,11 +149,11 @@ class FileImportManager {
static let FFT_ASSIMILATION_WOMAN_IN_MAN = "A calculer selon la pondération en vigueur" static let FFT_ASSIMILATION_WOMAN_IN_MAN = "A calculer selon la pondération en vigueur"
func createTeams(from fileContent: String, tournament: Tournament, fileProvider: FileProvider = .frenchFederation) async -> [TeamHolder] { func createTeams(from fileContent: String, tournament: Tournament, fileProvider: FileProvider = .frenchFederation) async throws -> [TeamHolder] {
switch fileProvider { switch fileProvider {
case .frenchFederation: case .frenchFederation:
return await _getFederalTeams(from: fileContent, tournament: tournament) return try await _getFederalTeams(from: fileContent, tournament: tournament)
case .padelClub: case .padelClub:
return await _getPadelClubTeams(from: fileContent, tournament: tournament) return await _getPadelClubTeams(from: fileContent, tournament: tournament)
case .unknown: case .unknown:
@ -192,7 +203,7 @@ class FileImportManager {
} }
} }
private func _getFederalTeams(from fileContent: String, tournament: Tournament) async -> [TeamHolder] { private func _getFederalTeams(from fileContent: String, tournament: Tournament) async throws -> [TeamHolder] {
let lines = fileContent.components(separatedBy: "\n") let lines = fileContent.components(separatedBy: "\n")
guard let firstLine = lines.first else { return [] } guard let firstLine = lines.first else { return [] }
var separator = "," var separator = ","
@ -200,6 +211,11 @@ class FileImportManager {
separator = ";" separator = ";"
} }
let headerCount = firstLine.components(separatedBy: separator).count let headerCount = firstLine.components(separatedBy: separator).count
guard headerCount > 12 else {
throw FileImportManagerError.unknownFormat
}
var results: [TeamHolder] = [] var results: [TeamHolder] = []
if headerCount <= 18 { if headerCount <= 18 {
Array(lines.dropFirst()).chunked(into: 2).forEach { teamLines in Array(lines.dropFirst()).chunked(into: 2).forEach { teamLines in

@ -26,6 +26,19 @@ struct MenuWarningView: View {
// TODO: Guard // TODO: Guard
@ViewBuilder @ViewBuilder
private func _actionView(players: [PlayerRegistration], privateMode: Bool = false) -> some View { private func _actionView(players: [PlayerRegistration], privateMode: Bool = false) -> some View {
Menu {
ForEach(players) { player in
if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "tel:\(number)") {
Link(destination: url) {
Text(player.playerLabel(.short))
Label(number, systemImage: "phone")
}
}
}
} label: {
Label("Appeler un joueur", systemImage: "phone")
}
Button("Message") { Button("Message") {
contactType = .message(date: date, recipients: players.compactMap({ $0.phoneNumber }), body: message, tournamentBuild: nil) contactType = .message(date: date, recipients: players.compactMap({ $0.phoneNumber }), body: message, tournamentBuild: nil)
} }

@ -86,14 +86,22 @@ struct EditablePlayerView: View {
if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "tel:\(number)") { if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "tel:\(number)") {
Link(destination: url) { Link(destination: url) {
Label("Appeler", systemImage: "phone") Label("Appeler", systemImage: "phone")
Text(number)
} }
} }
if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "sms:\(number)") { if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "sms:\(number)") {
Link(destination: url) { Link(destination: url) {
Label("SMS", systemImage: "message") Label("SMS", systemImage: "message")
Text(number)
} }
} }
if let mail = player.email, let url = URL(string: "mailto:\(mail)") {
Link(destination: url) {
Label("SMS", systemImage: "message")
Text(mail)
}
}
if editingOptions.contains(.name) { if editingOptions.contains(.name) {
Divider() Divider()
Button("Modifier le prénom") { Button("Modifier le prénom") {

@ -53,7 +53,11 @@ struct FileImportView: View {
RowButtonView("Démarrer l'importation") { RowButtonView("Démarrer l'importation") {
if let fileContent { if let fileContent {
await _startImport(fileContent: fileContent) do {
try await _startImport(fileContent: fileContent)
} catch {
errorMessage = error.localizedDescription
}
} }
} }
.disabled(fileContent == nil || convertingFile) .disabled(fileContent == nil || convertingFile)
@ -249,7 +253,7 @@ struct FileImportView: View {
} }
} }
func _startImport(fileContent: String) async { func _startImport(fileContent: String) async throws {
await MainActor.run { await MainActor.run {
errorMessage = nil errorMessage = nil
teams.removeAll() teams.removeAll()
@ -258,7 +262,7 @@ struct FileImportView: View {
await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: rankSourceDate) await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: rankSourceDate)
} }
self.teams = await FileImportManager.shared.createTeams(from: fileContent, tournament: tournament, fileProvider: fileProvider) self.teams = try await FileImportManager.shared.createTeams(from: fileContent, tournament: tournament, fileProvider: fileProvider)
await MainActor.run { await MainActor.run {
didImport = true didImport = true
} }

Loading…
Cancel
Save