wip
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
import type { LeagueVisibilityType } from '../../domain/value-objects/LeagueVisibility';
|
||||
|
||||
export type LeagueStructureMode = 'solo' | 'fixedTeams';
|
||||
|
||||
/**
|
||||
* League visibility determines public visibility and ranking status.
|
||||
* - 'ranked': Public, competitive, affects driver ratings. Requires min 10 drivers.
|
||||
* - 'unranked': Private, casual with friends. No rating impact. Any number of drivers.
|
||||
*
|
||||
* For backward compatibility, 'public'/'private' are also supported in the form,
|
||||
* but the domain uses 'ranked'/'unranked'.
|
||||
*/
|
||||
export type LeagueVisibilityFormValue = LeagueVisibilityType | 'public' | 'private';
|
||||
|
||||
export interface LeagueStructureFormDTO {
|
||||
mode: LeagueStructureMode;
|
||||
maxDrivers: number;
|
||||
@@ -50,12 +62,40 @@ export interface LeagueConfigFormModel {
|
||||
basics: {
|
||||
name: string;
|
||||
description?: string;
|
||||
visibility: 'public' | 'private';
|
||||
/**
|
||||
* League visibility/ranking mode.
|
||||
* - 'ranked' (or legacy 'public'): Competitive, public, affects ratings. Min 10 drivers.
|
||||
* - 'unranked' (or legacy 'private'): Casual with friends, no rating impact.
|
||||
*/
|
||||
visibility: LeagueVisibilityFormValue;
|
||||
gameId: string;
|
||||
/**
|
||||
* League logo as base64 data URL (optional).
|
||||
* Format: data:image/png;base64,... or data:image/jpeg;base64,...
|
||||
*/
|
||||
logoDataUrl?: string;
|
||||
};
|
||||
structure: LeagueStructureFormDTO;
|
||||
championships: LeagueChampionshipsFormDTO;
|
||||
scoring: LeagueScoringFormDTO;
|
||||
dropPolicy: LeagueDropPolicyFormDTO;
|
||||
timings: LeagueTimingsFormDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to normalize visibility values to new terminology.
|
||||
* Maps 'public' -> 'ranked' and 'private' -> 'unranked'.
|
||||
*/
|
||||
export function normalizeVisibility(value: LeagueVisibilityFormValue): LeagueVisibilityType {
|
||||
if (value === 'public' || value === 'ranked') return 'ranked';
|
||||
return 'unranked';
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to convert new terminology to legacy for backward compatibility.
|
||||
* Maps 'ranked' -> 'public' and 'unranked' -> 'private'.
|
||||
*/
|
||||
export function toLegacyVisibility(value: LeagueVisibilityFormValue): 'public' | 'private' {
|
||||
if (value === 'ranked' || value === 'public') return 'public';
|
||||
return 'private';
|
||||
}
|
||||
@@ -8,11 +8,27 @@ import type {
|
||||
LeagueScoringPresetProvider,
|
||||
LeagueScoringPresetDTO,
|
||||
} from '../ports/LeagueScoringPresetProvider';
|
||||
import {
|
||||
LeagueVisibility,
|
||||
MIN_RANKED_LEAGUE_DRIVERS,
|
||||
} from '../../domain/value-objects/LeagueVisibility';
|
||||
|
||||
/**
|
||||
* League visibility/ranking mode.
|
||||
* - 'ranked' (or legacy 'public'): Competitive, public, affects driver ratings. Min 10 drivers.
|
||||
* - 'unranked' (or legacy 'private'): Casual with friends, no rating impact.
|
||||
*/
|
||||
export type LeagueVisibilityInput = 'ranked' | 'unranked' | 'public' | 'private';
|
||||
|
||||
export interface CreateLeagueWithSeasonAndScoringCommand {
|
||||
name: string;
|
||||
description?: string;
|
||||
visibility: 'public' | 'private';
|
||||
/**
|
||||
* League visibility/ranking mode.
|
||||
* - 'ranked' (or legacy 'public'): Competitive, public, affects ratings. Requires min 10 drivers.
|
||||
* - 'unranked' (or legacy 'private'): Casual with friends, no rating impact.
|
||||
*/
|
||||
visibility: LeagueVisibilityInput;
|
||||
ownerId: string;
|
||||
gameId: string;
|
||||
maxDrivers?: number;
|
||||
@@ -137,5 +153,20 @@ export class CreateLeagueWithSeasonAndScoringUseCase {
|
||||
if (command.maxDrivers !== undefined && command.maxDrivers <= 0) {
|
||||
throw new Error('maxDrivers must be greater than 0 when provided');
|
||||
}
|
||||
|
||||
// Validate visibility-specific constraints
|
||||
const visibility = LeagueVisibility.fromString(command.visibility);
|
||||
|
||||
if (visibility.isRanked()) {
|
||||
// Ranked (public) leagues require minimum 10 drivers for competitive integrity
|
||||
const driverCount = command.maxDrivers ?? 0;
|
||||
if (driverCount < MIN_RANKED_LEAGUE_DRIVERS) {
|
||||
throw new Error(
|
||||
`Ranked leagues require at least ${MIN_RANKED_LEAGUE_DRIVERS} drivers. ` +
|
||||
`Current setting: ${driverCount}. ` +
|
||||
`For smaller groups, consider creating an Unranked (Friends) league instead.`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
278
packages/racing/domain/services/ScheduleCalculator.test.ts
Normal file
278
packages/racing/domain/services/ScheduleCalculator.test.ts
Normal file
@@ -0,0 +1,278 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { calculateRaceDates, getNextWeekday, type ScheduleConfig } from './ScheduleCalculator';
|
||||
import type { Weekday } from '../value-objects/Weekday';
|
||||
|
||||
describe('ScheduleCalculator', () => {
|
||||
describe('calculateRaceDates', () => {
|
||||
describe('with empty or invalid input', () => {
|
||||
it('should return empty array when weekdays is empty', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: [],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is 0', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 0,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is negative', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: -5,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('weekly scheduling', () => {
|
||||
it('should schedule 8 races on Saturdays starting from a Saturday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// All dates should be Saturdays
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(6); // Saturday
|
||||
});
|
||||
// First race should be Jan 6
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Last race should be 7 weeks later (Feb 24)
|
||||
expect(result.raceDates[7].toISOString().split('T')[0]).toBe('2024-02-24');
|
||||
});
|
||||
|
||||
it('should schedule races on multiple weekdays', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Wed', 'Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'), // Monday
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// Should alternate between Wednesday and Saturday
|
||||
result.raceDates.forEach(date => {
|
||||
const day = date.getDay();
|
||||
expect([3, 6]).toContain(day); // Wed=3, Sat=6
|
||||
});
|
||||
});
|
||||
|
||||
it('should schedule 8 races on Sundays', () => {
|
||||
// Given - January 7, 2024 is a Sunday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sun'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(0); // Sunday
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bi-weekly scheduling', () => {
|
||||
it('should schedule races every 2 weeks on Saturdays', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'everyNWeeks',
|
||||
rounds: 4,
|
||||
startDate: new Date('2024-01-06'),
|
||||
intervalWeeks: 2,
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(4);
|
||||
// First race Jan 6
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Second race 2 weeks later (Jan 20)
|
||||
expect(result.raceDates[1].toISOString().split('T')[0]).toBe('2024-01-20');
|
||||
// Third race 2 weeks later (Feb 3)
|
||||
expect(result.raceDates[2].toISOString().split('T')[0]).toBe('2024-02-03');
|
||||
// Fourth race 2 weeks later (Feb 17)
|
||||
expect(result.raceDates[3].toISOString().split('T')[0]).toBe('2024-02-17');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with start and end dates', () => {
|
||||
it('should evenly distribute races across the date range', () => {
|
||||
// Given - 3 month season
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-03-30'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// First race should be at or near start
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Races should be spread across the range, not consecutive weeks
|
||||
});
|
||||
|
||||
it('should use all available days if fewer than rounds requested', () => {
|
||||
// Given - short period with only 3 Saturdays
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 10,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-01-21'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// Only 3 Saturdays in this range: Jan 6, 13, 20
|
||||
expect(result.raceDates.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('season duration calculation', () => {
|
||||
it('should calculate correct season duration in weeks', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// 8 races, 1 week apart = 7 weeks duration
|
||||
expect(result.seasonDurationWeeks).toBe(7);
|
||||
});
|
||||
|
||||
it('should return 0 duration for single race', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 1,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(1);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNextWeekday', () => {
|
||||
it('should return next Saturday from a Monday', () => {
|
||||
// Given - January 1, 2024 is a Monday
|
||||
const fromDate = new Date('2024-01-01');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
expect(result.getDay()).toBe(6);
|
||||
});
|
||||
|
||||
it('should return next occurrence when already on that weekday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const fromDate = new Date('2024-01-06');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
// Should return NEXT Saturday (7 days later), not same day
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-13');
|
||||
});
|
||||
|
||||
it('should return next Sunday from a Friday', () => {
|
||||
// Given - January 5, 2024 is a Friday
|
||||
const fromDate = new Date('2024-01-05');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sun');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-07');
|
||||
expect(result.getDay()).toBe(0);
|
||||
});
|
||||
|
||||
it('should return next Wednesday from a Thursday', () => {
|
||||
// Given - January 4, 2024 is a Thursday
|
||||
const fromDate = new Date('2024-01-04');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Wed');
|
||||
|
||||
// Then
|
||||
// Next Wednesday is 6 days later
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-10');
|
||||
expect(result.getDay()).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
147
packages/racing/domain/services/ScheduleCalculator.ts
Normal file
147
packages/racing/domain/services/ScheduleCalculator.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import type { Weekday } from '../value-objects/Weekday';
|
||||
|
||||
export type RecurrenceStrategy = 'weekly' | 'everyNWeeks' | 'monthlyNthWeekday';
|
||||
|
||||
export interface ScheduleConfig {
|
||||
weekdays: Weekday[];
|
||||
frequency: RecurrenceStrategy;
|
||||
rounds: number;
|
||||
startDate: Date;
|
||||
endDate?: Date;
|
||||
intervalWeeks?: number;
|
||||
}
|
||||
|
||||
export interface ScheduleResult {
|
||||
raceDates: Date[];
|
||||
seasonDurationWeeks: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaScript getDay(): 0=Sunday, 1=Monday, 2=Tuesday, etc.
|
||||
*/
|
||||
const DAY_MAP: Record<Weekday, number> = {
|
||||
'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate race dates based on schedule configuration.
|
||||
*
|
||||
* If both startDate and endDate are provided, races are evenly distributed
|
||||
* across the selected weekdays within that range.
|
||||
*
|
||||
* If only startDate is provided, races are scheduled according to the
|
||||
* recurrence strategy (weekly or bi-weekly).
|
||||
*/
|
||||
export function calculateRaceDates(config: ScheduleConfig): ScheduleResult {
|
||||
const { weekdays, frequency, rounds, startDate, endDate, intervalWeeks } = config;
|
||||
const dates: Date[] = [];
|
||||
|
||||
if (weekdays.length === 0 || rounds <= 0) {
|
||||
return { raceDates: [], seasonDurationWeeks: 0 };
|
||||
}
|
||||
|
||||
// Convert weekday names to day numbers for faster lookup
|
||||
const selectedDayNumbers = new Set(weekdays.map(wd => DAY_MAP[wd]));
|
||||
|
||||
// If we have both start and end dates, evenly distribute races
|
||||
if (endDate && endDate > startDate) {
|
||||
const allPossibleDays: Date[] = [];
|
||||
const currentDate = new Date(startDate);
|
||||
currentDate.setHours(12, 0, 0, 0);
|
||||
|
||||
const endDateTime = new Date(endDate);
|
||||
endDateTime.setHours(12, 0, 0, 0);
|
||||
|
||||
while (currentDate <= endDateTime) {
|
||||
const dayOfWeek = currentDate.getDay();
|
||||
if (selectedDayNumbers.has(dayOfWeek)) {
|
||||
allPossibleDays.push(new Date(currentDate));
|
||||
}
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
}
|
||||
|
||||
// Evenly distribute the rounds across available days
|
||||
const totalPossible = allPossibleDays.length;
|
||||
if (totalPossible >= rounds) {
|
||||
const spacing = totalPossible / rounds;
|
||||
for (let i = 0; i < rounds; i++) {
|
||||
const index = Math.min(Math.floor(i * spacing), totalPossible - 1);
|
||||
dates.push(allPossibleDays[index]);
|
||||
}
|
||||
} else {
|
||||
// Not enough days - use all available
|
||||
dates.push(...allPossibleDays);
|
||||
}
|
||||
|
||||
const seasonDurationWeeks = dates.length > 1
|
||||
? Math.ceil((dates[dates.length - 1].getTime() - dates[0].getTime()) / (7 * 24 * 60 * 60 * 1000))
|
||||
: 0;
|
||||
|
||||
return { raceDates: dates, seasonDurationWeeks };
|
||||
}
|
||||
|
||||
// Schedule based on frequency (no end date)
|
||||
const currentDate = new Date(startDate);
|
||||
currentDate.setHours(12, 0, 0, 0);
|
||||
let roundsScheduled = 0;
|
||||
|
||||
// Generate race dates for up to 2 years to ensure we can schedule all rounds
|
||||
const maxDays = 365 * 2;
|
||||
let daysChecked = 0;
|
||||
const seasonStart = new Date(startDate);
|
||||
seasonStart.setHours(12, 0, 0, 0);
|
||||
|
||||
while (roundsScheduled < rounds && daysChecked < maxDays) {
|
||||
const dayOfWeek = currentDate.getDay();
|
||||
const isSelectedDay = selectedDayNumbers.has(dayOfWeek);
|
||||
|
||||
// Calculate which week this is from the start
|
||||
const daysSinceStart = Math.floor((currentDate.getTime() - seasonStart.getTime()) / (24 * 60 * 60 * 1000));
|
||||
const currentWeek = Math.floor(daysSinceStart / 7);
|
||||
|
||||
if (isSelectedDay) {
|
||||
let shouldRace = false;
|
||||
|
||||
if (frequency === 'weekly') {
|
||||
// Weekly: race every week on selected days
|
||||
shouldRace = true;
|
||||
} else if (frequency === 'everyNWeeks') {
|
||||
// Every N weeks: race only on matching week intervals
|
||||
const interval = intervalWeeks ?? 2;
|
||||
shouldRace = currentWeek % interval === 0;
|
||||
} else {
|
||||
// Default to weekly if frequency not set
|
||||
shouldRace = true;
|
||||
}
|
||||
|
||||
if (shouldRace) {
|
||||
dates.push(new Date(currentDate));
|
||||
roundsScheduled++;
|
||||
}
|
||||
}
|
||||
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
daysChecked++;
|
||||
}
|
||||
|
||||
const seasonDurationWeeks = dates.length > 1
|
||||
? Math.ceil((dates[dates.length - 1].getTime() - dates[0].getTime()) / (7 * 24 * 60 * 60 * 1000))
|
||||
: 0;
|
||||
|
||||
return { raceDates: dates, seasonDurationWeeks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next occurrence of a specific weekday from a given date.
|
||||
*/
|
||||
export function getNextWeekday(fromDate: Date, weekday: Weekday): Date {
|
||||
const targetDay = DAY_MAP[weekday];
|
||||
const result = new Date(fromDate);
|
||||
result.setHours(12, 0, 0, 0);
|
||||
|
||||
const currentDay = result.getDay();
|
||||
const daysUntilTarget = (targetDay - currentDay + 7) % 7 || 7;
|
||||
|
||||
result.setDate(result.getDate() + daysUntilTarget);
|
||||
return result;
|
||||
}
|
||||
176
packages/racing/domain/value-objects/GameConstraints.ts
Normal file
176
packages/racing/domain/value-objects/GameConstraints.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Domain Value Object: GameConstraints
|
||||
*
|
||||
* Represents game-specific constraints for leagues.
|
||||
* Different sim racing games have different maximum grid sizes.
|
||||
*/
|
||||
|
||||
export interface GameConstraintsData {
|
||||
readonly maxDrivers: number;
|
||||
readonly maxTeams: number;
|
||||
readonly defaultMaxDrivers: number;
|
||||
readonly minDrivers: number;
|
||||
readonly supportsTeams: boolean;
|
||||
readonly supportsMultiClass: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Game-specific constraints for popular sim racing games
|
||||
*/
|
||||
const GAME_CONSTRAINTS: Record<string, GameConstraintsData> = {
|
||||
iracing: {
|
||||
maxDrivers: 64,
|
||||
maxTeams: 32,
|
||||
defaultMaxDrivers: 24,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: true,
|
||||
},
|
||||
acc: {
|
||||
maxDrivers: 30,
|
||||
maxTeams: 15,
|
||||
defaultMaxDrivers: 24,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: false,
|
||||
},
|
||||
rf2: {
|
||||
maxDrivers: 64,
|
||||
maxTeams: 32,
|
||||
defaultMaxDrivers: 24,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: true,
|
||||
},
|
||||
ams2: {
|
||||
maxDrivers: 32,
|
||||
maxTeams: 16,
|
||||
defaultMaxDrivers: 20,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: true,
|
||||
},
|
||||
lmu: {
|
||||
maxDrivers: 32,
|
||||
maxTeams: 16,
|
||||
defaultMaxDrivers: 24,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: true,
|
||||
},
|
||||
// Default for unknown games
|
||||
default: {
|
||||
maxDrivers: 32,
|
||||
maxTeams: 16,
|
||||
defaultMaxDrivers: 20,
|
||||
minDrivers: 2,
|
||||
supportsTeams: true,
|
||||
supportsMultiClass: false,
|
||||
},
|
||||
};
|
||||
|
||||
export class GameConstraints {
|
||||
readonly gameId: string;
|
||||
readonly constraints: GameConstraintsData;
|
||||
|
||||
private constructor(gameId: string, constraints: GameConstraintsData) {
|
||||
this.gameId = gameId;
|
||||
this.constraints = constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get constraints for a specific game
|
||||
*/
|
||||
static forGame(gameId: string): GameConstraints {
|
||||
const lowerId = gameId.toLowerCase();
|
||||
const constraints = GAME_CONSTRAINTS[lowerId] ?? GAME_CONSTRAINTS.default;
|
||||
return new GameConstraints(lowerId, constraints);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all supported game IDs
|
||||
*/
|
||||
static getSupportedGames(): string[] {
|
||||
return Object.keys(GAME_CONSTRAINTS).filter(id => id !== 'default');
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum drivers allowed for this game
|
||||
*/
|
||||
get maxDrivers(): number {
|
||||
return this.constraints.maxDrivers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum teams allowed for this game
|
||||
*/
|
||||
get maxTeams(): number {
|
||||
return this.constraints.maxTeams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default driver count for new leagues
|
||||
*/
|
||||
get defaultMaxDrivers(): number {
|
||||
return this.constraints.defaultMaxDrivers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimum drivers required
|
||||
*/
|
||||
get minDrivers(): number {
|
||||
return this.constraints.minDrivers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this game supports team-based leagues
|
||||
*/
|
||||
get supportsTeams(): boolean {
|
||||
return this.constraints.supportsTeams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this game supports multi-class racing
|
||||
*/
|
||||
get supportsMultiClass(): boolean {
|
||||
return this.constraints.supportsMultiClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a driver count against game constraints
|
||||
*/
|
||||
validateDriverCount(count: number): { valid: boolean; error?: string } {
|
||||
if (count < this.minDrivers) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Minimum ${this.minDrivers} drivers required`,
|
||||
};
|
||||
}
|
||||
if (count > this.maxDrivers) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Maximum ${this.maxDrivers} drivers allowed for ${this.gameId.toUpperCase()}`,
|
||||
};
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a team count against game constraints
|
||||
*/
|
||||
validateTeamCount(count: number): { valid: boolean; error?: string } {
|
||||
if (!this.supportsTeams) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `${this.gameId.toUpperCase()} does not support team-based leagues`,
|
||||
};
|
||||
}
|
||||
if (count > this.maxTeams) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Maximum ${this.maxTeams} teams allowed for ${this.gameId.toUpperCase()}`,
|
||||
};
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
}
|
||||
89
packages/racing/domain/value-objects/LeagueDescription.ts
Normal file
89
packages/racing/domain/value-objects/LeagueDescription.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Domain Value Object: LeagueDescription
|
||||
*
|
||||
* Represents a valid league description with validation rules.
|
||||
*/
|
||||
|
||||
export interface LeagueDescriptionValidationResult {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const LEAGUE_DESCRIPTION_CONSTRAINTS = {
|
||||
minLength: 20,
|
||||
maxLength: 1000,
|
||||
recommendedMinLength: 50,
|
||||
} as const;
|
||||
|
||||
export class LeagueDescription {
|
||||
readonly value: string;
|
||||
|
||||
private constructor(value: string) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a league description without creating the value object
|
||||
*/
|
||||
static validate(value: string): LeagueDescriptionValidationResult {
|
||||
const trimmed = value?.trim() ?? '';
|
||||
|
||||
if (!trimmed) {
|
||||
return { valid: false, error: 'Description is required — help drivers understand your league' };
|
||||
}
|
||||
|
||||
if (trimmed.length < LEAGUE_DESCRIPTION_CONSTRAINTS.minLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Description must be at least ${LEAGUE_DESCRIPTION_CONSTRAINTS.minLength} characters — tell drivers what makes your league special`,
|
||||
};
|
||||
}
|
||||
|
||||
if (trimmed.length > LEAGUE_DESCRIPTION_CONSTRAINTS.maxLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Description must be ${LEAGUE_DESCRIPTION_CONSTRAINTS.maxLength} characters or less`,
|
||||
};
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if description meets recommended length for better engagement
|
||||
*/
|
||||
static isRecommendedLength(value: string): boolean {
|
||||
const trimmed = value?.trim() ?? '';
|
||||
return trimmed.length >= LEAGUE_DESCRIPTION_CONSTRAINTS.recommendedMinLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a LeagueDescription from a string value
|
||||
*/
|
||||
static create(value: string): LeagueDescription {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
throw new Error(validation.error);
|
||||
}
|
||||
return new LeagueDescription(value.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to create a LeagueDescription, returning null if invalid
|
||||
*/
|
||||
static tryCreate(value: string): LeagueDescription | null {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
return null;
|
||||
}
|
||||
return new LeagueDescription(value.trim());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: LeagueDescription): boolean {
|
||||
return this.value === other.value;
|
||||
}
|
||||
}
|
||||
102
packages/racing/domain/value-objects/LeagueName.ts
Normal file
102
packages/racing/domain/value-objects/LeagueName.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Domain Value Object: LeagueName
|
||||
*
|
||||
* Represents a valid league name with validation rules.
|
||||
*/
|
||||
|
||||
export interface LeagueNameValidationResult {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const LEAGUE_NAME_CONSTRAINTS = {
|
||||
minLength: 3,
|
||||
maxLength: 64,
|
||||
pattern: /^[a-zA-Z0-9].*$/, // Must start with alphanumeric
|
||||
forbiddenPatterns: [
|
||||
/^\s/, // No leading whitespace
|
||||
/\s$/, // No trailing whitespace
|
||||
/\s{2,}/, // No multiple consecutive spaces
|
||||
],
|
||||
} as const;
|
||||
|
||||
export class LeagueName {
|
||||
readonly value: string;
|
||||
|
||||
private constructor(value: string) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a league name without creating the value object
|
||||
*/
|
||||
static validate(value: string): LeagueNameValidationResult {
|
||||
const trimmed = value?.trim() ?? '';
|
||||
|
||||
if (!trimmed) {
|
||||
return { valid: false, error: 'League name is required' };
|
||||
}
|
||||
|
||||
if (trimmed.length < LEAGUE_NAME_CONSTRAINTS.minLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `League name must be at least ${LEAGUE_NAME_CONSTRAINTS.minLength} characters`,
|
||||
};
|
||||
}
|
||||
|
||||
if (trimmed.length > LEAGUE_NAME_CONSTRAINTS.maxLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `League name must be ${LEAGUE_NAME_CONSTRAINTS.maxLength} characters or less`,
|
||||
};
|
||||
}
|
||||
|
||||
if (!LEAGUE_NAME_CONSTRAINTS.pattern.test(trimmed)) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'League name must start with a letter or number',
|
||||
};
|
||||
}
|
||||
|
||||
for (const forbidden of LEAGUE_NAME_CONSTRAINTS.forbiddenPatterns) {
|
||||
if (forbidden.test(value)) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'League name cannot have leading/trailing spaces or multiple consecutive spaces',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a LeagueName from a string value
|
||||
*/
|
||||
static create(value: string): LeagueName {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
throw new Error(validation.error);
|
||||
}
|
||||
return new LeagueName(value.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to create a LeagueName, returning null if invalid
|
||||
*/
|
||||
static tryCreate(value: string): LeagueName | null {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
return null;
|
||||
}
|
||||
return new LeagueName(value.trim());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: LeagueName): boolean {
|
||||
return this.value === other.value;
|
||||
}
|
||||
}
|
||||
129
packages/racing/domain/value-objects/LeagueVisibility.ts
Normal file
129
packages/racing/domain/value-objects/LeagueVisibility.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* Domain Value Object: LeagueVisibility
|
||||
*
|
||||
* Represents the visibility and ranking status of a league.
|
||||
*
|
||||
* - 'ranked' (public): Competitive leagues visible to everyone, affects driver ratings.
|
||||
* Requires minimum 10 players to ensure competitive integrity.
|
||||
* - 'unranked' (private): Casual leagues for friends/private groups, no rating impact.
|
||||
* Can have any number of players.
|
||||
*/
|
||||
|
||||
export type LeagueVisibilityType = 'ranked' | 'unranked';
|
||||
|
||||
export interface LeagueVisibilityConstraints {
|
||||
readonly minDrivers: number;
|
||||
readonly isPubliclyVisible: boolean;
|
||||
readonly affectsRatings: boolean;
|
||||
readonly requiresApproval: boolean;
|
||||
}
|
||||
|
||||
const VISIBILITY_CONSTRAINTS: Record<LeagueVisibilityType, LeagueVisibilityConstraints> = {
|
||||
ranked: {
|
||||
minDrivers: 10,
|
||||
isPubliclyVisible: true,
|
||||
affectsRatings: true,
|
||||
requiresApproval: false, // Anyone can join public leagues
|
||||
},
|
||||
unranked: {
|
||||
minDrivers: 2,
|
||||
isPubliclyVisible: false,
|
||||
affectsRatings: false,
|
||||
requiresApproval: true, // Private leagues require invite/approval
|
||||
},
|
||||
};
|
||||
|
||||
export class LeagueVisibility {
|
||||
readonly type: LeagueVisibilityType;
|
||||
readonly constraints: LeagueVisibilityConstraints;
|
||||
|
||||
private constructor(type: LeagueVisibilityType) {
|
||||
this.type = type;
|
||||
this.constraints = VISIBILITY_CONSTRAINTS[type];
|
||||
}
|
||||
|
||||
static ranked(): LeagueVisibility {
|
||||
return new LeagueVisibility('ranked');
|
||||
}
|
||||
|
||||
static unranked(): LeagueVisibility {
|
||||
return new LeagueVisibility('unranked');
|
||||
}
|
||||
|
||||
static fromString(value: string): LeagueVisibility {
|
||||
// Support both old ('public'/'private') and new ('ranked'/'unranked') terminology
|
||||
if (value === 'ranked' || value === 'public') {
|
||||
return LeagueVisibility.ranked();
|
||||
}
|
||||
if (value === 'unranked' || value === 'private') {
|
||||
return LeagueVisibility.unranked();
|
||||
}
|
||||
throw new Error(`Invalid league visibility: ${value}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the given driver count meets the minimum requirement
|
||||
* for this visibility type.
|
||||
*/
|
||||
validateDriverCount(driverCount: number): { valid: boolean; error?: string } {
|
||||
if (driverCount < this.constraints.minDrivers) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `${this.type === 'ranked' ? 'Ranked' : 'Unranked'} leagues require at least ${this.constraints.minDrivers} drivers`,
|
||||
};
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is a ranked/public league
|
||||
*/
|
||||
isRanked(): boolean {
|
||||
return this.type === 'ranked';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is an unranked/private league
|
||||
*/
|
||||
isUnranked(): boolean {
|
||||
return this.type === 'unranked';
|
||||
}
|
||||
|
||||
/**
|
||||
* Human-readable label for UI display
|
||||
*/
|
||||
getLabel(): string {
|
||||
return this.type === 'ranked' ? 'Ranked (Public)' : 'Unranked (Friends)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Short description for UI tooltips
|
||||
*/
|
||||
getDescription(): string {
|
||||
return this.type === 'ranked'
|
||||
? 'Competitive league visible to everyone. Results affect driver ratings.'
|
||||
: 'Private league for friends. Results do not affect ratings.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to string for serialization
|
||||
*/
|
||||
toString(): LeagueVisibilityType {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* For backward compatibility with existing 'public'/'private' terminology
|
||||
*/
|
||||
toLegacyString(): 'public' | 'private' {
|
||||
return this.type === 'ranked' ? 'public' : 'private';
|
||||
}
|
||||
|
||||
equals(other: LeagueVisibility): boolean {
|
||||
return this.type === other.type;
|
||||
}
|
||||
}
|
||||
|
||||
// Export constants for validation
|
||||
export const MIN_RANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.ranked.minDrivers;
|
||||
export const MIN_UNRANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.unranked.minDrivers;
|
||||
Reference in New Issue
Block a user