173 lines
4.9 KiB
TypeScript
173 lines
4.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import {
|
|
createEmptyRiotLaneStats,
|
|
createEmptyRiotRecentStatsSummary,
|
|
RIOT_LANES,
|
|
type RiotLane,
|
|
type RiotLaneStats,
|
|
type RiotRecentStatsSummary,
|
|
} from '../global/database/schema';
|
|
import type { RiotMatchDto, RiotMatchParticipantDto } from './riot.types';
|
|
|
|
interface Aggregate {
|
|
games: number;
|
|
wins: number;
|
|
losses: number;
|
|
kills: number;
|
|
deaths: number;
|
|
assists: number;
|
|
cs: number;
|
|
csPerMin: number;
|
|
championCounts: Map<number, number>;
|
|
lastPlayedAtByChampion: Map<number, number>;
|
|
}
|
|
|
|
interface ComputeSummaryOptions {
|
|
seasonStartedAt?: Date;
|
|
}
|
|
|
|
@Injectable()
|
|
export class RiotRecentStatsService {
|
|
public computeSummary(
|
|
matches: RiotMatchDto[],
|
|
puuid: string,
|
|
options: ComputeSummaryOptions = {},
|
|
): RiotRecentStatsSummary {
|
|
const filteredMatches = this.filterMatchesBySeason(matches, options);
|
|
if (filteredMatches.length === 0) {
|
|
return createEmptyRiotRecentStatsSummary();
|
|
}
|
|
|
|
const overall = createAggregate();
|
|
const laneAggregates = new Map<RiotLane, Aggregate>(
|
|
RIOT_LANES.map((lane) => [lane, createAggregate()]),
|
|
);
|
|
|
|
for (const match of filteredMatches) {
|
|
const participant = match.info.participants.find((entry) => entry.puuid === puuid);
|
|
if (!participant) continue;
|
|
|
|
accumulateAggregate(overall, participant, match.info.gameCreation, match.info.gameDuration);
|
|
|
|
const lane = resolveLane(participant);
|
|
if (!lane) continue;
|
|
|
|
const aggregate = laneAggregates.get(lane);
|
|
if (!aggregate) continue;
|
|
accumulateAggregate(aggregate, participant, match.info.gameCreation, match.info.gameDuration);
|
|
}
|
|
|
|
if (overall.games === 0) {
|
|
return createEmptyRiotRecentStatsSummary();
|
|
}
|
|
|
|
const byLane: Partial<Record<RiotLane, RiotLaneStats>> = {};
|
|
for (const lane of RIOT_LANES) {
|
|
const aggregate = laneAggregates.get(lane);
|
|
if (!aggregate || aggregate.games === 0) continue;
|
|
byLane[lane] = finalizeAggregate(aggregate);
|
|
}
|
|
|
|
return {
|
|
overall: finalizeAggregate(overall),
|
|
byLane,
|
|
};
|
|
}
|
|
|
|
private filterMatchesBySeason(
|
|
matches: RiotMatchDto[],
|
|
options: ComputeSummaryOptions,
|
|
): RiotMatchDto[] {
|
|
const startedAt = options.seasonStartedAt?.getTime();
|
|
if (!startedAt) return matches;
|
|
return matches.filter((match) => match.info.gameCreation >= startedAt);
|
|
}
|
|
}
|
|
|
|
function createAggregate(): Aggregate {
|
|
return {
|
|
games: 0,
|
|
wins: 0,
|
|
losses: 0,
|
|
kills: 0,
|
|
deaths: 0,
|
|
assists: 0,
|
|
cs: 0,
|
|
csPerMin: 0,
|
|
championCounts: new Map(),
|
|
lastPlayedAtByChampion: new Map(),
|
|
};
|
|
}
|
|
|
|
function accumulateAggregate(
|
|
aggregate: Aggregate,
|
|
participant: RiotMatchParticipantDto,
|
|
gameCreation: number,
|
|
gameDurationSeconds: number,
|
|
): void {
|
|
aggregate.games += 1;
|
|
aggregate.wins += participant.win ? 1 : 0;
|
|
aggregate.losses += participant.win ? 0 : 1;
|
|
aggregate.kills += participant.kills;
|
|
aggregate.deaths += participant.deaths;
|
|
aggregate.assists += participant.assists;
|
|
|
|
const cs = participant.totalMinionsKilled + participant.neutralMinionsKilled;
|
|
aggregate.cs += cs;
|
|
if (gameDurationSeconds > 0) {
|
|
aggregate.csPerMin += cs / (gameDurationSeconds / 60);
|
|
}
|
|
|
|
aggregate.championCounts.set(
|
|
participant.championId,
|
|
(aggregate.championCounts.get(participant.championId) ?? 0) + 1,
|
|
);
|
|
aggregate.lastPlayedAtByChampion.set(participant.championId, gameCreation);
|
|
}
|
|
|
|
function finalizeAggregate(aggregate: Aggregate): RiotLaneStats {
|
|
if (aggregate.games === 0) {
|
|
return createEmptyRiotLaneStats();
|
|
}
|
|
|
|
const mostPlayedChampionIds = [...aggregate.championCounts.entries()]
|
|
.sort((left, right) => {
|
|
if (right[1] !== left[1]) return right[1] - left[1];
|
|
const rightLastPlayed = aggregate.lastPlayedAtByChampion.get(right[0]) ?? 0;
|
|
const leftLastPlayed = aggregate.lastPlayedAtByChampion.get(left[0]) ?? 0;
|
|
if (rightLastPlayed !== leftLastPlayed) {
|
|
return rightLastPlayed - leftLastPlayed;
|
|
}
|
|
return left[0] - right[0];
|
|
})
|
|
.slice(0, 3)
|
|
.map(([championId]) => championId);
|
|
|
|
return {
|
|
games: aggregate.games,
|
|
wins: aggregate.wins,
|
|
losses: aggregate.losses,
|
|
avgKills: roundToOneDecimal(aggregate.kills / aggregate.games),
|
|
avgDeaths: roundToOneDecimal(aggregate.deaths / aggregate.games),
|
|
avgAssists: roundToOneDecimal(aggregate.assists / aggregate.games),
|
|
avgCs: roundToOneDecimal(aggregate.cs / aggregate.games),
|
|
avgCsPerMin: roundToOneDecimal(aggregate.csPerMin / aggregate.games),
|
|
mostPlayedChampionIds,
|
|
};
|
|
}
|
|
|
|
function resolveLane(participant: RiotMatchParticipantDto): RiotLane | null {
|
|
const candidates = [participant.teamPosition, participant.individualPosition];
|
|
for (const candidate of candidates) {
|
|
if (!candidate) continue;
|
|
if (RIOT_LANES.includes(candidate as RiotLane)) {
|
|
return candidate as RiotLane;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function roundToOneDecimal(value: number): number {
|
|
return Math.round(value * 10) / 10;
|
|
}
|