From f3286e7fd1b5bec59aed0c67e1aa61fd2129dde7 Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Sun, 12 Jul 2026 19:32:19 +0000 Subject: [PATCH] feat: preserve normalized accepted game facts --- .../competition/competition.schema.spec.ts | 12 ++-- .../modules/competition/competition.schema.ts | 57 ++++++++++--------- .../migrations/20260712-competition-domain.ts | 50 ++++++++++++++++ packages/contracts/src/facts.ts | 5 +- 4 files changed, 92 insertions(+), 32 deletions(-) diff --git a/apps/api/src/modules/competition/competition.schema.spec.ts b/apps/api/src/modules/competition/competition.schema.spec.ts index bc72e78a..3e96e0a3 100644 --- a/apps/api/src/modules/competition/competition.schema.spec.ts +++ b/apps/api/src/modules/competition/competition.schema.spec.ts @@ -62,6 +62,9 @@ describe('Competition persistence schemas', () => { }); 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, [ { keys: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 }, @@ -115,7 +118,9 @@ describe('Competition persistence schemas', () => { }); function expectIndexes( - schema: { indexes(): Array<[Record, Record]> }, + schema: { + indexes(): Array<[Record, Record]>; + }, expected: Array<{ keys: Record; options?: Record; @@ -125,10 +130,7 @@ function expectIndexes( for (const candidate of expected) { expect(indexes).toEqual( expect.arrayContaining([ - [ - candidate.keys, - expect.objectContaining(candidate.options ?? {}), - ], + [candidate.keys, expect.objectContaining(candidate.options ?? {})], ]), ); } diff --git a/apps/api/src/modules/competition/competition.schema.ts b/apps/api/src/modules/competition/competition.schema.ts index 659199a8..d76f043a 100644 --- a/apps/api/src/modules/competition/competition.schema.ts +++ b/apps/api/src/modules/competition/competition.schema.ts @@ -21,8 +21,7 @@ export const COMPETITION_OUTBOX_STATES = [ 'published', 'failed', ] as const; -export type CompetitionOutboxState = - (typeof COMPETITION_OUTBOX_STATES)[number]; +export type CompetitionOutboxState = (typeof COMPETITION_OUTBOX_STATES)[number]; export const TOURNAMENT_CODE_ASSIGNMENT_STATES = [ 'reserved', @@ -87,6 +86,9 @@ export interface CompetitionFact { seriesId?: mongoose.Types.ObjectId; gameSlotId?: mongoose.Types.ObjectId; ordinal?: number; + startedAt?: Date; + durationSeconds?: number; + participants?: Array>; evidence?: Record; mapping?: Record; gameAcceptedFactId?: mongoose.Types.ObjectId; @@ -170,31 +172,31 @@ const CompetitionTeamSchema = new Schema( export const CompetitionSchema: mongoose.Schema = new Schema( - { - matchId: { - type: Schema.Types.ObjectId, - ref: 'match', - required: true, + { + matchId: { + type: Schema.Types.ObjectId, + ref: 'match', + 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' }, ); @@ -223,6 +225,9 @@ export const CompetitionFactSchema = new Schema( seriesId: { type: Schema.Types.ObjectId }, gameSlotId: { type: Schema.Types.ObjectId }, ordinal: { type: Number, min: 1 }, + startedAt: { type: Date }, + durationSeconds: { type: Number, min: 0 }, + participants: { type: Schema.Types.Mixed }, evidence: { type: Schema.Types.Mixed }, mapping: { type: Schema.Types.Mixed }, gameAcceptedFactId: { type: Schema.Types.ObjectId }, diff --git a/apps/api/src/modules/global/database/migrations/20260712-competition-domain.ts b/apps/api/src/modules/global/database/migrations/20260712-competition-domain.ts index 1c9a2f0f..501af5f5 100644 --- a/apps/api/src/modules/global/database/migrations/20260712-competition-domain.ts +++ b/apps/api/src/modules/global/database/migrations/20260712-competition-domain.ts @@ -169,6 +169,19 @@ export interface MigrationGameAcceptedFact extends MigrationFactBase { seriesId: string; gameSlotId: string; 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: | { type: 'riot'; riotMatchId: string; payloadHash: string } | { type: 'legacy'; legacyReference: string }; @@ -908,6 +921,39 @@ function addGameFacts( seriesId: planned.series.id, gameSlotId: slot.id, 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, mapping: { mappingVersion: 1, @@ -1434,6 +1480,10 @@ function positiveInteger(value: unknown): number | undefined { : undefined; } +function numericValue(value: unknown): number { + return typeof value === 'number' && Number.isFinite(value) ? value : 0; +} + function issue( code: string, message: string, diff --git a/packages/contracts/src/facts.ts b/packages/contracts/src/facts.ts index 06d242d7..b8eda4ee 100644 --- a/packages/contracts/src/facts.ts +++ b/packages/contracts/src/facts.ts @@ -10,6 +10,7 @@ import type { UserId, } from "./ids.js"; import type { IsoDateTime } from "./series.js"; +import type { GameParticipantView } from "./game.js"; export type CompetitionFactActor = | { type: "system" } @@ -35,6 +36,9 @@ export interface GameAcceptedFactView extends CompetitionFactViewBase { seriesId: SeriesId; gameSlotId: GameSlotId; ordinal: number; + startedAt: IsoDateTime; + durationSeconds: number; + participants: GameParticipantView[]; evidence: | { type: "riot"; riotMatchId: RiotMatchId; payloadHash: string } | { type: "legacy"; legacyReference: string }; @@ -82,4 +86,3 @@ export interface CompetitionFactsResponse { facts: CompetitionFactView[]; nextCursor: string | null; } -