fix: clone hydrated double-elimination brackets
This commit is contained in:
1 parent
e188828423
commit
03358cab4f
2 files changed
+83
-16
No files matched your search
@@ -734,7 +734,14 @@ function requireBracketMatch(
|
||||
const bracketMatch = bracket.bracketMatches.find(
|
||||
(match) => match.bracketMatchId === bracketMatchId,
|
||||
);
|
||||
if (!bracketMatch) throw new Error('Bracket match not found.');
|
||||
if (!bracketMatch) {
|
||||
throw new Error(
|
||||
`Bracket match not found: ${bracketMatchId}. Available: ${bracket.bracketMatches
|
||||
.map((match) => match.bracketMatchId)
|
||||
.filter(Boolean)
|
||||
.join(', ')}`,
|
||||
);
|
||||
}
|
||||
return bracketMatch;
|
||||
}
|
||||
|
||||
@@ -778,26 +785,46 @@ function requiredWinsForGameCount(gameCount: number): number {
|
||||
function cloneBracket(
|
||||
bracket: DoubleEliminationBracket,
|
||||
): DoubleEliminationBracket {
|
||||
const plainBracket = toPlainObject(bracket);
|
||||
return {
|
||||
...bracket,
|
||||
seeds: bracket.seeds.map((seed) => ({ ...seed })),
|
||||
bracketMatches: bracket.bracketMatches.map((match) => ({
|
||||
...match,
|
||||
teamASource: match.teamASource ? { ...match.teamASource } : undefined,
|
||||
teamBSource: match.teamBSource ? { ...match.teamBSource } : undefined,
|
||||
winnerAdvancesTo: match.winnerAdvancesTo
|
||||
? { ...match.winnerAdvancesTo }
|
||||
: undefined,
|
||||
loserAdvancesTo: match.loserAdvancesTo
|
||||
? { ...match.loserAdvancesTo }
|
||||
: undefined,
|
||||
walkover: match.walkover ? { ...match.walkover } : undefined,
|
||||
...plainBracket,
|
||||
seeds: plainBracket.seeds.map((seed) => ({ ...toPlainObject(seed) })),
|
||||
bracketMatches: plainBracket.bracketMatches.map((rawMatch) => {
|
||||
const match = toPlainObject(rawMatch);
|
||||
return {
|
||||
...match,
|
||||
teamASource: match.teamASource
|
||||
? { ...toPlainObject(match.teamASource) }
|
||||
: undefined,
|
||||
teamBSource: match.teamBSource
|
||||
? { ...toPlainObject(match.teamBSource) }
|
||||
: undefined,
|
||||
winnerAdvancesTo: match.winnerAdvancesTo
|
||||
? { ...toPlainObject(match.winnerAdvancesTo) }
|
||||
: undefined,
|
||||
loserAdvancesTo: match.loserAdvancesTo
|
||||
? { ...toPlainObject(match.loserAdvancesTo) }
|
||||
: undefined,
|
||||
walkover: match.walkover
|
||||
? { ...toPlainObject(match.walkover) }
|
||||
: undefined,
|
||||
};
|
||||
}),
|
||||
placements: { ...toPlainObject(plainBracket.placements) },
|
||||
events: plainBracket.events.map((event) => ({
|
||||
...toPlainObject(event),
|
||||
})),
|
||||
placements: { ...bracket.placements },
|
||||
events: bracket.events.map((event) => ({ ...event })),
|
||||
};
|
||||
}
|
||||
|
||||
function toPlainObject<T>(value: T): T {
|
||||
const candidate = value as { toObject?: () => T };
|
||||
if (candidate && typeof candidate.toObject === 'function') {
|
||||
return candidate.toObject();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function shuffleInPlace<T>(items: T[], rng: () => number = Math.random): T[] {
|
||||
for (let index = items.length - 1; index > 0; index -= 1) {
|
||||
const swapIndex = Math.floor(rng() * (index + 1));
|
||||
|
||||
Reference in new issue
Block a user