171 lines
5.1 KiB
TypeScript
171 lines
5.1 KiB
TypeScript
import type {
|
|
MatchDetailResponse,
|
|
MatchEventType,
|
|
MatchTierView,
|
|
} from '@lolympus/contracts';
|
|
import type {
|
|
MatchGameMode,
|
|
MatchStatus,
|
|
Position,
|
|
} from '../global/database/schema';
|
|
|
|
type IdLike = string | { toString(): string };
|
|
|
|
interface PublicMatchSource {
|
|
_id: IdLike;
|
|
communityId: IdLike;
|
|
competitionId?: IdLike;
|
|
shortCode?: string;
|
|
title: string;
|
|
eventType?: MatchEventType;
|
|
description: string;
|
|
mapId: number;
|
|
status: MatchStatus;
|
|
scheduledAt: Date | string;
|
|
maxParticipants: number;
|
|
gameCount: number;
|
|
gameMode: MatchGameMode;
|
|
roundRobinMaxRounds?: number;
|
|
doubleElimination?: {
|
|
resetFinalEnabled?: boolean;
|
|
grandFinalGameCount?: 1 | 3 | 5;
|
|
};
|
|
hostUserId?: IdLike;
|
|
participants: Array<{
|
|
userId: IdLike;
|
|
gameName?: string;
|
|
tagLine?: string;
|
|
profileIconId?: number;
|
|
tier?: unknown;
|
|
preferredPositions: Array<{ position: Position; priority: number }>;
|
|
joinedAt: Date | string;
|
|
excluded?: boolean;
|
|
[key: string]: unknown;
|
|
}>;
|
|
teams: Array<{
|
|
teamIndex: number;
|
|
name: string;
|
|
captainUserId?: IdLike;
|
|
members: Array<{
|
|
userId: IdLike;
|
|
assignedPosition: Position;
|
|
cost?: number;
|
|
[key: string]: unknown;
|
|
}>;
|
|
[key: string]: unknown;
|
|
}>;
|
|
prizes: string[];
|
|
notices: string[];
|
|
schedule: Array<{ time: string; label: string }>;
|
|
createdAt: Date | string;
|
|
updatedAt: Date | string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
/** Public Match DTO. Competitive results and Tournament Codes live elsewhere. */
|
|
export function presentPublicMatch(
|
|
source: PublicMatchSource,
|
|
options: { joined?: boolean } = {},
|
|
): MatchDetailResponse {
|
|
return {
|
|
_id: source._id.toString() as never,
|
|
communityId: source.communityId.toString() as never,
|
|
...(source.competitionId
|
|
? { competitionId: source.competitionId.toString() as never }
|
|
: {}),
|
|
...(source.shortCode ? { shortCode: source.shortCode } : {}),
|
|
title: source.title,
|
|
eventType: source.eventType ?? 'inhouse',
|
|
description: source.description,
|
|
mapId: source.mapId,
|
|
status: source.status,
|
|
scheduledAt: asIso(source.scheduledAt),
|
|
maxParticipants: source.maxParticipants,
|
|
gameCount: source.gameCount,
|
|
gameMode: source.gameMode === 'bracket' ? 'pair' : source.gameMode,
|
|
...(source.roundRobinMaxRounds === undefined
|
|
? {}
|
|
: { roundRobinMaxRounds: source.roundRobinMaxRounds }),
|
|
...(source.gameMode === 'double-elimination'
|
|
? {
|
|
doubleEliminationResetFinalEnabled:
|
|
source.doubleElimination?.resetFinalEnabled ?? false,
|
|
doubleEliminationGrandFinalGameCount:
|
|
source.doubleElimination?.grandFinalGameCount ?? 1,
|
|
}
|
|
: {}),
|
|
...(source.hostUserId
|
|
? { hostUserId: source.hostUserId.toString() as never }
|
|
: {}),
|
|
participants: source.participants.map((participant) => {
|
|
const tier = toPublicTier(participant.tier);
|
|
return {
|
|
userId: participant.userId.toString() as never,
|
|
...(participant.gameName ? { gameName: participant.gameName } : {}),
|
|
...(participant.tagLine ? { tagLine: participant.tagLine } : {}),
|
|
...(participant.profileIconId === undefined
|
|
? {}
|
|
: { profileIconId: participant.profileIconId }),
|
|
...(tier ? { tier } : {}),
|
|
preferredPositions: participant.preferredPositions.map(
|
|
(preference) => ({
|
|
position: preference.position,
|
|
priority: preference.priority,
|
|
}),
|
|
),
|
|
joinedAt: asIso(participant.joinedAt),
|
|
excluded: participant.excluded ?? false,
|
|
};
|
|
}),
|
|
teams: source.teams.map((team) => ({
|
|
teamIndex: team.teamIndex,
|
|
name: team.name,
|
|
...(team.captainUserId
|
|
? { captainUserId: team.captainUserId.toString() as never }
|
|
: {}),
|
|
members: team.members.map((member) => ({
|
|
userId: member.userId.toString() as never,
|
|
assignedPosition: member.assignedPosition,
|
|
...(member.cost === undefined ? {} : { cost: member.cost }),
|
|
})),
|
|
})),
|
|
prizes: [...source.prizes],
|
|
notices: [...source.notices],
|
|
schedule: source.schedule.map(({ time, label }) => ({ time, label })),
|
|
joined: options.joined ?? false,
|
|
createdAt: asIso(source.createdAt),
|
|
updatedAt: asIso(source.updatedAt),
|
|
};
|
|
}
|
|
|
|
function asIso(value: Date | string): string {
|
|
return value instanceof Date ? value.toISOString() : value;
|
|
}
|
|
|
|
function toPublicTier(value: unknown): MatchTierView | undefined {
|
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
return undefined;
|
|
}
|
|
const tier = value as Record<string, unknown>;
|
|
if (
|
|
typeof tier.queueType !== 'string' ||
|
|
typeof tier.tier !== 'string' ||
|
|
typeof tier.rank !== 'string' ||
|
|
typeof tier.leaguePoints !== 'number' ||
|
|
typeof tier.wins !== 'number' ||
|
|
typeof tier.losses !== 'number' ||
|
|
!(typeof tier.fetchedAt === 'string' || tier.fetchedAt instanceof Date)
|
|
) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
queueType: tier.queueType,
|
|
tier: tier.tier,
|
|
rank: tier.rank,
|
|
leaguePoints: tier.leaguePoints,
|
|
wins: tier.wins,
|
|
losses: tier.losses,
|
|
fetchedAt: asIso(tier.fetchedAt),
|
|
};
|
|
}
|