@@ -28,20 +28,39 @@ const createTBDMatches = (count: number) => Array.from({ length: count }, () =>
2828 winner : ""
2929} ) ) ;
3030
31- const DEFAULT_ROUNDS : Round [ ] = [
32- { name : "Rodada 1" , matches : createTBDMatches ( 8 ) , toBeDetermined : true } ,
33- { name : "Rodada 2 (1-0)" , matches : createTBDMatches ( 4 ) , toBeDetermined : true } ,
34- { name : "Rodada 2 (0-1)" , matches : createTBDMatches ( 4 ) , toBeDetermined : true } ,
35- { name : "Rodada 3 (2-0)" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ,
36- { name : "Rodada 3 (1-1)" , matches : createTBDMatches ( 4 ) , toBeDetermined : true } ,
37- { name : "Rodada 3 (0-2)" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ,
38- { name : "Rodada 4 (2-1)" , matches : createTBDMatches ( 3 ) , toBeDetermined : true } ,
39- { name : "Rodada 4 (1-2)" , matches : createTBDMatches ( 3 ) , toBeDetermined : true } ,
40- { name : "Rodada 5 (2-2)" , matches : createTBDMatches ( 3 ) , toBeDetermined : true } ,
41- { name : "Quartas de Final" , matches : createTBDMatches ( 4 ) , toBeDetermined : true } ,
42- { name : "Semifinal" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ,
43- { name : "Final" , matches : createTBDMatches ( 1 ) , toBeDetermined : true } ,
44- ] ;
31+ const generateStructure = ( count : number ) : Round [ ] => {
32+ const rounds : Round [ ] = [ ] ;
33+ const matchesPerRound = Math . ceil ( count / 2 ) ;
34+
35+ // Dynamic Swiss Rounds Count
36+ // 7-10 teams: 4 Rounds
37+ // 11+ teams: 5 Rounds
38+ const swissRoundsCount = count <= 10 ? 4 : 5 ;
39+
40+ for ( let i = 1 ; i <= swissRoundsCount ; i ++ ) {
41+ rounds . push ( {
42+ name : `Rodada ${ i } ` ,
43+ matches : createTBDMatches ( matchesPerRound ) ,
44+ toBeDetermined : true
45+ } ) ;
46+ }
47+
48+ // Dynamic Playoffs
49+ if ( count <= 9 ) {
50+ // Top 6 Format (Byes for Top 2)
51+ // Quartas: 3rd vs 6th, 4th vs 5th (2 matches)
52+ rounds . push ( { name : "Quartas de Final" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ) ;
53+ rounds . push ( { name : "Semifinal" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ) ;
54+ rounds . push ( { name : "Final" , matches : createTBDMatches ( 1 ) , toBeDetermined : true } ) ;
55+ } else {
56+ // Standard Top 8 Format
57+ rounds . push ( { name : "Quartas de Final" , matches : createTBDMatches ( 4 ) , toBeDetermined : true } ) ;
58+ rounds . push ( { name : "Semifinal" , matches : createTBDMatches ( 2 ) , toBeDetermined : true } ) ;
59+ rounds . push ( { name : "Final" , matches : createTBDMatches ( 1 ) , toBeDetermined : true } ) ;
60+ }
61+
62+ return rounds ;
63+ }
4564
4665export const fetchTournamentData = async ( ) => {
4766 const [ teamsData , matchesData ] = await Promise . all ( [
@@ -111,21 +130,36 @@ export const fetchTournamentData = async () => {
111130 roundsMap . get ( row . Fase ) ?. matches . push ( match ) ;
112131 } ) ;
113132
133+ const generatedStructure = generateStructure ( teams . length ) ;
114134 const mergedRounds : Round [ ] = [ ] ;
115- const processedNames = new Set < string > ( ) ;
135+ const processedFaseNames = new Set < string > ( ) ;
116136
117- DEFAULT_ROUNDS . forEach ( defaultRound => {
118- const name = defaultRound . name || "" ;
119- if ( roundsMap . has ( name ) ) {
120- mergedRounds . push ( roundsMap . get ( name ) ! ) ;
137+ generatedStructure . forEach ( genRound => {
138+ const stageName = genRound . name || "" ;
139+
140+ // Find all rounds in roundsMap that belong to this stage
141+ const matchingRounds : Round [ ] = [ ] ;
142+ roundsMap . forEach ( ( round , key ) => {
143+ let keyStage = key ;
144+ if ( keyStage . startsWith ( "Rodada" ) ) {
145+ keyStage = keyStage . split ( " " ) . slice ( 0 , 2 ) . join ( " " ) ;
146+ }
147+ if ( keyStage === stageName || key === stageName ) {
148+ matchingRounds . push ( round ) ;
149+ processedFaseNames . add ( key ) ;
150+ }
151+ } ) ;
152+
153+ if ( matchingRounds . length > 0 ) {
154+ mergedRounds . push ( ...matchingRounds ) ;
121155 } else {
122- mergedRounds . push ( defaultRound ) ;
156+ mergedRounds . push ( genRound ) ;
123157 }
124- processedNames . add ( name ) ;
125158 } ) ;
126159
127- roundsMap . forEach ( ( round , name ) => {
128- if ( ! processedNames . has ( name ) ) {
160+ // Add any remaining rounds from data that weren't in the structure
161+ roundsMap . forEach ( ( round , key ) => {
162+ if ( ! processedFaseNames . has ( key ) ) {
129163 mergedRounds . push ( round ) ;
130164 }
131165 } ) ;
0 commit comments