some changes
This commit is contained in:
@@ -19,18 +19,15 @@ export const generateTournamentBracket = createServerFn()
|
||||
toServerResult(async () => {
|
||||
logger.info('Generating tournament bracket', { tournamentId, teamCount: orderedTeamIds.length });
|
||||
|
||||
// Get tournament with teams
|
||||
const tournament = await pbAdmin.getTournament(tournamentId);
|
||||
if (!tournament) {
|
||||
throw new Error('Tournament not found');
|
||||
}
|
||||
|
||||
// Check if tournament already has matches
|
||||
if (tournament.matches && tournament.matches.length > 0) {
|
||||
throw new Error('Tournament already has matches generated');
|
||||
}
|
||||
|
||||
// Get bracket template based on team count
|
||||
const teamCount = orderedTeamIds.length;
|
||||
if (!Object.keys(brackets).includes(teamCount.toString())) {
|
||||
throw new Error(`Bracket not available for ${teamCount} teams`);
|
||||
@@ -38,16 +35,13 @@ export const generateTournamentBracket = createServerFn()
|
||||
|
||||
const bracketTemplate = brackets[teamCount as keyof typeof brackets] as any;
|
||||
|
||||
// Create seed to team mapping (index + 1 = seed)
|
||||
const seedToTeamId = new Map<number, string>();
|
||||
orderedTeamIds.forEach((teamId, index) => {
|
||||
seedToTeamId.set(index + 1, teamId);
|
||||
});
|
||||
|
||||
// Convert bracket template to match records
|
||||
const matchInputs: MatchInput[] = [];
|
||||
|
||||
// Process winners bracket
|
||||
bracketTemplate.winners.forEach((round: any[]) => {
|
||||
round.forEach((match: any) => {
|
||||
const matchInput: MatchInput = {
|
||||
@@ -67,7 +61,6 @@ export const generateTournamentBracket = createServerFn()
|
||||
tournament: tournamentId,
|
||||
};
|
||||
|
||||
// Assign teams based on seeds
|
||||
if (match.home_seed) {
|
||||
const teamId = seedToTeamId.get(match.home_seed);
|
||||
if (teamId) {
|
||||
@@ -88,7 +81,6 @@ export const generateTournamentBracket = createServerFn()
|
||||
});
|
||||
});
|
||||
|
||||
// Process losers bracket
|
||||
bracketTemplate.losers.forEach((round: any[]) => {
|
||||
round.forEach((match: any) => {
|
||||
const matchInput: MatchInput = {
|
||||
@@ -108,17 +100,12 @@ export const generateTournamentBracket = createServerFn()
|
||||
tournament: tournamentId,
|
||||
};
|
||||
|
||||
// Losers bracket matches don't start with teams
|
||||
// Teams come from winners bracket losses
|
||||
|
||||
matchInputs.push(matchInput);
|
||||
});
|
||||
});
|
||||
|
||||
// Create all matches
|
||||
const createdMatches = await pbAdmin.createMatches(matchInputs);
|
||||
|
||||
// Update tournament to include all match IDs in the matches relation
|
||||
const matchIds = createdMatches.map(match => match.id);
|
||||
await pbAdmin.updateTournamentMatches(tournamentId, matchIds);
|
||||
|
||||
@@ -133,4 +120,22 @@ export const generateTournamentBracket = createServerFn()
|
||||
matches: createdMatches,
|
||||
};
|
||||
})
|
||||
);
|
||||
);
|
||||
|
||||
export const startMatch = createServerFn()
|
||||
.validator(z.string())
|
||||
.middleware([superTokensAdminFunctionMiddleware])
|
||||
.handler(async ({ data }) =>
|
||||
toServerResult(async () => {
|
||||
logger.info('Starting match', data);
|
||||
|
||||
let match = await pbAdmin.getMatch(data);
|
||||
if (!match) {
|
||||
throw new Error('Match not found');
|
||||
}
|
||||
|
||||
match = await pbAdmin.updateMatch(data, {
|
||||
start_time: new Date().toISOString()
|
||||
})
|
||||
}
|
||||
));
|
||||
Reference in New Issue
Block a user