Skip to content

Commit f71530a

Browse files
author
lijiguo
committed
修复buddy rehatch的问题
1 parent c57ad65 commit f71530a

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/buddy/companion.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,21 @@ export function rollWithSeed(seed: string): Roll {
116116
return rollFrom(mulberry32(hashString(seed)))
117117
}
118118

119+
export function generateSeed(): string {
120+
return `rehatch-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
121+
}
122+
119123
export function companionUserId(): string {
120124
const config = getGlobalConfig()
121125
return config.oauthAccount?.accountUuid ?? config.userID ?? 'anon'
122126
}
123127

124-
// Regenerate bones from userId, merge with stored soul. Bones never persist
125-
// so species renames and SPECIES-array edits can't break stored companions,
126-
// and editing config.companion can't fake a rarity.
128+
// Regenerate bones from seed or userId, merge with stored soul.
127129
export function getCompanion(): Companion | undefined {
128130
const stored = getGlobalConfig().companion
129131
if (!stored) return undefined
130-
const { bones } = roll(companionUserId())
132+
const seed = stored.seed ?? companionUserId()
133+
const { bones } = rollWithSeed(seed)
131134
// bones last so stale bones fields in old-format configs get overridden
132135
return { ...stored, ...bones }
133136
}

src/buddy/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export type CompanionBones = {
111111
export type CompanionSoul = {
112112
name: string
113113
personality: string
114+
seed?: string
114115
}
115116

116117
export type Companion = CompanionBones &

src/commands/buddy/buddy.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
getCompanion,
3-
roll,
3+
rollWithSeed,
4+
generateSeed,
45
type Roll,
5-
companionUserId,
66
} from '../../buddy/companion.js'
77
import {
88
type StoredCompanion,
@@ -133,14 +133,16 @@ export const call: LocalCommandCall = async (args, _context) => {
133133
}
134134
}
135135

136-
const r = roll(companionUserId())
136+
const seed = generateSeed()
137+
const r = rollWithSeed(seed)
137138
const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy'
138139
const personality =
139140
SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.'
140141

141142
const stored: StoredCompanion = {
142143
name,
143144
personality,
145+
seed,
144146
hatchedAt: Date.now(),
145147
}
146148

@@ -212,14 +214,16 @@ export const call: LocalCommandCall = async (args, _context) => {
212214

213215
// /buddy rehatch — re-roll a new companion (replaces existing)
214216
if (sub === 'rehatch') {
215-
const r = roll(companionUserId())
217+
const seed = generateSeed()
218+
const r = rollWithSeed(seed)
216219
const name = SPECIES_NAMES[r.bones.species] ?? 'Buddy'
217220
const personality =
218221
SPECIES_PERSONALITY[r.bones.species] ?? 'Mysterious and code-savvy.'
219222

220223
const stored: StoredCompanion = {
221224
name,
222225
personality,
226+
seed,
223227
hatchedAt: Date.now(),
224228
}
225229

0 commit comments

Comments
 (0)