@ -702,17 +702,32 @@ defer {
return getRound ( atRoundIndex : roundIndex ) ? . playedMatches ( ) . filter { $0 . hasSpaceLeft ( ) } ? ? [ ]
return getRound ( atRoundIndex : roundIndex ) ? . playedMatches ( ) . filter { $0 . hasSpaceLeft ( ) } ? ? [ ]
}
}
func availableSeedGroups ( ) -> [ SeedInterval ] {
func availableSeedGroups ( includeAll : Bool = false ) -> [ SeedInterval ] {
let seeds = seeds ( )
let seeds = seeds ( )
var availableSeedGroup = Set < SeedInterval > ( )
var availableSeedGroup = Set < SeedInterval > ( )
for ( index , seed ) in seeds . enumerated ( ) {
for ( index , seed ) in seeds . enumerated ( ) {
if seed . isSeedable ( ) , let seedGroup = seedGroup ( for : index ) {
if seed . isSeedable ( ) , let seedGroup = seedGroup ( for : index ) {
if includeAll {
if let chunks = seedGroup . chunks ( ) {
chunksBy ( in : chunks , availableSeedGroup : & availableSeedGroup )
}
} else {
availableSeedGroup . insert ( seedGroup )
availableSeedGroup . insert ( seedGroup )
}
}
}
}
}
return availableSeedGroup . sorted ( by : < )
return availableSeedGroup . sorted ( by : < )
}
}
func chunksBy ( in chunks : [ SeedInterval ] , availableSeedGroup : inout Set < SeedInterval > ) {
chunks . forEach { chunk in
availableSeedGroup . insert ( chunk )
if let moreChunk = chunk . chunks ( ) {
self . chunksBy ( in : moreChunk , availableSeedGroup : & availableSeedGroup )
}
}
}
func seedGroup ( for alreadySetupSeeds : Int ) -> SeedInterval ? {
func seedGroup ( for alreadySetupSeeds : Int ) -> SeedInterval ? {
switch alreadySetupSeeds {
switch alreadySetupSeeds {
case 0. . . 1 :
case 0. . . 1 :
@ -771,20 +786,24 @@ defer {
let maxSpots = max ( availableSeedSpot . count , availableSeedOpponentSpot . count )
let maxSpots = max ( availableSeedSpot . count , availableSeedOpponentSpot . count )
if availableSeedGroup = = SeedInterval ( first : 3 , last : 4 ) , availableSeedSpot . count = = 6 {
if availableSeedGroup = = SeedInterval ( first : 3 , last : 4 ) , availableSeedSpot . count = = 6 {
return availableSeedGroup
return availableSeedGroup
} else if availableSeedGroup = = SeedInterval ( first : 5 , last : 8 ) , maxSpots = = 6 , availableSeeds . count = = 2 {
return SeedInterval ( first : 7 , last : 12 )
}
}
if availableSeeds . count = = availableSeedSpot . count && availableSeedGroup . count = = availableSeeds . count {
if availableSeeds . count = = availableSeedSpot . count && availableSeedGroup . count = = availableSeeds . count {
return availableSeedGroup
return availableSeedGroup
} else if availableSeeds . count = = availableSeedOpponentSpot . count && availableSeedGroup . count = = availableSeedOpponentSpot . count {
} else if availableSeeds . count = = availableSeedOpponentSpot . count && availableSeedGroup . count = = availableSeedOpponentSpot . count {
return availableSeedGroup
return availableSeedGroup
} else if let chunks = availableSeedGroup . chunks ( ) {
} else if let chunks = availableSeedGroup . chunks ( ) {
let seededTeamsCount = self . seededTeams ( ) . count
if let chunk = chunks . first ( where : { seedInterval in
if let chunk = chunks . first ( where : { seedInterval in
seedInterval . first >= self . seededTeams ( ) . c ount
return seedInterval . first = = seededTeamsC ount
} ) {
} ) {
return seedGroupAvailable ( atRoundIndex : roundIndex , availableSeedGroup : chunk )
return seedGroupAvailable ( atRoundIndex : roundIndex + 1 , availableSeedGroup : chunk )
} else {
let seeds = seeds ( )
if let firstIndex = seeds . firstIndex ( where : { $0 . isSeedable ( ) } ) {
return SeedInterval ( first : firstIndex + 1 , last : firstIndex + maxSpots )
}
}
}
} else if fullLeftSeeds . count % maxSpots = = 0 {
} else if fullLeftSeeds . count % maxSpots = = 0 || fullLeftSeeds . count >= maxSpots {
let seeds = seeds ( )
let seeds = seeds ( )
if let firstIndex = seeds . firstIndex ( where : { $0 . isSeedable ( ) } ) {
if let firstIndex = seeds . firstIndex ( where : { $0 . isSeedable ( ) } ) {
return SeedInterval ( first : firstIndex + 1 , last : firstIndex + maxSpots )
return SeedInterval ( first : firstIndex + 1 , last : firstIndex + maxSpots )
@ -820,14 +839,14 @@ defer {
for ( index , seed ) in availableSeeds . enumerated ( ) {
for ( index , seed ) in availableSeeds . enumerated ( ) {
seed . setSeedPosition ( inSpot : spots [ index ] , slot : nil , opposingSeeding : false )
seed . setSeedPosition ( inSpot : spots [ index ] , slot : nil , opposingSeeding : false )
}
}
} else if seedGroup = = SeedInterval ( first : 5 , last : 6 ) , availableSeedSpot . count = = 4 {
// } e l s e i f s e e d G r o u p = = S e e d I n t e r v a l ( f i r s t : 5 , l a s t : 6 ) , a v a i l a b l e S e e d S p o t . c o u n t = = 4 {
var spots = [ Match ] ( )
// v a r s p o t s = [ M a t c h ] ( )
spots . append ( availableSeedSpot [ 1 ] )
// s p o t s . a p p e n d ( a v a i l a b l e S e e d S p o t [ 1 ] )
spots . append ( availableSeedSpot [ 2 ] )
// s p o t s . a p p e n d ( a v a i l a b l e S e e d S p o t [ 2 ] )
spots = spots . shuffled ( )
// s p o t s = s p o t s . s h u f f l e d ( )
for ( index , seed ) in availableSeeds . enumerated ( ) {
// f o r ( i n d e x , s e e d ) i n a v a i l a b l e S e e d s . e n u m e r a t e d ( ) {
seed . setSeedPosition ( inSpot : spots [ index ] , slot : nil , opposingSeeding : false )
// s e e d . s e t S e e d P o s i t i o n ( i n S p o t : s p o t s [ i n d e x ] , s l o t : n i l , o p p o s i n g S e e d i n g : f a l s e )
}
// }
} else {
} else {
if availableSeeds . count <= availableSeedSpot . count {
if availableSeeds . count <= availableSeedSpot . count {
let spots = availableSeedSpot . shuffled ( )
let spots = availableSeedSpot . shuffled ( )