diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts index bb5de04..7a70639 100644 --- a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts @@ -417,6 +417,22 @@ describe('AnalysisPipelineService', () => { isTeamfight: false, }, ], + advantageEvents: [ + { + timestampMs: 10_000, + teamId: 100, + type: 'championKill', + score: 1, + participantIds: [1, 2], + }, + { + timestampMs: 900_000, + teamId: 100, + type: 'dragon', + score: 1.5, + participantIds: [4], + }, + ], objectiveWindows: [ { objectiveTimeMs: 900_000, diff --git a/apps/api/src/modules/analysis/timeline-derived.service.spec.ts b/apps/api/src/modules/analysis/timeline-derived.service.spec.ts index 9a286af..9575ffc 100644 --- a/apps/api/src/modules/analysis/timeline-derived.service.spec.ts +++ b/apps/api/src/modules/analysis/timeline-derived.service.spec.ts @@ -269,4 +269,121 @@ describe('TimelineDerivedService', () => { expect(derived.objectiveWindows).toHaveLength(1); expect(derived.objectiveWindows[0].monsterType).toBe('BARON_NASHOR'); }); + + it('builds weighted advantage events for kills, plates, towers, and objectives', () => { + const derived = service.buildDerivedData( + createTimelinePayload([ + [ + { + type: 'CHAMPION_KILL', + timestamp: 10_000, + killerId: 1, + victimId: 6, + assistingParticipantIds: [2], + victimDamageReceived: [{ participantId: 3, magicDamageDone: 80 }], + }, + { + type: 'TURRET_PLATE_DESTROYED', + timestamp: 20_000, + killerId: 0, + teamId: 200, + }, + { + type: 'BUILDING_KILL', + timestamp: 30_000, + killerId: 2, + assistingParticipantIds: [3, 4], + buildingType: 'TOWER_BUILDING', + teamId: 200, + towerType: 'OUTER_TURRET', + }, + { + type: 'ELITE_MONSTER_KILL', + timestamp: 40_000, + killerId: 3, + killerTeamId: 100, + monsterType: 'RIFTHERALD', + }, + { + type: 'ELITE_MONSTER_KILL', + timestamp: 50_000, + killerId: 8, + killerTeamId: 200, + monsterType: 'BARON_NASHOR', + }, + ], + ]), + ); + + expect(derived.advantageEvents).toEqual([ + { + timestampMs: 10_000, + teamId: 100, + type: 'championKill', + score: 1, + participantIds: [1, 2, 3], + }, + { + timestampMs: 20_000, + teamId: 100, + type: 'turretPlate', + score: 0.5, + participantIds: [], + }, + { + timestampMs: 30_000, + teamId: 100, + type: 'tower', + score: 1.5, + participantIds: [2, 3, 4], + }, + { + timestampMs: 40_000, + teamId: 100, + type: 'riftHerald', + score: 1.5, + participantIds: [3], + }, + { + timestampMs: 50_000, + teamId: 200, + type: 'baron', + score: 3, + participantIds: [8], + }, + ]); + }); + + it('omits advantage events when the scoring team cannot be inferred', () => { + const derived = service.buildDerivedData( + createTimelinePayload([ + [ + { + type: 'CHAMPION_KILL', + timestamp: 10_000, + killerId: 0, + victimId: 6, + }, + { + type: 'TURRET_PLATE_DESTROYED', + timestamp: 20_000, + killerId: 0, + }, + { + type: 'BUILDING_KILL', + timestamp: 30_000, + buildingType: 'TOWER_BUILDING', + }, + { + type: 'ELITE_MONSTER_KILL', + timestamp: 40_000, + killerId: 0, + monsterType: 'DRAGON', + }, + ], + ]), + ); + + expect(derived.advantageEvents).toEqual([]); + }); }); diff --git a/apps/api/src/modules/analysis/timeline-derived.service.ts b/apps/api/src/modules/analysis/timeline-derived.service.ts index 741504d..e5f8e96 100644 --- a/apps/api/src/modules/analysis/timeline-derived.service.ts +++ b/apps/api/src/modules/analysis/timeline-derived.service.ts @@ -1,3 +1,7 @@ +import { + ADVANTAGE_EVENT_WEIGHTS, + type AdvantageEventType, +} from '@persona-lenz/shared'; import { Injectable } from '@nestjs/common'; interface TimelinePosition { @@ -47,8 +51,17 @@ export interface TimelineObjectiveWindow { position?: TimelinePosition; } +export interface TimelineAdvantageEvent { + timestampMs: number; + teamId: number; + type: AdvantageEventType; + score: number; + participantIds: number[]; +} + export interface TimelineDerivedData { fightClusters: TimelineFightCluster[]; + advantageEvents: TimelineAdvantageEvent[]; objectiveWindows: TimelineObjectiveWindow[]; } @@ -167,6 +180,61 @@ function isObjectiveMonsterType(monsterType: string | undefined): boolean { return monsterType !== undefined && OBJECTIVE_MONSTER_TYPES.has(monsterType); } +function objectiveAdvantageEventType( + monsterType: string | undefined, +): AdvantageEventType | undefined { + switch (monsterType) { + case 'DRAGON': + return 'dragon'; + case 'RIFTHERALD': + return 'riftHerald'; + case 'BARON_NASHOR': + return 'baron'; + default: + return undefined; + } +} + +function teamIdFromParticipantId( + participantId: number | undefined, +): 100 | 200 | undefined { + if (participantId === undefined) { + return undefined; + } + if (participantId >= 1 && participantId <= 5) { + return 100; + } + if (participantId >= 6 && participantId <= 10) { + return 200; + } + return undefined; +} + +function toTeamId(value: unknown): 100 | 200 | undefined { + const teamId = asNumber(value); + return teamId === 100 || teamId === 200 ? teamId : undefined; +} + +function opposingTeamId(teamId: 100 | 200 | undefined): 100 | 200 | undefined { + if (teamId === 100) { + return 200; + } + if (teamId === 200) { + return 100; + } + return undefined; +} + +function scoringParticipantIds(event: TimelineChampionKillEvent): number[] { + return toSortedIds( + [ + event.killerId, + ...event.assistingParticipantIds, + ...event.victimDamageReceived.map((damage) => damage.participantId), + ].filter((id): id is number => id !== undefined), + ); +} + function parseAssistingParticipantIds(value: unknown): number[] { return toSortedIds( asArray(value).flatMap((item) => { @@ -239,11 +307,97 @@ function parseEliteMonsterKillEvent( }; } +function structureParticipantIds(event: Record): number[] { + return toSortedIds( + [ + asParticipantId(event.killerId), + ...parseAssistingParticipantIds(event.assistingParticipantIds), + ].filter((id): id is number => id !== undefined), + ); +} + +function createStructureAdvantageEvent( + event: Record, + type: AdvantageEventType, +): TimelineAdvantageEvent | undefined { + const timestamp = asNumber(event.timestamp); + const participantIds = structureParticipantIds(event); + const teamId = + teamIdFromParticipantId(participantIds[0]) ?? + opposingTeamId(toTeamId(event.teamId)); + if (timestamp === undefined || !teamId) { + return undefined; + } + + return { + timestampMs: timestamp, + teamId, + type, + score: ADVANTAGE_EVENT_WEIGHTS[type], + participantIds, + }; +} + +function createChampionKillAdvantageEvent( + event: TimelineChampionKillEvent, +): TimelineAdvantageEvent | undefined { + const teamId = teamIdFromParticipantId(event.killerId); + if (event.killerId === undefined || !teamId) { + return undefined; + } + + return { + timestampMs: event.timestamp, + teamId, + type: 'championKill', + score: ADVANTAGE_EVENT_WEIGHTS.championKill, + participantIds: scoringParticipantIds(event), + }; +} + +function createEliteMonsterAdvantageEvent( + event: TimelineEliteMonsterKillEvent, +): TimelineAdvantageEvent | undefined { + const type = objectiveAdvantageEventType(event.monsterType); + if (!type) { + return undefined; + } + const teamId = + toTeamId(event.killerTeamId) ?? + teamIdFromParticipantId(event.killerParticipantId); + if (!teamId) { + return undefined; + } + + return { + timestampMs: event.timestamp, + teamId, + type, + score: ADVANTAGE_EVENT_WEIGHTS[type], + participantIds: + event.killerParticipantId === undefined + ? [] + : [event.killerParticipantId], + }; +} + +function createBuildingAdvantageEvent( + event: Record, +): TimelineAdvantageEvent | undefined { + const buildingType = asString(event.buildingType); + const towerType = asString(event.towerType); + if (buildingType !== 'TOWER_BUILDING' && towerType === undefined) { + return undefined; + } + return createStructureAdvantageEvent(event, 'tower'); +} + function extractTimelineEvents(timeline: Record) { const info = asRecord(timeline.info); const frames = asArray(info.frames).map(asRecord); const championKills: TimelineChampionKillEvent[] = []; const eliteMonsterKills: TimelineEliteMonsterKillEvent[] = []; + const advantageEvents: TimelineAdvantageEvent[] = []; for (const frame of frames) { for (const rawEvent of asArray(frame.events)) { @@ -253,6 +407,11 @@ function extractTimelineEvents(timeline: Record) { const championKill = parseChampionKillEvent(event); if (championKill) { championKills.push(championKill); + const advantageEvent = + createChampionKillAdvantageEvent(championKill); + if (advantageEvent) { + advantageEvents.push(advantageEvent); + } } break; } @@ -260,6 +419,28 @@ function extractTimelineEvents(timeline: Record) { const eliteMonsterKill = parseEliteMonsterKillEvent(event); if (eliteMonsterKill) { eliteMonsterKills.push(eliteMonsterKill); + const advantageEvent = + createEliteMonsterAdvantageEvent(eliteMonsterKill); + if (advantageEvent) { + advantageEvents.push(advantageEvent); + } + } + break; + } + case 'TURRET_PLATE_DESTROYED': { + const advantageEvent = createStructureAdvantageEvent( + event, + 'turretPlate', + ); + if (advantageEvent) { + advantageEvents.push(advantageEvent); + } + break; + } + case 'BUILDING_KILL': { + const advantageEvent = createBuildingAdvantageEvent(event); + if (advantageEvent) { + advantageEvents.push(advantageEvent); } break; } @@ -274,6 +455,9 @@ function extractTimelineEvents(timeline: Record) { eliteMonsterKills: eliteMonsterKills.sort( (left, right) => left.timestamp - right.timestamp, ), + advantageEvents: advantageEvents.sort( + (left, right) => left.timestampMs - right.timestampMs, + ), }; } @@ -379,11 +563,12 @@ export class TimelineDerivedService { public buildDerivedData( timeline: Record, ): TimelineDerivedData { - const { championKills, eliteMonsterKills } = + const { advantageEvents, championKills, eliteMonsterKills } = extractTimelineEvents(timeline); return { fightClusters: buildFightClusters(championKills), + advantageEvents, objectiveWindows: buildObjectiveWindows(eliteMonsterKills), }; } diff --git a/apps/api/src/shared-contract.spec.ts b/apps/api/src/shared-contract.spec.ts index df22bd4..e771ed8 100644 --- a/apps/api/src/shared-contract.spec.ts +++ b/apps/api/src/shared-contract.spec.ts @@ -1,4 +1,5 @@ import { + ADVANTAGE_EVENT_WEIGHTS, FEATURE_SCHEMA_VERSION, METRICS, VECTOR_DIMENSION, @@ -61,6 +62,17 @@ describe('shared feature registry', () => { METRICS.find((metric) => metric.id === 'deathsPerMin')?.direction, ).toBe('lowerIsBetter'); }); + + it('defines shared advantage event weights for timeline-derived metrics', () => { + expect(ADVANTAGE_EVENT_WEIGHTS).toEqual({ + championKill: 1, + turretPlate: 0.5, + tower: 1.5, + dragon: 1.5, + riftHerald: 1.5, + baron: 3, + }); + }); }); describe('shared DTO contracts', () => { diff --git a/packages/shared/src/feature-registry.ts b/packages/shared/src/feature-registry.ts index 67b870d..c224f6d 100644 --- a/packages/shared/src/feature-registry.ts +++ b/packages/shared/src/feature-registry.ts @@ -219,3 +219,14 @@ export const VECTOR_METRICS = METRICS.filter( ); export const VECTOR_DIMENSION = VECTOR_METRICS.length; + +export const ADVANTAGE_EVENT_WEIGHTS = { + championKill: 1, + turretPlate: 0.5, + tower: 1.5, + dragon: 1.5, + riftHerald: 1.5, + baron: 3, +} as const; + +export type AdvantageEventType = keyof typeof ADVANTAGE_EVENT_WEIGHTS;