@linxin666/dsh-pet 0.4.8 → 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +7 -2
- package/README.zh.md +7 -2
- package/contracts/voice-pack-v1.schema.json +27 -0
- package/lib/client.js +24 -13
- package/lib/client.js.map +1 -1
- package/lib/index.js +160 -28
- package/lib/types/affinity.d.ts +26 -3
- package/lib/types/affinity.d.ts.map +1 -1
- package/lib/types/affinity.js +27 -4
- package/lib/types/client/PetDockEntry.d.ts +5 -0
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +1 -1
- package/lib/types/client/PetSprite.d.ts +5 -0
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +20 -8
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +15 -1
- package/lib/types/index.d.ts +1 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/ledger.d.ts +10 -1
- package/lib/types/ledger.d.ts.map +1 -1
- package/lib/types/ledger.js +11 -1
- package/lib/types/pair-access.d.ts.map +1 -1
- package/lib/types/pair-access.js +26 -3
- package/lib/types/service.d.ts +14 -12
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +22 -17
- package/lib/types/voice-pack.d.ts +20 -1
- package/lib/types/voice-pack.d.ts.map +1 -1
- package/lib/types/voice-pack.js +86 -3
- package/package.json +24 -24
- package/src/affinity.test.ts +42 -0
- package/src/affinity.ts +46 -3
- package/src/client/PetDockEntry.tsx +6 -0
- package/src/client/PetSprite.test.tsx +42 -0
- package/src/client/PetSprite.tsx +24 -8
- package/src/client/index.test.tsx +4 -0
- package/src/client/index.ts +15 -1
- package/src/index.ts +2 -0
- package/src/ledger.test.ts +19 -0
- package/src/ledger.ts +15 -1
- package/src/pair-access.ts +26 -3
- package/src/service.test.ts +61 -0
- package/src/service.ts +27 -18
- package/src/voice-pack.test.ts +112 -0
- package/src/voice-pack.ts +101 -3
package/src/voice-pack.test.ts
CHANGED
|
@@ -11,12 +11,15 @@ import {
|
|
|
11
11
|
mergeVoicePacks,
|
|
12
12
|
normalizePanel,
|
|
13
13
|
normalizePool,
|
|
14
|
+
normalizeRankLadder,
|
|
14
15
|
normalizeVoicePack,
|
|
15
16
|
normalizeWhisperCategories,
|
|
16
17
|
normalizeWhisperResults,
|
|
17
18
|
PANEL_ACTIONS,
|
|
18
19
|
PANEL_LABEL_KEYS,
|
|
19
20
|
PANEL_STAT_KEYS,
|
|
21
|
+
RANK_LADDER_MAX,
|
|
22
|
+
RANK_NAME_MAX,
|
|
20
23
|
VOICE_LINE_MAX,
|
|
21
24
|
VOICE_PACK_KEYS,
|
|
22
25
|
VOICE_PACK_V1,
|
|
@@ -186,6 +189,79 @@ describe('normalizePanel', () => {
|
|
|
186
189
|
})
|
|
187
190
|
})
|
|
188
191
|
|
|
192
|
+
describe('normalizeRankLadder', () => {
|
|
193
|
+
const valid = [
|
|
194
|
+
{ min: 0, name: '小煤球', nameEn: 'Little Coal Ball' },
|
|
195
|
+
{ min: 25, name: '火星伙伴', nameEn: 'Martian Buddy' },
|
|
196
|
+
]
|
|
197
|
+
|
|
198
|
+
it('accepts an ascending ladder starting at 0 and trims names', () => {
|
|
199
|
+
const warnings: string[] = []
|
|
200
|
+
expect(normalizeRankLadder([
|
|
201
|
+
{ min: 0, name: ' 小煤球 ', nameEn: ' Little Coal Ball ' },
|
|
202
|
+
{ min: 25, name: '火星伙伴' },
|
|
203
|
+
], m => warnings.push(m))).toEqual([
|
|
204
|
+
{ min: 0, name: '小煤球', nameEn: 'Little Coal Ball' },
|
|
205
|
+
{ min: 25, name: '火星伙伴' },
|
|
206
|
+
])
|
|
207
|
+
expect(warnings).toEqual([])
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('drops the whole ladder on a structural violation', () => {
|
|
211
|
+
const cases: [unknown, string][] = [
|
|
212
|
+
[{ min: 0, name: 'A' }, 'must be an array'],
|
|
213
|
+
[[], '1-' + RANK_LADDER_MAX],
|
|
214
|
+
[[{ min: 5, name: 'A' }], 'start at min 0'],
|
|
215
|
+
[[{ min: 0, name: 'A' }, { min: 0, name: 'B' }], 'sorted ascending'],
|
|
216
|
+
[[{ min: 0, name: 'A' }, { min: 10, name: 'B' }, { min: 5, name: 'C' }], 'sorted ascending'],
|
|
217
|
+
[[{ min: -1, name: 'A' }], 'non-negative integer'],
|
|
218
|
+
[[{ min: 1.5, name: 'A' }], 'non-negative integer'],
|
|
219
|
+
[[{ min: 0, name: '' }], '1-' + RANK_NAME_MAX],
|
|
220
|
+
[[{ min: 0, name: 'x'.repeat(RANK_NAME_MAX + 1) }], '1-' + RANK_NAME_MAX],
|
|
221
|
+
[[{ min: 0, name: 'A', nameEn: 7 }], 'nameEn must be a string'],
|
|
222
|
+
[[{ min: 0, name: 'A', nameEn: 'x'.repeat(RANK_NAME_MAX + 1) }], '1-' + RANK_NAME_MAX],
|
|
223
|
+
[[{ min: 0, name: 'A' }, 'bad'], 'must be an object'],
|
|
224
|
+
]
|
|
225
|
+
for (const [raw, expected] of cases) {
|
|
226
|
+
const warnings: string[] = []
|
|
227
|
+
expect(normalizeRankLadder(raw, m => warnings.push(m))).toBeUndefined()
|
|
228
|
+
expect(warnings.join('\n')).toContain(expected)
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('caps the ladder at sixteen entries', () => {
|
|
233
|
+
const entries = Array.from({ length: RANK_LADDER_MAX + 1 }, (_, index) => ({ min: index, name: 'R' + index }))
|
|
234
|
+
const warnings: string[] = []
|
|
235
|
+
expect(normalizeRankLadder(entries, m => warnings.push(m))).toBeUndefined()
|
|
236
|
+
expect(warnings.join('\n')).toContain('1-' + RANK_LADDER_MAX)
|
|
237
|
+
expect(normalizeRankLadder(entries.slice(0, RANK_LADDER_MAX))).toHaveLength(RANK_LADDER_MAX)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it('is read through panel.ranks without disturbing the other panel slots', () => {
|
|
241
|
+
const { pack, warnings } = collectWarnings({
|
|
242
|
+
panel: { ranks: valid, labels: { feed: '投喂' }, stats: { rank: '好感 {rank}' } },
|
|
243
|
+
})
|
|
244
|
+
expect(pack?.panel?.ranks).toEqual(valid)
|
|
245
|
+
expect(pack?.panel?.labels).toEqual({ feed: '投喂' })
|
|
246
|
+
expect(pack?.panel?.stats).toEqual({ rank: '好感 {rank}' })
|
|
247
|
+
expect(warnings).toEqual([])
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it('keeps a panel that declares only a ladder', () => {
|
|
251
|
+
const { pack } = collectWarnings({ panel: { ranks: valid } })
|
|
252
|
+
expect(pack?.panel?.ranks).toEqual(valid)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('drops an invalid ladder but keeps the rest of the panel', () => {
|
|
256
|
+
const { pack, warnings } = collectWarnings({
|
|
257
|
+
panel: { ranks: [{ min: 3, name: 'A' }], labels: { feed: '投喂' } },
|
|
258
|
+
})
|
|
259
|
+
expect(pack?.panel?.ranks).toBeUndefined()
|
|
260
|
+
expect(pack?.panel?.labels).toEqual({ feed: '投喂' })
|
|
261
|
+
expect(warnings.join('\n')).toContain('start at min 0')
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
189
265
|
describe('mergeVoicePacks', () => {
|
|
190
266
|
it('merges per-key with later layers winning', () => {
|
|
191
267
|
const global = normalizeVoicePack({
|
|
@@ -202,6 +278,30 @@ describe('mergeVoicePacks', () => {
|
|
|
202
278
|
expect(merged?.panel?.labels).toEqual({ feed: '宠物投喂', hide: '宠物藏' })
|
|
203
279
|
})
|
|
204
280
|
|
|
281
|
+
it('replaces the whole rank ladder from a later layer while other panel slots merge', () => {
|
|
282
|
+
const global = normalizeVoicePack({
|
|
283
|
+
panel: {
|
|
284
|
+
ranks: [{ min: 0, name: '全局小鲸' }, { min: 25, name: '全局伙伴' }],
|
|
285
|
+
labels: { feed: '全局投喂' },
|
|
286
|
+
},
|
|
287
|
+
})
|
|
288
|
+
const pet = normalizeVoicePack({
|
|
289
|
+
panel: {
|
|
290
|
+
ranks: [{ min: 0, name: '小煤球', nameEn: 'Little Coal Ball' }],
|
|
291
|
+
labels: { hide: '宠物藏' },
|
|
292
|
+
},
|
|
293
|
+
})
|
|
294
|
+
const merged = mergeVoicePacks(global, pet)
|
|
295
|
+
expect(merged?.panel?.ranks).toEqual([{ min: 0, name: '小煤球', nameEn: 'Little Coal Ball' }])
|
|
296
|
+
expect(merged?.panel?.labels).toEqual({ feed: '全局投喂', hide: '宠物藏' })
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
it('keeps the global ladder for a pet that declares none', () => {
|
|
300
|
+
const global = normalizeVoicePack({ panel: { ranks: [{ min: 0, name: '全局小鲸' }] } })
|
|
301
|
+
const pet = normalizeVoicePack({ panel: { labels: { feed: '投喂' } } })
|
|
302
|
+
expect(mergeVoicePacks(global, pet)?.panel?.ranks).toEqual([{ min: 0, name: '全局小鲸' }])
|
|
303
|
+
})
|
|
304
|
+
|
|
205
305
|
it('lets the pet pack replace whisper keys while the global stays', () => {
|
|
206
306
|
const global = normalizeVoicePack({ whispers: { categories: { thinking: ['全局思考'] }, results: { pass: ['全局全绿'] } } })
|
|
207
307
|
const pet = normalizeVoicePack({ whispers: { categories: { thinking: ['宠物思考'] } } })
|
|
@@ -270,6 +370,18 @@ describe('voice-pack schema file drift lock', () => {
|
|
|
270
370
|
expect(new Set(Object.keys(stats))).toEqual(new Set(PANEL_STAT_KEYS))
|
|
271
371
|
const actions = (panel.actions as { items?: { enum?: string[] } } | undefined)
|
|
272
372
|
expect(actions?.items?.enum).toEqual([...PANEL_ACTIONS])
|
|
373
|
+
const ranks = panel.ranks as {
|
|
374
|
+
minItems?: number
|
|
375
|
+
maxItems?: number
|
|
376
|
+
items?: { required?: string[]; properties?: Record<string, { maxLength?: number }> }
|
|
377
|
+
} | undefined
|
|
378
|
+
expect(ranks?.minItems).toBe(1)
|
|
379
|
+
expect(ranks?.maxItems).toBe(RANK_LADDER_MAX)
|
|
380
|
+
expect(ranks?.items?.required).toEqual(['min', 'name'])
|
|
381
|
+
const rankProps = ranks?.items?.properties ?? {}
|
|
382
|
+
expect(new Set(Object.keys(rankProps))).toEqual(new Set(['min', 'name', 'nameEn']))
|
|
383
|
+
expect(rankProps.name?.maxLength).toBe(RANK_NAME_MAX)
|
|
384
|
+
expect(rankProps.nameEn?.maxLength).toBe(RANK_NAME_MAX)
|
|
273
385
|
})
|
|
274
386
|
|
|
275
387
|
it('keeps the schema version const in sync', () => {
|
package/src/voice-pack.ts
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
type WhisperResult,
|
|
39
39
|
} from './chatter.ts'
|
|
40
40
|
import { normalizePetRemarks, type PetRemarks } from './remarks.ts'
|
|
41
|
+
import type { AffinityRankName } from './affinity.ts'
|
|
41
42
|
|
|
42
43
|
/** Schema version this module normalizes (optional field; missing = 1). */
|
|
43
44
|
export const VOICE_PACK_V1 = 1 as const
|
|
@@ -62,6 +63,11 @@ export interface PetPanelView {
|
|
|
62
63
|
stats?: Partial<Record<PanelStatKey, string>>
|
|
63
64
|
/** Actions to render in canonical order; absent = all three; [] = none. */
|
|
64
65
|
actions?: PanelAction[]
|
|
66
|
+
/**
|
|
67
|
+
* Display-only affinity ladder: replaces the built-in tier names for this
|
|
68
|
+
* pet. Points, rewards, cooldowns and the persisted ledger are untouched.
|
|
69
|
+
*/
|
|
70
|
+
ranks?: readonly AffinityRankName[]
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
/** One normalized voice pack (a pet's voice.json or the global override). */
|
|
@@ -82,6 +88,11 @@ export const VOICE_LINE_MAX = 160
|
|
|
82
88
|
export const VOICE_LABEL_MAX = 40
|
|
83
89
|
export const VOICE_STAT_MAX = 80
|
|
84
90
|
|
|
91
|
+
/** Hard cap on a pet-declared affinity ladder (entries). */
|
|
92
|
+
export const RANK_LADDER_MAX = 16
|
|
93
|
+
/** Longest accepted rank name in a pet-declared ladder (characters). */
|
|
94
|
+
export const RANK_NAME_MAX = 24
|
|
95
|
+
|
|
85
96
|
/** Any '{token}' placeholder (no nesting, no newlines). */
|
|
86
97
|
const PLACEHOLDER_PATTERN = /{[^{}]*}/g
|
|
87
98
|
|
|
@@ -200,7 +211,80 @@ export function normalizeWhisperResults(
|
|
|
200
211
|
return Object.keys(pools).length > 0 ? pools : undefined
|
|
201
212
|
}
|
|
202
213
|
|
|
203
|
-
/**
|
|
214
|
+
/** Validate one rank name slot of a declared ladder. */
|
|
215
|
+
function rankName(
|
|
216
|
+
raw: unknown,
|
|
217
|
+
index: number,
|
|
218
|
+
field: 'name' | 'nameEn',
|
|
219
|
+
onWarning: (message: string) => void,
|
|
220
|
+
): string | undefined {
|
|
221
|
+
if (typeof raw !== 'string') {
|
|
222
|
+
onWarning('panel.ranks[' + index + '].' + field + ' must be a string')
|
|
223
|
+
return undefined
|
|
224
|
+
}
|
|
225
|
+
const trimmed = raw.trim()
|
|
226
|
+
if (trimmed === '' || trimmed.length > RANK_NAME_MAX) {
|
|
227
|
+
onWarning('panel.ranks[' + index + '].' + field + ' must be 1-' + RANK_NAME_MAX + ' characters')
|
|
228
|
+
return undefined
|
|
229
|
+
}
|
|
230
|
+
return trimmed
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Normalize one pet-declared affinity ladder (`panel.ranks`). A ladder is
|
|
235
|
+
* all-or-nothing: it must be an array of 1-{@link RANK_LADDER_MAX} entries,
|
|
236
|
+
* start at min 0, sort ascending with unique minima, and carry names of
|
|
237
|
+
* 1-{@link RANK_NAME_MAX} characters. Any violation warns and drops the
|
|
238
|
+
* whole ladder, so the pet keeps the built-in names instead of a half-broken
|
|
239
|
+
* ladder; a bad ladder never rejects the pet.
|
|
240
|
+
*/
|
|
241
|
+
export function normalizeRankLadder(
|
|
242
|
+
raw: unknown,
|
|
243
|
+
onWarning: (message: string) => void = () => {},
|
|
244
|
+
): AffinityRankName[] | undefined {
|
|
245
|
+
if (!Array.isArray(raw)) {
|
|
246
|
+
onWarning('panel.ranks must be an array')
|
|
247
|
+
return undefined
|
|
248
|
+
}
|
|
249
|
+
if (raw.length === 0 || raw.length > RANK_LADDER_MAX) {
|
|
250
|
+
onWarning('panel.ranks must contain 1-' + RANK_LADDER_MAX + ' entries')
|
|
251
|
+
return undefined
|
|
252
|
+
}
|
|
253
|
+
const ladder: AffinityRankName[] = []
|
|
254
|
+
let previousMin = -1
|
|
255
|
+
for (let index = 0; index < raw.length; index++) {
|
|
256
|
+
const entry = raw[index]
|
|
257
|
+
if (!isRecord(entry)) {
|
|
258
|
+
onWarning('panel.ranks[' + index + '] must be an object')
|
|
259
|
+
return undefined
|
|
260
|
+
}
|
|
261
|
+
const min = entry.min
|
|
262
|
+
if (typeof min !== 'number' || !Number.isSafeInteger(min) || min < 0) {
|
|
263
|
+
onWarning('panel.ranks[' + index + '].min must be a non-negative integer')
|
|
264
|
+
return undefined
|
|
265
|
+
}
|
|
266
|
+
if (index === 0 && min !== 0) {
|
|
267
|
+
onWarning('panel.ranks must start at min 0')
|
|
268
|
+
return undefined
|
|
269
|
+
}
|
|
270
|
+
if (min <= previousMin) {
|
|
271
|
+
onWarning('panel.ranks must be sorted ascending with unique min values')
|
|
272
|
+
return undefined
|
|
273
|
+
}
|
|
274
|
+
previousMin = min
|
|
275
|
+
const defaultName = rankName(entry.name, index, 'name', onWarning)
|
|
276
|
+
if (defaultName === undefined) return undefined
|
|
277
|
+
let nameEn: string | undefined
|
|
278
|
+
if (entry.nameEn !== undefined) {
|
|
279
|
+
nameEn = rankName(entry.nameEn, index, 'nameEn', onWarning)
|
|
280
|
+
if (nameEn === undefined) return undefined
|
|
281
|
+
}
|
|
282
|
+
ladder.push({ min, name: defaultName, ...(nameEn === undefined ? {} : { nameEn }) })
|
|
283
|
+
}
|
|
284
|
+
return ladder
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Normalize the panel block (labels / stats / actions / ranks; warn-and-drop). */
|
|
204
288
|
export function normalizePanel(
|
|
205
289
|
raw: unknown,
|
|
206
290
|
onWarning: (message: string) => void = () => {},
|
|
@@ -265,7 +349,15 @@ export function normalizePanel(
|
|
|
265
349
|
panel.actions = PANEL_ACTIONS.filter(action => seen.has(action))
|
|
266
350
|
}
|
|
267
351
|
}
|
|
268
|
-
|
|
352
|
+
const ranksRaw = raw.ranks
|
|
353
|
+
if (ranksRaw !== undefined) {
|
|
354
|
+
const ladder = normalizeRankLadder(ranksRaw, onWarning)
|
|
355
|
+
if (ladder !== undefined) panel.ranks = ladder
|
|
356
|
+
}
|
|
357
|
+
if (
|
|
358
|
+
panel.labels === undefined && panel.stats === undefined
|
|
359
|
+
&& panel.actions === undefined && panel.ranks === undefined
|
|
360
|
+
) return undefined
|
|
269
361
|
return panel
|
|
270
362
|
}
|
|
271
363
|
|
|
@@ -406,6 +498,7 @@ export function mergeVoicePacks(...layers: (VoicePack | undefined)[]): VoicePack
|
|
|
406
498
|
const stats: NonNullable<PetPanelView['stats']> = {}
|
|
407
499
|
let remarks: PetRemarks | undefined
|
|
408
500
|
let ranks: Record<string, string> | undefined
|
|
501
|
+
let panelRanks: readonly AffinityRankName[] | undefined
|
|
409
502
|
let actions: PanelAction[] | undefined
|
|
410
503
|
let panelSeen = false
|
|
411
504
|
let any = false
|
|
@@ -427,6 +520,9 @@ export function mergeVoicePacks(...layers: (VoicePack | undefined)[]): VoicePack
|
|
|
427
520
|
if (layer.panel.labels !== undefined) Object.assign(labels, layer.panel.labels)
|
|
428
521
|
if (layer.panel.stats !== undefined) Object.assign(stats, layer.panel.stats)
|
|
429
522
|
if (layer.panel.actions !== undefined) actions = layer.panel.actions
|
|
523
|
+
// A ladder is atomic: a declaring layer replaces the previous ladder
|
|
524
|
+
// whole instead of merging tiers across layers.
|
|
525
|
+
if (layer.panel.ranks !== undefined) panelRanks = layer.panel.ranks
|
|
430
526
|
}
|
|
431
527
|
if (layer.remarks !== undefined) {
|
|
432
528
|
remarks = { ...(remarks ?? {}), ...layer.remarks }
|
|
@@ -440,8 +536,10 @@ export function mergeVoicePacks(...layers: (VoicePack | undefined)[]): VoicePack
|
|
|
440
536
|
...(Object.keys(labels).length > 0 ? { labels } : {}),
|
|
441
537
|
...(Object.keys(stats).length > 0 ? { stats } : {}),
|
|
442
538
|
...(actions === undefined ? {} : { actions }),
|
|
539
|
+
...(panelRanks === undefined ? {} : { ranks: panelRanks }),
|
|
443
540
|
}
|
|
444
|
-
const panelEmpty = panel.labels === undefined && panel.stats === undefined
|
|
541
|
+
const panelEmpty = panel.labels === undefined && panel.stats === undefined
|
|
542
|
+
&& panel.actions === undefined && panel.ranks === undefined
|
|
445
543
|
return {
|
|
446
544
|
overrides,
|
|
447
545
|
...(panelSeen && !panelEmpty ? { panel } : {}),
|