|
|
|
|
@ -93,64 +93,14 @@ struct RoundSettingsView: View { |
|
|
|
|
Section { |
|
|
|
|
let roundIndex = tournament.rounds().count |
|
|
|
|
RowButtonView("Ajouter " + RoundRule.roundName(fromRoundIndex: roundIndex), role: .destructive) { |
|
|
|
|
let round = Round(tournament: tournament.id, index: roundIndex, matchFormat: tournament.matchFormat) |
|
|
|
|
let matchCount = RoundRule.numberOfMatches(forRoundIndex: roundIndex) |
|
|
|
|
let matchStartIndex = RoundRule.matchIndex(fromRoundIndex: roundIndex) |
|
|
|
|
let nextRound = round.nextRound() |
|
|
|
|
var currentIndex = 0 |
|
|
|
|
let matches = (0..<matchCount).map { index in //0 is final match |
|
|
|
|
let computedIndex = index + matchStartIndex |
|
|
|
|
let match = Match(round: round.id, index: computedIndex, matchFormat: round.matchFormat) |
|
|
|
|
if let nextRound, let followingMatch = self.tournament.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 { |
|
|
|
|
//index du match courant impair = position haut du prochain match |
|
|
|
|
match.disabled = true |
|
|
|
|
} else if computedIndex%2 == 0 && followingMatch.team(.two) != nil { |
|
|
|
|
//index du match courant pair = position basse du prochain match |
|
|
|
|
match.disabled = true |
|
|
|
|
} else { |
|
|
|
|
match.setMatchName(Match.setServerTitle(upperRound: round, matchIndex: currentIndex)) |
|
|
|
|
currentIndex += 1 |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
match.setMatchName(Match.setServerTitle(upperRound: round, matchIndex: currentIndex)) |
|
|
|
|
currentIndex += 1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return match |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.rounds.addOrUpdate(instance: round) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
round.buildLoserBracket() |
|
|
|
|
matches.filter { $0.disabled }.forEach { |
|
|
|
|
$0._toggleLoserMatchDisableState(true) |
|
|
|
|
} |
|
|
|
|
await _addNewRound(roundIndex) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Section { |
|
|
|
|
if let lastRound = tournament.rounds().first { // first is final, last round |
|
|
|
|
RowButtonView("Supprimer " + lastRound.roundTitle(), role: .destructive) { |
|
|
|
|
do { |
|
|
|
|
let teams = lastRound.seeds() |
|
|
|
|
teams.forEach { team in |
|
|
|
|
team.resetBracketPosition() |
|
|
|
|
} |
|
|
|
|
try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
try tournamentStore.rounds.delete(instance: lastRound) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
await _removeRound(lastRound) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -199,6 +149,64 @@ struct RoundSettingsView: View { |
|
|
|
|
}) |
|
|
|
|
self.isEditingTournamentSeed.wrappedValue = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _addNewRound(_ roundIndex: Int) async { |
|
|
|
|
let round = Round(tournament: tournament.id, index: roundIndex, matchFormat: tournament.matchFormat) |
|
|
|
|
let matchCount = RoundRule.numberOfMatches(forRoundIndex: roundIndex) |
|
|
|
|
let matchStartIndex = RoundRule.matchIndex(fromRoundIndex: roundIndex) |
|
|
|
|
let nextRound = round.nextRound() |
|
|
|
|
var currentIndex = 0 |
|
|
|
|
let matches = (0..<matchCount).map { index in //0 is final match |
|
|
|
|
let computedIndex = index + matchStartIndex |
|
|
|
|
let match = Match(round: round.id, index: computedIndex, matchFormat: round.matchFormat) |
|
|
|
|
if let nextRound, let followingMatch = self.tournament.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 { |
|
|
|
|
//index du match courant impair = position haut du prochain match |
|
|
|
|
match.disabled = true |
|
|
|
|
} else if computedIndex%2 == 0 && followingMatch.team(.two) != nil { |
|
|
|
|
//index du match courant pair = position basse du prochain match |
|
|
|
|
match.disabled = true |
|
|
|
|
} else { |
|
|
|
|
match.setMatchName(Match.setServerTitle(upperRound: round, matchIndex: currentIndex)) |
|
|
|
|
currentIndex += 1 |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
match.setMatchName(Match.setServerTitle(upperRound: round, matchIndex: currentIndex)) |
|
|
|
|
currentIndex += 1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return match |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.rounds.addOrUpdate(instance: round) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try tournamentStore.matches.addOrUpdate(contentOfs: matches) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
round.buildLoserBracket() |
|
|
|
|
matches.filter { $0.disabled }.forEach { |
|
|
|
|
$0._toggleLoserMatchDisableState(true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _removeRound(_ lastRound: Round) async { |
|
|
|
|
do { |
|
|
|
|
let teams = lastRound.seeds() |
|
|
|
|
teams.forEach { team in |
|
|
|
|
team.resetBracketPosition() |
|
|
|
|
} |
|
|
|
|
try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) |
|
|
|
|
try tournamentStore.rounds.delete(instance: lastRound) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//#Preview { |
|
|
|
|
|