50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
export interface GroupConfig {
|
|
num_groups: number;
|
|
advance_per_group: number;
|
|
}
|
|
|
|
export function getGroupLabel(
|
|
seed: number | undefined,
|
|
groupConfig: GroupConfig | undefined
|
|
): string | undefined {
|
|
if (!seed || !groupConfig) return undefined;
|
|
|
|
const groupNames = ["A", "B", "C", "D", "E", "F", "G", "H"];
|
|
const numGroups = groupConfig.num_groups;
|
|
const advancePerGroup = groupConfig.advance_per_group;
|
|
|
|
const totalQualifiedTeams = numGroups * advancePerGroup;
|
|
const nextPowerOf2 = Math.pow(2, Math.ceil(Math.log2(totalQualifiedTeams)));
|
|
const wildcardsNeeded = nextPowerOf2 - totalQualifiedTeams;
|
|
|
|
if (seed > totalQualifiedTeams && wildcardsNeeded > 0) {
|
|
const wildcardNumber = seed - totalQualifiedTeams;
|
|
return `Wildcard ${wildcardNumber}`;
|
|
}
|
|
|
|
const pairIndex = Math.floor((seed - 1) / 2);
|
|
const isFirstInPair = (seed - 1) % 2 === 0;
|
|
|
|
if (isFirstInPair) {
|
|
const groupIndex = pairIndex % numGroups;
|
|
const rankIndex = Math.floor(pairIndex / numGroups);
|
|
|
|
const rank = rankIndex + 1;
|
|
const groupName = groupNames[groupIndex] || `${groupIndex + 1}`;
|
|
const rankSuffix =
|
|
rank === 1 ? "1st" : rank === 2 ? "2nd" : rank === 3 ? "3rd" : `${rank}th`;
|
|
|
|
return `${groupName} ${rankSuffix}`;
|
|
} else {
|
|
const groupIndex = (pairIndex + 1) % numGroups;
|
|
const rankIndex = advancePerGroup - 1 - Math.floor(pairIndex / numGroups);
|
|
|
|
const rank = rankIndex + 1;
|
|
const groupName = groupNames[groupIndex] || `${groupIndex + 1}`;
|
|
const rankSuffix =
|
|
rank === 1 ? "1st" : rank === 2 ? "2nd" : rank === 3 ? "3rd" : `${rank}th`;
|
|
|
|
return `${groupName} ${rankSuffix}`;
|
|
}
|
|
}
|