feat(api): add tournament result mapping
This commit is contained in:
1 parent
862d662a51
commit
06c190a41f
4 files changed
+354
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsDate, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
|
||||
export class CollectTournamentResultWindowDto {
|
||||
@IsDate()
|
||||
@Type(() => Date)
|
||||
startTime!: Date;
|
||||
|
||||
@IsDate()
|
||||
@Type(() => Date)
|
||||
endTime!: Date;
|
||||
}
|
||||
|
||||
export class CollectTournamentResultRiotMatchDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
@MaxLength(96)
|
||||
riotMatchId!: string;
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import mongoose from 'mongoose';
|
||||
import type { MatchDocument } from '../global/database/schema';
|
||||
import type { RiotMatchDto } from '../riot/riot.types';
|
||||
import { computeGameResult, type MatchPuuidLookup } from './team-mapping.util';
|
||||
|
||||
function makeMatch(): {
|
||||
match: MatchDocument;
|
||||
lookup: MatchPuuidLookup;
|
||||
blueUserIds: mongoose.Types.ObjectId[];
|
||||
redUserIds: mongoose.Types.ObjectId[];
|
||||
} {
|
||||
const blueUserIds = Array.from({ length: 5 }, () => new mongoose.Types.ObjectId());
|
||||
const redUserIds = Array.from({ length: 5 }, () => new mongoose.Types.ObjectId());
|
||||
const puuidByUserId = new Map<string, string>();
|
||||
const userIdByPuuid = new Map<string, mongoose.Types.ObjectId>();
|
||||
|
||||
for (const [index, userId] of [...blueUserIds, ...redUserIds].entries()) {
|
||||
const puuid = `puuid-${index}`;
|
||||
puuidByUserId.set(userId.toString(), puuid);
|
||||
userIdByPuuid.set(puuid, userId);
|
||||
}
|
||||
|
||||
const match = {
|
||||
teams: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
name: '1팀',
|
||||
members: blueUserIds.map((userId) => ({
|
||||
userId,
|
||||
assignedPosition: 'MID',
|
||||
})),
|
||||
},
|
||||
{
|
||||
teamIndex: 2,
|
||||
name: '2팀',
|
||||
members: redUserIds.map((userId) => ({
|
||||
userId,
|
||||
assignedPosition: 'MID',
|
||||
})),
|
||||
},
|
||||
],
|
||||
participants: [...blueUserIds, ...redUserIds].map((userId, index) => ({
|
||||
userId,
|
||||
puuid: `puuid-${index}`,
|
||||
preferredPositions: [],
|
||||
joinedAt: new Date(),
|
||||
})),
|
||||
} as unknown as MatchDocument;
|
||||
|
||||
return { match, lookup: { puuidByUserId, userIdByPuuid }, blueUserIds, redUserIds };
|
||||
}
|
||||
|
||||
function makeRiotMatch(params: {
|
||||
team100Puuids: string[];
|
||||
team200Puuids: string[];
|
||||
winningTeamId: 100 | 200;
|
||||
}): RiotMatchDto {
|
||||
const participants = [
|
||||
...params.team100Puuids.map((puuid, index) => ({
|
||||
puuid,
|
||||
participantId: index + 1,
|
||||
teamId: 100,
|
||||
championId: 10 + index,
|
||||
kills: index,
|
||||
deaths: 1,
|
||||
assists: 2,
|
||||
win: params.winningTeamId === 100,
|
||||
})),
|
||||
...params.team200Puuids.map((puuid, index) => ({
|
||||
puuid,
|
||||
participantId: index + 6,
|
||||
teamId: 200,
|
||||
championId: 20 + index,
|
||||
kills: index,
|
||||
deaths: 2,
|
||||
assists: 3,
|
||||
win: params.winningTeamId === 200,
|
||||
})),
|
||||
];
|
||||
|
||||
return {
|
||||
metadata: {
|
||||
dataVersion: '2',
|
||||
matchId: 'KR_1',
|
||||
participants: participants.map((participant) => participant.puuid),
|
||||
},
|
||||
info: {
|
||||
gameCreation: 1,
|
||||
gameDuration: 1800,
|
||||
participants,
|
||||
teams: [
|
||||
{ teamId: 100, win: params.winningTeamId === 100 },
|
||||
{ teamId: 200, win: params.winningTeamId === 200 },
|
||||
],
|
||||
},
|
||||
} as unknown as RiotMatchDto;
|
||||
}
|
||||
|
||||
describe('computeGameResult', () => {
|
||||
it('maps Riot teams to local blue/red by overlap', () => {
|
||||
const { match, lookup } = makeMatch();
|
||||
const detail = makeRiotMatch({
|
||||
team100Puuids: ['puuid-0', 'puuid-1', 'puuid-2', 'puuid-3', 'puuid-4'],
|
||||
team200Puuids: ['puuid-5', 'puuid-6', 'puuid-7', 'puuid-8', 'puuid-9'],
|
||||
winningTeamId: 100,
|
||||
});
|
||||
|
||||
const result = computeGameResult({ match, lookup, detail, gameNumber: 1 });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_1',
|
||||
winnerSide: 'blue',
|
||||
blueTeamIndex: 1,
|
||||
redTeamIndex: 2,
|
||||
winnerTeamIndex: 1,
|
||||
gameDuration: 1800,
|
||||
});
|
||||
expect(result?.participants).toHaveLength(10);
|
||||
expect(result?.participants[0]).toMatchObject({
|
||||
teamIndex: 1,
|
||||
side: 'blue',
|
||||
});
|
||||
});
|
||||
|
||||
it('handles reversed Riot side assignment', () => {
|
||||
const { match, lookup } = makeMatch();
|
||||
const detail = makeRiotMatch({
|
||||
team100Puuids: ['puuid-5', 'puuid-6', 'puuid-7', 'puuid-8', 'puuid-9'],
|
||||
team200Puuids: ['puuid-0', 'puuid-1', 'puuid-2', 'puuid-3', 'puuid-4'],
|
||||
winningTeamId: 100,
|
||||
});
|
||||
|
||||
const result = computeGameResult({ match, lookup, detail, gameNumber: 2 });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
blueTeamIndex: 2,
|
||||
redTeamIndex: 1,
|
||||
winnerSide: 'blue',
|
||||
winnerTeamIndex: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects matches with too little overlap', () => {
|
||||
const { match, lookup } = makeMatch();
|
||||
const detail = makeRiotMatch({
|
||||
team100Puuids: ['other-1', 'other-2', 'other-3', 'other-4', 'other-5'],
|
||||
team200Puuids: ['other-6', 'other-7', 'other-8', 'other-9', 'other-10'],
|
||||
winningTeamId: 100,
|
||||
});
|
||||
|
||||
const result = computeGameResult({ match, lookup, detail, gameNumber: 3 });
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
import mongoose from 'mongoose';
|
||||
import type { MatchDocument, MatchGameResult, TeamSide } from '../global/database/schema';
|
||||
import type { RiotMatchDto } from '../riot/riot.types';
|
||||
|
||||
const RIOT_BLUE_TEAM_ID = 100;
|
||||
const RIOT_RED_TEAM_ID = 200;
|
||||
const MINIMUM_TRUSTED_OVERLAP = 6;
|
||||
|
||||
export interface MatchPuuidLookup {
|
||||
puuidByUserId: Map<string, string>;
|
||||
userIdByPuuid: Map<string, mongoose.Types.ObjectId>;
|
||||
}
|
||||
|
||||
export function collectCaptainUserIds(match: MatchDocument): mongoose.Types.ObjectId[] {
|
||||
return match.teams
|
||||
.map((team) => team.captainUserId)
|
||||
.filter((userId): userId is mongoose.Types.ObjectId => Boolean(userId));
|
||||
}
|
||||
|
||||
export function collectMatchUserIds(match: MatchDocument): mongoose.Types.ObjectId[] {
|
||||
const ids = new Map<string, mongoose.Types.ObjectId>();
|
||||
for (const participant of match.participants) {
|
||||
ids.set(participant.userId.toString(), participant.userId);
|
||||
}
|
||||
for (const team of match.teams) {
|
||||
if (team.captainUserId) ids.set(team.captainUserId.toString(), team.captainUserId);
|
||||
for (const member of team.members) {
|
||||
ids.set(member.userId.toString(), member.userId);
|
||||
}
|
||||
}
|
||||
return [...ids.values()];
|
||||
}
|
||||
|
||||
export function collectTeamPuuids(
|
||||
match: MatchDocument,
|
||||
teamIndex: number,
|
||||
lookup: MatchPuuidLookup,
|
||||
): string[] {
|
||||
const team = match.teams.find((item) => item.teamIndex === teamIndex);
|
||||
if (!team) return [];
|
||||
const userIds = [
|
||||
...(team.captainUserId ? [team.captainUserId] : []),
|
||||
...team.members.map((member) => member.userId),
|
||||
];
|
||||
return userIds
|
||||
.map((userId) => lookup.puuidByUserId.get(userId.toString()))
|
||||
.filter((puuid): puuid is string => Boolean(puuid));
|
||||
}
|
||||
|
||||
export function computeGameResult(params: {
|
||||
match: MatchDocument;
|
||||
detail: RiotMatchDto;
|
||||
gameNumber: number;
|
||||
lookup: MatchPuuidLookup;
|
||||
teamAIndex?: number;
|
||||
teamBIndex?: number;
|
||||
}): MatchGameResult | null {
|
||||
const {
|
||||
match,
|
||||
detail,
|
||||
gameNumber,
|
||||
lookup,
|
||||
teamAIndex = 1,
|
||||
teamBIndex = 2,
|
||||
} = params;
|
||||
const teamAPuuids = collectTeamPuuids(match, teamAIndex, lookup);
|
||||
const teamBPuuids = collectTeamPuuids(match, teamBIndex, lookup);
|
||||
const team100Puuids = detail.info.participants
|
||||
.filter((participant) => participant.teamId === RIOT_BLUE_TEAM_ID)
|
||||
.map((participant) => participant.puuid);
|
||||
const team200Puuids = detail.info.participants
|
||||
.filter((participant) => participant.teamId === RIOT_RED_TEAM_ID)
|
||||
.map((participant) => participant.puuid);
|
||||
|
||||
const normalOverlap =
|
||||
countOverlap(team100Puuids, teamAPuuids) +
|
||||
countOverlap(team200Puuids, teamBPuuids);
|
||||
const reversedOverlap =
|
||||
countOverlap(team100Puuids, teamBPuuids) +
|
||||
countOverlap(team200Puuids, teamAPuuids);
|
||||
const blueTeamIndex = normalOverlap >= reversedOverlap ? teamAIndex : teamBIndex;
|
||||
const redTeamIndex = normalOverlap >= reversedOverlap ? teamBIndex : teamAIndex;
|
||||
const teamIndexByRiotTeamId: Record<number, number> = {
|
||||
[RIOT_BLUE_TEAM_ID]: blueTeamIndex,
|
||||
[RIOT_RED_TEAM_ID]: redTeamIndex,
|
||||
};
|
||||
const sideByRiotTeamId: Record<number, TeamSide> = {
|
||||
[RIOT_BLUE_TEAM_ID]: 'blue',
|
||||
[RIOT_RED_TEAM_ID]: 'red',
|
||||
};
|
||||
|
||||
if (Math.max(normalOverlap, reversedOverlap) < MINIMUM_TRUSTED_OVERLAP) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const winningTeam = detail.info.teams.find((team) => team.win);
|
||||
if (!winningTeam) return null;
|
||||
const winnerSide = sideByRiotTeamId[winningTeam.teamId];
|
||||
if (!winnerSide) return null;
|
||||
const winnerTeamIndex = teamIndexByRiotTeamId[winningTeam.teamId];
|
||||
if (!winnerTeamIndex) return null;
|
||||
|
||||
const participants = detail.info.participants
|
||||
.map((participant) => {
|
||||
const userId = lookup.userIdByPuuid.get(participant.puuid);
|
||||
const side = sideByRiotTeamId[participant.teamId];
|
||||
const teamIndex = teamIndexByRiotTeamId[participant.teamId];
|
||||
if (!userId || !side || !teamIndex) return null;
|
||||
return {
|
||||
userId,
|
||||
teamIndex,
|
||||
side,
|
||||
championId: participant.championId,
|
||||
kills: participant.kills,
|
||||
deaths: participant.deaths,
|
||||
assists: participant.assists,
|
||||
win: participant.win,
|
||||
};
|
||||
})
|
||||
.filter((participant): participant is NonNullable<typeof participant> =>
|
||||
Boolean(participant),
|
||||
);
|
||||
|
||||
return {
|
||||
gameNumber,
|
||||
riotMatchId: detail.metadata.matchId,
|
||||
blueTeamIndex,
|
||||
redTeamIndex,
|
||||
winnerTeamIndex,
|
||||
winnerSide,
|
||||
gameCreation: detail.info.gameCreation,
|
||||
gameDuration: detail.info.gameDuration,
|
||||
participants,
|
||||
};
|
||||
}
|
||||
|
||||
export function countOverlap(left: string[], right: string[]): number {
|
||||
const rightSet = new Set(right);
|
||||
return left.filter((item) => rightSet.has(item)).length;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { TeamSide } from '../global/database/schema';
|
||||
import type { RiotJobPriority } from '../riot-job-queue/riot-job-queue.types';
|
||||
|
||||
export interface PollTournamentResultJob {
|
||||
kind: 'poll-tournament-result';
|
||||
jobId: string;
|
||||
matchId: string;
|
||||
attempt: number;
|
||||
expandedToAllPuuids: boolean;
|
||||
enqueuedAt: number;
|
||||
priority: RiotJobPriority;
|
||||
}
|
||||
|
||||
export interface EnqueueTournamentResultPollResult {
|
||||
jobId: string;
|
||||
matchId: string;
|
||||
priority: RiotJobPriority;
|
||||
attempt: number;
|
||||
expandedToAllPuuids: boolean;
|
||||
}
|
||||
|
||||
export interface ManualTournamentResultCollectResponse {
|
||||
scanned: number;
|
||||
matched: number;
|
||||
skipped: number;
|
||||
riotMatchIds: string[];
|
||||
gameNumbers: number[];
|
||||
}
|
||||
|
||||
export interface ComputedGameParticipant {
|
||||
userId: string;
|
||||
teamIndex: number;
|
||||
side: TeamSide;
|
||||
championId: number;
|
||||
kills: number;
|
||||
deaths: number;
|
||||
assists: number;
|
||||
win: boolean;
|
||||
}
|
||||
Reference in new issue
Block a user