|
|
|
|
@ -143,11 +143,11 @@ class Tournament : ModelObject, Storable { |
|
|
|
|
return seeds().filter { $0.isSeedable() } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func lastSeedRound() -> Int? { |
|
|
|
|
func lastSeedRound() -> Int { |
|
|
|
|
if let last = seeds().filter({ $0.bracketPosition != nil }).last { |
|
|
|
|
return RoundRule.roundIndex(fromMatchIndex: last.bracketPosition! / 2) |
|
|
|
|
} else { |
|
|
|
|
return nil |
|
|
|
|
return 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -213,24 +213,67 @@ class Tournament : ModelObject, Storable { |
|
|
|
|
return availableSeeds |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func setSeeds(inRoundIndex roundIndex: Int, inSeedGroup seedGroup: SeedInterval) { |
|
|
|
|
let availableSeedSpot = availableSeedSpot(inRoundIndex: roundIndex) |
|
|
|
|
let availableSeedOpponentSpot = availableSeedOpponentSpot(inRoundIndex: roundIndex) |
|
|
|
|
let availableSeeds = seeds(inSeedGroup: seedGroup) |
|
|
|
|
func seedGroupAvailable(atRoundIndex roundIndex: Int) -> SeedInterval? { |
|
|
|
|
if let availableSeedGroup = availableSeedGroup() { |
|
|
|
|
return seedGroupAvailable(atRoundIndex: roundIndex, availableSeedGroup: availableSeedGroup) |
|
|
|
|
} else { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func seedGroupAvailable(atRoundIndex roundIndex: Int, availableSeedGroup: SeedInterval) -> SeedInterval? { |
|
|
|
|
|
|
|
|
|
if availableSeeds().isEmpty == false && roundIndex >= lastSeedRound() { |
|
|
|
|
|
|
|
|
|
if availableSeedGroup == SeedInterval(first: 1, last: 2) { return availableSeedGroup } |
|
|
|
|
|
|
|
|
|
let availableSeeds = seeds(inSeedGroup: availableSeedGroup) |
|
|
|
|
let availableSeedSpot = availableSeedSpot(inRoundIndex: roundIndex) |
|
|
|
|
let availableSeedOpponentSpot = availableSeedOpponentSpot(inRoundIndex: roundIndex) |
|
|
|
|
|
|
|
|
|
if availableSeeds.count == availableSeedSpot.count { |
|
|
|
|
return availableSeedGroup |
|
|
|
|
} else if (availableSeeds.count == availableSeedOpponentSpot.count && availableSeeds.count == self.availableSeeds().count) { |
|
|
|
|
return availableSeedGroup |
|
|
|
|
} else if let chunk = availableSeedGroup.chunk() { |
|
|
|
|
return seedGroupAvailable(atRoundIndex: roundIndex, availableSeedGroup: chunk) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if availableSeeds.count <= availableSeedSpot.count { |
|
|
|
|
let spots = availableSeedSpot.shuffled() |
|
|
|
|
for (index, seed) in availableSeeds.enumerated() { |
|
|
|
|
seed.setSeedPosition(inSpot: spots[index], slot: nil, opposingSeeding: false) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func setSeeds(inRoundIndex roundIndex: Int, inSeedGroup seedGroup: SeedInterval) { |
|
|
|
|
if seedGroup == SeedInterval(first: 1, last: 2) { |
|
|
|
|
let seeds = seeds() |
|
|
|
|
if let matches = getRound(atRoundIndex: roundIndex)?.playedMatches() { |
|
|
|
|
if let lastMatch = matches.last { |
|
|
|
|
seeds.prefix(1).first?.setSeedPosition(inSpot: lastMatch, slot: .two, opposingSeeding: false) |
|
|
|
|
} |
|
|
|
|
if let firstMatch = matches.first { |
|
|
|
|
seeds.prefix(2).dropFirst().first?.setSeedPosition(inSpot: firstMatch, slot: .one, opposingSeeding: false) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else if (availableSeeds.count <= availableSeedOpponentSpot.count && availableSeeds.count <= self.availableSeeds().count) { |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
let spots = availableSeedOpponentSpot.shuffled() |
|
|
|
|
for (index, seed) in availableSeeds.enumerated() { |
|
|
|
|
seed.setSeedPosition(inSpot: spots[index], slot: nil, opposingSeeding: true) |
|
|
|
|
let availableSeedSpot = availableSeedSpot(inRoundIndex: roundIndex) |
|
|
|
|
let availableSeedOpponentSpot = availableSeedOpponentSpot(inRoundIndex: roundIndex) |
|
|
|
|
let availableSeeds = seeds(inSeedGroup: seedGroup) |
|
|
|
|
|
|
|
|
|
if availableSeeds.count <= availableSeedSpot.count { |
|
|
|
|
let spots = availableSeedSpot.shuffled() |
|
|
|
|
for (index, seed) in availableSeeds.enumerated() { |
|
|
|
|
seed.setSeedPosition(inSpot: spots[index], slot: nil, opposingSeeding: false) |
|
|
|
|
} |
|
|
|
|
} else if (availableSeeds.count <= availableSeedOpponentSpot.count && availableSeeds.count <= self.availableSeeds().count) { |
|
|
|
|
|
|
|
|
|
let spots = availableSeedOpponentSpot.shuffled() |
|
|
|
|
for (index, seed) in availableSeeds.enumerated() { |
|
|
|
|
seed.setSeedPosition(inSpot: spots[index], slot: nil, opposingSeeding: true) |
|
|
|
|
} |
|
|
|
|
} else if let chunk = seedGroup.chunk() { |
|
|
|
|
setSeeds(inRoundIndex: roundIndex, inSeedGroup: chunk) |
|
|
|
|
} |
|
|
|
|
} else if let chunk = seedGroup.chunk() { |
|
|
|
|
setSeeds(inRoundIndex: roundIndex, inSeedGroup: chunk) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|