fix(db): consistent starting xp in crew_skills seeder (#45)

This commit is contained in:
2026-10-04 02:19:36 +00:00
parent 7f2121a717
commit 5bc411cb76
2 changed files with 13 additions and 11 deletions
+7 -5
View File
@@ -3,7 +3,7 @@ import type { GameDb } from '@/lib/db/client'
import { persist } from '@/lib/db/client'
import * as schema from '@/lib/db/schema'
import { generateStartingCrew, rollCrewMember, startingCrewRng, type CrewMember } from './crew'
import { STARTING_LEVELS, type SkillName } from './crew-costs'
import { STARTING_LEVELS, XP_THRESHOLDS, type SkillName } from './crew-costs'
import { BASE_CAPACITY, BASE_LEVEL, SHIP_TYPES } from './ship-costs'
import { checkMissionCompletion, topUpMissionBoard } from './missions'
import { resolveResearchTask, ResearchError } from './research'
@@ -138,9 +138,11 @@ async function seedStartingTech(db: GameDb, saveId: number): Promise<void> {
}
// Idempotent: for each crew member with no crew_skills rows yet, seed one row per skill
// (level = STARTING_LEVELS[role][skill], xp = 0). Called by createNewGame and the v8 backfill.
// A role missing from STARTING_LEVELS seeds four all-L0 rows instead of throwing — the
// seeder must never brick a save.
// (level = STARTING_LEVELS[role][skill], xp = the threshold that level implies — levelFromXp
// must round-trip: a seeded L2 starts at xp 10, not 0, or Task 14's grantXp (which recomputes
// level from cumulative xp) would regress the member L2 → L0). Called by createNewGame and the
// v8 backfill. A role missing from STARTING_LEVELS seeds four all-L0 rows instead of throwing
// — the seeder must never brick a save.
async function seedCrewSkills(db: GameDb, saveId: number) {
const crew = await db.db.select().from(schema.crew).where(eq(schema.crew.saveId, saveId))
const have = new Set((await db.db.select({ crewId: schema.crewSkills.crewId }).from(schema.crewSkills)).map((r) => r.crewId))
@@ -148,7 +150,7 @@ async function seedCrewSkills(db: GameDb, saveId: number) {
if (have.has(m.id)) continue // already seeded — idempotent
const levels = STARTING_LEVELS[m.role] ?? { NAVIGATION: 0, SCANNING: 0, ENGINEERING: 0, RESEARCH: 0 }
for (const skill of Object.keys(levels) as SkillName[]) {
await db.db.insert(schema.crewSkills).values({ saveId, crewId: m.id, skill, level: levels[skill], xp: 0 })
await db.db.insert(schema.crewSkills).values({ saveId, crewId: m.id, skill, level: levels[skill], xp: levels[skill] > 0 ? XP_THRESHOLDS[levels[skill] - 1] : 0 })
}
}
}