feat(api): add tournament result mapping

This commit is contained in:
dahyeon committed 2026-04-22 22:21:00 +09:00
1 parent 862d662a51
commit 06c190a41f
4 files changed
+354

No files matched your search

@@ -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();
});
});