feat: preserve normalized accepted game facts

This commit is contained in:
dahyeon committed 2026-07-12 19:32:19 +00:00
1 parent e6e434d13f
commit f3286e7fd1
4 files changed
+92 -32

No files matched your search

@@ -62,6 +62,9 @@ describe('Competition persistence schemas', () => {
}); });
it('makes fact ordering and command replay identities unique', () => { it('makes fact ordering and command replay identities unique', () => {
expect(CompetitionFactSchema.path('startedAt')).toBeDefined();
expect(CompetitionFactSchema.path('durationSeconds')).toBeDefined();
expect(CompetitionFactSchema.path('participants')).toBeDefined();
expectIndexes(CompetitionFactSchema, [ expectIndexes(CompetitionFactSchema, [
{ {
keys: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 }, keys: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 },
@@ -115,7 +118,9 @@ describe('Competition persistence schemas', () => {
}); });
function expectIndexes( function expectIndexes(
schema: { indexes(): Array<[Record<string, number>, Record<string, unknown>]> }, schema: {
indexes(): Array<[Record<string, number>, Record<string, unknown>]>;
},
expected: Array<{ expected: Array<{
keys: Record<string, number>; keys: Record<string, number>;
options?: Record<string, unknown>; options?: Record<string, unknown>;
@@ -125,10 +130,7 @@ function expectIndexes(
for (const candidate of expected) { for (const candidate of expected) {
expect(indexes).toEqual( expect(indexes).toEqual(
expect.arrayContaining([ expect.arrayContaining([
[ [candidate.keys, expect.objectContaining(candidate.options ?? {})],
candidate.keys,
expect.objectContaining(candidate.options ?? {}),
],
]), ]),
); );
} }
@@ -21,8 +21,7 @@ export const COMPETITION_OUTBOX_STATES = [
'published', 'published',
'failed', 'failed',
] as const; ] as const;
export type CompetitionOutboxState = export type CompetitionOutboxState = (typeof COMPETITION_OUTBOX_STATES)[number];
(typeof COMPETITION_OUTBOX_STATES)[number];
export const TOURNAMENT_CODE_ASSIGNMENT_STATES = [ export const TOURNAMENT_CODE_ASSIGNMENT_STATES = [
'reserved', 'reserved',
@@ -87,6 +86,9 @@ export interface CompetitionFact {
seriesId?: mongoose.Types.ObjectId; seriesId?: mongoose.Types.ObjectId;
gameSlotId?: mongoose.Types.ObjectId; gameSlotId?: mongoose.Types.ObjectId;
ordinal?: number; ordinal?: number;
startedAt?: Date;
durationSeconds?: number;
participants?: Array<Record<string, unknown>>;
evidence?: Record<string, unknown>; evidence?: Record<string, unknown>;
mapping?: Record<string, unknown>; mapping?: Record<string, unknown>;
gameAcceptedFactId?: mongoose.Types.ObjectId; gameAcceptedFactId?: mongoose.Types.ObjectId;
@@ -170,31 +172,31 @@ const CompetitionTeamSchema = new Schema<CompetitionTeamSnapshot>(
export const CompetitionSchema: mongoose.Schema<Competition> = export const CompetitionSchema: mongoose.Schema<Competition> =
new Schema<Competition>( new Schema<Competition>(
{ {
matchId: { matchId: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'match', ref: 'match',
required: true, required: true,
},
communityId: {
type: Schema.Types.ObjectId,
ref: 'community',
required: true,
},
schemaVersion: { type: Number, required: true, min: 1 },
projectorVersion: { type: Number, required: true, min: 1 },
revision: { type: Number, required: true, min: 0, default: 0 },
phase: {
type: String,
enum: COMPETITION_PHASES,
required: true,
default: 'ready',
},
teams: { type: [CompetitionTeamSchema], required: true },
format: { type: Schema.Types.Mixed, required: true },
seriesGraph: { type: Schema.Types.Mixed, required: true },
projection: { type: Schema.Types.Mixed, required: true },
}, },
communityId: {
type: Schema.Types.ObjectId,
ref: 'community',
required: true,
},
schemaVersion: { type: Number, required: true, min: 1 },
projectorVersion: { type: Number, required: true, min: 1 },
revision: { type: Number, required: true, min: 0, default: 0 },
phase: {
type: String,
enum: COMPETITION_PHASES,
required: true,
default: 'ready',
},
teams: { type: [CompetitionTeamSchema], required: true },
format: { type: Schema.Types.Mixed, required: true },
seriesGraph: { type: Schema.Types.Mixed, required: true },
projection: { type: Schema.Types.Mixed, required: true },
},
{ timestamps: true, collection: 'competition' }, { timestamps: true, collection: 'competition' },
); );
@@ -223,6 +225,9 @@ export const CompetitionFactSchema = new Schema<CompetitionFact>(
seriesId: { type: Schema.Types.ObjectId }, seriesId: { type: Schema.Types.ObjectId },
gameSlotId: { type: Schema.Types.ObjectId }, gameSlotId: { type: Schema.Types.ObjectId },
ordinal: { type: Number, min: 1 }, ordinal: { type: Number, min: 1 },
startedAt: { type: Date },
durationSeconds: { type: Number, min: 0 },
participants: { type: Schema.Types.Mixed },
evidence: { type: Schema.Types.Mixed }, evidence: { type: Schema.Types.Mixed },
mapping: { type: Schema.Types.Mixed }, mapping: { type: Schema.Types.Mixed },
gameAcceptedFactId: { type: Schema.Types.ObjectId }, gameAcceptedFactId: { type: Schema.Types.ObjectId },
@@ -169,6 +169,19 @@ export interface MigrationGameAcceptedFact extends MigrationFactBase {
seriesId: string; seriesId: string;
gameSlotId: string; gameSlotId: string;
ordinal: number; ordinal: number;
startedAt: string;
durationSeconds: number;
participants: Array<{
userId: string;
teamId: string;
side: 'blue' | 'red';
summonerName: string;
tagLine: string;
championId: number;
kills: number;
deaths: number;
assists: number;
}>;
evidence: evidence:
| { type: 'riot'; riotMatchId: string; payloadHash: string } | { type: 'riot'; riotMatchId: string; payloadHash: string }
| { type: 'legacy'; legacyReference: string }; | { type: 'legacy'; legacyReference: string };
@@ -908,6 +921,39 @@ function addGameFacts(
seriesId: planned.series.id, seriesId: planned.series.id,
gameSlotId: slot.id, gameSlotId: slot.id,
ordinal: planned.ordinal, ordinal: planned.ordinal,
startedAt: occurredAt,
durationSeconds:
typeof game.gameDuration === 'number' ? game.gameDuration : 0,
participants: (game.participants ?? []).flatMap((participant) => {
const userId = opaqueString(participant.userId);
const teamId = teamIds.get(participant.teamIndex ?? -1);
if (
!userId ||
!teamId ||
(participant.side !== 'blue' && participant.side !== 'red')
) {
return [];
}
return [
{
userId,
teamId,
side: participant.side,
summonerName:
typeof participant.summonerName === 'string'
? participant.summonerName
: '',
tagLine:
typeof participant.tagLine === 'string'
? participant.tagLine
: '',
championId: numericValue(participant.championId),
kills: numericValue(participant.kills),
deaths: numericValue(participant.deaths),
assists: numericValue(participant.assists),
},
];
}),
evidence, evidence,
mapping: { mapping: {
mappingVersion: 1, mappingVersion: 1,
@@ -1434,6 +1480,10 @@ function positiveInteger(value: unknown): number | undefined {
: undefined; : undefined;
} }
function numericValue(value: unknown): number {
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
}
function issue( function issue(
code: string, code: string,
message: string, message: string,
+4 -1
View File
@@ -10,6 +10,7 @@ import type {
UserId, UserId,
} from "./ids.js"; } from "./ids.js";
import type { IsoDateTime } from "./series.js"; import type { IsoDateTime } from "./series.js";
import type { GameParticipantView } from "./game.js";
export type CompetitionFactActor = export type CompetitionFactActor =
| { type: "system" } | { type: "system" }
@@ -35,6 +36,9 @@ export interface GameAcceptedFactView extends CompetitionFactViewBase {
seriesId: SeriesId; seriesId: SeriesId;
gameSlotId: GameSlotId; gameSlotId: GameSlotId;
ordinal: number; ordinal: number;
startedAt: IsoDateTime;
durationSeconds: number;
participants: GameParticipantView[];
evidence: evidence:
| { type: "riot"; riotMatchId: RiotMatchId; payloadHash: string } | { type: "riot"; riotMatchId: RiotMatchId; payloadHash: string }
| { type: "legacy"; legacyReference: string }; | { type: "legacy"; legacyReference: string };
@@ -82,4 +86,3 @@ export interface CompetitionFactsResponse {
facts: CompetitionFactView[]; facts: CompetitionFactView[];
nextCursor: string | null; nextCursor: string | null;
} }