fix(game): numbered name fallback when pool exhausted — server no longer freezes past 128 systems
CI / build + unit + e2e (pull_request) Successful in 2m38s
CI / build + unit + e2e (pull_request) Successful in 2m38s
Production freeze reported after the v0.9.3 (#21) deploy: every API route hung and all panels stayed at LOADING. Root cause was not the #21 layout (verified healthy across all 7 tabs/viewports) but makeName's unbounded rejection loop over a finite 16x8=128 system-name pool — once a save grew past ~128 systems, any jump into an undiscovered system spun forever on Node's single thread (sql.js is synchronous), blocking the whole process. makeName and makeCrewName now fall back to numbered variants ('Kepler Drift 2') once their pools are exhausted, so they always terminate.
This commit is contained in:
+17
-5
@@ -36,13 +36,25 @@ function title(word: string): string {
|
||||
return word.charAt(0) + word.slice(1).toLowerCase()
|
||||
}
|
||||
|
||||
// Picks a name not in `taken` and adds it, so callers can't accidentally reuse one.
|
||||
// Picks a name not in `taken` and adds it, so callers can't accidentally reuse one. The base
|
||||
// pool is finite (16×8=128), so once every base is taken the loop would spin forever — which
|
||||
// froze the whole server on a save that had grown past 128 systems (synchronous sql.js runs on
|
||||
// Node's single thread). Numbered variants keep names unique and terminate.
|
||||
export function makeName(rng: () => number, taken: Set<string>): string {
|
||||
for (;;) {
|
||||
const name = `${title(FIRST[Math.floor(rng() * FIRST.length)])} ${title(SECOND[Math.floor(rng() * SECOND.length)])}`
|
||||
if (!taken.has(name)) {
|
||||
taken.add(name)
|
||||
return name
|
||||
const base = `${title(FIRST[Math.floor(rng() * FIRST.length)])} ${title(SECOND[Math.floor(rng() * SECOND.length)])}`
|
||||
if (!taken.has(base)) {
|
||||
taken.add(base)
|
||||
return base
|
||||
}
|
||||
if (taken.size >= FIRST.length * SECOND.length) {
|
||||
for (let n = 2; ; n++) {
|
||||
const name = `${base} ${n}`
|
||||
if (!taken.has(name)) {
|
||||
taken.add(name)
|
||||
return name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user