174 lines
5.2 KiB
TypeScript
174 lines
5.2 KiB
TypeScript
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 collectCaptainAndLimitedTeamMemberUserIds(
|
|
match: MatchDocument,
|
|
memberLimitPerTeam: number,
|
|
): mongoose.Types.ObjectId[] {
|
|
const ids = new Map<string, mongoose.Types.ObjectId>();
|
|
for (const team of match.teams) {
|
|
if (team.captainUserId) {
|
|
ids.set(team.captainUserId.toString(), team.captainUserId);
|
|
}
|
|
let memberCount = 0;
|
|
for (const member of team.members) {
|
|
if (team.captainUserId?.toString() === member.userId.toString()) {
|
|
continue;
|
|
}
|
|
if (memberCount >= memberLimitPerTeam) break;
|
|
ids.set(member.userId.toString(), member.userId);
|
|
memberCount += 1;
|
|
}
|
|
}
|
|
return [...ids.values()];
|
|
}
|
|
|
|
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;
|
|
}
|