|
|
|
|
@ -77,31 +77,31 @@ final class Tournament: BaseTournament { |
|
|
|
|
try super.init(from: decoder) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var tournamentStore: TournamentStore { |
|
|
|
|
var tournamentStore: TournamentStore? { |
|
|
|
|
return TournamentLibrary.shared.store(tournamentId: self.id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override func deleteDependencies() { |
|
|
|
|
let store = self.tournamentStore |
|
|
|
|
let drawLogs = Array(self.tournamentStore.drawLogs) |
|
|
|
|
guard let store = self.tournamentStore else { return } |
|
|
|
|
let drawLogs = Array(store.drawLogs) |
|
|
|
|
for drawLog in drawLogs { |
|
|
|
|
drawLog.deleteDependencies() |
|
|
|
|
} |
|
|
|
|
store.drawLogs.deleteDependencies(drawLogs) |
|
|
|
|
|
|
|
|
|
let teams = self.tournamentStore.teamRegistrations |
|
|
|
|
let teams = store.teamRegistrations |
|
|
|
|
for team in Array(teams) { |
|
|
|
|
team.deleteDependencies() |
|
|
|
|
} |
|
|
|
|
store.teamRegistrations.deleteDependencies(teams) |
|
|
|
|
|
|
|
|
|
let groups = Array(self.tournamentStore.groupStages) |
|
|
|
|
let groups = Array(store.groupStages) |
|
|
|
|
for group in groups { |
|
|
|
|
group.deleteDependencies() |
|
|
|
|
} |
|
|
|
|
store.groupStages.deleteDependencies(groups) |
|
|
|
|
|
|
|
|
|
let rounds = Array(self.tournamentStore.rounds) |
|
|
|
|
let rounds = Array(store.rounds) |
|
|
|
|
for round in rounds { |
|
|
|
|
round.deleteDependencies() |
|
|
|
|
} |
|
|
|
|
@ -120,20 +120,24 @@ final class Tournament: BaseTournament { |
|
|
|
|
// MARK: - Computed Dependencies |
|
|
|
|
|
|
|
|
|
func unsortedTeams() -> [TeamRegistration] { |
|
|
|
|
return Array(self.tournamentStore.teamRegistrations) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return Array(tournamentStore.teamRegistrations) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func groupStages(atStep step: Int = 0) -> [GroupStage] { |
|
|
|
|
let groupStages: [GroupStage] = self.tournamentStore.groupStages.filter { $0.tournament == self.id && $0.step == step } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
let groupStages: [GroupStage] = tournamentStore.groupStages.filter { $0.tournament == self.id && $0.step == step } |
|
|
|
|
return groupStages.sorted(by: \.index) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func allGroupStages() -> [GroupStage] { |
|
|
|
|
return self.tournamentStore.groupStages.sorted(by: \GroupStage.computedOrder) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.groupStages.sorted(by: \GroupStage.computedOrder) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func allRounds() -> [Round] { |
|
|
|
|
return Array(self.tournamentStore.rounds) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return Array(tournamentStore.rounds) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - |
|
|
|
|
@ -354,7 +358,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getRound(atRoundIndex roundIndex: Int) -> Round? { |
|
|
|
|
return self.tournamentStore.rounds.first(where: { $0.index == roundIndex }) |
|
|
|
|
return self.tournamentStore?.rounds.first(where: { $0.index == roundIndex }) |
|
|
|
|
// return Store.main.filter(isIncluded: { $0.tournament == id && $0.index == roundIndex }).first |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -546,15 +550,18 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func allMatches() -> [Match] { |
|
|
|
|
return self.tournamentStore.matches.filter { $0.disabled == false } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.matches.filter { $0.disabled == false } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func _allMatchesIncludingDisabled() -> [Match] { |
|
|
|
|
return Array(self.tournamentStore.matches) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return Array(tournamentStore.matches) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func rounds() -> [Round] { |
|
|
|
|
let rounds: [Round] = self.tournamentStore.rounds.filter { $0.isUpperBracket() } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
let rounds: [Round] = tournamentStore.rounds.filter { $0.isUpperBracket() } |
|
|
|
|
return rounds.sorted(by: \.index).reversed() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -677,12 +684,14 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func unsortedTeamsWithoutWO() -> [TeamRegistration] { |
|
|
|
|
return self.tournamentStore.teamRegistrations.filter { $0.walkOut == false } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.teamRegistrations.filter { $0.walkOut == false } |
|
|
|
|
// return Store.main.filter { $0.tournament == self.id && $0.walkOut == false } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func walkoutTeams() -> [TeamRegistration] { |
|
|
|
|
return self.tournamentStore.teamRegistrations.filter { $0.walkOut == true } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.teamRegistrations.filter { $0.walkOut == true } |
|
|
|
|
// return Store.main.filter { $0.tournament == self.id && $0.walkOut == true } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -702,7 +711,8 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func unsortedPlayers() -> [PlayerRegistration] { |
|
|
|
|
return Array(self.tournamentStore.playerRegistrations) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return Array(tournamentStore.playerRegistrations) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func selectedPlayers() -> [PlayerRegistration] { |
|
|
|
|
@ -718,7 +728,8 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func players() -> [PlayerRegistration] { |
|
|
|
|
return self.tournamentStore.playerRegistrations.sorted(by: \.computedRank) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.playerRegistrations.sorted(by: \.computedRank) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func unrankValue(for malePlayer: Bool) -> Int? { |
|
|
|
|
@ -838,8 +849,10 @@ defer { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teamsToImport) |
|
|
|
|
self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: teams.flatMap { $0.players }) |
|
|
|
|
if let tournamentStore = self.tournamentStore { |
|
|
|
|
tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teamsToImport) |
|
|
|
|
tournamentStore.playerRegistrations.addOrUpdate(contentOfs: teams.flatMap { $0.players }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if state() == .build && groupStageCount > 0 && groupStageTeams().isEmpty { |
|
|
|
|
setGroupStage(randomize: groupStageSortMode == .random) |
|
|
|
|
@ -1020,7 +1033,7 @@ defer { |
|
|
|
|
_removeStrings(from: &teams, stringsToRemove: disabledIds) |
|
|
|
|
teams[interval.last] = disabledIds |
|
|
|
|
let teamNames : [String] = disabledIds.compactMap { |
|
|
|
|
let t : TeamRegistration? = tournamentStore.teamRegistrations.findById($0) |
|
|
|
|
let t : TeamRegistration? = self.tournamentStore?.teamRegistrations.findById($0) |
|
|
|
|
return t |
|
|
|
|
}.map { $0.canonicalName } |
|
|
|
|
print("winners.isEmpty", "\(interval.last) : ", teamNames) |
|
|
|
|
@ -1033,7 +1046,7 @@ defer { |
|
|
|
|
_removeStrings(from: &teams, stringsToRemove: winners) |
|
|
|
|
teams[interval.first + winners.count - 1] = winners |
|
|
|
|
let teamNames : [String] = winners.compactMap { |
|
|
|
|
let t: TeamRegistration? = tournamentStore.teamRegistrations.findById($0) |
|
|
|
|
let t: TeamRegistration? = tournamentStore?.teamRegistrations.findById($0) |
|
|
|
|
return t |
|
|
|
|
}.map { $0.canonicalName } |
|
|
|
|
print("winners", "\(interval.last + winners.count - 1) : ", teamNames) |
|
|
|
|
@ -1044,7 +1057,7 @@ defer { |
|
|
|
|
_removeStrings(from: &teams, stringsToRemove: losers) |
|
|
|
|
teams[interval.first + winners.count] = losers |
|
|
|
|
let loserTeamNames : [String] = losers.compactMap { |
|
|
|
|
let t: TeamRegistration? = tournamentStore.teamRegistrations.findById($0) |
|
|
|
|
let t: TeamRegistration? = tournamentStore?.teamRegistrations.findById($0) |
|
|
|
|
return t |
|
|
|
|
}.map { $0.canonicalName } |
|
|
|
|
print("losers", "\(interval.first + winners.count) : ", loserTeamNames) |
|
|
|
|
@ -1088,11 +1101,13 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func setRankings(finalRanks: [Int: [String]]) async -> [Int: [TeamRegistration]] { |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [:] } |
|
|
|
|
|
|
|
|
|
var rankings: [Int: [TeamRegistration]] = [:] |
|
|
|
|
|
|
|
|
|
finalRanks.keys.sorted().forEach { rank in |
|
|
|
|
if let rankedTeamIds = finalRanks[rank] { |
|
|
|
|
let teams: [TeamRegistration] = rankedTeamIds.compactMap { self.tournamentStore.teamRegistrations.findById($0) } |
|
|
|
|
let teams: [TeamRegistration] = rankedTeamIds.compactMap { tournamentStore.teamRegistrations.findById($0) } |
|
|
|
|
rankings[rank] = teams |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -1106,7 +1121,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
|
|
|
|
|
return rankings |
|
|
|
|
} |
|
|
|
|
@ -1121,7 +1136,7 @@ defer { |
|
|
|
|
teams.forEach { team in |
|
|
|
|
team.lockedWeight = team.weight |
|
|
|
|
} |
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func unlockRegistration() { |
|
|
|
|
@ -1130,7 +1145,7 @@ defer { |
|
|
|
|
teams.forEach { team in |
|
|
|
|
team.lockedWeight = nil |
|
|
|
|
} |
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateWeights() { |
|
|
|
|
@ -1139,9 +1154,9 @@ defer { |
|
|
|
|
let players = team.unsortedPlayers() |
|
|
|
|
players.forEach { $0.setComputedRank(in: self) } |
|
|
|
|
team.setWeight(from: players, inTournamentCategory: tournamentCategory) |
|
|
|
|
self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: players) |
|
|
|
|
self.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players) |
|
|
|
|
} |
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateRank(to newDate: Date?) async throws { |
|
|
|
|
@ -1472,27 +1487,30 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: wcs) |
|
|
|
|
try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: wcs) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func addEmptyTeamRegistration(_ count: Int) { |
|
|
|
|
|
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return } |
|
|
|
|
|
|
|
|
|
let teams = (0..<count).map { _ in |
|
|
|
|
let team = TeamRegistration(tournament: id) |
|
|
|
|
return team |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func buildGroupStages() { |
|
|
|
|
guard groupStages().isEmpty else { |
|
|
|
|
guard groupStages().isEmpty, let tournamentStore = self.tournamentStore else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1502,7 +1520,7 @@ defer { |
|
|
|
|
_groupStages.append(groupStage) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.tournamentStore.groupStages.addOrUpdate(contentOfs: _groupStages) |
|
|
|
|
tournamentStore.groupStages.addOrUpdate(contentOfs: _groupStages) |
|
|
|
|
refreshGroupStages() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1524,7 +1542,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try self.tournamentStore.rounds.addOrUpdate(contentOfs: rounds) |
|
|
|
|
try self.tournamentStore?.rounds.addOrUpdate(contentOfs: rounds) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -1540,7 +1558,7 @@ defer { |
|
|
|
|
(RoundRule.roundName(fromMatchIndex: $0.index), RoundRule.matchIndexWithinRound(fromMatchIndex: $0.index)) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
self.tournamentStore.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
self.tournamentStore?.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
|
|
|
|
|
rounds.forEach { round in |
|
|
|
|
round.buildLoserBracket() |
|
|
|
|
@ -1552,7 +1570,7 @@ defer { |
|
|
|
|
let matchIndex = bracketPosition / 2 |
|
|
|
|
let roundIndex = RoundRule.roundIndex(fromMatchIndex: matchIndex) |
|
|
|
|
if let round: Round = self.getRound(atRoundIndex: roundIndex) { |
|
|
|
|
return self.tournamentStore.matches.first(where: { $0.round == round.id && $0.index == matchIndex }) |
|
|
|
|
return self.tournamentStore?.matches.first(where: { $0.round == round.id && $0.index == matchIndex }) |
|
|
|
|
// return Store.main.filter(isIncluded: { $0.round == round.id && $0.index == matchIndex }).first |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -1570,7 +1588,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func deleteStructure() { |
|
|
|
|
self.tournamentStore.rounds.delete(contentOfs: rounds()) |
|
|
|
|
self.tournamentStore?.rounds.delete(contentOfs: rounds()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func resetBracketPosition() { |
|
|
|
|
@ -1578,7 +1596,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func deleteGroupStages() { |
|
|
|
|
self.tournamentStore.groupStages.delete(contentOfs: allGroupStages()) |
|
|
|
|
self.tournamentStore?.groupStages.delete(contentOfs: allGroupStages()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func refreshGroupStages(keepExistingMatches: Bool = false) { |
|
|
|
|
@ -1631,7 +1649,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func isFree() -> Bool { |
|
|
|
|
@ -1792,7 +1810,8 @@ defer { |
|
|
|
|
private let _currentSelectionSorting : [MySortDescriptor<TeamRegistration>] = [.keyPath(\.weight), .keyPath(\.id)] |
|
|
|
|
|
|
|
|
|
private func _matchSchedulers() -> [MatchScheduler] { |
|
|
|
|
return self.tournamentStore.matchSchedulers.filter { $0.tournament == self.id } |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.matchSchedulers.filter { $0.tournament == self.id } |
|
|
|
|
// DataStore.shared.matchSchedulers.filter(isIncluded: { $0.tournament == self.id }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1827,7 +1846,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func tournamentWinner() -> TeamRegistration? { |
|
|
|
|
let finals: Round? = self.tournamentStore.rounds.first(where: { $0.index == 0 && $0.isUpperBracket() }) |
|
|
|
|
let finals: Round? = self.tournamentStore?.rounds.first(where: { $0.index == 0 && $0.isUpperBracket() }) |
|
|
|
|
return finals?.playedMatches().first?.winner() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1890,7 +1909,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func groupStageLoserBracket() -> Round? { |
|
|
|
|
tournamentStore.rounds.first(where: { $0.groupStageLoserBracket }) |
|
|
|
|
self.tournamentStore?.rounds.first(where: { $0.groupStageLoserBracket }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func groupStageLoserBracketsInitialPlace() -> Int { |
|
|
|
|
@ -1901,14 +1920,14 @@ defer { |
|
|
|
|
let lastStep = lastStep() + 1 |
|
|
|
|
for i in 0..<teamsPerGroupStage { |
|
|
|
|
let gs = GroupStage(tournament: id, index: i, size: groupStageCount, step: lastStep) |
|
|
|
|
tournamentStore.groupStages.addOrUpdate(instance: gs) |
|
|
|
|
self.tournamentStore?.groupStages.addOrUpdate(instance: gs) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
groupStages(atStep: 1).forEach { $0.buildMatches() } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func lastStep() -> Int { |
|
|
|
|
self.tournamentStore.groupStages.sorted(by: \.step).last?.step ?? 0 |
|
|
|
|
self.tournamentStore?.groupStages.sorted(by: \.step).last?.step ?? 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func generateSmartLoserGroupStageBracket() { |
|
|
|
|
@ -1919,7 +1938,7 @@ defer { |
|
|
|
|
let match = Match(round: groupStageLoserBracket.id, index: placeCount, format: groupStageLoserBracket.matchFormat) |
|
|
|
|
match.setMatchName("\(placeCount)\(placeCount.ordinalFormattedSuffix(feminine: true)) place") |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.matches.addOrUpdate(instance: match) |
|
|
|
|
try tournamentStore?.matches.addOrUpdate(instance: match) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -1958,7 +1977,8 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func drawLogs() -> [DrawLog] { |
|
|
|
|
self.tournamentStore.drawLogs.sorted(by: \.drawDate) |
|
|
|
|
guard let tournamentStore = self.tournamentStore else { return [] } |
|
|
|
|
return tournamentStore.drawLogs.sorted(by: \.drawDate) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func seedSpotsLeft() -> Bool { |
|
|
|
|
@ -1989,7 +2009,7 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: seeds) |
|
|
|
|
try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: seeds) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -2004,12 +2024,12 @@ defer { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try tournamentStore.teamScores.delete(contentOfs: ts) |
|
|
|
|
try tournamentStore?.teamScores.delete(contentOfs: ts) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -2028,7 +2048,7 @@ defer { |
|
|
|
|
let matches = (0..<matchCount).map { index in //0 is final match |
|
|
|
|
let computedIndex = index + matchStartIndex |
|
|
|
|
let match = Match(round: round.id, index: computedIndex, format: round.matchFormat) |
|
|
|
|
if let nextRound, let followingMatch = self.tournamentStore.matches.first(where: { $0.round == nextRound.id && $0.index == (computedIndex - 1) / 2 }) { |
|
|
|
|
if let nextRound, let followingMatch = self.tournamentStore?.matches.first(where: { $0.round == nextRound.id && $0.index == (computedIndex - 1) / 2 }) { |
|
|
|
|
if followingMatch.disabled { |
|
|
|
|
match.disabled = true |
|
|
|
|
} else if computedIndex%2 == 1 && followingMatch.team(.one) != nil { |
|
|
|
|
@ -2049,12 +2069,12 @@ defer { |
|
|
|
|
return match |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.rounds.addOrUpdate(instance: round) |
|
|
|
|
try tournamentStore?.rounds.addOrUpdate(instance: round) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
try tournamentStore?.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -2085,17 +2105,17 @@ defer { |
|
|
|
|
|
|
|
|
|
func insertOnServer() throws { |
|
|
|
|
|
|
|
|
|
DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) |
|
|
|
|
|
|
|
|
|
for teamRegistration in self.tournamentStore.teamRegistrations { |
|
|
|
|
teamRegistration.insertOnServer() |
|
|
|
|
} |
|
|
|
|
for groupStage in self.tournamentStore.groupStages { |
|
|
|
|
groupStage.insertOnServer() |
|
|
|
|
} |
|
|
|
|
for round in self.tournamentStore.rounds { |
|
|
|
|
round.insertOnServer() |
|
|
|
|
} |
|
|
|
|
// DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) |
|
|
|
|
// |
|
|
|
|
// for teamRegistration in self.tournamentStore?.teamRegistrations { |
|
|
|
|
// teamRegistration.insertOnServer() |
|
|
|
|
// } |
|
|
|
|
// for groupStage in self.tournamentStore?.groupStages { |
|
|
|
|
// groupStage.insertOnServer() |
|
|
|
|
// } |
|
|
|
|
// for round in self.tournamentStore.rounds { |
|
|
|
|
// round.insertOnServer() |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|