@ -15,14 +15,14 @@ class Round: ModelObject, Storable {
var id : String = Store . randomId ( )
var tournament : String
var index : Int
var loser : String ?
var parent : String ?
var format : Int ?
var startDate : Date ?
internal init ( tournament : String , index : Int , loser : String ? = nil , matchFormat : MatchFormat ? = nil ) {
internal init ( tournament : String , index : Int , parent : String ? = nil , matchFormat : MatchFormat ? = nil ) {
self . tournament = tournament
self . index = index
self . loser = loser
self . parent = parent
self . format = matchFormat ? . rawValue
}
@ -53,7 +53,7 @@ class Round: ModelObject, Storable {
}
func upperMatches ( ofMatch match : Match ) -> [ Match ] {
if loser != nil , previousRound ( ) = = nil , let parentRound {
if parent != nil , previousRound ( ) = = nil , let parentRound {
let matchIndex = match . index
let indexInRound = RoundRule . matchIndexWithinRound ( fromMatchIndex : matchIndex )
return [ parentRound . getMatch ( atMatchIndexInRound : indexInRound * 2 ) , parentRound . getMatch ( atMatchIndexInRound : indexInRound * 2 + 1 ) ] . compactMap ( { $0 } )
@ -131,8 +131,8 @@ class Round: ModelObject, Storable {
case . one :
if let luckyLoser = match . teamScores . first ( where : { $0 . luckyLoser = = match . index * 2 } ) {
return luckyLoser . team
} else if let loser = upperBracketTopMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( loser )
} else if let parent = upperBracketTopMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( parent )
} else if let previousMatch = topPreviousRoundMatch ( ofMatch : match ) {
if let teamId = previousMatch . winningTeamId {
return Store . main . findById ( teamId )
@ -143,8 +143,8 @@ class Round: ModelObject, Storable {
case . two :
if let luckyLoser = match . teamScores . first ( where : { $0 . luckyLoser = = match . index * 2 + 1 } ) {
return luckyLoser . team
} else if let loser = upperBracketBottomMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( loser )
} else if let parent = upperBracketBottomMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( parent )
} else if let previousMatch = bottomPreviousRoundMatch ( ofMatch : match ) {
if let teamId = previousMatch . winningTeamId {
return Store . main . findById ( teamId )
@ -196,7 +196,7 @@ class Round: ModelObject, Storable {
}
func playedMatches ( ) -> [ Match ] {
if loser = = nil {
if parent = = nil {
Store . main . filter { $0 . round = = self . id && $0 . disabled = = false }
} else {
_matches ( )
@ -204,11 +204,11 @@ class Round: ModelObject, Storable {
}
func previousRound ( ) -> Round ? {
Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . loser = = loser && $0 . index = = index + 1 } ) . first
Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index + 1 } ) . first
}
func nextRound ( ) -> Round ? {
Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . loser = = loser && $0 . index = = index - 1 } ) . first
Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index - 1 } ) . first
}
func loserRounds ( forRoundIndex roundIndex : Int ) -> [ Round ] {
@ -314,7 +314,7 @@ class Round: ModelObject, Storable {
}
func seedInterval ( ) -> SeedInterval ? {
if loser = = nil {
if parent = = nil {
let numberOfMatches = RoundRule . numberOfMatches ( forRoundIndex : index + 1 )
let initialMatchIndexFromRoundIndex = RoundRule . matchIndex ( fromRoundIndex : index )
let seedsAfterThisRound : [ TeamRegistration ] = Store . main . filter ( isIncluded : {
@ -337,7 +337,7 @@ class Round: ModelObject, Storable {
}
func roundTitle ( _ displayStyle : DisplayStyle = . wide ) -> String {
if loser != nil {
if parent != nil {
return seedInterval ( ) ? . localizedLabel ( displayStyle ) ? ? " Pas trouvé "
}
return RoundRule . roundName ( fromRoundIndex : index )
@ -352,7 +352,7 @@ class Round: ModelObject, Storable {
}
func loserRounds ( ) -> [ Round ] {
return Store . main . filter ( isIncluded : { $0 . loser = = id } ) . sorted ( by : \ . index ) . reversed ( )
return Store . main . filter ( isIncluded : { $0 . parent = = id } ) . sorted ( by : \ . index ) . reversed ( )
}
func loserRoundsAndChildren ( ) -> [ Round ] {
@ -361,7 +361,7 @@ class Round: ModelObject, Storable {
}
func isLoserBracket ( ) -> Bool {
loser != nil
parent != nil
}
func buildLoserBracket ( ) {
@ -373,7 +373,7 @@ class Round: ModelObject, Storable {
let rounds = ( 0. . < roundCount ) . map { // i n d e x 0 i s t h e f i n a l
let round = Round ( tournament : tournament , index : $0 , matchFormat : loserBracketMatchFormat )
round . loser = id // p a r e n t
round . parent = id // p a r e n t
return round
}
@ -398,7 +398,7 @@ class Round: ModelObject, Storable {
}
var parentRound : Round ? {
guard let parent = loser else { return nil }
guard let parent = parent else { return nil }
return Store . main . findById ( parent )
}
@ -420,7 +420,7 @@ class Round: ModelObject, Storable {
case _id = " id "
case _tournament = " tournament "
case _index = " index "
case _loser = " loser "
case _parent = " parent "
case _format = " format "
case _startDate = " startDate "
}