|
|
|
|
@ -344,7 +344,7 @@ class Match: ModelObject, Storable { |
|
|
|
|
|
|
|
|
|
func followingMatch() -> Match? { |
|
|
|
|
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 { |
|
|
|
|
@ -478,16 +478,17 @@ class Match: ModelObject, Storable { |
|
|
|
|
_loserMatch()?.updateTeamScores() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func resetTeamScores() { |
|
|
|
|
let teamScores = teamScores |
|
|
|
|
func resetTeamScores(outsideOf newTeamScores: [TeamScore]) { |
|
|
|
|
let ids = newTeamScores.map { $0.id } |
|
|
|
|
let teamScores = teamScores.filter({ ids.contains($0.id) == false }) |
|
|
|
|
if teamScores.isEmpty == false { |
|
|
|
|
do { |
|
|
|
|
try DataStore.shared.teamScores.delete(contentOfs: teamScores) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
followingMatch()?.resetTeamScores() |
|
|
|
|
_loserMatch()?.resetTeamScores() |
|
|
|
|
followingMatch()?.resetTeamScores(outsideOf: []) |
|
|
|
|
_loserMatch()?.resetTeamScores(outsideOf: []) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -498,14 +499,21 @@ class Match: ModelObject, Storable { |
|
|
|
|
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() { |
|
|
|
|
resetTeamScores() |
|
|
|
|
let teams = createTeamScores() |
|
|
|
|
let teams = getOrCreateTeamScores() |
|
|
|
|
do { |
|
|
|
|
try DataStore.shared.teamScores.addOrUpdate(contentOfs: teams) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
resetTeamScores(outsideOf: teams) |
|
|
|
|
if teams.isEmpty == false { |
|
|
|
|
updateFollowingMatchTeamScore() |
|
|
|
|
} |
|
|
|
|
|