diff --git a/PadelClub/Data/DateInterval.swift b/PadelClub/Data/DateInterval.swift index a0f2d66..a82036f 100644 --- a/PadelClub/Data/DateInterval.swift +++ b/PadelClub/Data/DateInterval.swift @@ -53,5 +53,9 @@ class DateInterval: ModelObject, Storable { case _startDate = "startDate" case _endDate = "endDate" } - + + func insertOnServer() throws { + try DataStore.shared.dateIntervals.writeChangeAndInsertOnServer(instance: self) + } + } diff --git a/PadelClub/Data/Event.swift b/PadelClub/Data/Event.swift index bae99a8..f6c4f92 100644 --- a/PadelClub/Data/Event.swift +++ b/PadelClub/Data/Event.swift @@ -84,7 +84,10 @@ class Event: ModelObject, Storable { func insertOnServer() throws { try DataStore.shared.events.writeChangeAndInsertOnServer(instance: self) for tournament in self.tournaments { -// try tournament.insertOnServer() + try tournament.insertOnServer() + } + for dataInterval in self.courtsUnavailability { + try dataInterval.insertOnServer() } } diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index 78dc605..58d15ba 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -1,5 +1,5 @@ // -// GroupStage_v2.swift +// GroupStage.swift // Padel Tournament // // Created by razmig on 10/03/2024. @@ -41,12 +41,20 @@ class GroupStage: ModelObject, Storable { self.name = name } - func teamAt(groupStagePosition: Int) -> TeamRegistration? { - teams().first(where: { $0.groupStagePosition == groupStagePosition }) + // MARK: - Computed dependencies + + func _matches() -> [Match] { + Store.main.filter { $0.groupStage == self.id } } func tournamentObject() -> Tournament? { - Store.main.findById(tournament) + Store.main.findById(self.tournament) + } + + // MARK: - + + func teamAt(groupStagePosition: Int) -> TeamRegistration? { + teams().first(where: { $0.groupStagePosition == groupStagePosition }) } func groupStageTitle(_ displayStyle: DisplayStyle = .wide) -> String { @@ -244,10 +252,6 @@ class GroupStage: ModelObject, Storable { (size * (size - 1)) / 2 } - func _matches() -> [Match] { - Store.main.filter { $0.groupStage == self.id } - } - func unsortedPlayers() -> [PlayerRegistration] { unsortedTeams().flatMap({ $0.unsortedPlayers() }) } @@ -345,6 +349,13 @@ class GroupStage: ModelObject, Storable { try container.encodeNil(forKey: ._name) } } + + func insertOnServer() throws { + try DataStore.shared.groupStages.writeChangeAndInsertOnServer(instance: self) + for match in self._matches() { + try match.insertOnServer() + } + } } diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 9778728..492cfdd 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -1,5 +1,5 @@ // -// Match_v2.swift +// Match.swift // Padel Tournament // // Created by razmig on 10/03/2024. @@ -55,6 +55,18 @@ class Match: ModelObject, Storable { // self.order = order } + // MARK: - Computed dependencies + + var teamScores: [TeamScore] { + Store.main.filter { $0.match == self.id } + } + + // MARK: - + + override func deleteDependencies() throws { + try Store.main.deleteDependencies(items: self.teamScores) + } + func indexInRound() -> Int { if groupStage != nil { return index @@ -756,14 +768,6 @@ class Match: ModelObject, Storable { roundObject?.parent != nil } - var teamScores: [TeamScore] { - Store.main.filter { $0.match == self.id } - } - - override func deleteDependencies() throws { - try Store.main.deleteDependencies(items: self.teamScores) - } - enum CodingKeys: String, CodingKey { case _id = "id" case _round = "round" @@ -852,7 +856,14 @@ class Match: ModelObject, Storable { try container.encodeNil(forKey: ._courtIndex) } } - + + func insertOnServer() throws { + try DataStore.shared.matches.writeChangeAndInsertOnServer(instance: self) + for teamScore in self.teamScores { + try teamScore.insertOnServer() + } + } + } enum MatchDateSetup: Hashable, Identifiable { diff --git a/PadelClub/Data/PlayerRegistration.swift b/PadelClub/Data/PlayerRegistration.swift index db7f41b..595b208 100644 --- a/PadelClub/Data/PlayerRegistration.swift +++ b/PadelClub/Data/PlayerRegistration.swift @@ -471,6 +471,11 @@ class PlayerRegistration: ModelObject, Storable { return 15000 } } + + func insertOnServer() throws { + try DataStore.shared.playerRegistrations.writeChangeAndInsertOnServer(instance: self) + } + } extension PlayerRegistration: Hashable { diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index a2eddb9..7373407 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -1,5 +1,5 @@ // -// Round_v2.swift +// Round.swift // Padel Tournament // // Created by razmig on 10/03/2024. @@ -28,7 +28,19 @@ class Round: ModelObject, Storable { self.format = matchFormat self.startDate = startDate } - + + // MARK: - Computed dependencies + + func tournamentObject() -> Tournament? { + Store.main.findById(tournament) + } + + func _matches() -> [Match] { + Store.main.filter { $0.round == self.id } + } + + // MARK: - + var matchFormat: MatchFormat { get { format ?? .defaultFormatForMatchType(.bracket) @@ -47,14 +59,6 @@ class Round: ModelObject, Storable { playedMatches().allSatisfy({ $0.hasEnded() }) } - func tournamentObject() -> Tournament? { - Store.main.findById(tournament) - } - - func _matches() -> [Match] { - Store.main.filter { $0.round == self.id } - } - func upperMatches(ofMatch match: Match) -> [Match] { if parent != nil, previousRound() == nil, let parentRound { let matchIndex = match.index @@ -529,6 +533,13 @@ class Round: ModelObject, Storable { try container.encodeNil(forKey: ._startDate) } } + + func insertOnServer() throws { + try DataStore.shared.rounds.writeChangeAndInsertOnServer(instance: self) + for match in self._matches() { + try match.insertOnServer() + } + } } diff --git a/PadelClub/Data/TeamRegistration.swift b/PadelClub/Data/TeamRegistration.swift index eaf6cad..6319671 100644 --- a/PadelClub/Data/TeamRegistration.swift +++ b/PadelClub/Data/TeamRegistration.swift @@ -57,6 +57,18 @@ class TeamRegistration: ModelObject, Storable { self.qualified = qualified } + // MARK: - Computed dependencies + + func unsortedPlayers() -> [PlayerRegistration] { + Store.main.filter { $0.teamRegistration == self.id } + } + + // MARK: - + + override func deleteDependencies() throws { + try Store.main.deleteDependencies(items: self.unsortedPlayers()) + } + func isSeedable() -> Bool { bracketPosition == nil && groupStage == nil } @@ -301,10 +313,6 @@ class TeamRegistration: ModelObject, Storable { } } - func unsortedPlayers() -> [PlayerRegistration] { - Store.main.filter { $0.teamRegistration == self.id } - } - func setWeight(from players: [PlayerRegistration], inTournamentCategory tournamentCategory: TournamentCategory) { let significantPlayerCount = significantPlayerCount() weight = (players.prefix(significantPlayerCount).map { $0.computedRank } + missingPlayerType(inTournamentCategory: tournamentCategory).map { unrankValue(for: $0 == 1 ? true : false ) }).prefix(significantPlayerCount).reduce(0,+) @@ -353,10 +361,6 @@ class TeamRegistration: ModelObject, Storable { Store.main.findById(tournament) } - override func deleteDependencies() throws { - try Store.main.deleteDependencies(items: self.unsortedPlayers()) - } - enum CodingKeys: String, CodingKey { case _id = "id" case _tournament = "tournament" @@ -479,6 +483,13 @@ class TeamRegistration: ModelObject, Storable { } } + func insertOnServer() throws { + try DataStore.shared.teamRegistrations.writeChangeAndInsertOnServer(instance: self) + for playerRegistration in self.unsortedPlayers() { + try playerRegistration.insertOnServer() + } + } + } extension TeamRegistration: Hashable { diff --git a/PadelClub/Data/TeamScore.swift b/PadelClub/Data/TeamScore.swift index b224197..dfadc9f 100644 --- a/PadelClub/Data/TeamScore.swift +++ b/PadelClub/Data/TeamScore.swift @@ -42,12 +42,10 @@ class TeamScore: ModelObject, Storable { self.luckyLoser = nil } - func isWalkOut() -> Bool { - walkOut != nil - } + // MARK: - Computed dependencies func matchObject() -> Match? { - Store.main.findById(match) + Store.main.findById(self.match) } var team: TeamRegistration? { @@ -56,7 +54,13 @@ class TeamScore: ModelObject, Storable { } return DataStore.shared.teamRegistrations.findById(teamRegistration) } - + + // MARK: - + + func isWalkOut() -> Bool { + walkOut != nil + } + enum CodingKeys: String, CodingKey { case _id = "id" case _match = "match" @@ -99,5 +103,9 @@ class TeamScore: ModelObject, Storable { try container.encodeNil(forKey: ._luckyLoser) } } - + + func insertOnServer() throws { + try DataStore.shared.teamScores.writeChangeAndInsertOnServer(instance: self) + } + } diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index a02c79c..ad91e47 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -319,6 +319,28 @@ class Tournament : ModelObject, Storable { } } + override func deleteDependencies() throws { + try Store.main.deleteDependencies(items: self.unsortedTeams()) + try Store.main.deleteDependencies(items: self.groupStages()) + try Store.main.deleteDependencies(items: self.rounds()) + try Store.main.deleteDependencies(items: self._matchSchedulers()) + } + + // MARK: - Computed Dependencies + + func unsortedTeams() -> [TeamRegistration] { + Store.main.filter { $0.tournament == self.id } + } + + func groupStages() -> [GroupStage] { + Store.main.filter { $0.tournament == self.id }.sorted(by: \.index) + } + + func allRounds() -> [Round] { + Store.main.filter { $0.tournament == self.id } + } + + // MARK: - /// Warning: if the enum has more than 10 cases, the payment algo is broken enum TournamentPayment: Int, CaseIterable { @@ -632,10 +654,6 @@ class Tournament : ModelObject, Storable { closedRegistrationDate != nil } - func groupStages() -> [GroupStage] { - Store.main.filter { $0.tournament == self.id }.sorted(by: \.index) - } - func getActiveGroupStage() -> GroupStage? { let groupStages = groupStages() return groupStages.filter({ $0.hasStarted() && $0.hasEnded() == false }).sorted(by: \.index).first ?? groupStages.first @@ -681,10 +699,6 @@ class Tournament : ModelObject, Storable { return unsortedGroupStages.flatMap { $0._matches() } + allRounds().flatMap { $0._matches() } } - func allRounds() -> [Round] { - Store.main.filter { $0.tournament == self.id } - } - func rounds() -> [Round] { Store.main.filter { $0.tournament == self.id && $0.parent == nil }.sorted(by: \.index).reversed() } @@ -754,10 +768,6 @@ class Tournament : ModelObject, Storable { } } - func unsortedTeams() -> [TeamRegistration] { - Store.main.filter { $0.tournament == self.id } - } - func unsortedTeamsWithoutWO() -> [TeamRegistration] { Store.main.filter { $0.tournament == self.id && $0.walkOut == false } } @@ -1559,13 +1569,6 @@ class Tournament : ModelObject, Storable { private let _currentSelectionSorting : [MySortDescriptor] = [.keyPath(\.weight), .keyPath(\.registrationDate!)] - override func deleteDependencies() throws { - try Store.main.deleteDependencies(items: self.unsortedTeams()) - try Store.main.deleteDependencies(items: self.groupStages()) - try Store.main.deleteDependencies(items: self.rounds()) - try Store.main.deleteDependencies(items: self._matchSchedulers()) - } - private func _matchSchedulers() -> [MatchScheduler] { Store.main.filter(isIncluded: { $0.tournament == self.id }) } @@ -1601,91 +1604,25 @@ class Tournament : ModelObject, Storable { return final?.playedMatches().first?.winner() } - // MARK: - Payments & Crypto + // MARK: - - fileprivate var _currentPayment: TournamentPayment? = nil - fileprivate var _currentCanceled: Bool? = nil + func insertOnServer() throws { + + try DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) + for teamRegistration in self.unsortedTeams() { + try teamRegistration.insertOnServer() + } + for groupStage in self.groupStages() { + try groupStage.insertOnServer() + } + for round in self.allRounds() { + try round.insertOnServer() + } + + } -// func setPayment(_ payment: TournamentPayment) { -// -// let max: Int = TournamentPayment.allCases.count -// self._currentPayment = payment -// var sequence = (1...18).map { _ in Int.random(in: (0.. TournamentPayment? { -// if let payment { -// do { -// let decoded: String = try payment.decryptData(pass: Key.pass.rawValue) -// let sequence = decoded.compactMap { _numberFormatter.number(from: String($0))?.intValue } -// return TournamentPayment(rawValue: sequence[18]) -// } catch { -// Logger.error(error) -// } -// } -// return nil -// } - -// func setCanceled(_ canceled: Bool) { -// -// let max: Int = 9 -// self._currentCanceled = canceled -// var sequence = (1...18).map { _ in Int.random(in: (0.. Bool? { -// if let isCanceled { -// do { -// let decoded: String = try isCanceled.decryptData(pass: Key.pass.rawValue) -// let sequence = decoded.compactMap { _numberFormatter.number(from: String($0))?.intValue } -// return Bool.decodeInt(sequence[18]) -// } catch { -// Logger.error(error) -// } -// } -// return nil -// } + // MARK: - Payments & Crypto enum PaymentError: Error { case cantPayTournament