import mongoose from 'mongoose'; import type { MatchGameResult, MatchParticipant, MatchTeam, Position, RiotAccount, UserDocument, } from '../global/database/schema'; import type { RiotMatchDto, RiotMatchParticipantDto } from '../riot/riot.types'; const RIOT_BLUE_TEAM_ID = 100; const RIOT_RED_TEAM_ID = 200; export interface LegacyUserSnapshot { userId: mongoose.Types.ObjectId; account: RiotAccount; displayName: string; preferredPositions: UserDocument['preferredPositions']; } export type UserByPuuidMap = Map; export function buildUserByPuuidMap(users: UserDocument[]): UserByPuuidMap { const entries = new Map(); for (const user of users) { for (const account of user.riotAccounts) { entries.set(account.puuid, { userId: user._id, account, displayName: user.kakaoTalkNickname ?? user.discordNickname ?? account.gameName ?? user._id.toString(), preferredPositions: user.preferredPositions ?? [], }); } } return entries; } export function countKnownParticipants( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, ): { team100: number; team200: number; total: number } { const team100 = detail.info.participants.filter( (participant) => participant.teamId === RIOT_BLUE_TEAM_ID && userByPuuid.has(participant.puuid), ).length; const team200 = detail.info.participants.filter( (participant) => participant.teamId === RIOT_RED_TEAM_ID && userByPuuid.has(participant.puuid), ).length; return { team100, team200, total: team100 + team200 }; } export function resolveTeam100IsBlue( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, ): boolean { const counts = countKnownParticipants(detail, userByPuuid); return counts.team100 >= counts.team200; } export function buildParticipantsSnapshot( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, ): MatchParticipant[] { return detail.info.participants .map((participant): MatchParticipant | null => { const user = userByPuuid.get(participant.puuid); if (!user) return null; return { userId: user.userId, puuid: user.account.puuid, gameName: user.account.gameName || participant.riotIdGameName, tagLine: user.account.tagLine || participant.riotIdTagLine, profileIconId: user.account.profileIconId, tier: user.account.tiers.find( (tier) => tier.queueType === 'RANKED_SOLO_5x5', ) ?? user.account.tiers[0], preferredPositions: user.preferredPositions, joinedAt: new Date(detail.info.gameCreation), }; }) .filter((participant): participant is MatchParticipant => Boolean(participant), ); } export function buildTeams( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, ): MatchTeam[] { return [ { teamIndex: 1, teamId: RIOT_BLUE_TEAM_ID }, { teamIndex: 2, teamId: RIOT_RED_TEAM_ID }, ].map(({ teamIndex, teamId }) => { const members = detail.info.participants .filter((participant) => participant.teamId === teamId) .map((participant) => { const user = userByPuuid.get(participant.puuid); if (!user) return null; return { userId: user.userId, assignedPosition: resolveAssignedPosition(participant, user), }; }) .filter((member): member is NonNullable => Boolean(member), ); return { teamIndex, name: `${teamIndex}팀`, members, }; }); } export function buildGameResult( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, gameNumber = 1, ): MatchGameResult | null { const winningTeam = detail.info.teams.find((team) => team.win); if (!winningTeam) return null; const winnerSide = resolveSide(winningTeam.teamId); if (!winnerSide) return null; const winnerTeamIndex = winnerSide === 'blue' ? 1 : 2; const participants = detail.info.participants .map((participant) => { const user = userByPuuid.get(participant.puuid); const side = resolveSide(participant.teamId); if (!user || !side) return null; return { userId: user.userId, teamIndex: side === 'blue' ? 1 : 2, side, championId: participant.championId, kills: participant.kills, deaths: participant.deaths, assists: participant.assists, win: participant.win, }; }) .filter((participant): participant is NonNullable => Boolean(participant), ); return { gameNumber, riotMatchId: detail.metadata.matchId, blueTeamIndex: 1, redTeamIndex: 2, winnerTeamIndex, winnerSide, gameCreation: detail.info.gameCreation, gameDuration: detail.info.gameDuration, participants, }; } export function resolveExternalCount( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, ): number { return detail.info.participants.filter( (participant) => !userByPuuid.has(participant.puuid), ).length; } function resolveSide(teamId: number): 'blue' | 'red' | null { if (teamId === RIOT_BLUE_TEAM_ID) return 'blue'; if (teamId === RIOT_RED_TEAM_ID) return 'red'; return null; } function resolveAssignedPosition( participant: RiotMatchParticipantDto, user: LegacyUserSnapshot, ): Position { const lane = participant.teamPosition || participant.individualPosition; if (lane === 'TOP') return 'TOP'; if (lane === 'JUNGLE') return 'JGL'; if (lane === 'MIDDLE') return 'MID'; if (lane === 'BOTTOM') return 'ADC'; if (lane === 'UTILITY') return 'SUP'; return user.preferredPositions[0]?.position ?? 'TOP'; }