@ -12,42 +12,49 @@ import SwiftUI
@ Observable
@ Observable
final class TeamRegistration : BaseTeamRegistration , SideStorable {
final class TeamRegistration : BaseTeamRegistration , SideStorable {
// s t a t i c f u n c r e s o u r c e N a m e ( ) - > S t r i n g { " t e a m - r e g i s t r a t i o n s " }
// s t a t i c f u n c r e s o u r c e N a m e ( ) - > S t r i n g { " t e a m - r e g i s t r a t i o n s " }
// s t a t i c f u n c t o k e n E x e m p t e d M e t h o d s ( ) - > [ H T T P M e t h o d ] { r e t u r n [ ] }
// s t a t i c f u n c t o k e n E x e m p t e d M e t h o d s ( ) - > [ H T T P M e t h o d ] { r e t u r n [ ] }
// s t a t i c f u n c f i l t e r B y S t o r e I d e n t i f i e r ( ) - > B o o l { r e t u r n t r u e }
// s t a t i c f u n c f i l t e r B y S t o r e I d e n t i f i e r ( ) - > B o o l { r e t u r n t r u e }
// s t a t i c v a r r e l a t i o n s h i p N a m e s : [ S t r i n g ] = [ ]
// s t a t i c v a r r e l a t i o n s h i p N a m e s : [ S t r i n g ] = [ ]
//
//
// v a r i d : S t r i n g = S t o r e . r a n d o m I d ( )
// v a r i d : S t r i n g = S t o r e . r a n d o m I d ( )
// v a r l a s t U p d a t e : D a t e
// v a r l a s t U p d a t e : D a t e
// v a r t o u r n a m e n t : S t r i n g
// v a r t o u r n a m e n t : S t r i n g
// v a r g r o u p S t a g e : S t r i n g ?
// v a r g r o u p S t a g e : S t r i n g ?
// v a r r e g i s t r a t i o n D a t e : D a t e ?
// v a r r e g i s t r a t i o n D a t e : D a t e ?
// v a r c a l l D a t e : D a t e ?
// v a r c a l l D a t e : D a t e ?
// v a r b r a c k e t P o s i t i o n : I n t ?
// v a r b r a c k e t P o s i t i o n : I n t ?
// v a r g r o u p S t a g e P o s i t i o n : I n t ?
// v a r g r o u p S t a g e P o s i t i o n : I n t ?
// v a r c o m m e n t : S t r i n g ?
// v a r c o m m e n t : S t r i n g ?
// v a r s o u r c e : S t r i n g ?
// v a r s o u r c e : S t r i n g ?
// v a r s o u r c e V a l u e : S t r i n g ?
// v a r s o u r c e V a l u e : S t r i n g ?
// v a r l o g o : S t r i n g ?
// v a r l o g o : S t r i n g ?
// v a r n a m e : S t r i n g ?
// v a r n a m e : S t r i n g ?
//
//
// v a r w a l k O u t : B o o l = f a l s e
// v a r w a l k O u t : B o o l = f a l s e
// v a r w i l d C a r d B r a c k e t : B o o l = f a l s e
// v a r w i l d C a r d B r a c k e t : B o o l = f a l s e
// v a r w i l d C a r d G r o u p S t a g e : B o o l = f a l s e
// v a r w i l d C a r d G r o u p S t a g e : B o o l = f a l s e
// v a r w e i g h t : I n t = 0
// v a r w e i g h t : I n t = 0
// v a r l o c k e d W e i g h t : I n t ?
// v a r l o c k e d W e i g h t : I n t ?
// v a r c o n f i r m a t i o n D a t e : D a t e ?
// v a r c o n f i r m a t i o n D a t e : D a t e ?
// v a r q u a l i f i e d : B o o l = f a l s e
// v a r q u a l i f i e d : B o o l = f a l s e
// v a r f i n a l R a n k i n g : I n t ?
// v a r f i n a l R a n k i n g : I n t ?
// v a r p o i n t s E a r n e d : I n t ?
// v a r p o i n t s E a r n e d : I n t ?
//
//
// v a r s t o r e I d : S t r i n g ? = n i l
// v a r s t o r e I d : S t r i n g ? = n i l
init ( tournament : String , groupStage : String ? = nil , registrationDate : Date ? = nil , callDate : Date ? = nil , bracketPosition : Int ? = nil , groupStagePosition : Int ? = nil , comment : String ? = nil , source : String ? = nil , sourceValue : String ? = nil , logo : String ? = nil , name : String ? = nil , walkOut : Bool = false , wildCardBracket : Bool = false , wildCardGroupStage : Bool = false , weight : Int = 0 , lockedWeight : Int ? = nil , confirmationDate : Date ? = nil , qualified : Bool = false ) {
init (
tournament : String , groupStage : String ? = nil , registrationDate : Date ? = nil ,
callDate : Date ? = nil , bracketPosition : Int ? = nil , groupStagePosition : Int ? = nil ,
comment : String ? = nil , source : String ? = nil , sourceValue : String ? = nil ,
logo : String ? = nil , name : String ? = nil , walkOut : Bool = false ,
wildCardBracket : Bool = false , wildCardGroupStage : Bool = false , weight : Int = 0 ,
lockedWeight : Int ? = nil , confirmationDate : Date ? = nil , qualified : Bool = false
) {
super . init ( )
super . init ( )
// s e l f . s t o r e I d = t o u r n a m e n t
// s e l f . s t o r e I d = t o u r n a m e n t
self . tournament = tournament
self . tournament = tournament
self . groupStage = groupStage
self . groupStage = groupStage
self . registrationDate = registrationDate ? ? Date ( )
self . registrationDate = registrationDate ? ? Date ( )
@ -76,7 +83,6 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
players ( ) . anySatisfy ( { $0 . source = = nil } )
players ( ) . anySatisfy ( { $0 . source = = nil } )
}
}
func isOutOfTournament ( ) -> Bool {
func isOutOfTournament ( ) -> Bool {
walkOut
walkOut
}
}
@ -93,7 +99,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
guard let tournamentStore = self . tournamentStore else { return [ ] }
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . playerRegistrations . filter { $0 . teamRegistration = = self . id && $0 . coach = = false }
return tournamentStore . playerRegistrations . filter {
$0 . teamRegistration = = self . id && $0 . coach = = false
}
}
}
// MARK: -
// MARK: -
@ -130,20 +138,21 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
return unsortedPlayers . allSatisfy ( { $0 . hasArrived } )
return unsortedPlayers . allSatisfy ( { $0 . hasArrived } )
}
}
func isSeedable ( ) -> Bool {
func isSeedable ( ) -> Bool {
bracketPosition = = nil && groupStage = = nil
bracketPosition = = nil && groupStage = = nil
}
}
func setSeedPosition ( inSpot match : Match , slot : TeamPosition ? , opposingSeeding : Bool ) {
func setSeedPosition ( inSpot match : Match , slot : TeamPosition ? , opposingSeeding : Bool ) {
var teamPosition : TeamPosition {
var teamPosition : TeamPosition {
if let slot {
if let slot {
return slot
return slot
} else {
} else {
let matchIndex = match . index
let matchIndex = match . index
let seedRound = RoundRule . roundIndex ( fromMatchIndex : matchIndex )
let seedRound = RoundRule . roundIndex ( fromMatchIndex : matchIndex )
let numberOfMatches = RoundRule . numberOfMatches ( forRoundIndex : seedRound )
let numberOfMatches = RoundRule . numberOfMatches ( forRoundIndex : seedRound )
let isUpper = RoundRule . matchIndexWithinRound ( fromMatchIndex : matchIndex ) < ( numberOfMatches / 2 )
let isUpper =
RoundRule . matchIndexWithinRound ( fromMatchIndex : matchIndex )
< ( numberOfMatches / 2 )
var teamPosition = slot ? ? ( isUpper ? . one : . two )
var teamPosition = slot ? ? ( isUpper ? . one : . two )
if opposingSeeding {
if opposingSeeding {
teamPosition = slot ? ? ( isUpper ? . two : . one )
teamPosition = slot ? ? ( isUpper ? . two : . one )
@ -160,7 +169,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
}
}
if let tournament = tournamentObject ( ) {
if let tournament = tournamentObject ( ) {
if let index = index ( in : tournament . selectedSortedTeams ( ) ) {
if let index = index ( in : tournament . selectedSortedTeams ( ) ) {
let drawLog = DrawLog ( tournament : tournament . id , drawSeed : index , drawMatchIndex : match . index , drawTeamPosition : teamPosition , drawType : . seed )
let drawLog = DrawLog (
tournament : tournament . id , drawSeed : index , drawMatchIndex : match . index ,
drawTeamPosition : teamPosition , drawType : . seed )
do {
do {
try tournamentStore ? . drawLogs . addOrUpdate ( instance : drawLog )
try tournamentStore ? . drawLogs . addOrUpdate ( instance : drawLog )
} catch {
} catch {
@ -252,7 +263,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func hasMemberOfClub ( _ codeClubOrClubName : String ? ) -> Bool {
func hasMemberOfClub ( _ codeClubOrClubName : String ? ) -> Bool {
guard let codeClubOrClubName else { return true }
guard let codeClubOrClubName else { return true }
return unsortedPlayers ( ) . anySatisfy ( {
return unsortedPlayers ( ) . anySatisfy ( {
$0 . clubName ? . contains ( codeClubOrClubName ) = = true || $0 . clubName ? . contains ( codeClubOrClubName ) = = true
$0 . clubName ? . contains ( codeClubOrClubName ) = = true
|| $0 . clubName ? . contains ( codeClubOrClubName ) = = true
} )
} )
}
}
@ -260,13 +272,19 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
self . setWeight ( from : self . players ( ) , inTournamentCategory : tournamentCategory )
self . setWeight ( from : self . players ( ) , inTournamentCategory : tournamentCategory )
}
}
func teamLabel ( _ displayStyle : DisplayStyle = . wide , twoLines : Bool = false , separator : String = " & " ) -> String {
func teamLabel (
_ displayStyle : DisplayStyle = . wide , twoLines : Bool = false , separator : String = " & "
) -> String {
if let name { return name }
if let name { return name }
return players ( ) . map { $0 . playerLabel ( displayStyle ) } . joined ( separator : twoLines ? " \n " : " \( separator ) " )
return players ( ) . map { $0 . playerLabel ( displayStyle ) } . joined (
separator : twoLines ? " \n " : " \( separator ) " )
}
}
func teamLabelRanked ( displayRank : Bool , displayTeamName : Bool ) -> String {
func teamLabelRanked ( displayRank : Bool , displayTeamName : Bool ) -> String {
[ displayTeamName ? name : nil , displayRank ? seedIndex ( ) : nil , displayTeamName ? ( name = = nil ? teamLabel ( ) : name ) : teamLabel ( ) ] . compactMap ( { $0 } ) . joined ( separator : " " )
[
displayTeamName ? name : nil , displayRank ? seedIndex ( ) : nil ,
displayTeamName ? ( name = = nil ? teamLabel ( ) : name ) : teamLabel ( ) ,
] . compactMap ( { $0 } ) . joined ( separator : " " )
}
}
func seedIndex ( ) -> String ? {
func seedIndex ( ) -> String ? {
@ -279,8 +297,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
return teams . firstIndex ( where : { $0 . id = = id } )
return teams . firstIndex ( where : { $0 . id = = id } )
}
}
func formattedSeed ( in teams : [ TeamRegistration ] ) -> String {
func formattedSeed ( in teams : [ TeamRegistration ] ? = nil ) -> String {
if let index = index ( in : teams ) {
let selectedSortedTeams = teams ? ? tournamentObject ( ) ? . selectedSortedTeams ( ) ? ? [ ]
if let index = index ( in : selectedSortedTeams ) {
return " # \( index + 1 ) "
return " # \( index + 1 ) "
} else {
} else {
return " ### "
return " ### "
@ -288,13 +307,17 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
}
}
func contains ( _ searchField : String ) -> Bool {
func contains ( _ searchField : String ) -> Bool {
return unsortedPlayers ( ) . anySatisfy ( { $0 . contains ( searchField ) } ) || self . name ? . localizedCaseInsensitiveContains ( searchField ) = = true
return unsortedPlayers ( ) . anySatisfy ( { $0 . contains ( searchField ) } )
|| self . name ? . localizedCaseInsensitiveContains ( searchField ) = = true
}
}
func containsExactlyPlayerLicenses ( _ playerLicenses : [ String ? ] ) -> Bool {
func containsExactlyPlayerLicenses ( _ playerLicenses : [ String ? ] ) -> Bool {
let arrayOfIds : [ String ] = unsortedPlayers ( ) . compactMap ( { $0 . licenceId ? . strippedLicense ? . canonicalVersion } )
let arrayOfIds : [ String ] = unsortedPlayers ( ) . compactMap ( {
let ids : Set < String > = Set < String > ( arrayOfIds . sorted ( ) )
$0 . licenceId ? . strippedLicense ? . canonicalVersion
let searchedIds = Set < String > ( playerLicenses . compactMap ( { $0 ? . strippedLicense ? . canonicalVersion } ) . sorted ( ) )
} )
let ids : Set < String > = Set < String > ( arrayOfIds . sorted ( ) )
let searchedIds = Set < String > (
playerLicenses . compactMap ( { $0 ? . strippedLicense ? . canonicalVersion } ) . sorted ( ) )
if ids . isEmpty || searchedIds . isEmpty { return false }
if ids . isEmpty || searchedIds . isEmpty { return false }
return ids . hashValue = = searchedIds . hashValue
return ids . hashValue = = searchedIds . hashValue
}
}
@ -319,7 +342,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
let unsortedPlayers = unsortedPlayers ( )
let unsortedPlayers = unsortedPlayers ( )
if unsortedPlayers . isEmpty { return false }
if unsortedPlayers . isEmpty { return false }
return matches ( ) . isEmpty = = false || unsortedPlayers . allSatisfy ( { $0 . hasPaid ( ) || $0 . hasArrived } )
return matches ( ) . isEmpty = = false
|| unsortedPlayers . allSatisfy ( { $0 . hasPaid ( ) || $0 . hasArrived } )
}
}
func availableForSeedPick ( ) -> Bool {
func availableForSeedPick ( ) -> Bool {
@ -346,7 +370,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func initialRoundColor ( ) -> Color ? {
func initialRoundColor ( ) -> Color ? {
if walkOut { return Color . logoRed }
if walkOut { return Color . logoRed }
if groupStagePosition != nil { return Color . blue }
if groupStagePosition != nil { return Color . blue }
if let initialRound = initialRound ( ) , let colorHex = RoundRule . colors [ safe : initialRound . index ] {
if let initialRound = initialRound ( ) ,
let colorHex = RoundRule . colors [ safe : initialRound . index ]
{
return Color ( uiColor : . init ( fromHex : colorHex ) )
return Color ( uiColor : . init ( fromHex : colorHex ) )
} else {
} else {
return nil
return nil
@ -356,8 +382,12 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func resetGroupeStagePosition ( ) {
func resetGroupeStagePosition ( ) {
guard let tournamentStore = self . tournamentStore else { return }
guard let tournamentStore = self . tournamentStore else { return }
if let groupStage {
if let groupStage {
let matches = tournamentStore . matches . filter ( { $0 . groupStage = = groupStage } ) . map { $0 . id }
let matches = tournamentStore . matches . filter ( { $0 . groupStage = = groupStage } ) . map {
let teamScores = tournamentStore . teamScores . filter ( { $0 . teamRegistration = = id && matches . contains ( $0 . match ) } )
$0 . id
}
let teamScores = tournamentStore . teamScores . filter ( {
$0 . teamRegistration = = id && matches . contains ( $0 . match )
} )
tournamentStore . teamScores . delete ( contentOfs : teamScores )
tournamentStore . teamScores . delete ( contentOfs : teamScores )
}
}
// g r o u p S t a g e O b j e c t ( ) ? . _ m a t c h e s ( ) . f o r E a c h ( { $ 0 . u p d a t e T e a m S c o r e s ( ) } )
// g r o u p S t a g e O b j e c t ( ) ? . _ m a t c h e s ( ) . f o r E a c h ( { $ 0 . u p d a t e T e a m S c o r e s ( ) } )
@ -368,7 +398,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func resetBracketPosition ( ) {
func resetBracketPosition ( ) {
guard let tournamentStore = self . tournamentStore else { return }
guard let tournamentStore = self . tournamentStore else { return }
let matches = tournamentStore . matches . filter ( { $0 . groupStage = = nil } ) . map { $0 . id }
let matches = tournamentStore . matches . filter ( { $0 . groupStage = = nil } ) . map { $0 . id }
let teamScores = tournamentStore . teamScores . filter ( { $0 . teamRegistration = = id && matches . contains ( $0 . match ) } )
let teamScores = tournamentStore . teamScores . filter ( {
$0 . teamRegistration = = id && matches . contains ( $0 . match )
} )
tournamentStore . teamScores . delete ( contentOfs : teamScores )
tournamentStore . teamScores . delete ( contentOfs : teamScores )
self . bracketPosition = nil
self . bracketPosition = nil
@ -382,9 +414,13 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func pasteData ( _ exportFormat : ExportFormat = . rawText , _ index : Int = 0 ) -> String {
func pasteData ( _ exportFormat : ExportFormat = . rawText , _ index : Int = 0 ) -> String {
switch exportFormat {
switch exportFormat {
case . rawText :
case . rawText :
return [ playersPasteData ( exportFormat ) , formattedInscriptionDate ( exportFormat ) , name ] . compactMap ( { $0 } ) . joined ( separator : exportFormat . newLineSeparator ( ) )
return [ playersPasteData ( exportFormat ) , formattedInscriptionDate ( exportFormat ) , name ]
. compactMap ( { $0 } ) . joined ( separator : exportFormat . newLineSeparator ( ) )
case . csv :
case . csv :
return [ index . formatted ( ) , playersPasteData ( exportFormat ) , isWildCard ( ) ? " WC " : weight . formatted ( ) ] . joined ( separator : exportFormat . separator ( ) )
return [
index . formatted ( ) , playersPasteData ( exportFormat ) ,
isWildCard ( ) ? " WC " : weight . formatted ( ) ,
] . joined ( separator : exportFormat . separator ( ) )
}
}
}
}
@ -395,7 +431,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func formattedInscriptionDate ( _ exportFormat : ExportFormat = . rawText ) -> String ? {
func formattedInscriptionDate ( _ exportFormat : ExportFormat = . rawText ) -> String ? {
guard let registrationDate else { return nil }
guard let registrationDate else { return nil }
let formattedDate = registrationDate . formatted ( . dateTime . weekday ( ) . day ( ) . month ( ) . hour ( ) . minute ( ) )
let formattedDate = registrationDate . formatted (
. dateTime . weekday ( ) . day ( ) . month ( ) . hour ( ) . minute ( ) )
let onlineSuffix = hasRegisteredOnline ( ) ? " en ligne " : " "
let onlineSuffix = hasRegisteredOnline ( ) ? " en ligne " : " "
switch exportFormat {
switch exportFormat {
@ -411,7 +448,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
switch exportFormat {
switch exportFormat {
case . rawText :
case . rawText :
if let callDate {
if let callDate {
return " Convoqué le " + callDate . formatted ( . dateTime . weekday ( ) . day ( ) . month ( ) . hour ( ) . minute ( ) )
return " Convoqué le "
+ callDate . formatted ( . dateTime . weekday ( ) . day ( ) . month ( ) . hour ( ) . minute ( ) )
} else {
} else {
return nil
return nil
}
}
@ -427,18 +465,27 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func playersPasteData ( _ exportFormat : ExportFormat = . rawText ) -> String {
func playersPasteData ( _ exportFormat : ExportFormat = . rawText ) -> String {
switch exportFormat {
switch exportFormat {
case . rawText :
case . rawText :
return players ( ) . map { $0 . pasteData ( exportFormat ) } . joined ( separator : exportFormat . newLineSeparator ( ) )
return players ( ) . map { $0 . pasteData ( exportFormat ) } . joined (
separator : exportFormat . newLineSeparator ( ) )
case . csv :
case . csv :
return players ( ) . map { [ $0 . pasteData ( exportFormat ) , isWildCard ( ) ? " WC " : $0 . computedRank . formatted ( ) ] . joined ( separator : exportFormat . separator ( ) ) } . joined ( separator : exportFormat . separator ( ) )
return players ( ) . map {
[ $0 . pasteData ( exportFormat ) , isWildCard ( ) ? " WC " : $0 . computedRank . formatted ( ) ]
. joined ( separator : exportFormat . separator ( ) )
} . joined ( separator : exportFormat . separator ( ) )
}
}
}
}
func updatePlayers ( _ players : Set < PlayerRegistration > , inTournamentCategory tournamentCategory : TournamentCategory ) {
func updatePlayers (
_ players : Set < PlayerRegistration > ,
inTournamentCategory tournamentCategory : TournamentCategory
) {
let previousPlayers = Set ( unsortedPlayers ( ) )
let previousPlayers = Set ( unsortedPlayers ( ) )
players . forEach { player in
players . forEach { player in
previousPlayers . forEach { oldPlayer in
previousPlayers . forEach { oldPlayer in
if player . licenceId ? . strippedLicense = = oldPlayer . licenceId ? . strippedLicense , player . licenceId ? . strippedLicense != nil {
if player . licenceId ? . strippedLicense = = oldPlayer . licenceId ? . strippedLicense ,
player . licenceId ? . strippedLicense != nil
{
player . registeredOnline = oldPlayer . registeredOnline
player . registeredOnline = oldPlayer . registeredOnline
player . coach = oldPlayer . coach
player . coach = oldPlayer . coach
player . tournamentPlayed = oldPlayer . tournamentPlayed
player . tournamentPlayed = oldPlayer . tournamentPlayed
@ -450,7 +497,6 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
}
}
}
}
let playersToRemove = previousPlayers . subtracting ( players )
let playersToRemove = previousPlayers . subtracting ( players )
self . tournamentStore ? . playerRegistrations . delete ( contentOfs : playersToRemove )
self . tournamentStore ? . playerRegistrations . delete ( contentOfs : playersToRemove )
setWeight ( from : Array ( players ) , inTournamentCategory : tournamentCategory )
setWeight ( from : Array ( players ) , inTournamentCategory : tournamentCategory )
@ -459,11 +505,11 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
player . teamRegistration = id
player . teamRegistration = id
}
}
// d o {
// d o {
// t r y s e l f . t o u r n a m e n t S t o r e . p l a y e r R e g i s t r a t i o n s . a d d O r U p d a t e ( c o n t e n t O f s : p l a y e r s )
// t r y s e l f . t o u r n a m e n t S t o r e . p l a y e r R e g i s t r a t i o n s . a d d O r U p d a t e ( c o n t e n t O f s : p l a y e r s )
// } c a t c h {
// } c a t c h {
// L o g g e r . e r r o r ( e r r o r )
// L o g g e r . e r r o r ( e r r o r )
// }
// }
}
}
typealias TeamRange = ( left : TeamRegistration ? , right : TeamRegistration ? )
typealias TeamRange = ( left : TeamRegistration ? , right : TeamRegistration ? )
@ -485,14 +531,18 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
if groupStagePosition = = 0 {
if groupStagePosition = = 0 {
left = tournamentObject . seeds ( ) . last
left = tournamentObject . seeds ( ) . last
} else {
} else {
let previousHat = selectedSortedTeams . filter ( { $0 . groupStagePosition = = groupStagePosition - 1 } ) . sorted ( by : \ . weight )
let previousHat = selectedSortedTeams . filter ( {
$0 . groupStagePosition = = groupStagePosition - 1
} ) . sorted ( by : \ . weight )
left = previousHat . last
left = previousHat . last
}
}
var right : TeamRegistration ? = nil
var right : TeamRegistration ? = nil
if groupStagePosition = = tournamentObject . teamsPerGroupStage - 1 {
if groupStagePosition = = tournamentObject . teamsPerGroupStage - 1 {
right = nil
right = nil
} else {
} else {
let previousHat = selectedSortedTeams . filter ( { $0 . groupStagePosition = = groupStagePosition + 1 } ) . sorted ( by : \ . weight )
let previousHat = selectedSortedTeams . filter ( {
$0 . groupStagePosition = = groupStagePosition + 1
} ) . sorted ( by : \ . weight )
right = previousHat . first
right = previousHat . first
}
}
return ( left : left , right : right )
return ( left : left , right : right )
@ -506,8 +556,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
let predicates : [ AreInIncreasingOrder ] = [
let predicates : [ AreInIncreasingOrder ] = [
{ $0 . sex ? . rawValue ? ? 0 < $1 . sex ? . rawValue ? ? 0 } ,
{ $0 . sex ? . rawValue ? ? 0 < $1 . sex ? . rawValue ? ? 0 } ,
{ $0 . rank ? ? Int . max < $1 . rank ? ? Int . max } ,
{ $0 . rank ? ? Int . max < $1 . rank ? ? Int . max } ,
{ $0 . lastName < $1 . lastName } ,
{ $0 . lastName < $1 . lastName } ,
{ $0 . firstName < $1 . firstName }
{ $0 . firstName < $1 . firstName } ,
]
]
for predicate in predicates {
for predicate in predicates {
@ -527,9 +577,16 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
return store . playerRegistrations . filter { $0 . coach }
return store . playerRegistrations . filter { $0 . coach }
}
}
func setWeight ( from players : [ PlayerRegistration ] , inTournamentCategory tournamentCategory : TournamentCategory ) {
func setWeight (
from players : [ PlayerRegistration ] ,
inTournamentCategory tournamentCategory : TournamentCategory
) {
let significantPlayerCount = significantPlayerCount ( )
let significantPlayerCount = significantPlayerCount ( )
weight = ( players . prefix ( significantPlayerCount ) . map { $0 . computedRank } + missingPlayerType ( inTournamentCategory : tournamentCategory ) . map { unrankValue ( for : $0 = = 1 ? true : false ) } ) . prefix ( significantPlayerCount ) . reduce ( 0 , + )
weight =
( players . prefix ( significantPlayerCount ) . map { $0 . computedRank }
+ missingPlayerType ( inTournamentCategory : tournamentCategory ) . map {
unrankValue ( for : $0 = = 1 ? true : false )
} ) . prefix ( significantPlayerCount ) . reduce ( 0 , + )
}
}
func significantPlayerCount ( ) -> Int {
func significantPlayerCount ( ) -> Int {
@ -567,12 +624,13 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func initialMatch ( ) -> Match ? {
func initialMatch ( ) -> Match ? {
guard let bracketPosition else { return nil }
guard let bracketPosition else { return nil }
guard let initialRoundObject = initialRound ( ) else { return nil }
guard let initialRoundObject = initialRound ( ) else { return nil }
return self . tournamentStore ? . matches . first ( where : { $0 . round = = initialRoundObject . id && $0 . index = = bracketPosition / 2 } )
return self . tournamentStore ? . matches . first ( where : {
$0 . round = = initialRoundObject . id && $0 . index = = bracketPosition / 2
} )
}
}
func toggleSummonConfirmation ( ) {
func toggleSummonConfirmation ( ) {
if confirmationDate = = nil { confirmationDate = Date ( ) }
if confirmationDate = = nil { confirmationDate = Date ( ) } else { confirmationDate = nil }
else { confirmationDate = nil }
}
}
func didConfirmSummon ( ) -> Bool {
func didConfirmSummon ( ) -> Bool {
@ -595,7 +653,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func wildcardLabel ( ) -> String ? {
func wildcardLabel ( ) -> String ? {
if isWildCard ( ) {
if isWildCard ( ) {
let wildcardLabel : String = [ " wildcard " , ( wildCardBracket ? " tableau " : " poule " ) ] . joined ( separator : " " )
let wildcardLabel : String = [ " wildcard " , ( wildCardBracket ? " tableau " : " poule " ) ]
. joined ( separator : " " )
return wildcardLabel
return wildcardLabel
} else {
} else {
return nil
return nil
@ -606,7 +665,9 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func restingTime ( ) -> Date ? {
func restingTime ( ) -> Date ? {
if let _cachedRestingTime { return _cachedRestingTime . 1 }
if let _cachedRestingTime { return _cachedRestingTime . 1 }
let restingTime = matches ( ) . filter ( { $0 . hasEnded ( ) } ) . sorted ( by : \ . computedEndDateForSorting ) . last ? . endDate
let restingTime = matches ( ) . filter ( { $0 . hasEnded ( ) } ) . sorted (
by : \ . computedEndDateForSorting
) . last ? . endDate
_cachedRestingTime = ( true , restingTime )
_cachedRestingTime = ( true , restingTime )
return restingTime
return restingTime
}
}
@ -630,14 +691,18 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable {
func isDifferentPosition ( _ drawMatchIndex : Int ? ) -> Bool {
func isDifferentPosition ( _ drawMatchIndex : Int ? ) -> Bool {
if let bracketPosition , let drawMatchIndex {
if let bracketPosition , let drawMatchIndex {
return drawMatchIndex != bracketPosition
return drawMatchIndex != bracketPosition
} else if let bracketPosition {
} else if bracketPosition != nil {
return true
return true
} else if let drawMatchIndex {
} else if drawMatchIndex != nil {
return true
return true
}
}
return false
return false
}
}
func shouldDisplayRankAndWeight ( ) -> Bool {
unsortedPlayers ( ) . count > 0
}
func insertOnServer ( ) {
func insertOnServer ( ) {
self . tournamentStore ? . teamRegistrations . writeChangeAndInsertOnServer ( instance : self )
self . tournamentStore ? . teamRegistrations . writeChangeAndInsertOnServer ( instance : self )
for playerRegistration in self . unsortedPlayers ( ) {
for playerRegistration in self . unsortedPlayers ( ) {