Files
gridpilot.gg/packages/racing/domain/services/ScheduleCalculator.ts
2025-12-07 00:18:02 +01:00

147 lines
4.6 KiB
TypeScript

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;
}