|
|
|
|
@ -194,10 +194,14 @@ class Round: ModelObject, Storable { |
|
|
|
|
return $0.round == id && index == matchIndexInRound |
|
|
|
|
}).first |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func enabledMatches() -> [Match] { |
|
|
|
|
Store.main.filter { $0.round == self.id && $0.disabled == false } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func playedMatches() -> [Match] { |
|
|
|
|
if parent == nil { |
|
|
|
|
Store.main.filter { $0.round == self.id && $0.disabled == false } |
|
|
|
|
enabledMatches() |
|
|
|
|
} else { |
|
|
|
|
_matches() |
|
|
|
|
} |
|
|
|
|
@ -219,33 +223,27 @@ class Round: ModelObject, Storable { |
|
|
|
|
_matches().allSatisfy({ $0.disabled }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func resetRound(updateMatchFormat: MatchFormat? = nil) { |
|
|
|
|
let _updateMatchFormat = updateMatchFormat ?? self.matchFormat |
|
|
|
|
func resetFromRoundAllMatchesStartDate() { |
|
|
|
|
_matches().forEach({ |
|
|
|
|
$0.startDate = nil |
|
|
|
|
$0.matchFormat = updateMatchFormat ?? $0.matchFormat |
|
|
|
|
}) |
|
|
|
|
self.matchFormat = _updateMatchFormat |
|
|
|
|
loserRoundsAndChildren().forEach { round in |
|
|
|
|
round.resetRound(updateMatchFormat: _updateMatchFormat) |
|
|
|
|
round.resetFromRoundAllMatchesStartDate() |
|
|
|
|
} |
|
|
|
|
nextRound()?.resetRound(updateMatchFormat: _updateMatchFormat) |
|
|
|
|
nextRound()?.resetFromRoundAllMatchesStartDate() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func resetRound(from match: Match, updateMatchFormat: MatchFormat? = nil) { |
|
|
|
|
let _updateMatchFormat = updateMatchFormat ?? self.matchFormat |
|
|
|
|
self.matchFormat = _updateMatchFormat |
|
|
|
|
func resetFromRoundAllMatchesStartDate(from match: Match) { |
|
|
|
|
let matches = _matches() |
|
|
|
|
if let index = matches.firstIndex(where: { $0.id == match.id }) { |
|
|
|
|
matches[index...].forEach { match in |
|
|
|
|
match.startDate = nil |
|
|
|
|
match.matchFormat = _updateMatchFormat |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
loserRoundsAndChildren().forEach { round in |
|
|
|
|
round.resetRound(updateMatchFormat: _updateMatchFormat) |
|
|
|
|
round.resetFromRoundAllMatchesStartDate() |
|
|
|
|
} |
|
|
|
|
nextRound()?.resetRound(updateMatchFormat: _updateMatchFormat) |
|
|
|
|
nextRound()?.resetFromRoundAllMatchesStartDate() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getActiveLoserRound() -> Round? { |
|
|
|
|
@ -401,12 +399,27 @@ class Round: ModelObject, Storable { |
|
|
|
|
guard let parent = parent else { return nil } |
|
|
|
|
return Store.main.findById(parent) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateIfRequiredMatchFormat(_ updatedMatchFormat: MatchFormat, andLoserBracket: Bool) { |
|
|
|
|
if updatedMatchFormat.weight < self.matchFormat.weight { |
|
|
|
|
updateMatchFormatAndAllMatches(updatedMatchFormat) |
|
|
|
|
if andLoserBracket { |
|
|
|
|
loserRoundsAndChildren().forEach { round in |
|
|
|
|
round.updateIfRequiredMatchFormat(updatedMatchFormat, andLoserBracket: true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateMatchFormatAndAllMatches(_ updatedMatchFormat: MatchFormat) { |
|
|
|
|
self.matchFormat = updatedMatchFormat |
|
|
|
|
self.updateMatchFormatOfAllMatches(updatedMatchFormat) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateMatchFormat(_ matchFormat: MatchFormat) { |
|
|
|
|
self.matchFormat = matchFormat |
|
|
|
|
func updateMatchFormatOfAllMatches(_ updatedMatchFormat: MatchFormat) { |
|
|
|
|
let playedMatches = _matches() |
|
|
|
|
playedMatches.forEach { match in |
|
|
|
|
match.matchFormat = matchFormat |
|
|
|
|
match.matchFormat = updatedMatchFormat |
|
|
|
|
} |
|
|
|
|
try? DataStore.shared.matches.addOrUpdate(contentOfs: playedMatches) |
|
|
|
|
} |
|
|
|
|
|