feat: add match game detail analytics
This commit is contained in:
1 parent
c6da2013bd
commit
7b5604873e
18 files changed
+3452
-46
No files matched your search
@@ -3,9 +3,18 @@ import mongoose from 'mongoose';
|
||||
import type {
|
||||
MatchDocument,
|
||||
MatchGameResultParticipant,
|
||||
MatchStatsItemBuildEvent,
|
||||
MatchStatsObjectiveEvent,
|
||||
MatchStatsObjectiveSummary,
|
||||
MatchStatsSkillBuildEvent,
|
||||
MatchStatsTimelinePoint,
|
||||
TeamSide,
|
||||
} from '../global/database/schema';
|
||||
import type { RiotMatchDto, RiotMatchParticipantDto } from '../riot/riot.types';
|
||||
import type {
|
||||
RiotMatchDto,
|
||||
RiotMatchParticipantDto,
|
||||
RiotMatchTimelineDto,
|
||||
} from '../riot/riot.types';
|
||||
import type { InternalMatchCardView } from './community-stats.types';
|
||||
import type {
|
||||
InternalGameDetail,
|
||||
@@ -13,11 +22,18 @@ import type {
|
||||
MatchStatsFragment,
|
||||
} from './match-stats.types';
|
||||
|
||||
interface ParticipantBuildEvents {
|
||||
items: MatchStatsItemBuildEvent[];
|
||||
skills: MatchStatsSkillBuildEvent[];
|
||||
}
|
||||
|
||||
@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,
|
||||
@@ -25,8 +41,15 @@ export class MatchStatsFragmentBuilder {
|
||||
const matchCard = this.toMatchCard(params.match, games);
|
||||
const gameDetails = games.map((game) => {
|
||||
const raw = params.rawByRiotMatchId.get(game.riotMatchId);
|
||||
const timeline = params.timelineByRiotMatchId?.get(game.riotMatchId);
|
||||
return raw
|
||||
? this.toGameDetail(params.match, game, raw)
|
||||
? this.toGameDetail(
|
||||
params.match,
|
||||
game,
|
||||
raw,
|
||||
timeline,
|
||||
params.timelineLoadAttempted === true,
|
||||
)
|
||||
: this.toFallbackGameDetail(params.match, game);
|
||||
});
|
||||
const records = gameDetails.flatMap((detail) =>
|
||||
@@ -106,6 +129,8 @@ export class MatchStatsFragmentBuilder {
|
||||
match: MatchDocument,
|
||||
game: NonNullable<MatchDocument['result']>['games'][number],
|
||||
raw: RiotMatchDto,
|
||||
timeline?: RiotMatchTimelineDto,
|
||||
timelineLoadAttempted = false,
|
||||
): InternalGameDetail {
|
||||
const puuidByUserId = new Map(
|
||||
match.participants
|
||||
@@ -129,13 +154,29 @@ export class MatchStatsFragmentBuilder {
|
||||
rawTeamIdBySide.set(participant.side, rawParticipant.teamId);
|
||||
}
|
||||
}
|
||||
const sideByParticipantId = this.toSideByParticipantId(
|
||||
raw,
|
||||
rawTeamIdBySide,
|
||||
);
|
||||
const objectiveEvents = this.toObjectiveEvents(
|
||||
timeline,
|
||||
rawTeamIdBySide,
|
||||
sideByParticipantId,
|
||||
);
|
||||
const timelineObjectives = this.toTimelineObjectives(objectiveEvents);
|
||||
const buildEventsByParticipantId =
|
||||
this.toBuildEventsByParticipantId(timeline);
|
||||
|
||||
return {
|
||||
gameNumber: game.gameNumber,
|
||||
riotMatchId: game.riotMatchId,
|
||||
gameCreation: game.gameCreation,
|
||||
gameDuration: game.gameDuration,
|
||||
gameVersion: raw.info.gameVersion,
|
||||
winnerSide: game.winnerSide,
|
||||
timeline: this.toTimeline(raw, rawTeamIdBySide, timeline),
|
||||
timelineLoaded: timelineLoadAttempted || Boolean(timeline),
|
||||
objectiveEvents,
|
||||
teams: (['blue', 'red'] as TeamSide[]).map((side) => {
|
||||
const teamIndex =
|
||||
side === 'blue' ? game.blueTeamIndex : game.redTeamIndex;
|
||||
@@ -158,25 +199,35 @@ export class MatchStatsFragmentBuilder {
|
||||
teamIndex,
|
||||
side,
|
||||
win: game.winnerSide === side,
|
||||
objectives: {
|
||||
baron: rawTeam?.objectives?.baron?.kills ?? 0,
|
||||
dragon: rawTeam?.objectives?.dragon?.kills ?? 0,
|
||||
tower: rawTeam?.objectives?.tower?.kills ?? 0,
|
||||
},
|
||||
objectives: this.toObjectiveSummary(
|
||||
rawTeam,
|
||||
timelineObjectives.get(side),
|
||||
Boolean(timeline),
|
||||
),
|
||||
bans: (rawTeam?.bans ?? []).map((ban) => ({
|
||||
championId: ban.championId,
|
||||
pickTurn: ban.pickTurn,
|
||||
})),
|
||||
participants: game.participants
|
||||
.filter((participant) => participant.side === side)
|
||||
.map((participant) =>
|
||||
this.toRecord({
|
||||
.map((participant) => {
|
||||
const rawParticipant = rawByPuuid.get(
|
||||
puuidByUserId.get(participant.userId.toString()) ?? '',
|
||||
);
|
||||
return this.toRecord({
|
||||
match,
|
||||
game,
|
||||
participant,
|
||||
rawParticipant: rawByPuuid.get(
|
||||
puuidByUserId.get(participant.userId.toString()) ?? '',
|
||||
),
|
||||
rawParticipant,
|
||||
buildEvents: rawParticipant
|
||||
? buildEventsByParticipantId.get(
|
||||
rawParticipant.participantId,
|
||||
)
|
||||
: undefined,
|
||||
teamKills,
|
||||
teamDamage,
|
||||
}),
|
||||
),
|
||||
});
|
||||
}),
|
||||
};
|
||||
}),
|
||||
};
|
||||
@@ -192,11 +243,15 @@ export class MatchStatsFragmentBuilder {
|
||||
gameCreation: game.gameCreation,
|
||||
gameDuration: game.gameDuration,
|
||||
winnerSide: game.winnerSide,
|
||||
timeline: [],
|
||||
timelineLoaded: false,
|
||||
objectiveEvents: [],
|
||||
teams: (['blue', 'red'] as TeamSide[]).map((side) => ({
|
||||
teamIndex: side === 'blue' ? game.blueTeamIndex : game.redTeamIndex,
|
||||
side,
|
||||
win: game.winnerSide === side,
|
||||
objectives: { baron: 0, dragon: 0, tower: 0 },
|
||||
objectives: emptyObjectives(),
|
||||
bans: [],
|
||||
participants: game.participants
|
||||
.filter((participant) => participant.side === side)
|
||||
.map((participant) =>
|
||||
@@ -217,11 +272,19 @@ export class MatchStatsFragmentBuilder {
|
||||
game: NonNullable<MatchDocument['result']>['games'][number];
|
||||
participant: MatchGameResultParticipant;
|
||||
rawParticipant?: RiotMatchParticipantDto;
|
||||
buildEvents?: ParticipantBuildEvents;
|
||||
teamKills: number;
|
||||
teamDamage: number;
|
||||
}): InternalRecord {
|
||||
const { match, game, participant, rawParticipant, teamKills, teamDamage } =
|
||||
params;
|
||||
const {
|
||||
match,
|
||||
game,
|
||||
participant,
|
||||
rawParticipant,
|
||||
buildEvents,
|
||||
teamKills,
|
||||
teamDamage,
|
||||
} = params;
|
||||
const snapshot = match.participants.find((item) =>
|
||||
item.userId.equals(participant.userId),
|
||||
);
|
||||
@@ -290,6 +353,8 @@ export class MatchStatsFragmentBuilder {
|
||||
),
|
||||
wardsPlaced: rawParticipant?.wardsPlaced ?? 0,
|
||||
wardsKilled: rawParticipant?.wardsKilled ?? 0,
|
||||
firstBloodKill: booleanField(rawParticipant, 'firstBloodKill'),
|
||||
firstBloodAssist: booleanField(rawParticipant, 'firstBloodAssist'),
|
||||
items: [
|
||||
rawParticipant?.item0 ?? 0,
|
||||
rawParticipant?.item1 ?? 0,
|
||||
@@ -304,6 +369,8 @@ export class MatchStatsFragmentBuilder {
|
||||
rawParticipant?.summoner2Id ?? 0,
|
||||
],
|
||||
perks: rawParticipant ? this.resolvePerks(rawParticipant) : {},
|
||||
itemBuild: buildEvents?.items ?? [],
|
||||
skillBuild: buildEvents?.skills ?? [],
|
||||
rating,
|
||||
};
|
||||
}
|
||||
@@ -319,6 +386,250 @@ export class MatchStatsFragmentBuilder {
|
||||
primaryStyle: primary?.style,
|
||||
subStyle: sub?.style,
|
||||
keystone: primary?.selections[0]?.perk,
|
||||
selectedPerkIds: participant.perks.styles.flatMap((style) =>
|
||||
style.selections.map((selection) => selection.perk),
|
||||
),
|
||||
styles: participant.perks.styles.map((style) => ({
|
||||
styleId: style.style,
|
||||
perkIds: style.selections.map((selection) => selection.perk),
|
||||
})),
|
||||
statPerks: {
|
||||
offense: participant.perks.statPerks.offense,
|
||||
flex: participant.perks.statPerks.flex,
|
||||
defense: participant.perks.statPerks.defense,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private toBuildEventsByParticipantId(
|
||||
timeline?: RiotMatchTimelineDto,
|
||||
): Map<number, ParticipantBuildEvents> {
|
||||
const buildEventsByParticipantId = new Map<number, ParticipantBuildEvents>();
|
||||
if (!timeline?.info.frames.length) return buildEventsByParticipantId;
|
||||
|
||||
const getEvents = (participantId: number): ParticipantBuildEvents => {
|
||||
const current = buildEventsByParticipantId.get(participantId);
|
||||
if (current) return current;
|
||||
const created: ParticipantBuildEvents = { items: [], skills: [] };
|
||||
buildEventsByParticipantId.set(participantId, created);
|
||||
return created;
|
||||
};
|
||||
|
||||
for (const frame of timeline.info.frames) {
|
||||
for (const event of frame.events ?? []) {
|
||||
const participantId = event.participantId;
|
||||
if (!participantId) continue;
|
||||
const timestamp = Math.max(0, Math.round(event.timestamp / 1000));
|
||||
|
||||
if (event.type === 'SKILL_LEVEL_UP' && isSkillSlot(event.skillSlot)) {
|
||||
const skills = getEvents(participantId).skills;
|
||||
skills.push({
|
||||
timestamp,
|
||||
skillSlot: event.skillSlot,
|
||||
level: skills.length + 1,
|
||||
levelUpType: event.levelUpType,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const itemType = toItemBuildType(event.type);
|
||||
if (!itemType) continue;
|
||||
const itemId = event.itemId ?? event.afterId ?? event.beforeId ?? 0;
|
||||
if (itemId <= 0) continue;
|
||||
getEvents(participantId).items.push({
|
||||
timestamp,
|
||||
itemId,
|
||||
type: itemType,
|
||||
beforeId: event.beforeId,
|
||||
afterId: event.afterId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const events of buildEventsByParticipantId.values()) {
|
||||
events.items.sort((left, right) => left.timestamp - right.timestamp);
|
||||
events.skills.sort((left, right) => left.timestamp - right.timestamp);
|
||||
}
|
||||
|
||||
return buildEventsByParticipantId;
|
||||
}
|
||||
|
||||
private toTimeline(
|
||||
raw: RiotMatchDto,
|
||||
rawTeamIdBySide: Map<TeamSide, number>,
|
||||
timeline?: RiotMatchTimelineDto,
|
||||
): MatchStatsTimelinePoint[] {
|
||||
if (!timeline?.info.frames.length) return [];
|
||||
|
||||
const sideByParticipantId = this.toSideByParticipantId(
|
||||
raw,
|
||||
rawTeamIdBySide,
|
||||
);
|
||||
|
||||
const points = timeline.info.frames.map((frame) => {
|
||||
const point: MatchStatsTimelinePoint = {
|
||||
timestamp: Math.max(0, Math.round(frame.timestamp / 1000)),
|
||||
blue: { gold: 0, xp: 0 },
|
||||
red: { gold: 0, xp: 0 },
|
||||
goldDiff: 0,
|
||||
xpDiff: 0,
|
||||
};
|
||||
|
||||
for (const participantFrame of Object.values(frame.participantFrames)) {
|
||||
const side = sideByParticipantId.get(participantFrame.participantId);
|
||||
if (!side) continue;
|
||||
point[side].gold += participantFrame.totalGold;
|
||||
point[side].xp += participantFrame.xp;
|
||||
}
|
||||
point.goldDiff = point.blue.gold - point.red.gold;
|
||||
point.xpDiff = point.blue.xp - point.red.xp;
|
||||
return point;
|
||||
});
|
||||
|
||||
return points.filter(
|
||||
(point, index) =>
|
||||
index === 0 || point.timestamp !== points[index - 1].timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
private toSideByParticipantId(
|
||||
raw: RiotMatchDto,
|
||||
rawTeamIdBySide: Map<TeamSide, number>,
|
||||
): Map<number, TeamSide> {
|
||||
const blueTeamId = rawTeamIdBySide.get('blue') ?? 100;
|
||||
const redTeamId = rawTeamIdBySide.get('red') ?? 200;
|
||||
const sideByParticipantId = new Map<number, TeamSide>();
|
||||
for (const participant of raw.info.participants) {
|
||||
if (participant.teamId === blueTeamId) {
|
||||
sideByParticipantId.set(participant.participantId, 'blue');
|
||||
}
|
||||
if (participant.teamId === redTeamId) {
|
||||
sideByParticipantId.set(participant.participantId, 'red');
|
||||
}
|
||||
}
|
||||
return sideByParticipantId;
|
||||
}
|
||||
|
||||
private toObjectiveEvents(
|
||||
timeline: RiotMatchTimelineDto | undefined,
|
||||
rawTeamIdBySide: Map<TeamSide, number>,
|
||||
sideByParticipantId: Map<number, TeamSide>,
|
||||
): MatchStatsObjectiveEvent[] {
|
||||
if (!timeline?.info.frames.length) return [];
|
||||
|
||||
const sideByTeamId = new Map<number, TeamSide>();
|
||||
for (const [side, teamId] of rawTeamIdBySide.entries()) {
|
||||
sideByTeamId.set(teamId, side);
|
||||
}
|
||||
|
||||
const objectiveEvents: MatchStatsObjectiveEvent[] = [];
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return objectiveEvents.sort((left, right) => left.timestamp - right.timestamp);
|
||||
}
|
||||
|
||||
private toTimelineObjectives(
|
||||
objectiveEvents: MatchStatsObjectiveEvent[],
|
||||
): Map<TeamSide, MatchStatsObjectiveSummary> {
|
||||
const objectives = new Map<TeamSide, MatchStatsObjectiveSummary>([
|
||||
['blue', emptyObjectives()],
|
||||
['red', emptyObjectives()],
|
||||
]);
|
||||
|
||||
for (const event of objectiveEvents) {
|
||||
const current = objectives.get(event.side) ?? emptyObjectives();
|
||||
switch (event.type) {
|
||||
case 'baron':
|
||||
current.baron += 1;
|
||||
break;
|
||||
case 'riftHerald':
|
||||
current.riftHerald += 1;
|
||||
break;
|
||||
case 'voidGrub':
|
||||
current.voidGrub += 1;
|
||||
break;
|
||||
case 'dragon':
|
||||
current.dragon += 1;
|
||||
break;
|
||||
case 'elderDragon':
|
||||
current.elderDragon += 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
objectives.set(event.side, current);
|
||||
}
|
||||
|
||||
return objectives;
|
||||
}
|
||||
|
||||
private toObjectiveSummary(
|
||||
rawTeam:
|
||||
| RiotMatchDto['info']['teams'][number]
|
||||
| undefined,
|
||||
timelineObjectives: MatchStatsObjectiveSummary | undefined,
|
||||
hasTimeline: boolean,
|
||||
): MatchStatsObjectiveSummary {
|
||||
const rawObjectives = rawTeam?.objectives;
|
||||
return {
|
||||
tower: rawObjectives?.tower?.kills ?? 0,
|
||||
dragon: hasTimeline
|
||||
? (timelineObjectives?.dragon ?? 0)
|
||||
: (rawObjectives?.dragon?.kills ?? 0),
|
||||
inhibitor: rawObjectives?.inhibitor?.kills ?? 0,
|
||||
baron: hasTimeline
|
||||
? (timelineObjectives?.baron ?? 0)
|
||||
: (rawObjectives?.baron?.kills ?? 0),
|
||||
riftHerald: hasTimeline
|
||||
? (timelineObjectives?.riftHerald ?? 0)
|
||||
: (rawObjectives?.riftHerald?.kills ?? 0),
|
||||
voidGrub: hasTimeline
|
||||
? (timelineObjectives?.voidGrub ?? 0)
|
||||
: (rawObjectives?.horde?.kills ?? 0),
|
||||
elderDragon: timelineObjectives?.elderDragon ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -354,6 +665,27 @@ function average(value: number, total: number): number {
|
||||
return total > 0 ? round(value / total) : 0;
|
||||
}
|
||||
|
||||
function isSkillSlot(value: number | undefined): value is 1 | 2 | 3 | 4 {
|
||||
return value === 1 || value === 2 || value === 3 || value === 4;
|
||||
}
|
||||
|
||||
function toItemBuildType(
|
||||
value: string,
|
||||
): MatchStatsItemBuildEvent['type'] | null {
|
||||
switch (value) {
|
||||
case 'ITEM_PURCHASED':
|
||||
return 'purchased';
|
||||
case 'ITEM_SOLD':
|
||||
return 'sold';
|
||||
case 'ITEM_DESTROYED':
|
||||
return 'destroyed';
|
||||
case 'ITEM_UNDO':
|
||||
return 'undo';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function round(value: number): number {
|
||||
return Math.round(value * 10) / 10;
|
||||
}
|
||||
@@ -365,3 +697,22 @@ function numberField(
|
||||
const value = participant?.[key];
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
||||
}
|
||||
|
||||
function booleanField(
|
||||
participant: RiotMatchParticipantDto | undefined,
|
||||
key: string,
|
||||
): boolean {
|
||||
return participant?.[key] === true;
|
||||
}
|
||||
|
||||
function emptyObjectives(): MatchStatsObjectiveSummary {
|
||||
return {
|
||||
tower: 0,
|
||||
dragon: 0,
|
||||
inhibitor: 0,
|
||||
baron: 0,
|
||||
riftHerald: 0,
|
||||
voidGrub: 0,
|
||||
elderDragon: 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user