feat: enrich internal match game analysis
This commit is contained in:
1 parent
9f698a8fec
commit
aa675199af
22 files changed
+3123
-241
No files matched your search
@@ -3,10 +3,13 @@ import mongoose from 'mongoose';
|
||||
import type {
|
||||
MatchDocument,
|
||||
MatchGameResultParticipant,
|
||||
MatchStatsCheckpoint,
|
||||
MatchStatsItemBuildEvent,
|
||||
MatchStatsObjectiveEvent,
|
||||
MatchStatsObjectiveSummary,
|
||||
MatchStatsParticipantCheckpoints,
|
||||
MatchStatsSkillBuildEvent,
|
||||
MatchStatsTimelineEvent,
|
||||
MatchStatsTimelinePoint,
|
||||
TeamSide,
|
||||
} from '../global/database/schema';
|
||||
@@ -27,13 +30,17 @@ interface ParticipantBuildEvents {
|
||||
skills: MatchStatsSkillBuildEvent[];
|
||||
}
|
||||
|
||||
interface TimelineProjection {
|
||||
objectiveEvents: MatchStatsObjectiveEvent[];
|
||||
timelineEvents: MatchStatsTimelineEvent[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MatchStatsFragmentBuilder {
|
||||
public buildFragment(params: {
|
||||
match: MatchDocument;
|
||||
rawByRiotMatchId: Map<string, RiotMatchDto>;
|
||||
timelineByRiotMatchId?: Map<string, RiotMatchTimelineDto>;
|
||||
timelineLoadAttempted?: boolean;
|
||||
}): MatchStatsFragment {
|
||||
const games = [...(params.match.result?.games ?? [])].sort(
|
||||
(left, right) => left.gameNumber - right.gameNumber,
|
||||
@@ -43,13 +50,7 @@ export class MatchStatsFragmentBuilder {
|
||||
const raw = params.rawByRiotMatchId.get(game.riotMatchId);
|
||||
const timeline = params.timelineByRiotMatchId?.get(game.riotMatchId);
|
||||
return raw
|
||||
? this.toGameDetail(
|
||||
params.match,
|
||||
game,
|
||||
raw,
|
||||
timeline,
|
||||
params.timelineLoadAttempted === true,
|
||||
)
|
||||
? this.toGameDetail(params.match, game, raw, timeline)
|
||||
: this.toFallbackGameDetail(params.match, game);
|
||||
});
|
||||
const records = gameDetails.flatMap((detail) =>
|
||||
@@ -130,8 +131,10 @@ export class MatchStatsFragmentBuilder {
|
||||
game: NonNullable<MatchDocument['result']>['games'][number],
|
||||
raw: RiotMatchDto,
|
||||
timeline?: RiotMatchTimelineDto,
|
||||
timelineLoadAttempted = false,
|
||||
): InternalGameDetail {
|
||||
const usableTimeline = isUsableTimeline(timeline, raw.info.gameDuration)
|
||||
? timeline
|
||||
: undefined;
|
||||
const puuidByUserId = new Map(
|
||||
match.participants
|
||||
.filter((participant) => participant.puuid)
|
||||
@@ -158,14 +161,18 @@ export class MatchStatsFragmentBuilder {
|
||||
raw,
|
||||
rawTeamIdBySide,
|
||||
);
|
||||
const objectiveEvents = this.toObjectiveEvents(
|
||||
timeline,
|
||||
const { objectiveEvents, timelineEvents } = this.toTimelineEvents(
|
||||
usableTimeline,
|
||||
rawTeamIdBySide,
|
||||
sideByParticipantId,
|
||||
);
|
||||
const timelineObjectives = this.toTimelineObjectives(objectiveEvents);
|
||||
const buildEventsByParticipantId =
|
||||
this.toBuildEventsByParticipantId(timeline);
|
||||
this.toBuildEventsByParticipantId(usableTimeline);
|
||||
const checkpointsByParticipantId = this.toCheckpointsByParticipantId(
|
||||
usableTimeline,
|
||||
raw.info.gameDuration,
|
||||
);
|
||||
|
||||
return {
|
||||
gameNumber: game.gameNumber,
|
||||
@@ -174,9 +181,10 @@ export class MatchStatsFragmentBuilder {
|
||||
gameDuration: game.gameDuration,
|
||||
gameVersion: raw.info.gameVersion,
|
||||
winnerSide: game.winnerSide,
|
||||
timeline: this.toTimeline(raw, rawTeamIdBySide, timeline),
|
||||
timelineLoaded: timelineLoadAttempted || Boolean(timeline),
|
||||
timeline: this.toTimeline(raw, rawTeamIdBySide, usableTimeline),
|
||||
timelineLoaded: Boolean(usableTimeline),
|
||||
objectiveEvents,
|
||||
timelineEvents,
|
||||
teams: (['blue', 'red'] as TeamSide[]).map((side) => {
|
||||
const teamIndex =
|
||||
side === 'blue' ? game.blueTeamIndex : game.redTeamIndex;
|
||||
@@ -202,7 +210,6 @@ export class MatchStatsFragmentBuilder {
|
||||
objectives: this.toObjectiveSummary(
|
||||
rawTeam,
|
||||
timelineObjectives.get(side),
|
||||
Boolean(timeline),
|
||||
),
|
||||
bans: (rawTeam?.bans ?? []).map((ban) => ({
|
||||
championId: ban.championId,
|
||||
@@ -224,6 +231,9 @@ export class MatchStatsFragmentBuilder {
|
||||
rawParticipant.participantId,
|
||||
)
|
||||
: undefined,
|
||||
checkpoints: rawParticipant
|
||||
? checkpointsByParticipantId.get(rawParticipant.participantId)
|
||||
: undefined,
|
||||
teamKills,
|
||||
teamDamage,
|
||||
});
|
||||
@@ -246,6 +256,7 @@ export class MatchStatsFragmentBuilder {
|
||||
timeline: [],
|
||||
timelineLoaded: false,
|
||||
objectiveEvents: [],
|
||||
timelineEvents: [],
|
||||
teams: (['blue', 'red'] as TeamSide[]).map((side) => ({
|
||||
teamIndex: side === 'blue' ? game.blueTeamIndex : game.redTeamIndex,
|
||||
side,
|
||||
@@ -273,6 +284,7 @@ export class MatchStatsFragmentBuilder {
|
||||
participant: MatchGameResultParticipant;
|
||||
rawParticipant?: RiotMatchParticipantDto;
|
||||
buildEvents?: ParticipantBuildEvents;
|
||||
checkpoints?: MatchStatsParticipantCheckpoints;
|
||||
teamKills: number;
|
||||
teamDamage: number;
|
||||
}): InternalRecord {
|
||||
@@ -282,6 +294,7 @@ export class MatchStatsFragmentBuilder {
|
||||
participant,
|
||||
rawParticipant,
|
||||
buildEvents,
|
||||
checkpoints,
|
||||
teamKills,
|
||||
teamDamage,
|
||||
} = params;
|
||||
@@ -319,6 +332,8 @@ export class MatchStatsFragmentBuilder {
|
||||
riotMatchId: game.riotMatchId,
|
||||
gameCreation: game.gameCreation,
|
||||
gameDuration: game.gameDuration,
|
||||
participantId: rawParticipant?.participantId,
|
||||
checkpoints,
|
||||
userId: participant.userId.toString(),
|
||||
summonerName:
|
||||
snapshot?.gameName ??
|
||||
@@ -455,6 +470,107 @@ export class MatchStatsFragmentBuilder {
|
||||
return buildEventsByParticipantId;
|
||||
}
|
||||
|
||||
private toCheckpointsByParticipantId(
|
||||
timeline: RiotMatchTimelineDto | undefined,
|
||||
gameDurationSeconds: number,
|
||||
): Map<number, MatchStatsParticipantCheckpoints> {
|
||||
const checkpointsByParticipantId = new Map<
|
||||
number,
|
||||
MatchStatsParticipantCheckpoints
|
||||
>();
|
||||
if (
|
||||
!timeline?.info.frames.length ||
|
||||
!Number.isFinite(timeline.info.frameInterval) ||
|
||||
timeline.info.frameInterval <= 0
|
||||
) {
|
||||
return checkpointsByParticipantId;
|
||||
}
|
||||
|
||||
const participantIds = new Set<number>();
|
||||
const championKillEvents = timeline.info.frames.flatMap((frame) =>
|
||||
(frame.events ?? []).filter((event) => event.type === 'CHAMPION_KILL'),
|
||||
);
|
||||
for (const frame of timeline.info.frames) {
|
||||
for (const participantFrame of Object.values(frame.participantFrames)) {
|
||||
participantIds.add(participantFrame.participantId);
|
||||
}
|
||||
}
|
||||
|
||||
const targets = [
|
||||
['at10', 600_000],
|
||||
['at15', 900_000],
|
||||
] as const;
|
||||
for (const participantId of participantIds) {
|
||||
const checkpoints: MatchStatsParticipantCheckpoints = {};
|
||||
for (const [key, targetTimestamp] of targets) {
|
||||
if (gameDurationSeconds * 1_000 < targetTimestamp) continue;
|
||||
|
||||
const selectedFrame = timeline.info.frames.reduce<
|
||||
RiotMatchTimelineDto['info']['frames'][number] | undefined
|
||||
>((latest, frame) => {
|
||||
if (
|
||||
frame.timestamp > targetTimestamp ||
|
||||
!Object.values(frame.participantFrames).some(
|
||||
(participantFrame) =>
|
||||
participantFrame.participantId === participantId,
|
||||
)
|
||||
) {
|
||||
return latest;
|
||||
}
|
||||
return !latest || frame.timestamp > latest.timestamp ? frame : latest;
|
||||
}, undefined);
|
||||
if (
|
||||
!selectedFrame ||
|
||||
targetTimestamp - selectedFrame.timestamp >=
|
||||
timeline.info.frameInterval
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const participantFrame = Object.values(
|
||||
selectedFrame.participantFrames,
|
||||
).find((frame) => frame.participantId === participantId);
|
||||
if (
|
||||
!participantFrame ||
|
||||
typeof participantFrame.minionsKilled !== 'number' ||
|
||||
!Number.isFinite(participantFrame.minionsKilled) ||
|
||||
typeof participantFrame.jungleMinionsKilled !== 'number' ||
|
||||
!Number.isFinite(participantFrame.jungleMinionsKilled)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const checkpoint: MatchStatsCheckpoint = {
|
||||
timestamp: Math.max(0, Math.round(selectedFrame.timestamp / 1_000)),
|
||||
gold: participantFrame.totalGold,
|
||||
cs:
|
||||
participantFrame.minionsKilled +
|
||||
participantFrame.jungleMinionsKilled,
|
||||
xp: participantFrame.xp,
|
||||
kills: 0,
|
||||
deaths: 0,
|
||||
assists: 0,
|
||||
};
|
||||
for (const event of championKillEvents) {
|
||||
if (event.timestamp > selectedFrame.timestamp) continue;
|
||||
if (event.killerId === participantId && event.killerId > 0) {
|
||||
checkpoint.kills += 1;
|
||||
}
|
||||
if (event.victimId === participantId) checkpoint.deaths += 1;
|
||||
if (event.assistingParticipantIds?.includes(participantId)) {
|
||||
checkpoint.assists += 1;
|
||||
}
|
||||
}
|
||||
checkpoints[key] = checkpoint;
|
||||
}
|
||||
if (checkpoints.at10 || checkpoints.at15) {
|
||||
checkpointsByParticipantId.set(participantId, checkpoints);
|
||||
}
|
||||
}
|
||||
|
||||
return checkpointsByParticipantId;
|
||||
}
|
||||
|
||||
private toTimeline(
|
||||
raw: RiotMatchDto,
|
||||
rawTeamIdBySide: Map<TeamSide, number>,
|
||||
@@ -511,65 +627,124 @@ export class MatchStatsFragmentBuilder {
|
||||
return sideByParticipantId;
|
||||
}
|
||||
|
||||
private toObjectiveEvents(
|
||||
private toTimelineEvents(
|
||||
timeline: RiotMatchTimelineDto | undefined,
|
||||
rawTeamIdBySide: Map<TeamSide, number>,
|
||||
sideByParticipantId: Map<number, TeamSide>,
|
||||
): MatchStatsObjectiveEvent[] {
|
||||
if (!timeline?.info.frames.length) return [];
|
||||
): TimelineProjection {
|
||||
if (!timeline?.info.frames.length) {
|
||||
return { objectiveEvents: [], timelineEvents: [] };
|
||||
}
|
||||
|
||||
const sideByTeamId = new Map<number, TeamSide>();
|
||||
for (const [side, teamId] of rawTeamIdBySide.entries()) {
|
||||
sideByTeamId.set(teamId, side);
|
||||
}
|
||||
|
||||
const objectiveEvents: MatchStatsObjectiveEvent[] = [];
|
||||
const projected: Array<{
|
||||
sourceSequence: number;
|
||||
event: MatchStatsTimelineEvent;
|
||||
}> = [];
|
||||
let sourceSequence = 0;
|
||||
for (const frame of timeline.info.frames) {
|
||||
for (const event of frame.events ?? []) {
|
||||
if (event.type !== 'ELITE_MONSTER_KILL') continue;
|
||||
const side =
|
||||
(typeof event.killerTeamId === 'number'
|
||||
? sideByTeamId.get(event.killerTeamId)
|
||||
: undefined) ??
|
||||
(typeof event.killerId === 'number'
|
||||
? sideByParticipantId.get(event.killerId)
|
||||
: undefined);
|
||||
if (!side) continue;
|
||||
const currentSequence = sourceSequence;
|
||||
sourceSequence += 1;
|
||||
const timestamp = Math.max(0, Math.round(event.timestamp / 1000));
|
||||
switch (event.monsterType) {
|
||||
case 'BARON_NASHOR':
|
||||
objectiveEvents.push({ timestamp, side, type: 'baron' });
|
||||
break;
|
||||
case 'RIFTHERALD':
|
||||
objectiveEvents.push({ timestamp, side, type: 'riftHerald' });
|
||||
break;
|
||||
case 'HORDE':
|
||||
objectiveEvents.push({ timestamp, side, type: 'voidGrub' });
|
||||
break;
|
||||
case 'DRAGON':
|
||||
if (event.monsterSubType === 'ELDER_DRAGON') {
|
||||
objectiveEvents.push({
|
||||
timestamp,
|
||||
side,
|
||||
type: 'elderDragon',
|
||||
monsterSubType: event.monsterSubType,
|
||||
});
|
||||
} else {
|
||||
objectiveEvents.push({
|
||||
timestamp,
|
||||
side,
|
||||
type: 'dragon',
|
||||
monsterSubType: event.monsterSubType,
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (event.type === 'ELITE_MONSTER_KILL') {
|
||||
const side =
|
||||
(typeof event.killerTeamId === 'number'
|
||||
? sideByTeamId.get(event.killerTeamId)
|
||||
: undefined) ??
|
||||
(typeof event.killerId === 'number'
|
||||
? sideByParticipantId.get(event.killerId)
|
||||
: undefined);
|
||||
if (!side) continue;
|
||||
const objectiveEvent = toObjectiveTimelineEvent({
|
||||
timestamp,
|
||||
side,
|
||||
monsterType: event.monsterType,
|
||||
monsterSubType: event.monsterSubType,
|
||||
});
|
||||
if (objectiveEvent) {
|
||||
projected.push({
|
||||
sourceSequence: currentSequence,
|
||||
event: objectiveEvent,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.type === 'CHAMPION_KILL') {
|
||||
const side =
|
||||
typeof event.killerId === 'number' && event.killerId > 0
|
||||
? sideByParticipantId.get(event.killerId)
|
||||
: undefined;
|
||||
projected.push({
|
||||
sourceSequence: currentSequence,
|
||||
event: {
|
||||
timestamp,
|
||||
type: 'championKill',
|
||||
...(side ? { side } : {}),
|
||||
...(typeof event.killerId === 'number' && event.killerId > 0
|
||||
? { killerParticipantId: event.killerId }
|
||||
: {}),
|
||||
...(typeof event.victimId === 'number' && event.victimId > 0
|
||||
? { victimParticipantId: event.victimId }
|
||||
: {}),
|
||||
...(event.assistingParticipantIds
|
||||
? { assistingParticipantIds: event.assistingParticipantIds }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.type !== 'BUILDING_KILL') continue;
|
||||
const destroyedSide =
|
||||
typeof event.teamId === 'number'
|
||||
? sideByTeamId.get(event.teamId)
|
||||
: undefined;
|
||||
const side =
|
||||
(typeof event.killerId === 'number' && event.killerId > 0
|
||||
? sideByParticipantId.get(event.killerId)
|
||||
: undefined) ??
|
||||
(destroyedSide ? oppositeSide(destroyedSide) : undefined);
|
||||
const type =
|
||||
event.buildingType === 'TOWER_BUILDING'
|
||||
? 'tower'
|
||||
: event.buildingType === 'INHIBITOR_BUILDING'
|
||||
? 'inhibitor'
|
||||
: undefined;
|
||||
if (!side || !type) continue;
|
||||
projected.push({
|
||||
sourceSequence: currentSequence,
|
||||
event: {
|
||||
timestamp,
|
||||
side,
|
||||
type,
|
||||
...(typeof event.killerId === 'number' && event.killerId > 0
|
||||
? { killerParticipantId: event.killerId }
|
||||
: {}),
|
||||
...(type === 'tower' && event.towerType
|
||||
? { towerType: event.towerType }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return objectiveEvents.sort((left, right) => left.timestamp - right.timestamp);
|
||||
const timelineEvents = projected
|
||||
.sort(
|
||||
(left, right) =>
|
||||
left.event.timestamp - right.event.timestamp ||
|
||||
left.sourceSequence - right.sourceSequence,
|
||||
)
|
||||
.map(({ event }) => event);
|
||||
return {
|
||||
timelineEvents,
|
||||
objectiveEvents: timelineEvents.filter(isObjectiveTimelineEvent),
|
||||
};
|
||||
}
|
||||
|
||||
private toTimelineObjectives(
|
||||
@@ -608,29 +783,29 @@ export class MatchStatsFragmentBuilder {
|
||||
}
|
||||
|
||||
private toObjectiveSummary(
|
||||
rawTeam:
|
||||
| RiotMatchDto['info']['teams'][number]
|
||||
| undefined,
|
||||
rawTeam: RiotMatchDto['info']['teams'][number] | undefined,
|
||||
timelineObjectives: MatchStatsObjectiveSummary | undefined,
|
||||
hasTimeline: boolean,
|
||||
): MatchStatsObjectiveSummary {
|
||||
const rawObjectives = rawTeam?.objectives;
|
||||
const timelineDragon = timelineObjectives?.dragon ?? 0;
|
||||
const timelineElder = timelineObjectives?.elderDragon ?? 0;
|
||||
const hasProjectedDragon = timelineDragon + timelineElder > 0;
|
||||
return {
|
||||
tower: rawObjectives?.tower?.kills ?? 0,
|
||||
dragon: hasTimeline
|
||||
? (timelineObjectives?.dragon ?? 0)
|
||||
dragon: hasProjectedDragon
|
||||
? timelineDragon
|
||||
: (rawObjectives?.dragon?.kills ?? 0),
|
||||
inhibitor: rawObjectives?.inhibitor?.kills ?? 0,
|
||||
baron: hasTimeline
|
||||
baron: (timelineObjectives?.baron ?? 0) > 0
|
||||
? (timelineObjectives?.baron ?? 0)
|
||||
: (rawObjectives?.baron?.kills ?? 0),
|
||||
riftHerald: hasTimeline
|
||||
riftHerald: (timelineObjectives?.riftHerald ?? 0) > 0
|
||||
? (timelineObjectives?.riftHerald ?? 0)
|
||||
: (rawObjectives?.riftHerald?.kills ?? 0),
|
||||
voidGrub: hasTimeline
|
||||
voidGrub: (timelineObjectives?.voidGrub ?? 0) > 0
|
||||
? (timelineObjectives?.voidGrub ?? 0)
|
||||
: (rawObjectives?.horde?.kills ?? 0),
|
||||
elderDragon: timelineObjectives?.elderDragon ?? 0,
|
||||
elderDragon: hasProjectedDragon ? timelineElder : 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -687,6 +862,79 @@ function toItemBuildType(
|
||||
}
|
||||
}
|
||||
|
||||
function toObjectiveTimelineEvent(params: {
|
||||
timestamp: number;
|
||||
side: TeamSide;
|
||||
monsterType?: string;
|
||||
monsterSubType?: string;
|
||||
}): MatchStatsObjectiveEvent | null {
|
||||
switch (params.monsterType) {
|
||||
case 'BARON_NASHOR':
|
||||
return { timestamp: params.timestamp, side: params.side, type: 'baron' };
|
||||
case 'RIFTHERALD':
|
||||
return {
|
||||
timestamp: params.timestamp,
|
||||
side: params.side,
|
||||
type: 'riftHerald',
|
||||
};
|
||||
case 'HORDE':
|
||||
return {
|
||||
timestamp: params.timestamp,
|
||||
side: params.side,
|
||||
type: 'voidGrub',
|
||||
};
|
||||
case 'DRAGON':
|
||||
return {
|
||||
timestamp: params.timestamp,
|
||||
side: params.side,
|
||||
type:
|
||||
params.monsterSubType === 'ELDER_DRAGON' ? 'elderDragon' : 'dragon',
|
||||
monsterSubType: params.monsterSubType,
|
||||
};
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isObjectiveTimelineEvent(
|
||||
event: MatchStatsTimelineEvent,
|
||||
): event is MatchStatsObjectiveEvent {
|
||||
return (
|
||||
event.type === 'dragon' ||
|
||||
event.type === 'elderDragon' ||
|
||||
event.type === 'baron' ||
|
||||
event.type === 'riftHerald' ||
|
||||
event.type === 'voidGrub'
|
||||
);
|
||||
}
|
||||
|
||||
function oppositeSide(side: TeamSide): TeamSide {
|
||||
return side === 'blue' ? 'red' : 'blue';
|
||||
}
|
||||
|
||||
function isUsableTimeline(
|
||||
timeline: RiotMatchTimelineDto | undefined,
|
||||
gameDurationSeconds: number,
|
||||
): timeline is RiotMatchTimelineDto {
|
||||
if (
|
||||
!timeline ||
|
||||
!Number.isFinite(timeline.info.frameInterval) ||
|
||||
timeline.info.frameInterval <= 0 ||
|
||||
!Array.isArray(timeline.info.frames) ||
|
||||
timeline.info.frames.length === 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const finalTimestamp = Math.max(
|
||||
...timeline.info.frames.map((frame) => frame.timestamp),
|
||||
);
|
||||
return (
|
||||
Number.isFinite(finalTimestamp) &&
|
||||
Math.abs(gameDurationSeconds * 1_000 - finalTimestamp) <
|
||||
timeline.info.frameInterval
|
||||
);
|
||||
}
|
||||
|
||||
function round(value: number): number {
|
||||
return Math.round(value * 10) / 10;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user