Add timeline advantage events

This commit is contained in:
dahyeon committed 2026-07-02 23:31:19 -07:00
1 parent 1e2f1695ce
commit d35843a8e2
5 files changed
+342 -1

No files matched your search

@@ -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,
@@ -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([]);
});
});
@@ -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<string, unknown>): number[] {
return toSortedIds(
[
asParticipantId(event.killerId),
...parseAssistingParticipantIds(event.assistingParticipantIds),
].filter((id): id is number => id !== undefined),
);
}
function createStructureAdvantageEvent(
event: Record<string, unknown>,
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<string, unknown>,
): 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<string, unknown>) {
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<string, unknown>) {
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<string, unknown>) {
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<string, unknown>) {
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<string, unknown>,
): TimelineDerivedData {
const { championKills, eliteMonsterKills } =
const { advantageEvents, championKills, eliteMonsterKills } =
extractTimelineEvents(timeline);
return {
fightClusters: buildFightClusters(championKills),
advantageEvents,
objectiveWindows: buildObjectiveWindows(eliteMonsterKills),
};
}
+12
View File
@@ -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', () => {