Files
gridpilot.gg/adapters/bootstrap/racing/RacingRaceFactory.ts
2025-12-26 23:06:23 +01:00

79 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { League } from '@core/racing/domain/entities/League';
import { Race } from '@core/racing/domain/entities/Race';
export class RacingRaceFactory {
constructor(private readonly baseDate: Date) {}
create(leagues: League[]): Race[] {
const tracks = [
'Monza GP',
'Spa-Francorchamps',
'Suzuka',
'Mount Panorama',
'Silverstone GP',
'Interlagos',
'Imola',
'Laguna Seca',
];
const cars = ['GT3 Porsche 911', 'GT3 BMW M4', 'LMP3 Prototype', 'GT4 Alpine', 'Touring Civic'];
const leagueIds = leagues.map((l) => l.id.toString());
const demoLeagueId = 'league-5';
const races: Race[] = [];
for (let i = 1; i <= 25; i++) {
const leagueId = leagueIds[(i - 1) % leagueIds.length] ?? demoLeagueId;
const scheduledAt = this.addDays(this.baseDate, i <= 10 ? -35 + i : 1 + (i - 10) * 2);
const base = {
id: `race-${i}`,
leagueId,
scheduledAt,
track: tracks[(i - 1) % tracks.length]!,
car: cars[(i - 1) % cars.length]!,
};
if (i === 1) {
races.push(
Race.create({
...base,
leagueId: demoLeagueId,
scheduledAt: this.addMinutes(this.baseDate, -30),
status: 'running',
strengthOfField: 1530,
registeredCount: 16,
}),
);
continue;
}
if (scheduledAt < this.baseDate) {
races.push(
Race.create({
...base,
status: 'completed',
}),
);
continue;
}
races.push(
Race.create({
...base,
status: 'scheduled',
}),
);
}
return races;
}
private addDays(date: Date, days: number): Date {
return new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
}
private addMinutes(date: Date, minutes: number): Date {
return new Date(date.getTime() + minutes * 60 * 1000);
}
}