修复buddy rehatch的问题

This commit is contained in:
lijiguo 2026-04-01 18:18:23 +08:00
parent c57ad656ad
commit f71530a10c
3 changed files with 16 additions and 8 deletions

View File

@ -116,18 +116,21 @@ export function rollWithSeed(seed: string): Roll {
return rollFrom(mulberry32(hashString(seed))) return rollFrom(mulberry32(hashString(seed)))
} }
export function generateSeed(): string {
return `rehatch-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
}
export function companionUserId(): string { export function companionUserId(): string {
const config = getGlobalConfig() const config = getGlobalConfig()
return config.oauthAccount?.accountUuid ?? config.userID ?? 'anon' return config.oauthAccount?.accountUuid ?? config.userID ?? 'anon'
} }
// Regenerate bones from userId, merge with stored soul. Bones never persist // Regenerate bones from seed or userId, merge with stored soul.
// so species renames and SPECIES-array edits can't break stored companions,
// and editing config.companion can't fake a rarity.
export function getCompanion(): Companion | undefined { export function getCompanion(): Companion | undefined {
const stored = getGlobalConfig().companion const stored = getGlobalConfig().companion
if (!stored) return undefined if (!stored) return undefined
const { bones } = roll(companionUserId()) const seed = stored.seed ?? companionUserId()
const { bones } = rollWithSeed(seed)
// bones last so stale bones fields in old-format configs get overridden // bones last so stale bones fields in old-format configs get overridden
return { ...stored, ...bones } return { ...stored, ...bones }
} }

View File

@ -111,6 +111,7 @@ export type CompanionBones = {
export type CompanionSoul = { export type CompanionSoul = {
name: string name: string
personality: string personality: string
seed?: string
} }
export type Companion = CompanionBones & export type Companion = CompanionBones &

View File

@ -1,8 +1,8 @@
import { import {
getCompanion, getCompanion,
roll, rollWithSeed,
generateSeed,
type Roll, type Roll,
companionUserId,
} from '../../buddy/companion.js' } from '../../buddy/companion.js'
import { import {
type StoredCompanion, type StoredCompanion,
@ -133,7 +133,8 @@ export const call: LocalCommandCall = async (args, _context) => {
} }
} }
const r = roll(companionUserId()) const seed = generateSeed()
const r = rollWithSeed(seed)
const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy' const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy'
const personality = const personality =
SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.' SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.'
@ -141,6 +142,7 @@ export const call: LocalCommandCall = async (args, _context) => {
const stored: StoredCompanion = { const stored: StoredCompanion = {
name, name,
personality, personality,
seed,
hatchedAt: Date.now(), hatchedAt: Date.now(),
} }
@ -212,7 +214,8 @@ export const call: LocalCommandCall = async (args, _context) => {
// /buddy rehatch — re-roll a new companion (replaces existing) // /buddy rehatch — re-roll a new companion (replaces existing)
if (sub === 'rehatch') { if (sub === 'rehatch') {
const r = roll(companionUserId()) const seed = generateSeed()
const r = rollWithSeed(seed)
const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy' const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy'
const personality = const personality =
SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.' SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.'
@ -220,6 +223,7 @@ export const call: LocalCommandCall = async (args, _context) => {
const stored: StoredCompanion = { const stored: StoredCompanion = {
name, name,
personality, personality,
seed,
hatchedAt: Date.now(), hatchedAt: Date.now(),
} }