126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
/**
|
|
* In-memory race registration data for alpha prototype
|
|
*/
|
|
|
|
import { getMembership } from './memberships';
|
|
|
|
import { RaceRegistration } from '@gridpilot/racing/domain/entities/RaceRegistration';
|
|
|
|
// In-memory storage (Set for quick lookups)
|
|
const registrations = new Map<string, Set<string>>(); // raceId -> Set of driverIds
|
|
|
|
/**
|
|
* Generate registration key for storage
|
|
*/
|
|
function getRegistrationKey(raceId: string, driverId: string): string {
|
|
return `${raceId}:${driverId}`;
|
|
}
|
|
|
|
/**
|
|
* Check if driver is registered for a race
|
|
*/
|
|
export function isRegistered(raceId: string, driverId: string): boolean {
|
|
const raceRegistrations = registrations.get(raceId);
|
|
return raceRegistrations ? raceRegistrations.has(driverId) : false;
|
|
}
|
|
|
|
/**
|
|
* Get all registered drivers for a race
|
|
*/
|
|
export function getRegisteredDrivers(raceId: string): string[] {
|
|
const raceRegistrations = registrations.get(raceId);
|
|
return raceRegistrations ? Array.from(raceRegistrations) : [];
|
|
}
|
|
|
|
/**
|
|
* Get registration count for a race
|
|
*/
|
|
export function getRegistrationCount(raceId: string): number {
|
|
const raceRegistrations = registrations.get(raceId);
|
|
return raceRegistrations ? raceRegistrations.size : 0;
|
|
}
|
|
|
|
/**
|
|
* Register driver for a race
|
|
* Validates league membership before registering
|
|
*/
|
|
export function registerForRace(
|
|
raceId: string,
|
|
driverId: string,
|
|
leagueId: string
|
|
): void {
|
|
// Check if already registered
|
|
if (isRegistered(raceId, driverId)) {
|
|
throw new Error('Already registered for this race');
|
|
}
|
|
|
|
// Validate league membership
|
|
const membership = getMembership(leagueId, driverId);
|
|
if (!membership || membership.status !== 'active') {
|
|
throw new Error('Must be an active league member to register for races');
|
|
}
|
|
|
|
// Add registration
|
|
if (!registrations.has(raceId)) {
|
|
registrations.set(raceId, new Set());
|
|
}
|
|
registrations.get(raceId)!.add(driverId);
|
|
}
|
|
|
|
/**
|
|
* Withdraw from a race
|
|
*/
|
|
export function withdrawFromRace(raceId: string, driverId: string): void {
|
|
const raceRegistrations = registrations.get(raceId);
|
|
if (!raceRegistrations || !raceRegistrations.has(driverId)) {
|
|
throw new Error('Not registered for this race');
|
|
}
|
|
|
|
raceRegistrations.delete(driverId);
|
|
|
|
// Clean up empty sets
|
|
if (raceRegistrations.size === 0) {
|
|
registrations.delete(raceId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all races a driver is registered for
|
|
*/
|
|
export function getDriverRegistrations(driverId: string): string[] {
|
|
const raceIds: string[] = [];
|
|
|
|
for (const [raceId, driverSet] of registrations.entries()) {
|
|
if (driverSet.has(driverId)) {
|
|
raceIds.push(raceId);
|
|
}
|
|
}
|
|
|
|
return raceIds;
|
|
}
|
|
|
|
/**
|
|
* Clear all registrations for a race (e.g., when race is cancelled)
|
|
*/
|
|
export function clearRaceRegistrations(raceId: string): void {
|
|
registrations.delete(raceId);
|
|
}
|
|
|
|
/**
|
|
* Initialize with seed data
|
|
*/
|
|
export function initializeRegistrationData(): void {
|
|
registrations.clear();
|
|
|
|
// Add some initial registrations for testing
|
|
// Race 2 (Spa-Francorchamps - upcoming)
|
|
registerForRace('race-2', 'driver-1', 'league-1');
|
|
registerForRace('race-2', 'driver-2', 'league-1');
|
|
registerForRace('race-2', 'driver-3', 'league-1');
|
|
|
|
// Race 3 (Nürburgring GP - upcoming)
|
|
registerForRace('race-3', 'driver-1', 'league-1');
|
|
}
|
|
|
|
// Initialize on module load
|
|
initializeRegistrationData(); |