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
@@ -40,7 +40,8 @@ const DEFAULT_LIST_LIMIT = 20;
|
||||
const MAX_LIST_LIMIT = 50;
|
||||
const MAX_MATCH_SCAN = 300;
|
||||
const MIN_HIGHLIGHT_GAMES = 5;
|
||||
const MATCH_DETAIL_RESPONSE_CACHE_VERSION = 3;
|
||||
const MATCH_DETAIL_RESPONSE_CACHE_VERSION = 4;
|
||||
const INCOMPLETE_MATCH_DETAIL_TTL_SECONDS = 60;
|
||||
|
||||
interface CommunityStatsContext {
|
||||
community: CommunityDocument;
|
||||
@@ -59,6 +60,10 @@ interface BaseDataset {
|
||||
@Injectable()
|
||||
export class CommunityStatsService {
|
||||
private readonly logger = new Logger(CommunityStatsService.name);
|
||||
private readonly matchDetailInFlight = new Map<
|
||||
string,
|
||||
Promise<InternalMatchDetailResponse>
|
||||
>();
|
||||
private readonly userInternalHistoryInFlight = new Map<
|
||||
string,
|
||||
Promise<UserInternalHistoryResponse>
|
||||
@@ -219,6 +224,27 @@ export class CommunityStatsService {
|
||||
await this.cacheService.get<InternalMatchDetailResponse>(key);
|
||||
if (cached && isCompleteMatchDetailResponse(cached)) return cached;
|
||||
|
||||
const inFlight = this.matchDetailInFlight.get(key);
|
||||
if (inFlight) return inFlight;
|
||||
|
||||
const promise = this.buildMatchDetailResponse(params, key);
|
||||
this.matchDetailInFlight.set(key, promise);
|
||||
try {
|
||||
return await promise;
|
||||
} finally {
|
||||
if (this.matchDetailInFlight.get(key) === promise) {
|
||||
this.matchDetailInFlight.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async buildMatchDetailResponse(
|
||||
params: {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
},
|
||||
key: string,
|
||||
): Promise<InternalMatchDetailResponse> {
|
||||
const dataset = await this.buildBaseDataset(params.communityId, {
|
||||
seasonId: 'all',
|
||||
});
|
||||
@@ -246,6 +272,7 @@ export class CommunityStatsService {
|
||||
gameVersion: game.gameVersion,
|
||||
timeline: game.timeline ?? [],
|
||||
objectiveEvents: game.objectiveEvents ?? [],
|
||||
timelineEvents: game.timelineEvents ?? game.objectiveEvents ?? [],
|
||||
teams: game.teams.map((team) => ({
|
||||
teamIndex: team.teamIndex,
|
||||
side: team.side,
|
||||
@@ -253,6 +280,8 @@ export class CommunityStatsService {
|
||||
objectives: team.objectives,
|
||||
bans: team.bans ?? [],
|
||||
participants: team.participants.map((record) => ({
|
||||
participantId: record.participantId,
|
||||
checkpoints: record.checkpoints,
|
||||
userId: record.userId,
|
||||
summonerName: record.summonerName,
|
||||
tagLine: record.tagLine,
|
||||
@@ -289,7 +318,15 @@ export class CommunityStatsService {
|
||||
})),
|
||||
})),
|
||||
};
|
||||
await this.cacheService.setDetail(key, response);
|
||||
if (gameDetails.every((game) => game.timelineLoaded === true)) {
|
||||
await this.cacheService.setDetail(key, response);
|
||||
} else {
|
||||
await this.cacheService.set(
|
||||
key,
|
||||
response,
|
||||
INCOMPLETE_MATCH_DETAIL_TTL_SECONDS,
|
||||
);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -1160,6 +1197,7 @@ function isCompleteMatchDetailResponse(
|
||||
(game) =>
|
||||
Array.isArray(game.timeline) &&
|
||||
Array.isArray(game.objectiveEvents) &&
|
||||
Array.isArray(game.timelineEvents) &&
|
||||
game.teams.every(
|
||||
(team) =>
|
||||
Array.isArray(team.bans) &&
|
||||
@@ -1167,8 +1205,41 @@ function isCompleteMatchDetailResponse(
|
||||
(participant) =>
|
||||
typeof participant.tagLine === 'string' &&
|
||||
Array.isArray(participant.itemBuild) &&
|
||||
Array.isArray(participant.skillBuild),
|
||||
Array.isArray(participant.skillBuild) &&
|
||||
isOptionalFiniteNumber(participant.participantId) &&
|
||||
isCompleteParticipantCheckpoints(participant.checkpoints),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function isCompleteParticipantCheckpoints(value: unknown): boolean {
|
||||
if (value === undefined) return true;
|
||||
if (!isRecord(value)) return false;
|
||||
return ['at10', 'at15'].every((key) => {
|
||||
const checkpoint = value[key];
|
||||
if (checkpoint === undefined) return true;
|
||||
if (!isRecord(checkpoint)) return false;
|
||||
return [
|
||||
'timestamp',
|
||||
'gold',
|
||||
'cs',
|
||||
'xp',
|
||||
'kills',
|
||||
'deaths',
|
||||
'assists',
|
||||
].every((field) => isFiniteNumber(checkpoint[field]));
|
||||
});
|
||||
}
|
||||
|
||||
function isOptionalFiniteNumber(value: unknown): boolean {
|
||||
return value === undefined || isFiniteNumber(value);
|
||||
}
|
||||
|
||||
function isFiniteNumber(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isFinite(value);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
Reference in new issue
Block a user