@ -77,31 +77,31 @@ final class Tournament: BaseTournament {
try super . init ( from : decoder )
try super . init ( from : decoder )
}
}
var tournamentStore : TournamentStore {
var tournamentStore : TournamentStore ? {
return TournamentLibrary . shared . store ( tournamentId : self . id )
return TournamentLibrary . shared . store ( tournamentId : self . id )
}
}
override func deleteDependencies ( ) {
override func deleteDependencies ( ) {
let store = self . tournamentStore
guard let store = self . tournamentStore else { return }
let drawLogs = Array ( self . tournamentS tore. drawLogs )
let drawLogs = Array ( s tore. drawLogs )
for drawLog in drawLogs {
for drawLog in drawLogs {
drawLog . deleteDependencies ( )
drawLog . deleteDependencies ( )
}
}
store . drawLogs . deleteDependencies ( drawLogs )
store . drawLogs . deleteDependencies ( drawLogs )
let teams = self . tournamentS tore. teamRegistrations
let teams = s tore. teamRegistrations
for team in Array ( teams ) {
for team in Array ( teams ) {
team . deleteDependencies ( )
team . deleteDependencies ( )
}
}
store . teamRegistrations . deleteDependencies ( teams )
store . teamRegistrations . deleteDependencies ( teams )
let groups = Array ( self . tournamentS tore. groupStages )
let groups = Array ( s tore. groupStages )
for group in groups {
for group in groups {
group . deleteDependencies ( )
group . deleteDependencies ( )
}
}
store . groupStages . deleteDependencies ( groups )
store . groupStages . deleteDependencies ( groups )
let rounds = Array ( self . tournamentS tore. rounds )
let rounds = Array ( s tore. rounds )
for round in rounds {
for round in rounds {
round . deleteDependencies ( )
round . deleteDependencies ( )
}
}
@ -120,20 +120,24 @@ final class Tournament: BaseTournament {
// MARK: - C o m p u t e d D e p e n d e n c i e s
// MARK: - C o m p u t e d D e p e n d e n c i e s
func unsortedTeams ( ) -> [ TeamRegistration ] {
func unsortedTeams ( ) -> [ TeamRegistration ] {
return Array ( self . tournamentStore . teamRegistrations )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return Array ( tournamentStore . teamRegistrations )
}
}
func groupStages ( atStep step : Int = 0 ) -> [ GroupStage ] {
func groupStages ( atStep step : Int = 0 ) -> [ GroupStage ] {
let groupStages : [ GroupStage ] = self . tournamentStore . groupStages . filter { $0 . tournament = = self . id && $0 . step = = step }
guard let tournamentStore = self . tournamentStore else { return [ ] }
let groupStages : [ GroupStage ] = tournamentStore . groupStages . filter { $0 . tournament = = self . id && $0 . step = = step }
return groupStages . sorted ( by : \ . index )
return groupStages . sorted ( by : \ . index )
}
}
func allGroupStages ( ) -> [ GroupStage ] {
func allGroupStages ( ) -> [ GroupStage ] {
return self . tournamentStore . groupStages . sorted ( by : \ GroupStage . computedOrder )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . groupStages . sorted ( by : \ GroupStage . computedOrder )
}
}
func allRounds ( ) -> [ Round ] {
func allRounds ( ) -> [ Round ] {
return Array ( self . tournamentStore . rounds )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return Array ( tournamentStore . rounds )
}
}
// MARK: -
// MARK: -
@ -354,7 +358,7 @@ defer {
}
}
func getRound ( atRoundIndex roundIndex : Int ) -> Round ? {
func getRound ( atRoundIndex roundIndex : Int ) -> Round ? {
return self . tournamentStore . rounds . first ( where : { $0 . index = = roundIndex } )
return self . tournamentStore ? . rounds . first ( where : { $0 . index = = roundIndex } )
// 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 = = i d & & $ 0 . i n d e x = = r o u n d I n d e x } ) . f i r s t
// 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 = = i d & & $ 0 . i n d e x = = r o u n d I n d e x } ) . f i r s t
}
}
@ -546,15 +550,18 @@ defer {
}
}
func allMatches ( ) -> [ Match ] {
func allMatches ( ) -> [ Match ] {
return self . tournamentStore . matches . filter { $0 . disabled = = false }
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . matches . filter { $0 . disabled = = false }
}
}
func _allMatchesIncludingDisabled ( ) -> [ Match ] {
func _allMatchesIncludingDisabled ( ) -> [ Match ] {
return Array ( self . tournamentStore . matches )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return Array ( tournamentStore . matches )
}
}
func rounds ( ) -> [ Round ] {
func rounds ( ) -> [ Round ] {
let rounds : [ Round ] = self . tournamentStore . rounds . filter { $0 . isUpperBracket ( ) }
guard let tournamentStore = self . tournamentStore else { return [ ] }
let rounds : [ Round ] = tournamentStore . rounds . filter { $0 . isUpperBracket ( ) }
return rounds . sorted ( by : \ . index ) . reversed ( )
return rounds . sorted ( by : \ . index ) . reversed ( )
}
}
@ -677,12 +684,14 @@ defer {
}
}
func unsortedTeamsWithoutWO ( ) -> [ TeamRegistration ] {
func unsortedTeamsWithoutWO ( ) -> [ TeamRegistration ] {
return self . tournamentStore . teamRegistrations . filter { $0 . walkOut = = false }
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . teamRegistrations . filter { $0 . walkOut = = false }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . t o u r n a m e n t = = s e l f . i d & & $ 0 . w a l k O u t = = f a l s e }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . t o u r n a m e n t = = s e l f . i d & & $ 0 . w a l k O u t = = f a l s e }
}
}
func walkoutTeams ( ) -> [ TeamRegistration ] {
func walkoutTeams ( ) -> [ TeamRegistration ] {
return self . tournamentStore . teamRegistrations . filter { $0 . walkOut = = true }
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . teamRegistrations . filter { $0 . walkOut = = true }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . t o u r n a m e n t = = s e l f . i d & & $ 0 . w a l k O u t = = t r u e }
// r e t u r n S t o r e . m a i n . f i l t e r { $ 0 . t o u r n a m e n t = = s e l f . i d & & $ 0 . w a l k O u t = = t r u e }
}
}
@ -702,7 +711,8 @@ defer {
}
}
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
func unsortedPlayers ( ) -> [ PlayerRegistration ] {
return Array ( self . tournamentStore . playerRegistrations )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return Array ( tournamentStore . playerRegistrations )
}
}
func selectedPlayers ( ) -> [ PlayerRegistration ] {
func selectedPlayers ( ) -> [ PlayerRegistration ] {
@ -718,7 +728,8 @@ defer {
}
}
func players ( ) -> [ PlayerRegistration ] {
func players ( ) -> [ PlayerRegistration ] {
return self . tournamentStore . playerRegistrations . sorted ( by : \ . computedRank )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . playerRegistrations . sorted ( by : \ . computedRank )
}
}
func unrankValue ( for malePlayer : Bool ) -> Int ? {
func unrankValue ( for malePlayer : Bool ) -> Int ? {
@ -838,8 +849,10 @@ defer {
}
}
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teamsToImport )
if let tournamentStore = self . tournamentStore {
self . tournamentStore . playerRegistrations . addOrUpdate ( contentOfs : teams . flatMap { $0 . players } )
tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teamsToImport )
tournamentStore . playerRegistrations . addOrUpdate ( contentOfs : teams . flatMap { $0 . players } )
}
if state ( ) = = . build && groupStageCount > 0 && groupStageTeams ( ) . isEmpty {
if state ( ) = = . build && groupStageCount > 0 && groupStageTeams ( ) . isEmpty {
setGroupStage ( randomize : groupStageSortMode = = . random )
setGroupStage ( randomize : groupStageSortMode = = . random )
@ -1020,7 +1033,7 @@ defer {
_removeStrings ( from : & teams , stringsToRemove : disabledIds )
_removeStrings ( from : & teams , stringsToRemove : disabledIds )
teams [ interval . last ] = disabledIds
teams [ interval . last ] = disabledIds
let teamNames : [ String ] = disabledIds . compactMap {
let teamNames : [ String ] = disabledIds . compactMap {
let t : TeamRegistration ? = tournamentStore . teamRegistrations . findById ( $0 )
let t : TeamRegistration ? = self . tournamentStore ? . teamRegistrations . findById ( $0 )
return t
return t
} . map { $0 . canonicalName }
} . map { $0 . canonicalName }
print ( " winners.isEmpty " , " \( interval . last ) : " , teamNames )
print ( " winners.isEmpty " , " \( interval . last ) : " , teamNames )
@ -1033,7 +1046,7 @@ defer {
_removeStrings ( from : & teams , stringsToRemove : winners )
_removeStrings ( from : & teams , stringsToRemove : winners )
teams [ interval . first + winners . count - 1 ] = winners
teams [ interval . first + winners . count - 1 ] = winners
let teamNames : [ String ] = winners . compactMap {
let teamNames : [ String ] = winners . compactMap {
let t : TeamRegistration ? = tournamentStore . teamRegistrations . findById ( $0 )
let t : TeamRegistration ? = tournamentStore ? . teamRegistrations . findById ( $0 )
return t
return t
} . map { $0 . canonicalName }
} . map { $0 . canonicalName }
print ( " winners " , " \( interval . last + winners . count - 1 ) : " , teamNames )
print ( " winners " , " \( interval . last + winners . count - 1 ) : " , teamNames )
@ -1044,7 +1057,7 @@ defer {
_removeStrings ( from : & teams , stringsToRemove : losers )
_removeStrings ( from : & teams , stringsToRemove : losers )
teams [ interval . first + winners . count ] = losers
teams [ interval . first + winners . count ] = losers
let loserTeamNames : [ String ] = losers . compactMap {
let loserTeamNames : [ String ] = losers . compactMap {
let t : TeamRegistration ? = tournamentStore . teamRegistrations . findById ( $0 )
let t : TeamRegistration ? = tournamentStore ? . teamRegistrations . findById ( $0 )
return t
return t
} . map { $0 . canonicalName }
} . map { $0 . canonicalName }
print ( " losers " , " \( interval . first + winners . count ) : " , loserTeamNames )
print ( " losers " , " \( interval . first + winners . count ) : " , loserTeamNames )
@ -1088,11 +1101,13 @@ defer {
}
}
func setRankings ( finalRanks : [ Int : [ String ] ] ) async -> [ Int : [ TeamRegistration ] ] {
func setRankings ( finalRanks : [ Int : [ String ] ] ) async -> [ Int : [ TeamRegistration ] ] {
guard let tournamentStore = self . tournamentStore else { return [ : ] }
var rankings : [ Int : [ TeamRegistration ] ] = [ : ]
var rankings : [ Int : [ TeamRegistration ] ] = [ : ]
finalRanks . keys . sorted ( ) . forEach { rank in
finalRanks . keys . sorted ( ) . forEach { rank in
if let rankedTeamIds = finalRanks [ rank ] {
if let rankedTeamIds = finalRanks [ rank ] {
let teams : [ TeamRegistration ] = rankedTeamIds . compactMap { self . tournamentStore . teamRegistrations . findById ( $0 ) }
let teams : [ TeamRegistration ] = rankedTeamIds . compactMap { tournamentStore . teamRegistrations . findById ( $0 ) }
rankings [ rank ] = teams
rankings [ rank ] = teams
}
}
}
}
@ -1106,7 +1121,7 @@ defer {
}
}
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
return rankings
return rankings
}
}
@ -1121,7 +1136,7 @@ defer {
teams . forEach { team in
teams . forEach { team in
team . lockedWeight = team . weight
team . lockedWeight = team . weight
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teams )
self . tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : teams )
}
}
func unlockRegistration ( ) {
func unlockRegistration ( ) {
@ -1130,7 +1145,7 @@ defer {
teams . forEach { team in
teams . forEach { team in
team . lockedWeight = nil
team . lockedWeight = nil
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teams )
self . tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : teams )
}
}
func updateWeights ( ) {
func updateWeights ( ) {
@ -1139,9 +1154,9 @@ defer {
let players = team . unsortedPlayers ( )
let players = team . unsortedPlayers ( )
players . forEach { $0 . setComputedRank ( in : self ) }
players . forEach { $0 . setComputedRank ( in : self ) }
team . setWeight ( from : players , inTournamentCategory : tournamentCategory )
team . setWeight ( from : players , inTournamentCategory : tournamentCategory )
self . tournamentStore . playerRegistrations . addOrUpdate ( contentOfs : players )
self . tournamentStore ? . playerRegistrations . addOrUpdate ( contentOfs : players )
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teams )
self . tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : teams )
}
}
func updateRank ( to newDate : Date ? ) async throws {
func updateRank ( to newDate : Date ? ) async throws {
@ -1472,27 +1487,30 @@ defer {
}
}
do {
do {
try self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : wcs )
try self . tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : wcs )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
}
}
func addEmptyTeamRegistration ( _ count : Int ) {
func addEmptyTeamRegistration ( _ count : Int ) {
guard let tournamentStore = self . tournamentStore else { return }
let teams = ( 0. . < count ) . map { _ in
let teams = ( 0. . < count ) . map { _ in
let team = TeamRegistration ( tournament : id )
let team = TeamRegistration ( tournament : id )
return team
return team
}
}
do {
do {
try self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teams )
try tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : teams )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
}
}
func buildGroupStages ( ) {
func buildGroupStages ( ) {
guard groupStages ( ) . isEmpty else {
guard groupStages ( ) . isEmpty , let tournamentStore = self . tournamentStore else {
return
return
}
}
@ -1502,7 +1520,7 @@ defer {
_groupStages . append ( groupStage )
_groupStages . append ( groupStage )
}
}
self . tournamentStore . groupStages . addOrUpdate ( contentOfs : _groupStages )
tournamentStore . groupStages . addOrUpdate ( contentOfs : _groupStages )
refreshGroupStages ( )
refreshGroupStages ( )
}
}
@ -1524,7 +1542,7 @@ defer {
}
}
do {
do {
try self . tournamentStore . rounds . addOrUpdate ( contentOfs : rounds )
try self . tournamentStore ? . rounds . addOrUpdate ( contentOfs : rounds )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
@ -1540,7 +1558,7 @@ defer {
( RoundRule . roundName ( fromMatchIndex : $0 . index ) , RoundRule . matchIndexWithinRound ( fromMatchIndex : $0 . index ) )
( RoundRule . roundName ( fromMatchIndex : $0 . index ) , RoundRule . matchIndexWithinRound ( fromMatchIndex : $0 . index ) )
} )
} )
self . tournamentStore . matches . addOrUpdate ( contentOfs : matches )
self . tournamentStore ? . matches . addOrUpdate ( contentOfs : matches )
rounds . forEach { round in
rounds . forEach { round in
round . buildLoserBracket ( )
round . buildLoserBracket ( )
@ -1552,7 +1570,7 @@ defer {
let matchIndex = bracketPosition / 2
let matchIndex = bracketPosition / 2
let roundIndex = RoundRule . roundIndex ( fromMatchIndex : matchIndex )
let roundIndex = RoundRule . roundIndex ( fromMatchIndex : matchIndex )
if let round : Round = self . getRound ( atRoundIndex : roundIndex ) {
if let round : Round = self . getRound ( atRoundIndex : roundIndex ) {
return self . tournamentStore . matches . first ( where : { $0 . round = = round . id && $0 . index = = matchIndex } )
return self . tournamentStore ? . matches . first ( where : { $0 . round = = round . id && $0 . index = = 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 . r o u n d = = r o u n d . i d & & $ 0 . i n d e x = = m a t c h I n d e x } ) . f i r s t
// 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 . r o u n d = = r o u n d . i d & & $ 0 . i n d e x = = m a t c h I n d e x } ) . f i r s t
}
}
@ -1570,7 +1588,7 @@ defer {
}
}
func deleteStructure ( ) {
func deleteStructure ( ) {
self . tournamentStore . rounds . delete ( contentOfs : rounds ( ) )
self . tournamentStore ? . rounds . delete ( contentOfs : rounds ( ) )
}
}
func resetBracketPosition ( ) {
func resetBracketPosition ( ) {
@ -1578,7 +1596,7 @@ defer {
}
}
func deleteGroupStages ( ) {
func deleteGroupStages ( ) {
self . tournamentStore . groupStages . delete ( contentOfs : allGroupStages ( ) )
self . tournamentStore ? . groupStages . delete ( contentOfs : allGroupStages ( ) )
}
}
func refreshGroupStages ( keepExistingMatches : Bool = false ) {
func refreshGroupStages ( keepExistingMatches : Bool = false ) {
@ -1631,7 +1649,7 @@ defer {
}
}
}
}
self . tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
self . tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
}
}
func isFree ( ) -> Bool {
func isFree ( ) -> Bool {
@ -1792,7 +1810,8 @@ defer {
private let _currentSelectionSorting : [ MySortDescriptor < TeamRegistration > ] = [ . keyPath ( \ . weight ) , . keyPath ( \ . id ) ]
private let _currentSelectionSorting : [ MySortDescriptor < TeamRegistration > ] = [ . keyPath ( \ . weight ) , . keyPath ( \ . id ) ]
private func _matchSchedulers ( ) -> [ MatchScheduler ] {
private func _matchSchedulers ( ) -> [ MatchScheduler ] {
return self . tournamentStore . matchSchedulers . filter { $0 . tournament = = self . id }
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . 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 } )
// 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 } )
}
}
@ -1827,7 +1846,7 @@ defer {
}
}
func tournamentWinner ( ) -> TeamRegistration ? {
func tournamentWinner ( ) -> TeamRegistration ? {
let finals : Round ? = self . tournamentStore . rounds . first ( where : { $0 . index = = 0 && $0 . isUpperBracket ( ) } )
let finals : Round ? = self . tournamentStore ? . rounds . first ( where : { $0 . index = = 0 && $0 . isUpperBracket ( ) } )
return finals ? . playedMatches ( ) . first ? . winner ( )
return finals ? . playedMatches ( ) . first ? . winner ( )
}
}
@ -1890,7 +1909,7 @@ defer {
}
}
func groupStageLoserBracket ( ) -> Round ? {
func groupStageLoserBracket ( ) -> Round ? {
tournamentStore . rounds . first ( where : { $0 . groupStageLoserBracket } )
self . tournamentStore ? . rounds . first ( where : { $0 . groupStageLoserBracket } )
}
}
func groupStageLoserBracketsInitialPlace ( ) -> Int {
func groupStageLoserBracketsInitialPlace ( ) -> Int {
@ -1901,14 +1920,14 @@ defer {
let lastStep = lastStep ( ) + 1
let lastStep = lastStep ( ) + 1
for i in 0. . < teamsPerGroupStage {
for i in 0. . < teamsPerGroupStage {
let gs = GroupStage ( tournament : id , index : i , size : groupStageCount , step : lastStep )
let gs = GroupStage ( tournament : id , index : i , size : groupStageCount , step : lastStep )
tournamentStore . groupStages . addOrUpdate ( instance : gs )
self . tournamentStore ? . groupStages . addOrUpdate ( instance : gs )
}
}
groupStages ( atStep : 1 ) . forEach { $0 . buildMatches ( ) }
groupStages ( atStep : 1 ) . forEach { $0 . buildMatches ( ) }
}
}
func lastStep ( ) -> Int {
func lastStep ( ) -> Int {
self . tournamentStore . groupStages . sorted ( by : \ . step ) . last ? . step ? ? 0
self . tournamentStore ? . groupStages . sorted ( by : \ . step ) . last ? . step ? ? 0
}
}
func generateSmartLoserGroupStageBracket ( ) {
func generateSmartLoserGroupStageBracket ( ) {
@ -1919,7 +1938,7 @@ defer {
let match = Match ( round : groupStageLoserBracket . id , index : placeCount , format : groupStageLoserBracket . matchFormat )
let match = Match ( round : groupStageLoserBracket . id , index : placeCount , format : groupStageLoserBracket . matchFormat )
match . setMatchName ( " \( placeCount ) \( placeCount . ordinalFormattedSuffix ( feminine : true ) ) place " )
match . setMatchName ( " \( placeCount ) \( placeCount . ordinalFormattedSuffix ( feminine : true ) ) place " )
do {
do {
try tournamentStore . matches . addOrUpdate ( instance : match )
try tournamentStore ? . matches . addOrUpdate ( instance : match )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
@ -1958,7 +1977,8 @@ defer {
}
}
func drawLogs ( ) -> [ DrawLog ] {
func drawLogs ( ) -> [ DrawLog ] {
self . tournamentStore . drawLogs . sorted ( by : \ . drawDate )
guard let tournamentStore = self . tournamentStore else { return [ ] }
return tournamentStore . drawLogs . sorted ( by : \ . drawDate )
}
}
func seedSpotsLeft ( ) -> Bool {
func seedSpotsLeft ( ) -> Bool {
@ -1989,7 +2009,7 @@ defer {
}
}
do {
do {
try tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : seeds )
try tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : seeds )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
@ -2004,12 +2024,12 @@ defer {
}
}
do {
do {
try tournamentStore . teamScores . delete ( contentOfs : ts )
try tournamentStore ? . teamScores . delete ( contentOfs : ts )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
do {
do {
try tournamentStore . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
try tournamentStore ? . teamRegistrations . addOrUpdate ( contentOfs : unsortedTeams ( ) )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
@ -2028,7 +2048,7 @@ defer {
let matches = ( 0. . < matchCount ) . map { index in // 0 i s f i n a l m a t c h
let matches = ( 0. . < matchCount ) . map { index in // 0 i s f i n a l m a t c h
let computedIndex = index + matchStartIndex
let computedIndex = index + matchStartIndex
let match = Match ( round : round . id , index : computedIndex , format : round . matchFormat )
let match = Match ( round : round . id , index : computedIndex , format : round . matchFormat )
if let nextRound , let followingMatch = self . tournamentStore . matches . first ( where : { $0 . round = = nextRound . id && $0 . index = = ( computedIndex - 1 ) / 2 } ) {
if let nextRound , let followingMatch = self . tournamentStore ? . matches . first ( where : { $0 . round = = nextRound . id && $0 . index = = ( computedIndex - 1 ) / 2 } ) {
if followingMatch . disabled {
if followingMatch . disabled {
match . disabled = true
match . disabled = true
} else if computedIndex % 2 = = 1 && followingMatch . team ( . one ) != nil {
} else if computedIndex % 2 = = 1 && followingMatch . team ( . one ) != nil {
@ -2049,12 +2069,12 @@ defer {
return match
return match
}
}
do {
do {
try tournamentStore . rounds . addOrUpdate ( instance : round )
try tournamentStore ? . rounds . addOrUpdate ( instance : round )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
do {
do {
try tournamentStore . matches . addOrUpdate ( contentOfs : matches )
try tournamentStore ? . matches . addOrUpdate ( contentOfs : matches )
} catch {
} catch {
Logger . error ( error )
Logger . error ( error )
}
}
@ -2085,17 +2105,17 @@ defer {
func insertOnServer ( ) throws {
func insertOnServer ( ) throws {
DataStore . shared . tournaments . writeChangeAndInsertOnServer ( instance : self )
// D a t a S t o r e . s h a r e d . t o u r n a m e n t s . w r i t e C h a n g e A n d I n s e r t O n S e r v e r ( i n s t a n c e : s e l f )
//
for teamRegistration in self . tournamentStore . teamRegistrations {
// f o r t e a m R e g i s t r a t i o n i n s e l f . t o u r n a m e n t S t o r e ? . t e a m R e g i s t r a t i o n s {
teamRegistration . insertOnServer ( )
// t e a m R e g i s t r a t i o n . i n s e r t O n S e r v e r ( )
}
// }
for groupStage in self . tournamentStore . groupStages {
// f o r g r o u p S t a g e i n s e l f . t o u r n a m e n t S t o r e ? . g r o u p S t a g e s {
groupStage . insertOnServer ( )
// g r o u p S t a g e . i n s e r t O n S e r v e r ( )
}
// }
for round in self . tournamentStore . rounds {
// f o r r o u n d i n s e l f . t o u r n a m e n t S t o r e . r o u n d s {
round . insertOnServer ( )
// r o u n d . i n s e r t O n S e r v e r ( )
}
// }
}
}