more seeds

This commit is contained in:
2025-12-27 10:43:55 +01:00
parent 58d9a1c762
commit 91612e4256
16 changed files with 1713 additions and 72 deletions

View File

@@ -8,12 +8,45 @@ export class RacingResultFactory {
const completed = races.filter((r) => r.status === 'completed');
for (const race of completed) {
const participants = drivers.slice(0, Math.min(16, drivers.length));
if (drivers.length === 0) continue;
for (let idx = 0; idx < participants.length; idx++) {
const driver = participants[idx]!;
const rng = this.mulberry32(this.hashToSeed(`${race.id}:${race.leagueId}:${race.trackId}`));
const minSize = Math.min(6, drivers.length);
const maxSize = Math.min(26, drivers.length);
const participantCount = Math.max(
minSize,
Math.min(maxSize, minSize + Math.floor(rng() * (maxSize - minSize + 1))),
);
const offset = Math.floor(rng() * drivers.length);
const participants = Array.from({ length: participantCount }, (_, idx) => drivers[(offset + idx) % drivers.length]!);
const finishOrder = [...participants];
this.shuffleInPlace(finishOrder, rng);
const gridOrder = [...participants];
this.shuffleInPlace(gridOrder, rng);
const baseLap = 82_000 + Math.floor(rng() * 12_000); // 1:22.000 - 1:34.000-ish
for (let idx = 0; idx < finishOrder.length; idx++) {
const driver = finishOrder[idx]!;
const position = idx + 1;
const startPosition = ((idx + 3) % participants.length) + 1;
const startPosition = gridOrder.findIndex(d => d.id.toString() === driver.id.toString()) + 1;
const lapJitter = Math.floor(rng() * 900);
const fastestLap = baseLap + lapJitter + (position - 1) * 35;
const incidents =
rng() < 0.55
? 0
: rng() < 0.85
? 1
: rng() < 0.95
? 2
: 3 + Math.floor(rng() * 6);
results.push(
RaceResult.create({
@@ -21,9 +54,9 @@ export class RacingResultFactory {
raceId: race.id,
driverId: driver.id,
position,
startPosition,
fastestLap: 88_000 + idx * 120,
incidents: idx % 4 === 0 ? 2 : 0,
startPosition: Math.max(1, startPosition),
fastestLap,
incidents,
}),
);
}
@@ -31,4 +64,33 @@ export class RacingResultFactory {
return results;
}
private shuffleInPlace<T>(items: T[], rng: () => number): void {
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(rng() * (i + 1));
const tmp = items[i]!;
items[i] = items[j]!;
items[j] = tmp;
}
}
private hashToSeed(input: string): number {
let hash = 2166136261;
for (let i = 0; i < input.length; i++) {
hash ^= input.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
private mulberry32(seed: number): () => number {
let a = seed >>> 0;
return () => {
a |= 0;
a = (a + 0x6D2B79F5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
}