Files
lolympus-tournament-support/apps/api/src/modules/match/dto/create-match.dto.ts
T

233 lines
4.2 KiB
TypeScript

import { Type } from 'class-transformer';
import {
ArrayMaxSize,
ArrayMinSize,
registerDecorator,
IsArray,
IsBoolean,
IsDate,
IsEnum,
IsInt,
IsIn,
IsOptional,
IsString,
MaxLength,
Min,
MinLength,
ValidateNested,
ValidationArguments,
ValidationOptions,
} from 'class-validator';
import { POSITIONS } from '../../global/database/schema';
import type { MatchGameMode, Position } from '../../global/database/schema';
const LIVE_MATCH_GAME_MODES = [
'pair',
'round-robin',
'double-elimination',
] as const;
type LiveMatchGameMode = Exclude<MatchGameMode, 'bracket'>;
function IsMultipleOf(
multiple: number,
validationOptions?: ValidationOptions,
): PropertyDecorator {
return (object, propertyName) => {
registerDecorator({
name: 'isMultipleOf',
target: object.constructor,
propertyName: String(propertyName),
constraints: [multiple],
options: validationOptions,
validator: {
validate(value: unknown, args: ValidationArguments) {
const [divisor] = args.constraints as [number];
return (
typeof value === 'number' &&
Number.isInteger(value) &&
value % divisor === 0
);
},
defaultMessage(args: ValidationArguments) {
const [divisor] = args.constraints as [number];
return `${args.property} must be a multiple of ${divisor}.`;
},
},
});
};
}
export class PositionPreferenceDto {
@IsEnum(POSITIONS)
position!: Position;
@IsInt()
@Min(1)
priority!: number;
}
class MatchScheduleItemDto {
@IsString()
@MaxLength(32)
time!: string;
@IsString()
@MaxLength(128)
label!: string;
}
export class CreateMatchDto {
@IsString()
@MinLength(1)
@MaxLength(128)
title!: string;
@IsOptional()
@IsString()
@MaxLength(2048)
description?: string;
@IsOptional()
@IsInt()
mapId?: number;
@IsDate()
@Type(() => Date)
scheduledAt!: Date;
@IsOptional()
@IsInt()
@Min(10)
@IsMultipleOf(10)
maxParticipants?: number;
@IsOptional()
@IsInt()
@Min(1)
gameCount?: number;
@IsOptional()
@IsIn(LIVE_MATCH_GAME_MODES)
gameMode?: LiveMatchGameMode;
@IsOptional()
@IsInt()
@Min(1)
roundRobinMaxRounds?: number;
@IsOptional()
@IsBoolean()
doubleEliminationResetFinalEnabled?: boolean;
@IsOptional()
@IsArray()
@IsString({ each: true })
@ArrayMaxSize(32)
prizes?: string[];
@IsOptional()
@IsArray()
@IsString({ each: true })
@ArrayMaxSize(32)
notices?: string[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => MatchScheduleItemDto)
@ArrayMaxSize(32)
schedule?: MatchScheduleItemDto[];
}
export class UpdateMatchDto {
@IsOptional()
@IsString()
@MaxLength(128)
title?: string;
@IsOptional()
@IsString()
@MaxLength(2048)
description?: string;
@IsOptional()
@IsInt()
mapId?: number;
@IsOptional()
@IsDate()
@Type(() => Date)
scheduledAt?: Date;
@IsOptional()
@IsInt()
@Min(10)
@IsMultipleOf(10)
maxParticipants?: number;
@IsOptional()
@IsInt()
@Min(1)
gameCount?: number;
@IsOptional()
@IsIn(LIVE_MATCH_GAME_MODES)
gameMode?: LiveMatchGameMode;
@IsOptional()
@IsInt()
@Min(1)
roundRobinMaxRounds?: number;
@IsOptional()
@IsBoolean()
doubleEliminationResetFinalEnabled?: boolean;
@IsOptional()
@IsArray()
@IsString({ each: true })
prizes?: string[];
@IsOptional()
@IsArray()
@IsString({ each: true })
notices?: string[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => MatchScheduleItemDto)
schedule?: MatchScheduleItemDto[];
}
export class UpdateParticipantDto {
@IsEnum(POSITIONS)
mainPosition!: Position;
@IsEnum(POSITIONS)
subPosition!: Position;
}
export class JoinMatchDto {
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(5)
@ValidateNested({ each: true })
@Type(() => PositionPreferenceDto)
preferredPositions!: PositionPreferenceDto[];
}
export class AddParticipantDto {
@IsOptional()
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(5)
@ValidateNested({ each: true })
@Type(() => PositionPreferenceDto)
preferredPositions?: PositionPreferenceDto[];
@IsOptional()
@IsString()
riotPuuid?: string;
}