import mongoose from 'mongoose'; import type { MatchGameResult, MatchParticipant, MatchTeam, Position, RiotAccount, TeamSide, 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, team100IsBlue: boolean, ): MatchTeam[] { return (['blue', 'red'] as TeamSide[]).map((side) => { const teamId = resolveRiotTeamId(side, team100IsBlue); 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 { side, groupIndex: 1, name: side === 'blue' ? '블루팀' : '레드팀', members, }; }); } export function buildGameResult( detail: RiotMatchDto, userByPuuid: UserByPuuidMap, team100IsBlue: boolean, gameNumber = 1, ): MatchGameResult | null { const winningTeam = detail.info.teams.find((team) => team.win); if (!winningTeam) return null; const winnerSide = resolveSide(winningTeam.teamId, team100IsBlue); if (!winnerSide) return null; const participants = detail.info.participants .map((participant) => { const user = userByPuuid.get(participant.puuid); const side = resolveSide(participant.teamId, team100IsBlue); if (!user || !side) return null; return { userId: user.userId, 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, winnerSide, blueGroupIndex: 1, redGroupIndex: 1, 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 resolveRiotTeamId(side: TeamSide, team100IsBlue: boolean): number { if (side === 'blue') { return team100IsBlue ? RIOT_BLUE_TEAM_ID : RIOT_RED_TEAM_ID; } return team100IsBlue ? RIOT_RED_TEAM_ID : RIOT_BLUE_TEAM_ID; } function resolveSide(teamId: number, team100IsBlue: boolean): TeamSide | null { if (teamId === RIOT_BLUE_TEAM_ID) return team100IsBlue ? 'blue' : 'red'; if (teamId === RIOT_RED_TEAM_ID) return team100IsBlue ? 'red' : 'blue'; 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'; }