|
|
|
|
@ -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<TeamRegistration>] = [.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 |
|
|
|
|
|
|
|
|
|
fileprivate var _currentPayment: TournamentPayment? = nil |
|
|
|
|
fileprivate var _currentCanceled: Bool? = nil |
|
|
|
|
// MARK: - |
|
|
|
|
|
|
|
|
|
func insertOnServer() throws { |
|
|
|
|
|
|
|
|
|
// func setPayment(_ payment: TournamentPayment) { |
|
|
|
|
// |
|
|
|
|
// let max: Int = TournamentPayment.allCases.count |
|
|
|
|
// self._currentPayment = payment |
|
|
|
|
// var sequence = (1...18).map { _ in Int.random(in: (0..<max)) } |
|
|
|
|
// sequence.append(payment.rawValue) |
|
|
|
|
// sequence.append(contentsOf: (1...13).map { _ in Int.random(in: (0..<max ))} ) |
|
|
|
|
// |
|
|
|
|
// let stringCombo: [String] = sequence.map { $0.formatted() } |
|
|
|
|
// let joined: String = stringCombo.joined(separator: "") |
|
|
|
|
// if let data = joined.data(using: .utf8) { |
|
|
|
|
// do { |
|
|
|
|
// self.payment = try data.encrypt(pass: Key.pass.rawValue) |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
try DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) |
|
|
|
|
|
|
|
|
|
// var currentPayment: TournamentPayment? { |
|
|
|
|
// if let current = self._currentPayment { |
|
|
|
|
// return current |
|
|
|
|
// } |
|
|
|
|
// self._currentPayment = self.decryptPayment() |
|
|
|
|
// return self._currentPayment |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// func decryptPayment() -> 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..<max)) } |
|
|
|
|
// sequence.append(canceled.encodedValue) |
|
|
|
|
// sequence.append(contentsOf: (1...13).map { _ in Int.random(in: (0..<max ))} ) |
|
|
|
|
// |
|
|
|
|
// let stringCombo: [String] = sequence.map { $0.formatted() } |
|
|
|
|
// let joined: String = stringCombo.joined(separator: "") |
|
|
|
|
// if let data = joined.data(using: .utf8) { |
|
|
|
|
// do { |
|
|
|
|
// self.isCanceled = try data.encrypt(pass: Key.pass.rawValue) |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
for teamRegistration in self.unsortedTeams() { |
|
|
|
|
try teamRegistration.insertOnServer() |
|
|
|
|
} |
|
|
|
|
for groupStage in self.groupStages() { |
|
|
|
|
try groupStage.insertOnServer() |
|
|
|
|
} |
|
|
|
|
for round in self.allRounds() { |
|
|
|
|
try round.insertOnServer() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// var currentCanceled: Bool? { |
|
|
|
|
// if let current = self._currentCanceled { |
|
|
|
|
// return current |
|
|
|
|
// } |
|
|
|
|
// self._currentCanceled = self.decryptCanceled() |
|
|
|
|
// return self._currentCanceled |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// func decryptCanceled() -> 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 |
|
|
|
|
|