856 lines
24 KiB
TypeScript
856 lines
24 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 CommunityRole = 'owner' | 'admin' | 'member';
|
|
export const COMMUNITY_ROLES: CommunityRole[] = ['owner', 'admin', 'member'];
|
|
|
|
export interface PositionPreference {
|
|
position: Position;
|
|
priority: number; // 1 = 최우선, 숫자가 작을수록 높은 우선순위
|
|
}
|
|
|
|
export type AuctionPhase =
|
|
| 'waiting'
|
|
| 'presenting'
|
|
| 'bidding'
|
|
| 'sold'
|
|
| 'complete';
|
|
export const AUCTION_PHASES: AuctionPhase[] = [
|
|
'waiting',
|
|
'presenting',
|
|
'bidding',
|
|
'sold',
|
|
'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;
|
|
|
|
// 최근 경기 요약
|
|
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;
|
|
}
|
|
|
|
export type CommunityDocument = HydratedDocument<Community>;
|
|
export type IdentityDocument = HydratedDocument<Identity>;
|
|
export type UserDocument = HydratedDocument<User>;
|
|
|
|
// ===== 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>;
|
|
|
|
// ===== Match (내전) =====
|
|
export interface MatchParticipant {
|
|
userId: mongoose.Types.ObjectId;
|
|
// 표시용 스냅샷 (대표 RiotAccount 기준)
|
|
puuid?: string;
|
|
gameName?: string;
|
|
tagLine?: string;
|
|
profileIconId?: number;
|
|
tier?: TierInfo;
|
|
preferredPositions: PositionPreference[];
|
|
joinedAt: Date;
|
|
}
|
|
|
|
export interface MatchTeamMember {
|
|
userId: mongoose.Types.ObjectId;
|
|
assignedPosition: Position;
|
|
cost?: number; // 경매로 영입된 경우의 입찰가
|
|
}
|
|
|
|
export interface MatchTeam {
|
|
side: TeamSide;
|
|
name: string;
|
|
captainUserId?: mongoose.Types.ObjectId;
|
|
members: MatchTeamMember[];
|
|
}
|
|
|
|
export interface MatchResult {
|
|
winnerSide: TeamSide;
|
|
blueScore: number;
|
|
redScore: number;
|
|
mvpUserId?: mongoose.Types.ObjectId;
|
|
riotMatchIds: string[]; // 내전에서 플레이된 Riot matchId들
|
|
}
|
|
|
|
export interface MatchScheduleItem {
|
|
time: string;
|
|
label: string;
|
|
}
|
|
|
|
export interface Match extends MongooseTimestamps {
|
|
communityId: mongoose.Types.ObjectId;
|
|
title: string;
|
|
description: string;
|
|
mapId: number; // Riot Match-V5 mapId (11=소환사의 협곡, 12=ARAM 등)
|
|
status: MatchStatus;
|
|
scheduledAt: Date;
|
|
maxParticipants: number;
|
|
hostUserId?: mongoose.Types.ObjectId;
|
|
participants: MatchParticipant[];
|
|
teams: MatchTeam[];
|
|
result?: MatchResult;
|
|
prizes: string[];
|
|
notices: string[];
|
|
schedule: MatchScheduleItem[];
|
|
}
|
|
|
|
export type MatchDocument = HydratedDocument<Match>;
|
|
|
|
// ===== Auction (경매) =====
|
|
export interface AuctionPlayerSnapshot {
|
|
userId: mongoose.Types.ObjectId;
|
|
// 표시용 스냅샷(대표 RiotAccount 기준) — RiotAccount에서 복사
|
|
puuid: string;
|
|
gameName: string;
|
|
tagLine: string;
|
|
profileIconId: number;
|
|
preferredPositions: PositionPreference[];
|
|
tiers: TierInfo[];
|
|
masteries: ChampionMasteryEntry[];
|
|
recentStatsSummary?: RiotRecentStatsSummary;
|
|
}
|
|
|
|
export interface AuctionTeamMember {
|
|
userId: mongoose.Types.ObjectId;
|
|
assignedPosition: Position;
|
|
cost: number;
|
|
}
|
|
|
|
export interface AuctionTeam {
|
|
side: TeamSide;
|
|
captainUserId: mongoose.Types.ObjectId;
|
|
points: number;
|
|
members: AuctionTeamMember[];
|
|
}
|
|
|
|
export interface AuctionBid {
|
|
side: TeamSide;
|
|
amount: number;
|
|
bidderUserId: mongoose.Types.ObjectId;
|
|
timestamp: Date;
|
|
}
|
|
|
|
export interface Auction extends MongooseTimestamps {
|
|
matchId: mongoose.Types.ObjectId;
|
|
phase: AuctionPhase;
|
|
teams: AuctionTeam[];
|
|
order: AuctionPlayerSnapshot[];
|
|
currentIndex: number;
|
|
bids: AuctionBid[];
|
|
currentBid: number;
|
|
currentBidder?: TeamSide;
|
|
maxTimerSeconds: number;
|
|
currentDeadline?: Date;
|
|
initialPoints: number;
|
|
startedAt?: Date;
|
|
completedAt?: Date;
|
|
}
|
|
|
|
export type AuctionDocument = HydratedDocument<Auction>;
|
|
|
|
// ===== 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 },
|
|
|
|
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 },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'community',
|
|
},
|
|
);
|
|
|
|
CommunitySchema.index({ name: 1 }, { unique: true });
|
|
CommunitySchema.index({ ownerUserId: 1 });
|
|
CommunitySchema.index({ dakaoProjectId: 1 }, { unique: true, sparse: true });
|
|
|
|
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 });
|
|
|
|
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 },
|
|
},
|
|
{ _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>(
|
|
{
|
|
side: { type: String, enum: TEAM_SIDES, required: true },
|
|
name: { type: String, required: true },
|
|
captainUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
members: { type: [MatchTeamMemberSubSchema], required: true, default: [] },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchResultSubSchema = new Schema<MatchResult>(
|
|
{
|
|
winnerSide: { type: String, enum: TEAM_SIDES, required: true },
|
|
blueScore: { type: Number, required: true, default: 0 },
|
|
redScore: { type: Number, required: true, default: 0 },
|
|
mvpUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
riotMatchIds: { type: [String], required: true, default: [] },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const MatchScheduleItemSubSchema = new Schema<MatchScheduleItem>(
|
|
{
|
|
time: { type: String, required: true },
|
|
label: { type: String, required: true },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
export const MatchSchema = new Schema<Match>(
|
|
{
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
title: { type: String, required: true },
|
|
description: { type: String, required: true, 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 },
|
|
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 },
|
|
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({ title: 'text', description: 'text' });
|
|
|
|
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: [] },
|
|
masteries: {
|
|
type: [ChampionMasteryEntrySubSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
recentStatsSummary: { type: RiotRecentStatsSummarySubSchema },
|
|
},
|
|
{ _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>(
|
|
{
|
|
side: { type: String, enum: TEAM_SIDES, required: true },
|
|
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>(
|
|
{
|
|
side: { type: String, enum: TEAM_SIDES, required: true },
|
|
amount: { type: Number, required: true },
|
|
bidderUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
timestamp: { 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: [],
|
|
},
|
|
currentIndex: { type: Number, required: true, default: 0 },
|
|
bids: { type: [AuctionBidSubSchema], required: true, default: [] },
|
|
currentBid: { type: Number, required: true, default: 0 },
|
|
currentBidder: { type: String, enum: TEAM_SIDES, required: false },
|
|
maxTimerSeconds: { type: Number, required: true, default: 30 },
|
|
currentDeadline: { type: Date, required: false },
|
|
initialPoints: { type: Number, required: true, default: 1000 },
|
|
startedAt: { type: Date, required: false },
|
|
completedAt: { type: Date, required: false },
|
|
},
|
|
{
|
|
timestamps: true,
|
|
collection: 'auction',
|
|
},
|
|
);
|
|
|
|
export const tournamentModelDefinitions: ModelDefinition[] = [
|
|
{ name: 'identity', schema: IdentitySchema, schemaName: 'identity' },
|
|
{ name: 'community', schema: CommunitySchema, schemaName: 'community' },
|
|
{ name: 'user', schema: UserSchema, schemaName: 'user' },
|
|
{ name: 'riotMatch', schema: RiotMatchSchema, schemaName: 'riotMatch' },
|
|
{ name: 'match', schema: MatchSchema, schemaName: 'match' },
|
|
{ name: 'auction', schema: AuctionSchema, schemaName: 'auction' },
|
|
];
|
|
|
|
export interface TournamentModelTypes {
|
|
identity: Identity;
|
|
community: Community;
|
|
user: User;
|
|
riotMatch: RiotMatch;
|
|
match: Match;
|
|
auction: Auction;
|
|
}
|