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
@@ -1,21 +1,33 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import type { MatchDocument } from '../global/database/schema';
|
||||
import type { RiotMatchDto } from '../riot/riot.types';
|
||||
import { RiotMatchService } from '../riot/riot-match.service';
|
||||
import {
|
||||
RIOT_REGIONAL_ROUTES,
|
||||
type RiotMatchDto,
|
||||
type RiotMatchTimelineDto,
|
||||
type RiotRegionalRoute,
|
||||
} from '../riot/riot.types';
|
||||
import { MatchStatsFragmentBuilder } from './match-stats-fragment.builder';
|
||||
import { MatchStatsRepository } from './match-stats.repository';
|
||||
import { MATCH_STATS_BUILDER_VERSION } from './match-stats.types';
|
||||
|
||||
@Injectable()
|
||||
export class MatchStatsService {
|
||||
private readonly logger = new Logger(MatchStatsService.name);
|
||||
|
||||
constructor(
|
||||
private readonly connectionService: ConnectionService,
|
||||
private readonly repository: MatchStatsRepository,
|
||||
private readonly builder: MatchStatsFragmentBuilder,
|
||||
private readonly riotMatchService: RiotMatchService,
|
||||
) {}
|
||||
|
||||
public async buildAndUpsert(matchId: mongoose.Types.ObjectId): Promise<void> {
|
||||
public async buildAndUpsert(
|
||||
matchId: mongoose.Types.ObjectId,
|
||||
options: { includeTimeline?: boolean } = {},
|
||||
): Promise<void> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const RiotMatchModel = await this.connectionService.getModel('riotMatch');
|
||||
const match = await MatchModel.findById(matchId).exec();
|
||||
@@ -41,7 +53,51 @@ export class MatchStatsService {
|
||||
const rawByRiotMatchId = new Map(
|
||||
riotDocs.map((doc) => [doc.matchId, doc.raw as unknown as RiotMatchDto]),
|
||||
);
|
||||
const fragment = this.builder.buildFragment({ match, rawByRiotMatchId });
|
||||
const timelineByRiotMatchId = new Map<string, RiotMatchTimelineDto>();
|
||||
for (const doc of riotDocs) {
|
||||
if (isRiotMatchTimelineDto(doc.timeline)) {
|
||||
timelineByRiotMatchId.set(doc.matchId, doc.timeline);
|
||||
}
|
||||
}
|
||||
if (
|
||||
options.includeTimeline &&
|
||||
riotDocs.length > 0 &&
|
||||
timelineByRiotMatchId.size < riotDocs.length
|
||||
) {
|
||||
const region = await this.resolveRiotRegion(match.communityId);
|
||||
for (const doc of riotDocs) {
|
||||
if (timelineByRiotMatchId.has(doc.matchId)) continue;
|
||||
try {
|
||||
const timeline = await this.riotMatchService.getMatchTimeline(
|
||||
doc.matchId,
|
||||
{ region },
|
||||
);
|
||||
await RiotMatchModel.updateOne(
|
||||
{ matchId: doc.matchId },
|
||||
{
|
||||
$set: {
|
||||
timeline: timeline as unknown as Record<string, unknown>,
|
||||
timelineFetchedAt: new Date(),
|
||||
},
|
||||
},
|
||||
).exec();
|
||||
timelineByRiotMatchId.set(doc.matchId, timeline);
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`Failed to fetch match timeline for ${doc.matchId}: ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fragment = this.builder.buildFragment({
|
||||
match,
|
||||
rawByRiotMatchId,
|
||||
timelineByRiotMatchId,
|
||||
timelineLoadAttempted: options.includeTimeline === true,
|
||||
});
|
||||
|
||||
await this.repository.upsert({
|
||||
matchId: match._id,
|
||||
@@ -101,4 +157,29 @@ export class MatchStatsService {
|
||||
Boolean(match.result?.games?.length)
|
||||
);
|
||||
}
|
||||
|
||||
private async resolveRiotRegion(
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
): Promise<RiotRegionalRoute> {
|
||||
const CommunityModel = await this.connectionService.getModel('community');
|
||||
const community = await CommunityModel.findById(communityId)
|
||||
.select({ riotRegion: 1 })
|
||||
.lean()
|
||||
.exec();
|
||||
const region = community?.riotRegion;
|
||||
return RIOT_REGIONAL_ROUTES.includes(region as RiotRegionalRoute)
|
||||
? (region as RiotRegionalRoute)
|
||||
: 'asia';
|
||||
}
|
||||
}
|
||||
|
||||
function isRiotMatchTimelineDto(
|
||||
value: unknown,
|
||||
): value is RiotMatchTimelineDto {
|
||||
if (!isRecord(value) || !isRecord(value.info)) return false;
|
||||
return Array.isArray(value.info.frames);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null;
|
||||
}
|
||||
Reference in new issue
Block a user