fix: recover legacy tournament code transitions

This commit is contained in:
dahyeon committed 2026-07-13 05:34:12 +00:00
1 parent 489d040d8f
commit ef47de7e4f
5 files changed
+775 -5

No files matched your search

@@ -46,6 +46,8 @@ interface LegacyGameInput {
winnerSide?: 'blue' | 'red';
gameCreation?: number | string | Date;
gameDuration?: number;
riotGameEndedAt?: number | string | Date;
riotGameEndedAtSource?: 'gameEndTimestamp' | 'gameCreation';
participants?: Array<{
userId?: unknown;
teamIndex?: number;
@@ -54,6 +56,16 @@ interface LegacyGameInput {
}>;
}
type LegacyUsedOccurrenceSource =
| 'riot-archive-gameEndTimestamp'
| 'riot-archive-gameCreation'
| 'legacy-gameCreation-plus-duration';
interface LegacyUsedOccurrenceEvidence {
at: string;
source: LegacyUsedOccurrenceSource;
}
interface LegacySeriesResultInput {
bracketMatchId?: string;
teamAIndex: number;
@@ -410,6 +422,7 @@ export function planCompetitionMigration(
seriesGraph,
teamBuild.teamIdByLegacyIndex,
reasons,
warnings,
);
if (reasons.length > 0) {
return quarantined(matchId, reasons, warnings);
@@ -2030,6 +2043,7 @@ function buildAssignments(
graph: SeriesDefinition[],
teamIds: ReadonlyMap<number, string>,
reasons: CompetitionMigrationIssue[],
warnings: CompetitionMigrationIssue[],
): CompetitionMigrationAssignment[] {
const result: CompetitionMigrationAssignment[] = [];
for (const code of input.tournamentCodes ?? []) {
@@ -2102,11 +2116,28 @@ function buildAssignments(
);
continue;
}
const legacyGame = riotMatchId
? input.result?.games.find(
(game) => game.riotMatchId?.trim() === riotMatchId,
)
: undefined;
const timestamps = tournamentCodeTimestamps(
input,
code,
tournamentCodeId,
reasons,
warnings,
riotMatchId &&
acceptedGame?.evidence.type === 'riot' &&
acceptedGame.evidence.riotMatchId === riotMatchId &&
legacyGame
? {
riotMatchId,
gameSlotId: slot.id,
factRecordedAt: acceptedGame.recordedAt,
legacyOccurrences: legacyUsedOccurrenceEvidence(legacyGame),
}
: undefined,
);
if (!timestamps) continue;
result.push({
@@ -2173,6 +2204,13 @@ function tournamentCodeTimestamps(
code: LegacyTournamentCodeInput,
tournamentCodeId: string,
reasons: CompetitionMigrationIssue[],
warnings: CompetitionMigrationIssue[],
corroboratedRiotTransition?: {
riotMatchId: string;
gameSlotId: string;
factRecordedAt: string;
legacyOccurrences: LegacyUsedOccurrenceEvidence[];
},
): Pick<
CompetitionMigrationAssignment,
'reservedAt' | 'usedAt' | 'releasedAt' | 'voidedAt'
@@ -2216,7 +2254,7 @@ function tournamentCodeTimestamps(
(code.state === 'released' ? raw.releasedAt : undefined) ??
(code.state === 'voided' ? raw.voidedAt : undefined) ??
sourceFallbackDate(input);
const usedAt =
let usedAt =
code.state === 'used' || code.state === 'voided'
? (raw.usedAt ?? undefined)
: undefined;
@@ -2226,6 +2264,67 @@ function tournamentCodeTimestamps(
code.state === 'voided'
? (raw.voidedAt ?? raw.updatedAt ?? usedAt ?? reservedAt)
: undefined;
// Legacy result collection and repair paths recorded usedAt from Riot game
// occurrence, while updatedAt captured the persisted state transition. Keep
// target lifecycle meaning uniform by using that transition only when an
// archived Riot fact, exact identity, and source occurrence all corroborate.
const occurrenceEvidence = corroboratedRiotTransition?.legacyOccurrences.find(
(candidate) => candidate.at === raw.usedAt,
);
if (
code.state === 'used' &&
raw.reservedAt &&
raw.usedAt &&
occurrenceEvidence &&
corroboratedRiotTransition
) {
const persistedTransitionAt = optionalDate(code.updatedAt);
if (
!persistedTransitionAt ||
persistedTransitionAt < reservedAt ||
persistedTransitionAt < raw.usedAt ||
persistedTransitionAt > corroboratedRiotTransition.factRecordedAt
) {
reasons.push(
issue(
'INVALID_TOURNAMENT_CODE_TIMELINE',
'Legacy used TournamentCode has no valid persisted transition time.',
{
tournamentCodeId,
state: code.state,
reservedAt,
originalUsedAt: raw.usedAt,
sourceUpdatedAt: persistedTransitionAt,
factRecordedAt: corroboratedRiotTransition.factRecordedAt,
},
),
);
return null;
}
const originalUsedAt = raw.usedAt;
usedAt = persistedTransitionAt;
if (originalUsedAt !== usedAt) {
warnings.push(
issue(
'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
'Legacy used TournamentCode usedAt represented Riot game time and was normalized to its persisted transition time.',
{
tournamentCodeId,
originalReservedAt: reservedAt,
originalUsedAt,
sourceUpdatedAt: persistedTransitionAt,
normalizedUsedAt: usedAt,
factRecordedAt: corroboratedRiotTransition.factRecordedAt,
legacyOccurrenceSource: occurrenceEvidence.source,
riotMatchId: corroboratedRiotTransition.riotMatchId,
gameSlotId: corroboratedRiotTransition.gameSlotId,
strategy: 'use-persisted-transition-time',
},
),
);
}
}
const ordered = [reservedAt, usedAt, releasedAt, voidedAt].filter(
(value): value is string => value !== undefined,
);
@@ -2282,6 +2381,50 @@ function gameOccurrence(
return normalizeDate(game.gameCreation, sourceFallbackDate(input));
}
function legacyUsedOccurrenceEvidence(
game: LegacyGameInput,
): LegacyUsedOccurrenceEvidence[] {
const candidates: LegacyUsedOccurrenceEvidence[] = [];
const archivedGameEnd = optionalRiotDate(game.riotGameEndedAt);
if (archivedGameEnd && game.riotGameEndedAtSource) {
candidates.push({
at: archivedGameEnd,
source:
game.riotGameEndedAtSource === 'gameEndTimestamp'
? 'riot-archive-gameEndTimestamp'
: 'riot-archive-gameCreation',
});
}
const gameCreation = optionalRiotDate(game.gameCreation);
if (
gameCreation &&
typeof game.gameDuration === 'number' &&
Number.isFinite(game.gameDuration) &&
game.gameDuration >= 0
) {
const computedGameEnd = optionalDate(
Date.parse(gameCreation) + game.gameDuration * 1000,
);
if (computedGameEnd) {
candidates.push({
at: computedGameEnd,
source: 'legacy-gameCreation-plus-duration',
});
}
}
return candidates.filter(
(candidate, index) =>
candidates.findIndex((other) => other.at === candidate.at) === index,
);
}
function optionalRiotDate(value: unknown): string | null | undefined {
if (typeof value !== 'number') return optionalDate(value);
return optionalDate(value < 10_000_000_000 ? value * 1000 : value);
}
function compareLegacyGames(
left: Omit<PlannedGame, 'ordinal'>,
right: Omit<PlannedGame, 'ordinal'>,