Add timeline death events
This commit is contained in:
1 parent
d35843a8e2
commit
df0e43d884
5 files changed
+528
-12
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import {
|
||||
ADVANTAGE_EVENT_WEIGHTS,
|
||||
SUMMONERS_RIFT_MAP,
|
||||
type AdvantageEventType,
|
||||
type SummonersRiftMapZone,
|
||||
} from '@persona-lenz/shared';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@@ -13,6 +15,7 @@ interface TimelineChampionKillEvent {
|
||||
timestamp: number;
|
||||
killerId?: number;
|
||||
victimId?: number;
|
||||
shutdownBounty?: number;
|
||||
assistingParticipantIds: number[];
|
||||
position?: TimelinePosition;
|
||||
victimDamageReceived: Array<{
|
||||
@@ -30,6 +33,11 @@ interface TimelineEliteMonsterKillEvent {
|
||||
position?: TimelinePosition;
|
||||
}
|
||||
|
||||
interface TimelineFrameSnapshot {
|
||||
timestamp: number;
|
||||
participantPositions: Map<number, TimelinePosition>;
|
||||
}
|
||||
|
||||
export interface TimelineFightCluster {
|
||||
startTimeMs: number;
|
||||
endTimeMs: number;
|
||||
@@ -59,9 +67,20 @@ export interface TimelineAdvantageEvent {
|
||||
participantIds: number[];
|
||||
}
|
||||
|
||||
export interface TimelineDeathEvent {
|
||||
t: number;
|
||||
victimParticipantId: number;
|
||||
x: number;
|
||||
y: number;
|
||||
zone: SummonersRiftMapZone;
|
||||
shutdownBountyGiven: number;
|
||||
nearestAllyDistanceApprox: number | null;
|
||||
}
|
||||
|
||||
export interface TimelineDerivedData {
|
||||
fightClusters: TimelineFightCluster[];
|
||||
advantageEvents: TimelineAdvantageEvent[];
|
||||
deathEvents: TimelineDeathEvent[];
|
||||
objectiveWindows: TimelineObjectiveWindow[];
|
||||
}
|
||||
|
||||
@@ -129,6 +148,82 @@ function distance(
|
||||
return Math.hypot(left.x - right.x, left.y - right.y);
|
||||
}
|
||||
|
||||
function isWithinRadius(
|
||||
position: TimelinePosition,
|
||||
center: TimelinePosition,
|
||||
radius: number,
|
||||
): boolean {
|
||||
return distance(position, center) <= radius;
|
||||
}
|
||||
|
||||
function classifySummonersRiftPosition(
|
||||
position: TimelinePosition | undefined,
|
||||
): SummonersRiftMapZone {
|
||||
if (!position) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
const {
|
||||
baronPit,
|
||||
baseRadius,
|
||||
coordinateMax,
|
||||
coordinateMin,
|
||||
dragonPit,
|
||||
jungleMidline,
|
||||
laneAxisHalfWidth,
|
||||
objectivePitRadius,
|
||||
riverBandHalfWidth,
|
||||
} = SUMMONERS_RIFT_MAP;
|
||||
const { x, y } = position;
|
||||
|
||||
if (
|
||||
x < coordinateMin ||
|
||||
y < coordinateMin ||
|
||||
x > coordinateMax ||
|
||||
y > coordinateMax
|
||||
) {
|
||||
return 'unknown';
|
||||
}
|
||||
if (isWithinRadius(position, dragonPit, objectivePitRadius)) {
|
||||
return 'dragonPit';
|
||||
}
|
||||
if (isWithinRadius(position, baronPit, objectivePitRadius)) {
|
||||
return 'baronPit';
|
||||
}
|
||||
if (x <= baseRadius && y <= baseRadius) {
|
||||
return 'blueBase';
|
||||
}
|
||||
if (x >= coordinateMax - baseRadius && y >= coordinateMax - baseRadius) {
|
||||
return 'redBase';
|
||||
}
|
||||
if (Math.abs(x + y - coordinateMax) <= riverBandHalfWidth) {
|
||||
return 'river';
|
||||
}
|
||||
if (x <= laneAxisHalfWidth || y >= coordinateMax - laneAxisHalfWidth) {
|
||||
return 'topLane';
|
||||
}
|
||||
if (Math.abs(x - y) <= laneAxisHalfWidth) {
|
||||
return 'middleLane';
|
||||
}
|
||||
if (y <= laneAxisHalfWidth || x >= coordinateMax - laneAxisHalfWidth) {
|
||||
return 'bottomLane';
|
||||
}
|
||||
if (x < jungleMidline && y >= jungleMidline) {
|
||||
return 'northWestJungle';
|
||||
}
|
||||
if (x >= jungleMidline && y >= jungleMidline) {
|
||||
return 'northEastJungle';
|
||||
}
|
||||
if (x < jungleMidline && y < jungleMidline) {
|
||||
return 'southWestJungle';
|
||||
}
|
||||
if (x >= jungleMidline && y < jungleMidline) {
|
||||
return 'southEastJungle';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
function isConnectedKill(
|
||||
left: TimelineChampionKillEvent,
|
||||
right: TimelineChampionKillEvent,
|
||||
@@ -281,6 +376,7 @@ function parseChampionKillEvent(
|
||||
timestamp,
|
||||
killerId: asParticipantId(event.killerId),
|
||||
victimId: asParticipantId(event.victimId),
|
||||
shutdownBounty: asNumber(event.shutdownBounty),
|
||||
assistingParticipantIds: parseAssistingParticipantIds(
|
||||
event.assistingParticipantIds,
|
||||
),
|
||||
@@ -289,6 +385,33 @@ function parseChampionKillEvent(
|
||||
};
|
||||
}
|
||||
|
||||
function parseTimelineFrame(
|
||||
frame: Record<string, unknown>,
|
||||
): TimelineFrameSnapshot | undefined {
|
||||
const timestamp = asNumber(frame.timestamp);
|
||||
if (timestamp === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const participantFrames = asRecord(frame.participantFrames);
|
||||
const participantPositions = new Map<number, TimelinePosition>();
|
||||
for (const [participantIdValue, participantFrameValue] of Object.entries(
|
||||
participantFrames,
|
||||
)) {
|
||||
const participantId = asParticipantId(Number(participantIdValue));
|
||||
const participantFrame = asRecord(participantFrameValue);
|
||||
const position = parsePosition(participantFrame.position);
|
||||
if (participantId !== undefined && position) {
|
||||
participantPositions.set(participantId, position);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
timestamp,
|
||||
participantPositions,
|
||||
};
|
||||
}
|
||||
|
||||
function parseEliteMonsterKillEvent(
|
||||
event: Record<string, unknown>,
|
||||
): TimelineEliteMonsterKillEvent | undefined {
|
||||
@@ -398,8 +521,14 @@ function extractTimelineEvents(timeline: Record<string, unknown>) {
|
||||
const championKills: TimelineChampionKillEvent[] = [];
|
||||
const eliteMonsterKills: TimelineEliteMonsterKillEvent[] = [];
|
||||
const advantageEvents: TimelineAdvantageEvent[] = [];
|
||||
const frameSnapshots: TimelineFrameSnapshot[] = [];
|
||||
|
||||
for (const frame of frames) {
|
||||
const frameSnapshot = parseTimelineFrame(frame);
|
||||
if (frameSnapshot) {
|
||||
frameSnapshots.push(frameSnapshot);
|
||||
}
|
||||
|
||||
for (const rawEvent of asArray(frame.events)) {
|
||||
const event = asRecord(rawEvent);
|
||||
switch (asString(event.type)) {
|
||||
@@ -458,6 +587,9 @@ function extractTimelineEvents(timeline: Record<string, unknown>) {
|
||||
advantageEvents: advantageEvents.sort(
|
||||
(left, right) => left.timestampMs - right.timestampMs,
|
||||
),
|
||||
frameSnapshots: frameSnapshots.sort(
|
||||
(left, right) => left.timestamp - right.timestamp,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -558,17 +690,134 @@ function buildObjectiveWindows(
|
||||
}));
|
||||
}
|
||||
|
||||
function interpolatePosition(
|
||||
frameSnapshots: TimelineFrameSnapshot[],
|
||||
participantId: number,
|
||||
timestampMs: number,
|
||||
): TimelinePosition | undefined {
|
||||
let previous: { timestamp: number; position: TimelinePosition } | undefined;
|
||||
let next: { timestamp: number; position: TimelinePosition } | undefined;
|
||||
|
||||
for (const frame of frameSnapshots) {
|
||||
const position = frame.participantPositions.get(participantId);
|
||||
if (!position) {
|
||||
continue;
|
||||
}
|
||||
if (frame.timestamp <= timestampMs) {
|
||||
previous = { timestamp: frame.timestamp, position };
|
||||
}
|
||||
if (frame.timestamp >= timestampMs) {
|
||||
next = { timestamp: frame.timestamp, position };
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (previous && next) {
|
||||
if (previous.timestamp === next.timestamp) {
|
||||
return previous.position;
|
||||
}
|
||||
|
||||
const ratio =
|
||||
(timestampMs - previous.timestamp) /
|
||||
(next.timestamp - previous.timestamp);
|
||||
return {
|
||||
x: previous.position.x + (next.position.x - previous.position.x) * ratio,
|
||||
y: previous.position.y + (next.position.y - previous.position.y) * ratio,
|
||||
};
|
||||
}
|
||||
|
||||
return previous?.position ?? next?.position;
|
||||
}
|
||||
|
||||
function nearestAllyDistanceApprox(
|
||||
frameSnapshots: TimelineFrameSnapshot[],
|
||||
victimParticipantId: number,
|
||||
deathPosition: TimelinePosition,
|
||||
timestampMs: number,
|
||||
): number | null {
|
||||
const victimTeamId = teamIdFromParticipantId(victimParticipantId);
|
||||
if (!victimTeamId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let nearestDistance = Number.POSITIVE_INFINITY;
|
||||
for (let participantId = 1; participantId <= 10; participantId += 1) {
|
||||
if (
|
||||
participantId === victimParticipantId ||
|
||||
teamIdFromParticipantId(participantId) !== victimTeamId
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const allyPosition = interpolatePosition(
|
||||
frameSnapshots,
|
||||
participantId,
|
||||
timestampMs,
|
||||
);
|
||||
if (!allyPosition) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nearestDistance = Math.min(
|
||||
nearestDistance,
|
||||
distance(deathPosition, allyPosition),
|
||||
);
|
||||
}
|
||||
|
||||
return Number.isFinite(nearestDistance) ? Math.round(nearestDistance) : null;
|
||||
}
|
||||
|
||||
function buildDeathEvents(
|
||||
championKills: TimelineChampionKillEvent[],
|
||||
frameSnapshots: TimelineFrameSnapshot[],
|
||||
): TimelineDeathEvent[] {
|
||||
return championKills.flatMap((event) => {
|
||||
if (event.victimId === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const position =
|
||||
event.position ??
|
||||
interpolatePosition(frameSnapshots, event.victimId, event.timestamp);
|
||||
if (!position) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
t: event.timestamp,
|
||||
victimParticipantId: event.victimId,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
zone: classifySummonersRiftPosition(position),
|
||||
shutdownBountyGiven: event.shutdownBounty ?? 0,
|
||||
nearestAllyDistanceApprox: nearestAllyDistanceApprox(
|
||||
frameSnapshots,
|
||||
event.victimId,
|
||||
position,
|
||||
event.timestamp,
|
||||
),
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TimelineDerivedService {
|
||||
public buildDerivedData(
|
||||
timeline: Record<string, unknown>,
|
||||
): TimelineDerivedData {
|
||||
const { advantageEvents, championKills, eliteMonsterKills } =
|
||||
extractTimelineEvents(timeline);
|
||||
const {
|
||||
advantageEvents,
|
||||
championKills,
|
||||
eliteMonsterKills,
|
||||
frameSnapshots,
|
||||
} = extractTimelineEvents(timeline);
|
||||
|
||||
return {
|
||||
fightClusters: buildFightClusters(championKills),
|
||||
advantageEvents,
|
||||
deathEvents: buildDeathEvents(championKills, frameSnapshots),
|
||||
objectiveWindows: buildObjectiveWindows(eliteMonsterKills),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user