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', () => {
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<string, number>, Record<string, unknown>]> },
schema: {
indexes(): Array<[Record<string, number>, Record<string, unknown>]>;
},
expected: Array<{
keys: Record<string, number>;
options?: Record<string, unknown>;
@@ -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 ?? {})],
]),
);
}
@@ -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<Record<string, unknown>>;
evidence?: Record<string, unknown>;
mapping?: Record<string, unknown>;
gameAcceptedFactId?: mongoose.Types.ObjectId;
@@ -170,31 +172,31 @@ const CompetitionTeamSchema = new Schema<CompetitionTeamSnapshot>(
export const CompetitionSchema: mongoose.Schema<Competition> =
new Schema<Competition>(
{
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<CompetitionFact>(
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 },
@@ -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,
+4 -1
View File
@@ -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;
}