@ -13,6 +13,7 @@ import SwiftUI
class Round : ModelObject , Storable {
static func resourceName ( ) -> String { " rounds " }
static func tokenExemptedMethods ( ) -> [ HTTPMethod ] { return [ ] }
static func filterByStoreIdentifier ( ) -> Bool { return true }
var id : String = Store . randomId ( )
var tournament : String
@ -31,16 +32,22 @@ class Round: ModelObject, Storable {
// MARK: - C o m p u t e d d e p e n d e n c i e s
var tournamentStore : TournamentStore {
return TournamentStore . instance ( tournamentId : self . tournament )
}
func tournamentObject ( ) -> Tournament ? {
return Store . main . findById ( tournament )
}
func _matches ( ) -> [ Match ] {
return Store . main . filter { $0 . round = = self . id }
return self . tournamentStore . matches . filter { $0 . round = = self . id }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . r o u n d = = s e l f . i d }
}
func getDisabledMatches ( ) -> [ Match ] {
return Store . main . filter { $0 . round = = self . id && $0 . disabled = = true }
return self . tournamentStore . matches . filter { $0 . round = = self . id && $0 . disabled = = true }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . r o u n d = = s e l f . i d & & $ 0 . d i s a b l e d = = t r u e }
}
// MARK: -
@ -54,7 +61,6 @@ class Round: ModelObject, Storable {
}
}
func hasStarted ( ) -> Bool {
return playedMatches ( ) . anySatisfy ( { $0 . hasStarted ( ) } )
}
@ -74,9 +80,14 @@ class Round: ModelObject, Storable {
func previousMatches ( ofMatch match : Match ) -> [ Match ] {
guard let previousRound = previousRound ( ) else { return [ ] }
return Store . main . filter {
return self . tournamentStore . filter {
( $0 . index = = match . topPreviousRoundMatchIndex ( ) || $0 . index = = match . bottomPreviousRoundMatchIndex ( ) ) && $0 . round = = previousRound . id
}
// r e t u r n S t o r e . m a i n . f i l t e r {
// ( $ 0 . i n d e x = = m a t c h . t o p P r e v i o u s R o u n d M a t c h I n d e x ( ) | | $ 0 . i n d e x = = m a t c h . b o t t o m P r e v i o u s R o u n d M a t c h I n d e x ( ) ) & & $ 0 . r o u n d = = p r e v i o u s R o u n d . i d
// }
}
func precedentMatches ( ofMatch match : Match ) -> [ Match ] {
@ -102,35 +113,49 @@ class Round: ModelObject, Storable {
}
func seed ( _ team : TeamPosition , inMatchIndex matchIndex : Int ) -> TeamRegistration ? {
return Store . main . filter ( isIncluded : {
return self . tournament Store. tea mRegistr at io ns . first ( where : {
$0 . tournament = = tournament
&& $0 . bracketPosition != nil
&& ( $0 . bracketPosition ! / 2 ) = = matchIndex
&& ( $0 . bracketPosition ! % 2 ) = = team . rawValue
} ) . first
} )
// r e t u r n S t o r e . m a i n . f i l t e r ( i s I n c l u d e d : {
// $ 0 . t o u r n a m e n t = = t o u r n a m e n t
// & & $ 0 . b r a c k e t P o s i t i o n ! = n i l
// & & ( $ 0 . b r a c k e t P o s i t i o n ! / 2 ) = = m a t c h I n d e x
// & & ( $ 0 . b r a c k e t P o s i t i o n ! % 2 ) = = t e a m . r a w V a l u e
// } ) . f i r s t
}
func seeds ( inMatchIndex matchIndex : Int ) -> [ TeamRegistration ] {
return Store . main . filter ( isIncluded : {
return self . tournamentStore . teamRegistrations . filter {
$0 . tournament = = tournament
&& $0 . bracketPosition != nil
&& ( $0 . bracketPosition ! / 2 ) = = matchIndex
} )
}
// r e t u r n S t o r e . m a i n . f i l t e r ( i s I n c l u d e d : {
// $ 0 . t o u r n a m e n t = = t o u r n a m e n t
// & & $ 0 . b r a c k e t P o s i t i o n ! = n i l
// & & ( $ 0 . b r a c k e t P o s i t i o n ! / 2 ) = = m a t c h I n d e x
// } )
}
func seeds ( ) -> [ TeamRegistration ] {
let initialMatchIndex = RoundRule . matchIndex ( fromRoundIndex : index )
let numberOfMatches = RoundRule . numberOfMatches ( forRoundIndex : index )
return Store . main . filter ( isIncluded : {
return self . tournamentStore . teamRegistrations . filter {
$0 . tournament = = tournament
&& $0 . bracketPosition != nil
&& ( $0 . bracketPosition ! / 2 ) >= initialMatchIndex
&& ( $0 . bracketPosition ! / 2 ) < initialMatchIndex + numberOfMatches
} )
}
}
func losers ( ) -> [ TeamRegistration ] {
return _matches ( ) . compactMap { $0 . losingTeamId } . compactMap { Store . main . findById ( $0 ) }
let teamIds : [ String ] = self . _matches ( ) . compactMap { $0 . losingTeamId }
return teamIds . compactMap { self . tournamentStore . teamRegistrations . findById ( $0 ) }
}
func teams ( ) -> [ TeamRegistration ] {
@ -147,10 +172,10 @@ class Round: ModelObject, Storable {
if let luckyLoser = match . teamScores . first ( where : { $0 . luckyLoser = = match . index * 2 } ) {
return luckyLoser . team
} else if let parent = upperBracketTopMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( parent )
return self . tournament Store. tea mRegistr at io ns . findById ( parent )
} else if let previousMatch = topPreviousRoundMatch ( ofMatch : match ) {
if let teamId = previousMatch . winningTeamId {
return Store . main . findById ( teamId )
return self . tournament Store. tea mRegistr at io ns . findById ( teamId )
} else if previousMatch . disabled {
return previousMatch . teams ( ) . first
}
@ -159,10 +184,10 @@ class Round: ModelObject, Storable {
if let luckyLoser = match . teamScores . first ( where : { $0 . luckyLoser = = match . index * 2 + 1 } ) {
return luckyLoser . team
} else if let parent = upperBracketBottomMatch ( ofMatchIndex : match . index ) ? . losingTeamId {
return Store . main . findById ( parent )
return self . tournament Store. tea mRegistr at io ns . findById ( parent )
} else if let previousMatch = bottomPreviousRoundMatch ( ofMatch : match ) {
if let teamId = previousMatch . winningTeamId {
return Store . main . findById ( teamId )
return self . tournament Store. tea mRegistr at io ns . findById ( teamId )
} else if previousMatch . disabled {
return previousMatch . teams ( ) . first
}
@ -190,29 +215,39 @@ class Round: ModelObject, Storable {
func topPreviousRoundMatch ( ofMatch match : Match ) -> Match ? {
guard let previousRound = previousRound ( ) else { return nil }
let matches : [ Match ] = Store . main . filter {
let matches : [ Match ] = self . tournamentStore . matches . filter {
$0 . index = = match . topPreviousRoundMatchIndex ( ) && $0 . round = = previousRound . id
}
// l e t m a t c h e s : [ M a t c h ] = S t o r e . m a i n . f i l t e r {
// $ 0 . i n d e x = = m a t c h . t o p P r e v i o u s R o u n d M a t c h I n d e x ( ) & & $ 0 . r o u n d = = p r e v i o u s R o u n d . i d
// }
return matches . sorted ( by : \ . index ) . first
}
func bottomPreviousRoundMatch ( ofMatch match : Match ) -> Match ? {
guard let previousRound = previousRound ( ) else { return nil }
let matches : [ Match ] = Store . main . filter {
let matches : [ Match ] = self . tournament Store. matches . filter {
$0 . index = = match . bottomPreviousRoundMatchIndex ( ) && $0 . round = = previousRound . id
}
return matches . sorted ( by : \ . index ) . first
}
func getMatch ( atMatchIndexInRound matchIndexInRound : Int ) -> Match ? {
Store . main . filter ( isIncluded : {
return self . tournament Store. matches . first ( where : {
let index = RoundRule . matchIndexWithinRound ( fromMatchIndex : $0 . index )
return $0 . round = = id && index = = matchIndexInRound
} ) . first
} )
// S t o r e . m a i n . f i l t e r ( i s I n c l u d e d : {
// l e t i n d e x = R o u n d R u l e . m a t c h I n d e x W i t h i n R o u n d ( f r o m M a t c h I n d e x : $ 0 . i n d e x )
// r e t u r n $ 0 . r o u n d = = i d & & i n d e x = = m a t c h I n d e x I n R o u n d
// } ) . f i r s t
}
func enabledMatches ( ) -> [ Match ] {
return Store . main . filter { $0 . round = = self . id && $0 . disabled = = false }
return self . tournamentStore . matches . filter { $0 . round = = self . id && $0 . disabled = = false }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . r o u n d = = s e l f . i d & & $ 0 . d i s a b l e d = = f a l s e }
}
func displayableMatches ( ) -> [ Match ] {
@ -234,11 +269,12 @@ class Round: ModelObject, Storable {
}
func previousRound ( ) -> Round ? {
return Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index + 1 } ) . first
return self . tournamentStore . rounds . first ( where : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index + 1 } )
// r e t u r n S t o r e . m a i n . f i l t e r ( i s I n c l u d e d : { $ 0 . t o u r n a m e n t = = t o u r n a m e n t & & $ 0 . p a r e n t = = p a r e n t & & $ 0 . i n d e x = = i n d e x + 1 } ) . f i r s t
}
func nextRound ( ) -> Round ? {
return Store . main . filter ( isIncluded : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index - 1 } ) . first
return self . tournament Store. rounds . first ( where : { $0 . tournament = = tournament && $0 . parent = = parent && $0 . index = = index - 1 } )
}
func loserRounds ( forRoundIndex roundIndex : Int ) -> [ Round ] {
@ -302,7 +338,7 @@ class Round: ModelObject, Storable {
// }
}
do {
try DataStore . shared . matches . addOrUpdate ( contentOfs : _matches )
try self . tournamentStore . matches . addOrUpdate ( contentOfs : _matches )
} catch {
Logger . error ( error )
}
@ -352,11 +388,18 @@ class Round: ModelObject, Storable {
func correspondingLoserRoundTitle ( _ displayStyle : DisplayStyle = . wide ) -> String {
let initialMatchIndexFromRoundIndex = RoundRule . matchIndex ( fromRoundIndex : index )
let seedsAfterThisRound : [ TeamRegistration ] = Store . main . filter ( isIncluded : {
let seedsAfterThisRound : [ TeamRegistration ] = self . tournamentStore . teamRegistrations . filter {
$0 . tournament = = tournament
&& $0 . bracketPosition != nil
&& ( $0 . bracketPosition ! / 2 ) < initialMatchIndexFromRoundIndex
} )
}
// l e t s e e d s A f t e r T h i s R o u n d : [ T e a m R e g i s t r a t i o n ] = S t o r e . m a i n . f i l t e r ( i s I n c l u d e d : {
// $ 0 . t o u r n a m e n t = = t o u r n a m e n t
// & & $ 0 . b r a c k e t P o s i t i o n ! = n i l
// & & ( $ 0 . b r a c k e t P o s i t i o n ! / 2 ) < i n i t i a l M a t c h I n d e x F r o m R o u n d I n d e x
// } )
let playedMatches = playedMatches ( )
let seedInterval = SeedInterval ( first : playedMatches . count + seedsAfterThisRound . count + 1 , last : playedMatches . count * 2 + seedsAfterThisRound . count )
return seedInterval . localizedLabel ( displayStyle )
@ -370,11 +413,11 @@ class Round: ModelObject, Storable {
if parent = = nil {
let numberOfMatches = RoundRule . numberOfMatches ( forRoundIndex : index + 1 )
let initialMatchIndexFromRoundIndex = RoundRule . matchIndex ( fromRoundIndex : index )
let seedsAfterThisRound : [ TeamRegistration ] = Store . main . filter ( isIncluded : {
let seedsAfterThisRound : [ TeamRegistration ] = self . tournamentStore . teamRegistrations . filter {
$0 . tournament = = tournament
&& $0 . bracketPosition != nil
&& ( $0 . bracketPosition ! / 2 ) < initialMatchIndexFromRoundIndex
} )
}
let playedMatches = playedMatches ( )
let reduce = numberOfMatches / 2 - ( playedMatches . count + seedsAfterThisRound . count )
return SeedInterval ( first : 1 , last : numberOfMatches , reduce : reduce )
@ -419,7 +462,8 @@ class Round: ModelObject, Storable {
}
func loserRounds ( ) -> [ Round ] {
return Store . main . filter ( isIncluded : { $0 . parent = = id } ) . sorted ( by : \ . index ) . reversed ( )
let rounds : [ Round ] = self . tournamentStore . rounds . filter { $0 . parent = = id }
return rounds . sorted ( by : \ . index ) . reversed ( )
}
func loserRoundsAndChildren ( ) -> [ Round ] {
@ -449,7 +493,7 @@ class Round: ModelObject, Storable {
}
do {
try DataStore . shared . rounds . addOrUpdate ( contentOfs : rounds )
try self . tournamentStore . rounds . addOrUpdate ( contentOfs : rounds )
} catch {
Logger . error ( error )
}
@ -462,7 +506,7 @@ class Round: ModelObject, Storable {
}
do {
try DataStore . shared . matches . addOrUpdate ( contentOfs : matches )
try self . tournamentStore . matches . addOrUpdate ( contentOfs : matches )
} catch {
Logger . error ( error )
}
@ -474,7 +518,7 @@ class Round: ModelObject, Storable {
var parentRound : Round ? {
guard let parent = parent else { return nil }
return Store . main . findById ( parent )
return self . tournament Store. rounds . findById ( parent )
}
func updateMatchFormat ( _ updatedMatchFormat : MatchFormat , checkIfPossible : Bool , andLoserBracket : Bool ) {
@ -499,15 +543,15 @@ class Round: ModelObject, Storable {
match . matchFormat = updatedMatchFormat
}
do {
try DataStore . shared . matches . addOrUpdate ( contentOfs : playedMatches )
try self . tournamentStore . matches . addOrUpdate ( contentOfs : playedMatches )
} catch {
Logger . error ( error )
}
}
override func deleteDependencies ( ) throws {
DataStore . shared . matches . deleteDependencies ( self . _matches ( ) )
DataStore . shared . rounds . deleteDependencies ( self . loserRoundsAndChildren ( ) )
self . tournamentStore . matches . deleteDependencies ( self . _matches ( ) )
self . tournamentStore . rounds . deleteDependencies ( self . loserRoundsAndChildren ( ) )
}
enum CodingKeys : String , CodingKey {
@ -545,10 +589,10 @@ class Round: ModelObject, Storable {
}
}
func insertOnServer ( ) throws {
try DataStore . shared . rounds . writeChangeAndInsertOnServer ( instance : self )
func insertOnServer ( ) {
self . tournamentStore . rounds . writeChangeAndInsertOnServer ( instance : self )
for match in self . _matches ( ) {
try match . insertOnServer ( )
match . insertOnServer ( )
}
}