Files
gridpilot.gg/adapters/bootstrap/racing/RacingRaceFactory.ts
2025-12-27 01:53:36 +01:00

74 lines
2.0 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';
import { Track } from '@core/racing/domain/entities/Track';
export class RacingRaceFactory {
constructor(private readonly baseDate: Date) {}
create(leagues: League[], tracks: Track[]): Race[] {
const cars = ['GT3 Porsche 911', 'GT3 BMW M4', 'LMP3 Prototype', 'GT4 Alpine', 'Touring Civic'];
const leagueIds = leagues.map((l) => l.id.toString());
const trackIds = tracks.map((t) => t.id);
const demoLeagueId = 'league-5';
const races: Race[] = [];
for (let i = 1; i <= 50; i++) {
const leagueId = leagueIds[(i - 1) % leagueIds.length] ?? demoLeagueId;
const trackId = trackIds[(i - 1) % trackIds.length]!;
const track = tracks.find(t => t.id === trackId)!;
const scheduledAt = this.addDays(this.baseDate, i <= 10 ? -35 + i : 1 + (i - 10) * 2);
const base = {
id: `race-${i}`,
leagueId,
scheduledAt,
track: track.name.toString(),
trackId: track.id,
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);
}
}