@ -348,7 +348,8 @@ class Tournament : ModelObject, Storable {
}
}
func groupStages ( ) -> [ GroupStage ] {
func groupStages ( ) -> [ GroupStage ] {
return Store . main . filter { $0 . tournament = = self . id } . sorted ( by : \ . index )
let groupStages : [ GroupStage ] = DataStore . shared . groupStages . filter { $0 . tournament = = self . id }
return groupStages . sorted ( by : \ . index )
}
}
func allRounds ( ) -> [ Round ] {
func allRounds ( ) -> [ Round ] {
@ -713,8 +714,11 @@ class Tournament : ModelObject, Storable {
}
}
func getActiveRound ( withSeeds : Bool = false ) -> Round ? {
func getActiveRound ( withSeeds : Bool = false ) -> Round ? {
let rounds = rounds ( )
let rounds : [ Round ] = self . rounds ( )
let round = rounds . filter ( { $0 . hasStarted ( ) && $0 . hasEnded ( ) = = false } ) . sorted ( by : \ . index ) . reversed ( ) . first ? ? rounds . last ( where : { $0 . hasEnded ( ) } ) ? ? rounds . first
let unfinishedRounds : [ Round ] = rounds . filter { $0 . hasStarted ( ) && $0 . hasEnded ( ) = = false }
let sortedRounds : [ Round ] = unfinishedRounds . sorted ( by : \ . index ) . reversed ( )
let round = sortedRounds . first ? ? rounds . last ( where : { $0 . hasEnded ( ) } ) ? ? rounds . first
if withSeeds {
if withSeeds {
if round ? . seeds ( ) . isEmpty = = false {
if round ? . seeds ( ) . isEmpty = = false {
@ -728,13 +732,14 @@ class Tournament : ModelObject, Storable {
}
}
func getPlayedMatchDateIntervals ( in event : Event ) -> [ DateInterval ] {
func getPlayedMatchDateIntervals ( in event : Event ) -> [ DateInterval ] {
allMatches ( ) . filter { $0 . courtIndex != nil && $0 . startDate != nil } . map { match in
let allMatches : [ Match ] = self . allMatches ( ) . filter { $0 . courtIndex != nil && $0 . startDate != nil }
return allMatches . map { match in
DateInterval ( event : event . id , courtIndex : match . courtIndex ! , startDate : match . startDate ! , endDate : match . estimatedEndDate ( additionalEstimationDuration ) ! )
DateInterval ( event : event . id , courtIndex : match . courtIndex ! , startDate : match . startDate ! , endDate : match . estimatedEndDate ( additionalEstimationDuration ) ! )
}
}
}
}
func allRoundMatches ( ) -> [ Match ] {
func allRoundMatches ( ) -> [ Match ] {
allRounds ( ) . flatMap { $0 . _matches ( ) }
return allRounds ( ) . flatMap { $0 . _matches ( ) }
}
}
func allMatches ( ) -> [ Match ] {
func allMatches ( ) -> [ Match ] {
@ -776,15 +781,27 @@ class Tournament : ModelObject, Storable {
let wcBracket = _teams . filter { $0 . wildCardBracket } . sorted ( using : _currentSelectionSorting , order : . ascending )
let wcBracket = _teams . filter { $0 . wildCardBracket } . sorted ( using : _currentSelectionSorting , order : . ascending )
let groupStageSpots = groupStageSpots ( )
let groupStageSpots : Int = self . groupStageSpots ( )
var bracketSeeds = min ( teamCount , _completeTeams . count ) - groupStageSpots - wcBracket . count
var bracketSeeds : Int = min ( teamCount , _completeTeams . count ) - groupStageSpots - wcBracket . count
var groupStageTeamCount = groupStageSpots - wcGroupStage . count
var groupStageTeamCount : Int = groupStageSpots - wcGroupStage . count
if groupStageTeamCount < 0 { groupStageTeamCount = 0 }
if groupStageTeamCount < 0 { groupStageTeamCount = 0 }
if bracketSeeds < 0 { bracketSeeds = 0 }
if bracketSeeds < 0 { bracketSeeds = 0 }
if prioritizeClubMembers {
if prioritizeClubMembers {
let bracketTeams = ( _completeTeams . filter { $0 . hasMemberOfClub ( clubName ) } + _completeTeams . filter { $0 . hasMemberOfClub ( clubName ) = = false } . sorted ( using : defaultSorting , order : . ascending ) ) . prefix ( bracketSeeds ) . sorted ( using : _currentSelectionSorting , order : . ascending ) + wcBracket
var bracketTeams : [ TeamRegistration ] = [ ]
bracketTeams . append ( contentsOf : _completeTeams . filter { $0 . hasMemberOfClub ( clubName ) } )
let others : [ TeamRegistration ] = _completeTeams . filter { $0 . hasMemberOfClub ( clubName ) = = false }
let sortedOthers : [ TeamRegistration ] = others . sorted ( using : defaultSorting , order : . ascending )
bracketTeams . append ( contentsOf : sortedOthers )
bracketTeams = bracketTeams
. prefix ( bracketSeeds )
. sorted ( using : _currentSelectionSorting , order : . ascending )
bracketTeams . append ( contentsOf : wcBracket )
// l e t b r a c k e t T e a m s : [ T e a m R e g i s t r a t i o n ] = ( _ c o m p l e t e T e a m s . f i l t e r { $ 0 . h a s M e m b e r O f C l u b ( c l u b N a m e ) } + _ c o m p l e t e T e a m s . f i l t e r { $ 0 . h a s M e m b e r O f C l u b ( c l u b N a m e ) = = f a l s e } . s o r t e d ( u s i n g : d e f a u l t S o r t i n g , o r d e r : . a s c e n d i n g ) ) . p r e f i x ( b r a c k e t S e e d s ) . s o r t e d ( u s i n g : _ c u r r e n t S e l e c t i o n S o r t i n g , o r d e r : . a s c e n d i n g ) + w c B r a c k e t
let groupStageTeamsNoFiltering = Set ( _completeTeams ) . subtracting ( bracketTeams )
let groupStageTeamsNoFiltering = Set ( _completeTeams ) . subtracting ( bracketTeams )
let groupStageTeams = ( groupStageTeamsNoFiltering . filter { $0 . hasMemberOfClub ( clubName ) } + groupStageTeamsNoFiltering . filter { $0 . hasMemberOfClub ( clubName ) = = false } . sorted ( using : defaultSorting , order : . ascending ) ) . prefix ( groupStageTeamCount ) . sorted ( using : _currentSelectionSorting , order : . ascending ) + wcGroupStage
let groupStageTeams = ( groupStageTeamsNoFiltering . filter { $0 . hasMemberOfClub ( clubName ) } + groupStageTeamsNoFiltering . filter { $0 . hasMemberOfClub ( clubName ) = = false } . sorted ( using : defaultSorting , order : . ascending ) ) . prefix ( groupStageTeamCount ) . sorted ( using : _currentSelectionSorting , order : . ascending ) + wcGroupStage
@ -804,11 +821,11 @@ class Tournament : ModelObject, Storable {
}
}
func bracketCut ( ) -> Int {
func bracketCut ( ) -> Int {
max ( 0 , teamCount - groupStageCut ( ) )
return max ( 0 , teamCount - groupStageCut ( ) )
}
}
func groupStageCut ( ) -> Int {
func groupStageCut ( ) -> Int {
groupStageSpots ( )
return groupStageSpots ( )
}
}
func cutLabel ( index : Int ) -> String {
func cutLabel ( index : Int ) -> String {
@ -822,11 +839,11 @@ class Tournament : ModelObject, Storable {
}
}
func unsortedTeamsWithoutWO ( ) -> [ TeamRegistration ] {
func unsortedTeamsWithoutWO ( ) -> [ TeamRegistration ] {
Store . main . filter { $0 . tournament = = self . id && $0 . walkOut = = false }
return Store . main . filter { $0 . tournament = = self . id && $0 . walkOut = = false }
}
}
func walkoutTeams ( ) -> [ TeamRegistration ] {
func walkoutTeams ( ) -> [ TeamRegistration ] {
Store . main . filter { $0 . tournament = = self . id && $0 . walkOut = = true }
return Store . main . filter { $0 . tournament = = self . id && $0 . walkOut = = true }
}
}
func duplicates ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
func duplicates ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
@ -841,15 +858,15 @@ class Tournament : ModelObject, Storable {
}
}
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
unsortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) }
return self . unsortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) }
}
}
func selectedPlayers ( ) -> [ PlayerRegistration ] {
func selectedPlayers ( ) -> [ PlayerRegistration ] {
selectedSortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) } . sorted ( by : \ . computedRank )
return self . selectedSortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) } . sorted ( by : \ . computedRank )
}
}
func players ( ) -> [ PlayerRegistration ] {
func players ( ) -> [ PlayerRegistration ] {
unsortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) } . sorted ( by : \ . computedRank )
return self . unsortedTeams ( ) . flatMap { $0 . unsortedPlayers ( ) } . sorted ( by : \ . computedRank )
}
}
func unrankValue ( for malePlayer : Bool ) -> Int ? {
func unrankValue ( for malePlayer : Bool ) -> Int ? {
@ -867,12 +884,12 @@ class Tournament : ModelObject, Storable {
// t o d o
// t o d o
var clubName : String ? {
var clubName : String ? {
eventObject ( ) ? . clubObject ( ) ? . name
return self . eventObject ( ) ? . clubObject ( ) ? . name
}
}
// t o d o
// t o d o
func significantPlayerCount ( ) -> Int {
func significantPlayerCount ( ) -> Int {
2
return 2
}
}
func inadequatePlayers ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
func inadequatePlayers ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
@ -911,7 +928,7 @@ class Tournament : ModelObject, Storable {
}
}
func playersWithoutValidLicense ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
func playersWithoutValidLicense ( in players : [ PlayerRegistration ] ) -> [ PlayerRegistration ] {
let licenseYearValidity = licenseYearValidity ( )
let licenseYearValidity = self . licenseYearValidity ( )
return players . filter ( {
return players . filter ( {
( $0 . isImported ( ) && $0 . isValidLicenseNumber ( year : licenseYearValidity ) = = false ) || ( $0 . isImported ( ) = = false && ( $0 . licenceId = = nil || $0 . formattedLicense ( ) . isLicenseNumber = = false || $0 . licenceId ? . isEmpty = = true ) )
( $0 . isImported ( ) && $0 . isValidLicenseNumber ( year : licenseYearValidity ) = = false ) || ( $0 . isImported ( ) = = false && ( $0 . licenceId = = nil || $0 . formattedLicense ( ) . isLicenseNumber = = false || $0 . licenceId ? . isEmpty = = true ) )
} )
} )
@ -1060,7 +1077,7 @@ class Tournament : ModelObject, Storable {
ids . insert ( finalist )
ids . insert ( finalist )
}
}
let others : [ Round ] = rounds . flatMap { round in
let others : [ Round ] = rounds . flatMap { round in
round . loserRoundsAndChildren ( ) . filter { $0 . isRankDisabled ( ) = = false && $0 . hasNextRound ( ) = = false }
round . loserRoundsAndChildren ( ) . filter { $0 . isRankDisabled ( ) = = false && $0 . hasNextRound ( ) = = false }
} . compactMap ( { $0 } )
} . compactMap ( { $0 } )
@ -1101,7 +1118,6 @@ class Tournament : ModelObject, Storable {
}
}
}
}
return teams
return teams
}
}
@ -1244,13 +1260,14 @@ class Tournament : ModelObject, Storable {
// r e t u r n q u a l i f i e d T e a m s ( ) . c o u n t = = q u a l i f i e d F r o m G r o u p S t a g e ( ) + g r o u p S t a g e A d d i t i o n a l Q u a l i f i e d
// r e t u r n q u a l i f i e d T e a m s ( ) . c o u n t = = q u a l i f i e d F r o m G r o u p S t a g e ( ) + g r o u p S t a g e A d d i t i o n a l Q u a l i f i e d
}
}
func paymentMethodMessage ( ) -> String ? {
fileprivate func _ paymentMethodMessage( ) -> String ? {
return DataStore . shared . user . summonsAvailablePaymentMethods ? ? ContactType . defaultAvailablePaymentMethods
return DataStore . shared . user . summonsAvailablePaymentMethods ? ? ContactType . defaultAvailablePaymentMethods
}
}
var entryFeeMessage : String {
var entryFeeMessage : String {
if let entryFee {
if let entryFee {
return [ " Inscription: " + entryFee . formatted ( . currency ( code : " EUR " ) ) + " par joueur. " , paymentMethodMessage ( ) ] . compactMap { $0 } . joined ( separator : " \n " )
let message : String = " Inscription: \( entryFee . formatted ( . currency ( code : " EUR " ) ) ) par joueur. "
return [ message , self . _paymentMethodMessage ( ) ] . compactMap { $0 } . joined ( separator : " \n " )
} else {
} else {
return " Inscription: gratuite. "
return " Inscription: gratuite. "
}
}
@ -1551,7 +1568,8 @@ class Tournament : ModelObject, Storable {
}
}
func addTeam ( _ players : Set < PlayerRegistration > , registrationDate : Date ? = nil , name : String ? = nil ) -> TeamRegistration {
func addTeam ( _ players : Set < PlayerRegistration > , registrationDate : Date ? = nil , name : String ? = nil ) -> TeamRegistration {
let team = TeamRegistration ( tournament : id , registrationDate : registrationDate ? ? Date ( ) , name : name )
let date : Date = registrationDate ? ? Date ( )
let team = TeamRegistration ( tournament : id , registrationDate : date , name : name )
team . setWeight ( from : Array ( players ) , inTournamentCategory : tournamentCategory )
team . setWeight ( from : Array ( players ) , inTournamentCategory : tournamentCategory )
players . forEach { player in
players . forEach { player in
player . teamRegistration = team . id
player . teamRegistration = team . id
@ -1686,7 +1704,8 @@ class Tournament : ModelObject, Storable {
private let _currentSelectionSorting : [ MySortDescriptor < TeamRegistration > ] = [ . keyPath ( \ . weight ) , . keyPath ( \ . registrationDate ! ) ]
private let _currentSelectionSorting : [ MySortDescriptor < TeamRegistration > ] = [ . keyPath ( \ . weight ) , . keyPath ( \ . registrationDate ! ) ]
private func _matchSchedulers ( ) -> [ MatchScheduler ] {
private func _matchSchedulers ( ) -> [ MatchScheduler ] {
return Store . main . filter ( isIncluded : { $0 . tournament = = self . id } )
return DataStore . shared . matchSchedulers . filter { $0 . tournament = = self . id }
// D a t a S t o r e . s h a r e d . m a t c h S c h e d u l e r s . 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 = = s e l f . i d } )
}
}
func matchScheduler ( ) -> MatchScheduler ? {
func matchScheduler ( ) -> MatchScheduler ? {