refactor
This commit is contained in:
@@ -12,49 +12,28 @@ import type { IRaceRepository } from '@core/racing/domain/repositories/IRaceRepo
|
||||
import type { ILeagueRepository } from '@core/racing/domain/repositories/ILeagueRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
/**
|
||||
* Points systems presets
|
||||
*/
|
||||
const POINTS_SYSTEMS: Record<string, Record<number, number>> = {
|
||||
'f1-2024': {
|
||||
1: 25, 2: 18, 3: 15, 4: 12, 5: 10,
|
||||
6: 8, 7: 6, 8: 4, 9: 2, 10: 1
|
||||
},
|
||||
'indycar': {
|
||||
1: 50, 2: 40, 3: 35, 4: 32, 5: 30,
|
||||
6: 28, 7: 26, 8: 24, 9: 22, 10: 20,
|
||||
11: 19, 12: 18, 13: 17, 14: 16, 15: 15
|
||||
}
|
||||
};
|
||||
|
||||
export class InMemoryStandingRepository implements IStandingRepository {
|
||||
private standings: Map<string, Standing>;
|
||||
private resultRepository: IResultRepository | null;
|
||||
private raceRepository: IRaceRepository | null;
|
||||
private leagueRepository: ILeagueRepository | null;
|
||||
private readonly logger: Logger;
|
||||
private readonly pointsSystems: Record<string, Record<number, number>>;
|
||||
|
||||
constructor(
|
||||
logger: Logger,
|
||||
seedData?: Standing[],
|
||||
pointsSystems: Record<string, Record<number, number>>,
|
||||
resultRepository?: IResultRepository | null,
|
||||
raceRepository?: IRaceRepository | null,
|
||||
leagueRepository?: ILeagueRepository | null
|
||||
) {
|
||||
this.logger = logger;
|
||||
this.pointsSystems = pointsSystems;
|
||||
this.logger.info('InMemoryStandingRepository initialized.');
|
||||
this.standings = new Map();
|
||||
this.resultRepository = resultRepository ?? null;
|
||||
this.raceRepository = raceRepository ?? null;
|
||||
this.leagueRepository = leagueRepository ?? null;
|
||||
|
||||
if (seedData) {
|
||||
seedData.forEach(standing => {
|
||||
const key = this.getKey(standing.leagueId, standing.driverId);
|
||||
this.standings.set(key, standing);
|
||||
this.logger.debug(`Seeded standing for league ${standing.leagueId}, driver ${standing.driverId}.`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private getKey(leagueId: string, driverId: string): string {
|
||||
@@ -65,14 +44,14 @@ export class InMemoryStandingRepository implements IStandingRepository {
|
||||
this.logger.debug(`Finding standings for league id: ${leagueId}`);
|
||||
try {
|
||||
const standings = Array.from(this.standings.values())
|
||||
.filter(standing => standing.leagueId === leagueId)
|
||||
.filter(standing => standing.leagueId.toString() === leagueId)
|
||||
.sort((a, b) => {
|
||||
// Sort by position (lower is better)
|
||||
if (a.position !== b.position) {
|
||||
return a.position - b.position;
|
||||
if (a.position.toNumber() !== b.position.toNumber()) {
|
||||
return a.position.toNumber() - b.position.toNumber();
|
||||
}
|
||||
// If positions are equal, sort by points (higher is better)
|
||||
return b.points - a.points;
|
||||
return b.points.toNumber() - a.points.toNumber();
|
||||
});
|
||||
this.logger.info(`Found ${standings.length} standings for league id: ${leagueId}.`);
|
||||
return standings;
|
||||
@@ -162,12 +141,11 @@ export class InMemoryStandingRepository implements IStandingRepository {
|
||||
async deleteByLeagueId(leagueId: string): Promise<void> {
|
||||
this.logger.debug(`Deleting all standings for league id: ${leagueId}`);
|
||||
try {
|
||||
const initialCount = Array.from(this.standings.values()).filter(s => s.leagueId === leagueId).length;
|
||||
const toDelete = Array.from(this.standings.values())
|
||||
.filter(standing => standing.leagueId === leagueId);
|
||||
|
||||
.filter(standing => standing.leagueId.toString() === leagueId);
|
||||
|
||||
toDelete.forEach(standing => {
|
||||
const key = this.getKey(standing.leagueId, standing.driverId);
|
||||
const key = this.getKey(standing.leagueId.toString(), standing.driverId.toString());
|
||||
this.standings.delete(key);
|
||||
});
|
||||
this.logger.info(`Deleted ${toDelete.length} standings for league id: ${leagueId}.`);
|
||||
@@ -209,8 +187,8 @@ export class InMemoryStandingRepository implements IStandingRepository {
|
||||
// Get points system
|
||||
const resolvedPointsSystem =
|
||||
league.settings.customPoints ??
|
||||
POINTS_SYSTEMS[league.settings.pointsSystem] ??
|
||||
POINTS_SYSTEMS['f1-2024'];
|
||||
this.pointsSystems[league.settings.pointsSystem] ??
|
||||
this.pointsSystems['f1-2024'];
|
||||
|
||||
if (!resolvedPointsSystem) {
|
||||
this.logger.error(`No points system configured for league ${leagueId}.`);
|
||||
@@ -292,11 +270,4 @@ export class InMemoryStandingRepository implements IStandingRepository {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available points systems
|
||||
*/
|
||||
static getPointsSystems(): Record<string, Record<number, number>> {
|
||||
return POINTS_SYSTEMS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user