1790 lines
49 KiB
TypeScript
1790 lines
49 KiB
TypeScript
import mongoose, { Schema, HydratedDocument } from 'mongoose';
|
|
import type {
|
|
RiotPlatformRoute,
|
|
RiotRegionalRoute,
|
|
} from '../../riot/riot.types';
|
|
import { ModelDefinition } from './types';
|
|
|
|
export type Position = 'TOP' | 'JGL' | 'MID' | 'ADC' | 'SUP';
|
|
export const POSITIONS: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP'];
|
|
|
|
export type RiotLane = 'TOP' | 'JUNGLE' | 'MIDDLE' | 'BOTTOM' | 'UTILITY';
|
|
export const RIOT_LANES: RiotLane[] = [
|
|
'TOP',
|
|
'JUNGLE',
|
|
'MIDDLE',
|
|
'BOTTOM',
|
|
'UTILITY',
|
|
];
|
|
|
|
export type MatchStatus =
|
|
| 'recruiting'
|
|
| 'team-built'
|
|
| 'in-progress'
|
|
| 'finished';
|
|
export const MATCH_STATUSES: MatchStatus[] = [
|
|
'recruiting',
|
|
'team-built',
|
|
'in-progress',
|
|
'finished',
|
|
];
|
|
|
|
export type TeamSide = 'blue' | 'red';
|
|
export const TEAM_SIDES: TeamSide[] = ['blue', 'red'];
|
|
|
|
export type MatchGameMode = 'pair' | 'bracket' | 'round-robin';
|
|
export const MATCH_GAME_MODES: MatchGameMode[] = [
|
|
'pair',
|
|
'bracket',
|
|
'round-robin',
|
|
];
|
|
|
|
export type CommunityRole = 'owner' | 'admin' | 'member';
|
|
export const COMMUNITY_ROLES: CommunityRole[] = ['owner', 'admin', 'member'];
|
|
|
|
export type TournamentCodeState =
|
|
| 'available'
|
|
| 'reserved'
|
|
| 'used'
|
|
| 'expired'
|
|
| 'released';
|
|
export const TOURNAMENT_CODE_STATES: TournamentCodeState[] = [
|
|
'available',
|
|
'reserved',
|
|
'used',
|
|
'expired',
|
|
'released',
|
|
];
|
|
export type TournamentMapType = 'SUMMONERS_RIFT';
|
|
export const TOURNAMENT_MAP_TYPES: TournamentMapType[] = ['SUMMONERS_RIFT'];
|
|
export type TournamentSpectatorType = 'NONE' | 'LOBBYONLY' | 'ALL';
|
|
export const TOURNAMENT_SPECTATOR_TYPES: TournamentSpectatorType[] = [
|
|
'NONE',
|
|
'LOBBYONLY',
|
|
'ALL',
|
|
];
|
|
|
|
export type MatchResultPollOutcome = 'matched' | 'not-found' | 'failed';
|
|
export type MatchResultPollStateValue =
|
|
| 'polling'
|
|
| 'complete'
|
|
| 'expired'
|
|
| 'failed';
|
|
export const MATCH_RESULT_POLL_STATES: MatchResultPollStateValue[] = [
|
|
'polling',
|
|
'complete',
|
|
'expired',
|
|
'failed',
|
|
];
|
|
|
|
export type LegacyImportJobState =
|
|
| 'pending'
|
|
| 'running'
|
|
| 'completed'
|
|
| 'failed'
|
|
| 'cancelled';
|
|
export const LEGACY_IMPORT_JOB_STATES: LegacyImportJobState[] = [
|
|
'pending',
|
|
'running',
|
|
'completed',
|
|
'failed',
|
|
'cancelled',
|
|
];
|
|
|
|
export type DakaoInboundMessageState = 'processing' | 'processed' | 'failed';
|
|
export const DAKAO_INBOUND_MESSAGE_STATES: DakaoInboundMessageState[] = [
|
|
'processing',
|
|
'processed',
|
|
'failed',
|
|
];
|
|
|
|
export type DakaoOutboxMessageState =
|
|
| 'pending'
|
|
| 'published'
|
|
| 'sent'
|
|
| 'failed';
|
|
export const DAKAO_OUTBOX_MESSAGE_STATES: DakaoOutboxMessageState[] = [
|
|
'pending',
|
|
'published',
|
|
'sent',
|
|
'failed',
|
|
];
|
|
|
|
export type DakaoOutboxCompensationState =
|
|
| 'none'
|
|
| 'pending'
|
|
| 'completed'
|
|
| 'failed';
|
|
export const DAKAO_OUTBOX_COMPENSATION_STATES: DakaoOutboxCompensationState[] =
|
|
['none', 'pending', 'completed', 'failed'];
|
|
|
|
export type DakaoSendResultStatus = 'succeeded' | 'failed';
|
|
export const DAKAO_SEND_RESULT_STATUSES: DakaoSendResultStatus[] = [
|
|
'succeeded',
|
|
'failed',
|
|
];
|
|
|
|
export interface PositionPreference {
|
|
position: Position;
|
|
priority: number; // 1 = 최우선, 숫자가 작을수록 높은 우선순위
|
|
}
|
|
|
|
export type AuctionPhase =
|
|
| 'waiting'
|
|
| 'presenting'
|
|
| 'bidding'
|
|
| 'sold'
|
|
| 'passed'
|
|
| 'complete';
|
|
export const AUCTION_PHASES: AuctionPhase[] = [
|
|
'waiting',
|
|
'presenting',
|
|
'bidding',
|
|
'sold',
|
|
'passed',
|
|
'complete',
|
|
];
|
|
export const AUCTION_TOP_TIER_THRESHOLDS = [
|
|
'NONE',
|
|
'EMERALD',
|
|
'DIAMOND',
|
|
'MASTER',
|
|
'MANUAL',
|
|
] as const;
|
|
export type AuctionTopTierThreshold =
|
|
(typeof AUCTION_TOP_TIER_THRESHOLDS)[number];
|
|
export type AuctionActionType =
|
|
| 'start'
|
|
| 'open-bidding'
|
|
| 'bid'
|
|
| 'sold'
|
|
| 'passed'
|
|
| 'next'
|
|
| 'complete';
|
|
|
|
export interface MongooseTimestamps {
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
// ===== Identity =====
|
|
export interface Identity extends MongooseTimestamps {
|
|
entraOid: string;
|
|
tenantId?: string;
|
|
email?: string;
|
|
displayName?: string;
|
|
lastSignInAt?: Date;
|
|
}
|
|
|
|
// ===== Tier =====
|
|
export interface TierInfo {
|
|
leagueId?: string;
|
|
queueType: string; // RANKED_SOLO_5x5, RANKED_FLEX_SR 등
|
|
tier: string; // IRON, BRONZE, ..., CHALLENGER
|
|
rank: string; // I | II | III | IV
|
|
puuid?: string;
|
|
leaguePoints: number;
|
|
wins: number;
|
|
losses: number;
|
|
veteran?: boolean;
|
|
inactive?: boolean;
|
|
freshBlood?: boolean;
|
|
hotStreak?: boolean;
|
|
miniSeries?: Record<string, unknown>;
|
|
fetchedAt: Date; // Riot API로부터 조회된 시각
|
|
}
|
|
|
|
export interface TierHistoryEntry {
|
|
queueType: string;
|
|
tier: string;
|
|
rank?: string;
|
|
leaguePoints?: number;
|
|
wins?: number;
|
|
losses?: number;
|
|
recordedAt: Date;
|
|
season?: string; // e.g., 'S13', 'S14'
|
|
peakTier?: string;
|
|
peakRank?: string;
|
|
}
|
|
|
|
// ===== RiotAccount (User 하위에 임베딩) =====
|
|
/**
|
|
* 챔피언 단위 집계 기록. 필드 추가가 잦을 것으로 예상되어 Mixed로 저장.
|
|
* key: championId (string), value: championId별 누적/평균 지표 자유형 JSON
|
|
*/
|
|
export type ChampionRecordMap = Record<string, Record<string, unknown>>;
|
|
|
|
export interface ChampionMasteryEntry {
|
|
puuid: string;
|
|
championId: number;
|
|
championLevel: number;
|
|
championPoints: number;
|
|
lastPlayTime?: number;
|
|
championPointsSinceLastLevel?: number;
|
|
championPointsUntilNextLevel?: number;
|
|
markRequiredForNextLevel?: number;
|
|
tokensEarned?: number;
|
|
championSeasonMilestone?: number;
|
|
milestoneGrades?: string[];
|
|
nextSeasonMilestone?: Record<string, unknown>;
|
|
}
|
|
|
|
export interface RiotLaneStats {
|
|
games: number;
|
|
wins: number;
|
|
losses: number;
|
|
avgKills: number;
|
|
avgDeaths: number;
|
|
avgAssists: number;
|
|
avgCs: number;
|
|
avgCsPerMin: number;
|
|
mostPlayedChampionIds: number[];
|
|
}
|
|
|
|
export interface RiotRecentStatsSummary {
|
|
overall: RiotLaneStats;
|
|
byLane: Partial<Record<RiotLane, RiotLaneStats>>;
|
|
}
|
|
|
|
export function createEmptyRiotLaneStats(): RiotLaneStats {
|
|
return {
|
|
games: 0,
|
|
wins: 0,
|
|
losses: 0,
|
|
avgKills: 0,
|
|
avgDeaths: 0,
|
|
avgAssists: 0,
|
|
avgCs: 0,
|
|
avgCsPerMin: 0,
|
|
mostPlayedChampionIds: [],
|
|
};
|
|
}
|
|
|
|
export function createEmptyRiotRecentStatsSummary(): RiotRecentStatsSummary {
|
|
return {
|
|
overall: createEmptyRiotLaneStats(),
|
|
byLane: {},
|
|
};
|
|
}
|
|
|
|
export interface RiotAccount extends MongooseTimestamps {
|
|
puuid: string;
|
|
gameName: string;
|
|
tagLine: string;
|
|
profileIconId: number;
|
|
revisionDate: number;
|
|
summonerLevel: number;
|
|
isPrimary: boolean;
|
|
|
|
// 현재 티어들(솔랭/자랭 각각)
|
|
tiers: TierInfo[];
|
|
// 과거 티어 기록
|
|
tierHistory: TierHistoryEntry[];
|
|
|
|
// Riot matchId 참조 (원본은 RiotMatch 컬렉션에 저장)
|
|
matchIds: string[];
|
|
matchIdsFetchedAt?: Date;
|
|
matchIdsByQueueFetchedAt?: Map<string, Date>;
|
|
lastRefreshRequestedAt?: Date;
|
|
|
|
// 최근 경기 요약
|
|
recentStatsSummary?: RiotRecentStatsSummary;
|
|
recentStatsFetchedAt?: Date;
|
|
|
|
// 챔피언 숙련도
|
|
championMasteries: ChampionMasteryEntry[];
|
|
championMasteriesFetchedAt?: Date;
|
|
|
|
// 챔피언별 기록 (JSON, 필드 추가 자유로움)
|
|
championRecords: ChampionRecordMap;
|
|
championRecordsFetchedAt?: Date;
|
|
}
|
|
|
|
// ===== User =====
|
|
export interface User extends MongooseTimestamps {
|
|
communityId: mongoose.Types.ObjectId;
|
|
identityId?: mongoose.Types.ObjectId;
|
|
role: CommunityRole;
|
|
kakaoTalkId?: string;
|
|
kakaoTalkNickname?: string;
|
|
discordId?: string;
|
|
discordNickname?: string;
|
|
riotAccounts: RiotAccount[];
|
|
preferredPositions: PositionPreference[];
|
|
isActivated: boolean;
|
|
}
|
|
|
|
export interface Community extends MongooseTimestamps {
|
|
name: string;
|
|
description: string;
|
|
ownerUserId: mongoose.Types.ObjectId;
|
|
dakaoProjectId?: mongoose.Types.ObjectId;
|
|
riotPlatform?: RiotPlatformRoute;
|
|
riotRegion?: RiotRegionalRoute;
|
|
seasonStartedAt?: Date;
|
|
seasons?: CommunitySeason[];
|
|
matchGameCount?: number;
|
|
}
|
|
|
|
export interface CommunitySeason {
|
|
id: string;
|
|
label: string;
|
|
startedAt: Date;
|
|
endedAt?: Date;
|
|
}
|
|
|
|
export interface DakaoInboundMessage extends MongooseTimestamps {
|
|
consumer: string;
|
|
projectId: string;
|
|
chatLogLogId: string;
|
|
state: DakaoInboundMessageState;
|
|
attempts: number;
|
|
processingExpiresAt?: Date;
|
|
processedAt?: Date;
|
|
lastError?: string;
|
|
}
|
|
|
|
export interface DakaoOutboxMessage extends MongooseTimestamps {
|
|
eventId: string;
|
|
eventSource: string;
|
|
type: string;
|
|
subject: string;
|
|
correlationId?: string;
|
|
resultSubjects?: {
|
|
succeeded: string;
|
|
failed: string;
|
|
};
|
|
payload: Record<string, unknown>;
|
|
state: DakaoOutboxMessageState;
|
|
publishAttempts: number;
|
|
publishedAt?: Date;
|
|
terminalAt?: Date;
|
|
resultEventId?: string;
|
|
lastPublishError?: string;
|
|
terminalError?: Record<string, unknown>;
|
|
compensationState: DakaoOutboxCompensationState;
|
|
compensationReason?: string;
|
|
}
|
|
|
|
export interface DakaoSendResultEvent extends MongooseTimestamps {
|
|
eventId: string;
|
|
requestEventId: string;
|
|
eventSource: string;
|
|
requestEventSource: string;
|
|
type: string;
|
|
subject: string;
|
|
status: DakaoSendResultStatus;
|
|
correlationId?: string;
|
|
handledAt: Date;
|
|
providerMessageId?: string;
|
|
error?: Record<string, unknown>;
|
|
raw: Record<string, unknown>;
|
|
}
|
|
|
|
export type CommunityDocument = HydratedDocument<Community>;
|
|
export type IdentityDocument = HydratedDocument<Identity>;
|
|
export type UserDocument = HydratedDocument<User>;
|
|
export type DakaoInboundMessageDocument = HydratedDocument<DakaoInboundMessage>;
|
|
export type DakaoOutboxMessageDocument = HydratedDocument<DakaoOutboxMessage>;
|
|
export type DakaoSendResultEventDocument =
|
|
HydratedDocument<DakaoSendResultEvent>;
|
|
|
|
// ===== RiotMatch (Riot 원본 매치 아카이브) =====
|
|
export interface RiotMatch extends MongooseTimestamps {
|
|
matchId: string; // Riot matchId (예: KR_7123456789)
|
|
gameCreation: number;
|
|
gameDuration: number;
|
|
gameVersion?: string;
|
|
queueId?: number;
|
|
participantPuuids: string[];
|
|
raw: Record<string, unknown>; // Riot API Match-V5 원본 응답 전체
|
|
fetchedAt: Date;
|
|
}
|
|
|
|
export type RiotMatchDocument = HydratedDocument<RiotMatch>;
|
|
|
|
// ===== TournamentCode (운영자가 미리 업로드한 토너먼트 코드 풀) =====
|
|
export interface TournamentCode extends MongooseTimestamps {
|
|
code: string;
|
|
region: RiotPlatformRoute;
|
|
mapType: TournamentMapType;
|
|
teamSize: number;
|
|
spectatorType: TournamentSpectatorType;
|
|
metadata?: Record<string, unknown>;
|
|
expiresAt: Date;
|
|
state: TournamentCodeState;
|
|
matchId?: mongoose.Types.ObjectId;
|
|
gameNumber?: number;
|
|
reservedAt?: Date;
|
|
usedAt?: Date;
|
|
releasedAt?: Date;
|
|
riotMatchId?: string;
|
|
uploadedBy: mongoose.Types.ObjectId;
|
|
uploadedAt: Date;
|
|
source?: string;
|
|
notes?: string;
|
|
}
|
|
|
|
export type TournamentCodeDocument = HydratedDocument<TournamentCode>;
|
|
|
|
// ===== Match (내전) =====
|
|
export interface MatchParticipant {
|
|
userId: mongoose.Types.ObjectId;
|
|
// 표시용 스냅샷 (대표 RiotAccount 기준)
|
|
puuid?: string;
|
|
gameName?: string;
|
|
tagLine?: string;
|
|
profileIconId?: number;
|
|
tier?: TierInfo;
|
|
preferredPositions: PositionPreference[];
|
|
joinedAt: Date;
|
|
excluded?: boolean;
|
|
}
|
|
|
|
export interface MatchTeamMember {
|
|
userId: mongoose.Types.ObjectId;
|
|
assignedPosition: Position;
|
|
cost?: number; // 경매로 영입된 경우의 입찰가
|
|
}
|
|
|
|
export interface MatchTeam {
|
|
teamIndex: number;
|
|
name: string;
|
|
captainUserId?: mongoose.Types.ObjectId;
|
|
members: MatchTeamMember[];
|
|
}
|
|
|
|
export interface MatchGameResultParticipant {
|
|
userId: mongoose.Types.ObjectId;
|
|
teamIndex: number;
|
|
side: TeamSide;
|
|
championId: number;
|
|
kills: number;
|
|
deaths: number;
|
|
assists: number;
|
|
win: boolean;
|
|
}
|
|
|
|
export interface MatchGameResult {
|
|
gameNumber: number;
|
|
riotMatchId: string;
|
|
blueTeamIndex: number;
|
|
redTeamIndex: number;
|
|
winnerTeamIndex: number;
|
|
winnerSide: TeamSide;
|
|
gameCreation: number;
|
|
gameDuration: number;
|
|
participants: MatchGameResultParticipant[];
|
|
}
|
|
|
|
export interface MatchSeriesResult {
|
|
teamAIndex: number;
|
|
teamBIndex: number;
|
|
teamAWins: number;
|
|
teamBWins: number;
|
|
winnerTeamIndex?: number;
|
|
}
|
|
|
|
export interface MatchGameAssignment {
|
|
gameNumber: number;
|
|
teamAIndex: number;
|
|
teamBIndex: number;
|
|
}
|
|
|
|
export interface MatchResult {
|
|
winnerTeamIndex?: number;
|
|
mvpUserId?: mongoose.Types.ObjectId;
|
|
riotMatchIds: string[]; // 내전에서 플레이된 Riot matchId들
|
|
games: MatchGameResult[];
|
|
seriesResults?: MatchSeriesResult[];
|
|
}
|
|
|
|
export interface MatchResultPollState {
|
|
startedAt: Date;
|
|
lastPolledAt?: Date;
|
|
lastPollOutcome?: MatchResultPollOutcome;
|
|
matchedCount: number;
|
|
expandedToAllPuuids: boolean;
|
|
state: MatchResultPollStateValue;
|
|
}
|
|
|
|
export interface MatchScheduleItem {
|
|
time: string;
|
|
label: string;
|
|
}
|
|
|
|
export interface Match extends MongooseTimestamps {
|
|
communityId: mongoose.Types.ObjectId;
|
|
shortCode?: string;
|
|
title: string;
|
|
description: string;
|
|
mapId: number; // Riot Match-V5 mapId (11=소환사의 협곡, 12=ARAM 등)
|
|
status: MatchStatus;
|
|
scheduledAt: Date;
|
|
maxParticipants: number;
|
|
gameCount: number;
|
|
gameMode: MatchGameMode;
|
|
tournamentCodeIds: mongoose.Types.ObjectId[];
|
|
gameSchedule: MatchGameAssignment[];
|
|
teamBuiltAt?: Date;
|
|
hostUserId?: mongoose.Types.ObjectId;
|
|
participants: MatchParticipant[];
|
|
teams: MatchTeam[];
|
|
result?: MatchResult;
|
|
resultPollState?: MatchResultPollState;
|
|
legacyImported?: boolean;
|
|
legacyImportSource?: {
|
|
importedAt: Date;
|
|
importedBy: mongoose.Types.ObjectId;
|
|
triggeringPuuid: string;
|
|
jobId?: string;
|
|
};
|
|
prizes: string[];
|
|
notices: string[];
|
|
schedule: MatchScheduleItem[];
|
|
}
|
|
|
|
export type MatchDocument = HydratedDocument<Match>;
|
|
|
|
// ===== Legacy Import Job =====
|
|
export interface LegacyImportJobMemberOverride {
|
|
userId: mongoose.Types.ObjectId;
|
|
puuid: string;
|
|
startTime: Date;
|
|
endTime: Date;
|
|
}
|
|
|
|
export interface LegacyImportJobError {
|
|
puuid?: string;
|
|
matchId?: string;
|
|
reason: string;
|
|
at: Date;
|
|
}
|
|
|
|
export interface LegacyImportJob extends MongooseTimestamps {
|
|
jobId: string;
|
|
communityId: mongoose.Types.ObjectId;
|
|
triggeredBy: mongoose.Types.ObjectId;
|
|
startedAt: Date;
|
|
completedAt?: Date;
|
|
state: LegacyImportJobState;
|
|
windowStart: Date;
|
|
windowEnd: Date;
|
|
memberOverrides: LegacyImportJobMemberOverride[];
|
|
totalMembers: number;
|
|
processedMembers: number;
|
|
totalDiscoveredMatchIds: number;
|
|
totalImportedMatches: number;
|
|
totalSkippedDuplicate: number;
|
|
totalErrors: number;
|
|
averageMemberSeconds?: number;
|
|
errors?: LegacyImportJobError[];
|
|
}
|
|
|
|
export type LegacyImportJobDocument = HydratedDocument<LegacyImportJob>;
|
|
|
|
// ===== Auction (경매) =====
|
|
export interface AuctionPlayerSnapshot {
|
|
userId: mongoose.Types.ObjectId;
|
|
// 표시용 스냅샷(대표 RiotAccount 기준) — RiotAccount에서 복사
|
|
puuid: string;
|
|
gameName: string;
|
|
tagLine: string;
|
|
profileIconId: number;
|
|
preferredPositions: PositionPreference[];
|
|
tiers: TierInfo[];
|
|
tierHistory: TierHistoryEntry[];
|
|
masteries: ChampionMasteryEntry[];
|
|
recentStatsSummary?: RiotRecentStatsSummary;
|
|
passCount: number;
|
|
}
|
|
|
|
export interface AuctionTeamMember {
|
|
userId: mongoose.Types.ObjectId;
|
|
assignedPosition: Position;
|
|
cost: number;
|
|
}
|
|
|
|
export interface AuctionTeam {
|
|
teamIndex: number;
|
|
captainUserId: mongoose.Types.ObjectId;
|
|
points: number;
|
|
members: AuctionTeamMember[];
|
|
}
|
|
|
|
export interface AuctionBid {
|
|
teamIndex: number;
|
|
amount: number;
|
|
bidderUserId: mongoose.Types.ObjectId;
|
|
timestamp: Date;
|
|
}
|
|
|
|
export interface AuctionChatMessage {
|
|
id: string;
|
|
userId?: mongoose.Types.ObjectId;
|
|
displayName: string;
|
|
message: string;
|
|
sentAt: Date;
|
|
}
|
|
|
|
export interface Auction extends MongooseTimestamps {
|
|
matchId: mongoose.Types.ObjectId;
|
|
phase: AuctionPhase;
|
|
teams: AuctionTeam[];
|
|
order: AuctionPlayerSnapshot[];
|
|
passedQueue: AuctionPlayerSnapshot[];
|
|
auctionRound: number;
|
|
currentIndex: number;
|
|
bids: AuctionBid[];
|
|
currentBid: number;
|
|
currentBidderTeamIndex?: number;
|
|
maxTimerSeconds: number;
|
|
bidExtensionSeconds: number;
|
|
currentDeadline?: Date;
|
|
initialPoints: number;
|
|
topTierThreshold: AuctionTopTierThreshold;
|
|
manualTopTierUserIds: mongoose.Types.ObjectId[];
|
|
chatMessages: AuctionChatMessage[];
|
|
startedAt?: Date;
|
|
completedAt?: Date;
|
|
}
|
|
|
|
export type AuctionDocument = HydratedDocument<Auction>;
|
|
|
|
export interface AuctionHistoryEntry extends MongooseTimestamps {
|
|
auctionId: mongoose.Types.ObjectId;
|
|
matchId: mongoose.Types.ObjectId;
|
|
sequence: number;
|
|
action: AuctionActionType;
|
|
actorUserId?: mongoose.Types.ObjectId;
|
|
snapshotBefore: Record<string, unknown>;
|
|
takenAt: Date;
|
|
}
|
|
|
|
export type AuctionHistoryEntryDocument = HydratedDocument<AuctionHistoryEntry>;
|
|
|
|
// ===== MatchStats (precomputed internal stats fragment) =====
|
|
export interface MatchStatsRecord {
|
|
matchId: string;
|
|
title: string;
|
|
scheduledAt: Date;
|
|
gameNumber: number;
|
|
riotMatchId: string;
|
|
gameCreation: number;
|
|
gameDuration: number;
|
|
userId: string;
|
|
summonerName: string;
|
|
profileIconId: number;
|
|
teamIndex: number;
|
|
side: TeamSide;
|
|
championId: number;
|
|
championLevel: number;
|
|
teamPosition: string;
|
|
kills: number;
|
|
deaths: number;
|
|
assists: number;
|
|
win: boolean;
|
|
cs: number;
|
|
csPerMin: number;
|
|
gold: number;
|
|
gpm: number;
|
|
damageDealtToChampions: number;
|
|
damageTaken: number;
|
|
damageShare: number;
|
|
killParticipation: number;
|
|
visionScore: number;
|
|
controlWardsBought: number;
|
|
wardsPlaced: number;
|
|
wardsKilled: number;
|
|
items: number[];
|
|
trinket: number;
|
|
summonerSpells: [number, number];
|
|
perks: { primaryStyle?: number; subStyle?: number; keystone?: number };
|
|
rating: number;
|
|
}
|
|
|
|
export interface MatchStatsGameDetail {
|
|
gameNumber: number;
|
|
riotMatchId: string;
|
|
gameCreation: number;
|
|
gameDuration: number;
|
|
winnerSide: TeamSide;
|
|
teams: Array<{
|
|
teamIndex: number;
|
|
side: TeamSide;
|
|
win: boolean;
|
|
objectives: { baron: number; dragon: number; tower: number };
|
|
participants: MatchStatsRecord[];
|
|
}>;
|
|
}
|
|
|
|
export interface MatchStats extends MongooseTimestamps {
|
|
matchId: mongoose.Types.ObjectId;
|
|
communityId: mongoose.Types.ObjectId;
|
|
scheduledAt: Date;
|
|
gameCreationMin: number;
|
|
gameCreationMax: number;
|
|
userIds: mongoose.Types.ObjectId[];
|
|
championIds: number[];
|
|
matchCard: Record<string, unknown>;
|
|
records: MatchStatsRecord[];
|
|
gameDetails: MatchStatsGameDetail[];
|
|
builtAt: Date;
|
|
builderVersion: number;
|
|
}
|
|
|
|
export type MatchStatsDocument = HydratedDocument<MatchStats>;
|
|
|
|
// ===== Sub-schemas =====
|
|
const PositionPreferenceSubSchema = new Schema<PositionPreference>(
|
|
{
|
|
position: { type: String, enum: POSITIONS, required: true },
|
|
priority: { type: Number, required: true, min: 1 },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const TierInfoSubSchema = new Schema<TierInfo>(
|
|
{
|
|
leagueId: { type: String },
|
|
queueType: { type: String, required: true },
|
|
tier: { type: String, required: true },
|
|
rank: { type: String, required: true, default: '' },
|
|
puuid: { type: String },
|
|
leaguePoints: { type: Number, required: true, default: 0 },
|
|
wins: { type: Number, required: true, default: 0 },
|
|
losses: { type: Number, required: true, default: 0 },
|
|
veteran: { type: Boolean },
|
|
inactive: { type: Boolean },
|
|
freshBlood: { type: Boolean },
|
|
hotStreak: { type: Boolean },
|
|
miniSeries: { type: Schema.Types.Mixed },
|
|
fetchedAt: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const TierHistoryEntrySubSchema = new Schema<TierHistoryEntry>(
|
|
{
|
|
queueType: { type: String, required: true },
|
|
tier: { type: String, required: true },
|
|
rank: { type: String },
|
|
leaguePoints: { type: Number },
|
|
wins: { type: Number },
|
|
losses: { type: Number },
|
|
recordedAt: { type: Date, required: true },
|
|
season: { type: String },
|
|
peakTier: { type: String },
|
|
peakRank: { type: String },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const ChampionMasteryEntrySubSchema = new Schema<ChampionMasteryEntry>(
|
|
{
|
|
puuid: { type: String, required: true },
|
|
championId: { type: Number, required: true },
|
|
championLevel: { type: Number, required: true, default: 0 },
|
|
championPoints: { type: Number, required: true, default: 0 },
|
|
lastPlayTime: { type: Number },
|
|
championPointsSinceLastLevel: { type: Number },
|
|
championPointsUntilNextLevel: { type: Number },
|
|
markRequiredForNextLevel: { type: Number },
|
|
tokensEarned: { type: Number },
|
|
championSeasonMilestone: { type: Number },
|
|
milestoneGrades: { type: [String] },
|
|
nextSeasonMilestone: { type: Schema.Types.Mixed },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const RiotLaneStatsSubSchema = new Schema<RiotLaneStats>(
|
|
{
|
|
games: { type: Number, required: true, default: 0 },
|
|
wins: { type: Number, required: true, default: 0 },
|
|
losses: { type: Number, required: true, default: 0 },
|
|
avgKills: { type: Number, required: true, default: 0 },
|
|
avgDeaths: { type: Number, required: true, default: 0 },
|
|
avgAssists: { type: Number, required: true, default: 0 },
|
|
avgCs: { type: Number, required: true, default: 0 },
|
|
avgCsPerMin: { type: Number, required: true, default: 0 },
|
|
mostPlayedChampionIds: { type: [Number], required: true, default: [] },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const RiotRecentStatsByLaneSubSchema = new Schema<
|
|
Partial<Record<RiotLane, RiotLaneStats>>
|
|
>(
|
|
{
|
|
TOP: { type: RiotLaneStatsSubSchema, required: false },
|
|
JUNGLE: { type: RiotLaneStatsSubSchema, required: false },
|
|
MIDDLE: { type: RiotLaneStatsSubSchema, required: false },
|
|
BOTTOM: { type: RiotLaneStatsSubSchema, required: false },
|
|
UTILITY: { type: RiotLaneStatsSubSchema, required: false },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const RiotRecentStatsSummarySubSchema = new Schema<RiotRecentStatsSummary>(
|
|
{
|
|
overall: {
|
|
type: RiotLaneStatsSubSchema,
|
|
required: true,
|
|
default: createEmptyRiotLaneStats,
|
|
},
|
|
byLane: {
|
|
type: RiotRecentStatsByLaneSubSchema,
|
|
required: true,
|
|
default: () => ({}),
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const RiotAccountSubSchema = new Schema<RiotAccount>(
|
|
{
|
|
puuid: { type: String, required: true },
|
|
gameName: { type: String, required: true },
|
|
tagLine: { type: String, required: true },
|
|
profileIconId: { type: Number, required: true, default: 0 },
|
|
revisionDate: { type: Number, required: true, default: 0 },
|
|
summonerLevel: { type: Number, required: true, default: 0 },
|
|
isPrimary: { type: Boolean, required: true, default: false },
|
|
|
|
tiers: { type: [TierInfoSubSchema], required: true, default: [] },
|
|
tierHistory: {
|
|
type: [TierHistoryEntrySubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
|
|
matchIds: { type: [String], required: true, default: [] },
|
|
matchIdsFetchedAt: { type: Date },
|
|
matchIdsByQueueFetchedAt: { type: Map, of: Date },
|
|
lastRefreshRequestedAt: { type: Date },
|
|
|
|
recentStatsSummary: { type: RiotRecentStatsSummarySubSchema },
|
|
recentStatsFetchedAt: { type: Date },
|
|
|
|
championMasteries: {
|
|
type: [ChampionMasteryEntrySubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
championMasteriesFetchedAt: { type: Date },
|
|
|
|
championRecords: {
|
|
type: Schema.Types.Mixed,
|
|
required: true,
|
|
default: () => ({}),
|
|
},
|
|
championRecordsFetchedAt: { type: Date },
|
|
},
|
|
{
|
|
_id: false,
|
|
timestamps: true,
|
|
},
|
|
);
|
|
|
|
export const IdentitySchema = new Schema<Identity>(
|
|
{
|
|
entraOid: { type: String, required: true },
|
|
tenantId: { type: String },
|
|
email: { type: String },
|
|
displayName: { type: String },
|
|
lastSignInAt: { type: Date },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'identity',
|
|
},
|
|
);
|
|
|
|
IdentitySchema.index({ entraOid: 1 }, { unique: true });
|
|
IdentitySchema.index({ email: 1 });
|
|
|
|
export const CommunitySchema = new Schema<Community>(
|
|
{
|
|
name: { type: String, required: true },
|
|
description: { type: String, required: true, default: '' },
|
|
ownerUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
dakaoProjectId: { type: Schema.Types.ObjectId },
|
|
riotPlatform: { type: String },
|
|
riotRegion: { type: String },
|
|
seasonStartedAt: { type: Date },
|
|
matchGameCount: { type: Number, required: false, default: 3 },
|
|
seasons: {
|
|
type: [
|
|
new Schema<CommunitySeason>(
|
|
{
|
|
id: { type: String, required: true },
|
|
label: { type: String, required: true },
|
|
startedAt: { type: Date, required: true },
|
|
endedAt: { type: Date },
|
|
},
|
|
{ _id: false },
|
|
),
|
|
],
|
|
required: false,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'community',
|
|
},
|
|
);
|
|
|
|
CommunitySchema.index({ name: 1 }, { unique: true });
|
|
CommunitySchema.index({ ownerUserId: 1 });
|
|
CommunitySchema.index({ dakaoProjectId: 1 }, { unique: true, sparse: true });
|
|
|
|
export const DakaoInboundMessageSchema = new Schema<DakaoInboundMessage>(
|
|
{
|
|
consumer: { type: String, required: true },
|
|
projectId: { type: String, required: true },
|
|
chatLogLogId: { type: String, required: true },
|
|
state: {
|
|
type: String,
|
|
enum: DAKAO_INBOUND_MESSAGE_STATES,
|
|
required: true,
|
|
default: 'processing',
|
|
},
|
|
attempts: { type: Number, required: true, default: 1 },
|
|
processingExpiresAt: { type: Date },
|
|
processedAt: { type: Date },
|
|
lastError: { type: String },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'dakao_inbound_message',
|
|
},
|
|
);
|
|
|
|
DakaoInboundMessageSchema.index(
|
|
{ consumer: 1, projectId: 1, chatLogLogId: 1 },
|
|
{ unique: true },
|
|
);
|
|
DakaoInboundMessageSchema.index({ state: 1, processingExpiresAt: 1 });
|
|
|
|
export const DakaoOutboxMessageSchema = new Schema<DakaoOutboxMessage>(
|
|
{
|
|
eventId: { type: String, required: true, unique: true },
|
|
eventSource: { type: String, required: true },
|
|
type: { type: String, required: true },
|
|
subject: { type: String, required: true },
|
|
correlationId: { type: String },
|
|
resultSubjects: {
|
|
succeeded: { type: String },
|
|
failed: { type: String },
|
|
},
|
|
payload: { type: Schema.Types.Mixed, required: true },
|
|
state: {
|
|
type: String,
|
|
enum: DAKAO_OUTBOX_MESSAGE_STATES,
|
|
required: true,
|
|
default: 'pending',
|
|
},
|
|
publishAttempts: { type: Number, required: true, default: 0 },
|
|
publishedAt: { type: Date },
|
|
terminalAt: { type: Date },
|
|
resultEventId: { type: String },
|
|
lastPublishError: { type: String },
|
|
terminalError: { type: Schema.Types.Mixed },
|
|
compensationState: {
|
|
type: String,
|
|
enum: DAKAO_OUTBOX_COMPENSATION_STATES,
|
|
required: true,
|
|
default: 'none',
|
|
},
|
|
compensationReason: { type: String },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'dakao_outbox_message',
|
|
},
|
|
);
|
|
|
|
DakaoOutboxMessageSchema.index({ state: 1, updatedAt: 1 });
|
|
DakaoOutboxMessageSchema.index({ correlationId: 1 }, { sparse: true });
|
|
|
|
export const DakaoSendResultEventSchema = new Schema<DakaoSendResultEvent>(
|
|
{
|
|
eventId: { type: String, required: true, unique: true },
|
|
requestEventId: { type: String, required: true },
|
|
eventSource: { type: String, required: true },
|
|
requestEventSource: { type: String, required: true },
|
|
type: { type: String, required: true },
|
|
subject: { type: String, required: true },
|
|
status: {
|
|
type: String,
|
|
enum: DAKAO_SEND_RESULT_STATUSES,
|
|
required: true,
|
|
},
|
|
correlationId: { type: String },
|
|
handledAt: { type: Date, required: true },
|
|
providerMessageId: { type: String },
|
|
error: { type: Schema.Types.Mixed },
|
|
raw: { type: Schema.Types.Mixed, required: true },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'dakao_send_result_event',
|
|
},
|
|
);
|
|
|
|
DakaoSendResultEventSchema.index({ requestEventId: 1, status: 1 });
|
|
|
|
export const UserSchema = new Schema<User>(
|
|
{
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
identityId: { type: Schema.Types.ObjectId, ref: 'identity' },
|
|
role: {
|
|
type: String,
|
|
enum: COMMUNITY_ROLES,
|
|
required: true,
|
|
default: 'member',
|
|
},
|
|
kakaoTalkId: { type: String },
|
|
kakaoTalkNickname: { type: String },
|
|
discordId: { type: String },
|
|
discordNickname: { type: String },
|
|
riotAccounts: {
|
|
type: [RiotAccountSubSchema],
|
|
required: true,
|
|
default: [],
|
|
validate: [
|
|
{
|
|
validator: function (accounts: RiotAccount[]) {
|
|
const puuids = accounts.map((a) => a.puuid);
|
|
return puuids.length === new Set(puuids).size;
|
|
},
|
|
message: 'Duplicate Riot puuid within a user is not allowed.',
|
|
},
|
|
{
|
|
validator: function (accounts: RiotAccount[]) {
|
|
if (accounts.length === 0) return true;
|
|
const primaryCount = accounts.filter((a) => a.isPrimary).length;
|
|
return primaryCount === 1;
|
|
},
|
|
message:
|
|
'Exactly one Riot account must be marked as primary when accounts exist.',
|
|
},
|
|
],
|
|
},
|
|
preferredPositions: {
|
|
type: [PositionPreferenceSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
isActivated: { type: Boolean, required: true, default: true },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'user',
|
|
},
|
|
);
|
|
|
|
UserSchema.index({ communityId: 1 });
|
|
UserSchema.index({ communityId: 1, role: 1 });
|
|
UserSchema.index(
|
|
{ communityId: 1, identityId: 1 },
|
|
{
|
|
unique: true,
|
|
partialFilterExpression: { identityId: { $type: 'objectId' } },
|
|
},
|
|
);
|
|
UserSchema.index(
|
|
{ identityId: 1 },
|
|
{ partialFilterExpression: { identityId: { $type: 'objectId' } } },
|
|
);
|
|
UserSchema.index(
|
|
{ communityId: 1, kakaoTalkId: 1 },
|
|
{
|
|
unique: true,
|
|
partialFilterExpression: { kakaoTalkId: { $type: 'string' } },
|
|
},
|
|
);
|
|
UserSchema.index(
|
|
{ communityId: 1, discordId: 1 },
|
|
{
|
|
unique: true,
|
|
partialFilterExpression: { discordId: { $type: 'string' } },
|
|
},
|
|
);
|
|
UserSchema.index(
|
|
{ communityId: 1, 'riotAccounts.puuid': 1 },
|
|
{
|
|
unique: true,
|
|
partialFilterExpression: { 'riotAccounts.puuid': { $type: 'string' } },
|
|
},
|
|
);
|
|
|
|
export const RiotMatchSchema = new Schema<RiotMatch>(
|
|
{
|
|
matchId: { type: String, required: true, unique: true },
|
|
gameCreation: { type: Number, required: true, default: 0 },
|
|
gameDuration: { type: Number, required: true, default: 0 },
|
|
gameVersion: { type: String },
|
|
queueId: { type: Number },
|
|
participantPuuids: { type: [String], required: true, default: [] },
|
|
raw: { type: Schema.Types.Mixed, required: true, default: () => ({}) },
|
|
fetchedAt: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'riot_match',
|
|
},
|
|
);
|
|
|
|
RiotMatchSchema.index({ participantPuuids: 1, gameCreation: -1 });
|
|
RiotMatchSchema.index({ participantPuuids: 1, queueId: 1, gameCreation: -1 });
|
|
RiotMatchSchema.index({ 'raw.info.tournamentCode': 1 }, { sparse: true });
|
|
|
|
export const TournamentCodeSchema = new Schema<TournamentCode>(
|
|
{
|
|
code: {
|
|
type: String,
|
|
required: true,
|
|
trim: true,
|
|
uppercase: true,
|
|
},
|
|
region: { type: String, required: true, default: 'kr' },
|
|
mapType: {
|
|
type: String,
|
|
enum: TOURNAMENT_MAP_TYPES,
|
|
required: true,
|
|
default: 'SUMMONERS_RIFT',
|
|
},
|
|
teamSize: { type: Number, required: true, default: 5 },
|
|
spectatorType: {
|
|
type: String,
|
|
enum: TOURNAMENT_SPECTATOR_TYPES,
|
|
required: true,
|
|
default: 'ALL',
|
|
},
|
|
metadata: { type: Schema.Types.Mixed },
|
|
expiresAt: { type: Date, required: true },
|
|
state: {
|
|
type: String,
|
|
enum: TOURNAMENT_CODE_STATES,
|
|
required: true,
|
|
default: 'available',
|
|
},
|
|
matchId: { type: Schema.Types.ObjectId, ref: 'match' },
|
|
gameNumber: { type: Number },
|
|
reservedAt: { type: Date },
|
|
usedAt: { type: Date },
|
|
releasedAt: { type: Date },
|
|
riotMatchId: { type: String },
|
|
uploadedBy: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
uploadedAt: { type: Date, required: true, default: Date.now },
|
|
source: { type: String },
|
|
notes: { type: String },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'tournament_code',
|
|
},
|
|
);
|
|
|
|
TournamentCodeSchema.index({ code: 1 }, { unique: true });
|
|
TournamentCodeSchema.index({ state: 1, expiresAt: 1 });
|
|
TournamentCodeSchema.index({ matchId: 1, gameNumber: 1 });
|
|
TournamentCodeSchema.index({ state: 1, uploadedAt: -1 });
|
|
TournamentCodeSchema.index({ riotMatchId: 1 }, { sparse: true });
|
|
|
|
const MatchParticipantSubSchema = new Schema<MatchParticipant>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
puuid: { type: String },
|
|
gameName: { type: String },
|
|
tagLine: { type: String },
|
|
profileIconId: { type: Number },
|
|
tier: { type: TierInfoSubSchema },
|
|
preferredPositions: {
|
|
type: [PositionPreferenceSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
joinedAt: { type: Date, required: true, default: Date.now },
|
|
excluded: { type: Boolean, required: true, default: false },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchTeamMemberSubSchema = new Schema<MatchTeamMember>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
assignedPosition: { type: String, enum: POSITIONS, required: true },
|
|
cost: { type: Number, required: false },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchTeamSubSchema = new Schema<MatchTeam>(
|
|
{
|
|
teamIndex: { type: Number, required: true, min: 1 },
|
|
name: { type: String, required: true },
|
|
captainUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
members: { type: [MatchTeamMemberSubSchema], required: true, default: [] },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchGameResultParticipantSubSchema =
|
|
new Schema<MatchGameResultParticipant>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
teamIndex: { type: Number, required: true, min: 1 },
|
|
side: { type: String, enum: TEAM_SIDES, required: true },
|
|
championId: { type: Number, required: true },
|
|
kills: { type: Number, required: true, default: 0 },
|
|
deaths: { type: Number, required: true, default: 0 },
|
|
assists: { type: Number, required: true, default: 0 },
|
|
win: { type: Boolean, required: true, default: false },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchGameResultSubSchema = new Schema<MatchGameResult>(
|
|
{
|
|
gameNumber: { type: Number, required: true },
|
|
riotMatchId: { type: String, required: true },
|
|
blueTeamIndex: { type: Number, required: true, min: 1 },
|
|
redTeamIndex: { type: Number, required: true, min: 1 },
|
|
winnerTeamIndex: { type: Number, required: true, min: 1 },
|
|
winnerSide: { type: String, enum: TEAM_SIDES, required: true },
|
|
gameCreation: { type: Number, required: true },
|
|
gameDuration: { type: Number, required: true },
|
|
participants: {
|
|
type: [MatchGameResultParticipantSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchSeriesResultSubSchema = new Schema<MatchSeriesResult>(
|
|
{
|
|
teamAIndex: { type: Number, required: true, min: 1 },
|
|
teamBIndex: { type: Number, required: true, min: 1 },
|
|
teamAWins: { type: Number, required: true, default: 0 },
|
|
teamBWins: { type: Number, required: true, default: 0 },
|
|
winnerTeamIndex: { type: Number, required: false, min: 1 },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchGameAssignmentSubSchema = new Schema<MatchGameAssignment>(
|
|
{
|
|
gameNumber: { type: Number, required: true, min: 1 },
|
|
teamAIndex: { type: Number, required: true, min: 1 },
|
|
teamBIndex: { type: Number, required: true, min: 1 },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchResultSubSchema = new Schema<MatchResult>(
|
|
{
|
|
winnerTeamIndex: { type: Number, required: false, min: 1 },
|
|
mvpUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
riotMatchIds: { type: [String], required: true, default: [] },
|
|
games: { type: [MatchGameResultSubSchema], required: true, default: [] },
|
|
seriesResults: {
|
|
type: [MatchSeriesResultSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchResultPollStateSubSchema = new Schema<MatchResultPollState>(
|
|
{
|
|
startedAt: { type: Date, required: true },
|
|
lastPolledAt: { type: Date },
|
|
lastPollOutcome: {
|
|
type: String,
|
|
enum: ['matched', 'not-found', 'failed'],
|
|
required: false,
|
|
},
|
|
matchedCount: { type: Number, required: true, default: 0 },
|
|
expandedToAllPuuids: { type: Boolean, required: true, default: false },
|
|
state: {
|
|
type: String,
|
|
enum: MATCH_RESULT_POLL_STATES,
|
|
required: true,
|
|
default: 'polling',
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchScheduleItemSubSchema = new Schema<MatchScheduleItem>(
|
|
{
|
|
time: { type: String, required: true },
|
|
label: { type: String, required: true },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchLegacyImportSourceSubSchema = new Schema<
|
|
NonNullable<Match['legacyImportSource']>
|
|
>(
|
|
{
|
|
importedAt: { type: Date, required: true },
|
|
importedBy: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
triggeringPuuid: { type: String, required: true },
|
|
jobId: { type: String },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
export const MatchSchema = new Schema<Match>(
|
|
{
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
shortCode: {
|
|
type: String,
|
|
required: false,
|
|
trim: true,
|
|
uppercase: true,
|
|
minlength: 4,
|
|
maxlength: 6,
|
|
},
|
|
title: { type: String, required: true },
|
|
description: { type: String, required: false, default: '' },
|
|
mapId: { type: Number, required: true, default: 11 },
|
|
status: {
|
|
type: String,
|
|
enum: MATCH_STATUSES,
|
|
required: true,
|
|
default: 'recruiting',
|
|
},
|
|
scheduledAt: { type: Date, required: true },
|
|
maxParticipants: {
|
|
type: Number,
|
|
required: true,
|
|
default: 10,
|
|
min: 10,
|
|
validate: {
|
|
validator: (value: number) =>
|
|
Number.isInteger(value) && value % 10 === 0,
|
|
message: 'maxParticipants must be a multiple of 10.',
|
|
},
|
|
},
|
|
gameCount: { type: Number, required: true, default: 3 },
|
|
gameMode: {
|
|
type: String,
|
|
enum: MATCH_GAME_MODES,
|
|
required: true,
|
|
default: 'pair',
|
|
},
|
|
tournamentCodeIds: {
|
|
type: [{ type: Schema.Types.ObjectId, ref: 'tournamentCode' }],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
gameSchedule: {
|
|
type: [MatchGameAssignmentSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
teamBuiltAt: { type: Date },
|
|
hostUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
participants: {
|
|
type: [MatchParticipantSubSchema],
|
|
required: true,
|
|
default: [],
|
|
validate: {
|
|
validator: function (parts: MatchParticipant[]) {
|
|
const ids = parts.map((p) => p.userId.toString());
|
|
return ids.length === new Set(ids).size;
|
|
},
|
|
message: 'Duplicate participants are not allowed.',
|
|
},
|
|
},
|
|
teams: { type: [MatchTeamSubSchema], required: true, default: [] },
|
|
result: { type: MatchResultSubSchema, required: false },
|
|
resultPollState: {
|
|
type: MatchResultPollStateSubSchema,
|
|
required: false,
|
|
},
|
|
legacyImported: { type: Boolean, required: false, default: false },
|
|
legacyImportSource: {
|
|
type: MatchLegacyImportSourceSubSchema,
|
|
required: false,
|
|
},
|
|
prizes: { type: [String], required: true, default: [] },
|
|
notices: { type: [String], required: true, default: [] },
|
|
schedule: {
|
|
type: [MatchScheduleItemSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'match',
|
|
},
|
|
);
|
|
|
|
MatchSchema.index({ communityId: 1, scheduledAt: -1 });
|
|
MatchSchema.index({ communityId: 1, status: 1, scheduledAt: -1 });
|
|
MatchSchema.index(
|
|
{ communityId: 1, shortCode: 1 },
|
|
{
|
|
unique: true,
|
|
partialFilterExpression: { shortCode: { $type: 'string' } },
|
|
},
|
|
);
|
|
MatchSchema.index({ legacyImported: 1, scheduledAt: -1 });
|
|
MatchSchema.index({ 'result.games.riotMatchId': 1 }, { sparse: true });
|
|
MatchSchema.index({ title: 'text', description: 'text' });
|
|
|
|
const LegacyImportJobMemberOverrideSubSchema =
|
|
new Schema<LegacyImportJobMemberOverride>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
puuid: { type: String, required: true },
|
|
startTime: { type: Date, required: true },
|
|
endTime: { type: Date, required: true },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const LegacyImportJobErrorSubSchema = new Schema<LegacyImportJobError>(
|
|
{
|
|
puuid: { type: String },
|
|
matchId: { type: String },
|
|
reason: { type: String, required: true },
|
|
at: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
export const LegacyImportJobSchema = new Schema<LegacyImportJob>(
|
|
{
|
|
jobId: { type: String, required: true, unique: true },
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
triggeredBy: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
startedAt: { type: Date, required: true, default: Date.now },
|
|
completedAt: { type: Date },
|
|
state: {
|
|
type: String,
|
|
enum: LEGACY_IMPORT_JOB_STATES,
|
|
required: true,
|
|
default: 'pending',
|
|
},
|
|
windowStart: { type: Date, required: true },
|
|
windowEnd: { type: Date, required: true },
|
|
memberOverrides: {
|
|
type: [LegacyImportJobMemberOverrideSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
totalMembers: { type: Number, required: true, default: 0 },
|
|
processedMembers: { type: Number, required: true, default: 0 },
|
|
totalDiscoveredMatchIds: { type: Number, required: true, default: 0 },
|
|
totalImportedMatches: { type: Number, required: true, default: 0 },
|
|
totalSkippedDuplicate: { type: Number, required: true, default: 0 },
|
|
totalErrors: { type: Number, required: true, default: 0 },
|
|
averageMemberSeconds: { type: Number },
|
|
errors: {
|
|
type: [LegacyImportJobErrorSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'legacy_import_job',
|
|
suppressReservedKeysWarning: true,
|
|
},
|
|
);
|
|
|
|
LegacyImportJobSchema.index({ communityId: 1, startedAt: -1 });
|
|
LegacyImportJobSchema.index({ communityId: 1, state: 1 });
|
|
|
|
const AuctionPlayerSnapshotSubSchema = new Schema<AuctionPlayerSnapshot>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
puuid: { type: String, required: true },
|
|
gameName: { type: String, required: true },
|
|
tagLine: { type: String, required: true },
|
|
profileIconId: { type: Number, required: true, default: 0 },
|
|
preferredPositions: {
|
|
type: [PositionPreferenceSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
tiers: { type: [TierInfoSubSchema], required: true, default: [] },
|
|
tierHistory: {
|
|
type: [TierHistoryEntrySubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
masteries: {
|
|
type: [ChampionMasteryEntrySubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
recentStatsSummary: { type: RiotRecentStatsSummarySubSchema },
|
|
passCount: { type: Number, required: true, default: 0 },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const AuctionTeamMemberSubSchema = new Schema<AuctionTeamMember>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
assignedPosition: { type: String, enum: POSITIONS, required: true },
|
|
cost: { type: Number, required: true, default: 0 },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const AuctionTeamSubSchema = new Schema<AuctionTeam>(
|
|
{
|
|
teamIndex: { type: Number, required: true, min: 1 },
|
|
captainUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
points: { type: Number, required: true, default: 0 },
|
|
members: {
|
|
type: [AuctionTeamMemberSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const AuctionBidSubSchema = new Schema<AuctionBid>(
|
|
{
|
|
teamIndex: { type: Number, required: true, min: 1 },
|
|
amount: { type: Number, required: true },
|
|
bidderUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
timestamp: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const AuctionChatMessageSubSchema = new Schema<AuctionChatMessage>(
|
|
{
|
|
id: { type: String, required: true },
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: false },
|
|
displayName: { type: String, required: true },
|
|
message: { type: String, required: true },
|
|
sentAt: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
export const AuctionSchema = new Schema<Auction>(
|
|
{
|
|
matchId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'match',
|
|
required: true,
|
|
unique: true,
|
|
},
|
|
phase: {
|
|
type: String,
|
|
enum: AUCTION_PHASES,
|
|
required: true,
|
|
default: 'waiting',
|
|
},
|
|
teams: { type: [AuctionTeamSubSchema], required: true, default: [] },
|
|
order: {
|
|
type: [AuctionPlayerSnapshotSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
passedQueue: {
|
|
type: [AuctionPlayerSnapshotSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
auctionRound: { type: Number, required: true, default: 1, min: 1 },
|
|
currentIndex: { type: Number, required: true, default: 0 },
|
|
bids: { type: [AuctionBidSubSchema], required: true, default: [] },
|
|
currentBid: { type: Number, required: true, default: 0 },
|
|
currentBidderTeamIndex: { type: Number, required: false, min: 1 },
|
|
maxTimerSeconds: { type: Number, required: true, default: 30 },
|
|
bidExtensionSeconds: { type: Number, required: true, default: 5, min: 0 },
|
|
currentDeadline: { type: Date, required: false },
|
|
initialPoints: { type: Number, required: true, default: 1000 },
|
|
topTierThreshold: {
|
|
type: String,
|
|
enum: AUCTION_TOP_TIER_THRESHOLDS,
|
|
required: true,
|
|
default: 'NONE',
|
|
},
|
|
manualTopTierUserIds: {
|
|
type: [Schema.Types.ObjectId],
|
|
ref: 'user',
|
|
required: true,
|
|
default: [],
|
|
},
|
|
chatMessages: {
|
|
type: [AuctionChatMessageSubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
startedAt: { type: Date, required: false },
|
|
completedAt: { type: Date, required: false },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'auction',
|
|
},
|
|
);
|
|
|
|
const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
|
{
|
|
auctionId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'auction',
|
|
required: true,
|
|
index: true,
|
|
},
|
|
matchId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'match',
|
|
required: true,
|
|
index: true,
|
|
},
|
|
sequence: { type: Number, required: true, min: 1 },
|
|
action: {
|
|
type: String,
|
|
enum: [
|
|
'start',
|
|
'open-bidding',
|
|
'bid',
|
|
'sold',
|
|
'passed',
|
|
'next',
|
|
'complete',
|
|
],
|
|
required: true,
|
|
},
|
|
actorUserId: { type: Schema.Types.ObjectId, ref: 'user', required: false },
|
|
snapshotBefore: { type: Schema.Types.Mixed, required: true },
|
|
takenAt: { type: Date, required: true, default: Date.now },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'auctionHistory',
|
|
},
|
|
);
|
|
|
|
AuctionHistoryEntrySchema.index({ auctionId: 1, sequence: -1 });
|
|
|
|
export const MatchStatsSchema: mongoose.Schema<MatchStats> = new Schema(
|
|
{
|
|
matchId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'match',
|
|
required: true,
|
|
unique: true,
|
|
},
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
scheduledAt: { type: Date, required: true },
|
|
gameCreationMin: { type: Number, required: true },
|
|
gameCreationMax: { type: Number, required: true },
|
|
userIds: { type: [Schema.Types.ObjectId], required: true, default: [] },
|
|
championIds: { type: [Number], required: true, default: [] },
|
|
matchCard: { type: Schema.Types.Mixed, required: true },
|
|
records: { type: [Schema.Types.Mixed], required: true, default: [] },
|
|
gameDetails: { type: [Schema.Types.Mixed], required: true, default: [] },
|
|
builtAt: { type: Date, required: true, default: Date.now },
|
|
builderVersion: { type: Number, required: true, default: 1 },
|
|
} as mongoose.SchemaDefinition,
|
|
{ timestamps: true, collection: 'match_stats' },
|
|
) as mongoose.Schema<MatchStats>;
|
|
|
|
MatchStatsSchema.index({ communityId: 1, scheduledAt: -1 });
|
|
MatchStatsSchema.index({
|
|
communityId: 1,
|
|
gameCreationMin: 1,
|
|
gameCreationMax: 1,
|
|
});
|
|
MatchStatsSchema.index({ communityId: 1, userIds: 1 });
|
|
MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
|
|
|
export const tournamentModelDefinitions: ModelDefinition[] = [
|
|
{ name: 'identity', schema: IdentitySchema, schemaName: 'identity' },
|
|
{ name: 'community', schema: CommunitySchema, schemaName: 'community' },
|
|
{
|
|
name: 'dakaoInboundMessage',
|
|
schema: DakaoInboundMessageSchema,
|
|
schemaName: 'dakaoInboundMessage',
|
|
},
|
|
{
|
|
name: 'dakaoOutboxMessage',
|
|
schema: DakaoOutboxMessageSchema,
|
|
schemaName: 'dakaoOutboxMessage',
|
|
},
|
|
{
|
|
name: 'dakaoSendResultEvent',
|
|
schema: DakaoSendResultEventSchema,
|
|
schemaName: 'dakaoSendResultEvent',
|
|
},
|
|
{ name: 'user', schema: UserSchema, schemaName: 'user' },
|
|
{ name: 'riotMatch', schema: RiotMatchSchema, schemaName: 'riotMatch' },
|
|
{
|
|
name: 'tournamentCode',
|
|
schema: TournamentCodeSchema,
|
|
schemaName: 'tournamentCode',
|
|
},
|
|
{
|
|
name: 'legacyImportJob',
|
|
schema: LegacyImportJobSchema,
|
|
schemaName: 'legacyImportJob',
|
|
},
|
|
{ name: 'match', schema: MatchSchema, schemaName: 'match' },
|
|
{ name: 'auction', schema: AuctionSchema, schemaName: 'auction' },
|
|
{
|
|
name: 'auctionHistory',
|
|
schema: AuctionHistoryEntrySchema,
|
|
schemaName: 'auctionHistory',
|
|
},
|
|
{
|
|
name: 'matchStats',
|
|
schema: MatchStatsSchema,
|
|
schemaName: 'matchStats',
|
|
},
|
|
];
|
|
|
|
export interface TournamentModelTypes {
|
|
identity: Identity;
|
|
community: Community;
|
|
dakaoInboundMessage: DakaoInboundMessage;
|
|
dakaoOutboxMessage: DakaoOutboxMessage;
|
|
dakaoSendResultEvent: DakaoSendResultEvent;
|
|
user: User;
|
|
riotMatch: RiotMatch;
|
|
tournamentCode: TournamentCode;
|
|
legacyImportJob: LegacyImportJob;
|
|
match: Match;
|
|
auction: Auction;
|
|
auctionHistory: AuctionHistoryEntry;
|
|
matchStats: MatchStats;
|
|
}
|