@linxin666/dsh-pet 0.1.18 → 0.1.19
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 +12 -5
- package/README.zh.md +12 -5
- package/lib/client.js +48 -11
- package/lib/client.js.map +1 -1
- package/lib/index.js +100 -30
- package/lib/invariant.js +2 -2
- package/lib/state-Diiq8vjk.js +394 -0
- package/lib/types/affinity.d.ts +31 -3
- package/lib/types/affinity.d.ts.map +1 -1
- package/lib/types/affinity.js +21 -7
- package/lib/types/client/PetSettingsCard.d.ts +5 -0
- package/lib/types/client/PetSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PetSettingsCard.js +7 -0
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +8 -2
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +14 -8
- package/lib/types/client/settings-form.d.ts +8 -0
- package/lib/types/client/settings-form.d.ts.map +1 -1
- package/lib/types/client/settings-form.js +15 -1
- package/lib/types/event-projection.d.ts.map +1 -1
- package/lib/types/event-projection.js +3 -1
- package/lib/types/index.d.ts +4 -2
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +2 -1
- package/lib/types/ledger.d.ts +16 -1
- package/lib/types/ledger.d.ts.map +1 -1
- package/lib/types/ledger.js +29 -4
- package/lib/types/registry.d.ts +10 -0
- package/lib/types/registry.d.ts.map +1 -1
- package/lib/types/registry.js +3 -0
- package/lib/types/remarks.d.ts +56 -0
- package/lib/types/remarks.d.ts.map +1 -0
- package/lib/types/remarks.js +157 -0
- package/lib/types/service.d.ts +40 -3
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +90 -18
- package/lib/types/state.js +5 -5
- package/lib/types/treats.d.ts +3 -0
- package/lib/types/treats.d.ts.map +1 -1
- package/lib/types/treats.js +5 -2
- package/package.json +1 -1
- package/src/affinity.test.ts +19 -0
- package/src/affinity.ts +22 -7
- package/src/client/PetSettingsCard.tsx +8 -0
- package/src/client/PetSprite.test.tsx +33 -0
- package/src/client/PetSprite.tsx +16 -1
- package/src/client/index.ts +14 -8
- package/src/client/pet.module.css +19 -0
- package/src/client/settings-form.ts +15 -1
- package/src/event-projection.ts +3 -1
- package/src/index.ts +12 -1
- package/src/ledger.test.ts +40 -9
- package/src/ledger.ts +33 -4
- package/src/persist.test.ts +1 -1
- package/src/registry.test.ts +27 -0
- package/src/registry.ts +12 -0
- package/src/remarks.test.ts +89 -0
- package/src/remarks.ts +177 -0
- package/src/service.ts +116 -18
- package/src/state.test.ts +9 -0
- package/src/state.ts +5 -5
- package/src/treats.test.ts +13 -11
- package/src/treats.ts +5 -2
- package/lib/state-CFyJv0sQ.js +0 -213
package/src/ledger.test.ts
CHANGED
|
@@ -5,13 +5,11 @@ import { emptyPersist } from './persist.ts'
|
|
|
5
5
|
import { PetLedger } from './ledger.ts'
|
|
6
6
|
|
|
7
7
|
describe('PetLedger', () => {
|
|
8
|
-
it('settles the economy on completed turns (work treat per
|
|
8
|
+
it('settles the economy on completed turns (work treat per 30 turns)', () => {
|
|
9
9
|
const ledger = new PetLedger(emptyPersist())
|
|
10
10
|
const n = 1_000_000
|
|
11
|
-
ledger.rewardTurn('s1',
|
|
12
|
-
ledger.
|
|
13
|
-
ledger.rewardTurn('s1', 3, n + 2)
|
|
14
|
-
expect(ledger.snapshot.affinity.turns).toBe(3)
|
|
11
|
+
for (let turn = 1; turn <= 30; turn++) ledger.rewardTurn('s1', turn, n + turn)
|
|
12
|
+
expect(ledger.snapshot.affinity.turns).toBe(30)
|
|
15
13
|
expect(ledger.snapshot.treats.treats).toBe(1)
|
|
16
14
|
expect(ledger.takeDirty()).toBe(true)
|
|
17
15
|
})
|
|
@@ -25,6 +23,19 @@ describe('PetLedger', () => {
|
|
|
25
23
|
expect(ledger.snapshot.affinity.turns).toBe(1)
|
|
26
24
|
})
|
|
27
25
|
|
|
26
|
+
it('forgetSession re-arms a session so a repeated reward can award again', () => {
|
|
27
|
+
const ledger = new PetLedger(emptyPersist())
|
|
28
|
+
const n = 1_000_000
|
|
29
|
+
ledger.rewardTurn('s1', 1, n)
|
|
30
|
+
expect(ledger.snapshot.affinity.turns).toBe(1)
|
|
31
|
+
// The disposed-session eviction drops only the per-session bookkeeping.
|
|
32
|
+
ledger.forgetSession('s1')
|
|
33
|
+
// The same turn number is now treated as fresh: a same-turn retry on a
|
|
34
|
+
// new session lifecycle re-awards rather than being deduplicated.
|
|
35
|
+
expect(ledger.rewardTurn('s1', 1, n + 1)).toBe(true)
|
|
36
|
+
expect(ledger.snapshot.affinity.turns).toBe(2)
|
|
37
|
+
})
|
|
38
|
+
|
|
28
39
|
it('a read of the view does not mark dirty (no settle on read)', () => {
|
|
29
40
|
const ledger = new PetLedger(emptyPersist())
|
|
30
41
|
ledger.affinityView(1_000_000)
|
|
@@ -34,16 +45,36 @@ describe('PetLedger', () => {
|
|
|
34
45
|
it('feed consumes a treat and applies the feed reward', () => {
|
|
35
46
|
const ledger = new PetLedger(emptyPersist())
|
|
36
47
|
const n = 1_000_000
|
|
37
|
-
ledger.rewardTurn('s1',
|
|
38
|
-
ledger.rewardTurn('s1', 2, n + 1)
|
|
39
|
-
ledger.rewardTurn('s1', 3, n + 2)
|
|
48
|
+
for (let turn = 1; turn <= 30; turn++) ledger.rewardTurn('s1', turn, n + turn)
|
|
40
49
|
expect(ledger.snapshot.treats.treats).toBe(1)
|
|
41
|
-
const res = ledger.interact('feed', n +
|
|
50
|
+
const res = ledger.interact('feed', n + 40)
|
|
42
51
|
expect(res.delta).toBe(defaultAffinityConfig.feedReward)
|
|
43
52
|
expect(ledger.snapshot.treats.treats).toBe(0)
|
|
44
53
|
expect(ledger.snapshot.affinity.feeds).toBe(1)
|
|
45
54
|
})
|
|
46
55
|
|
|
56
|
+
it('cycles per-pet custom remarks per slot and falls back to built-ins', () => {
|
|
57
|
+
const ledger = new PetLedger(emptyPersist(), {
|
|
58
|
+
remarks: { pet: ['第一句', '第二句'] },
|
|
59
|
+
})
|
|
60
|
+
const n = 1_000_000
|
|
61
|
+
expect(ledger.interact('pet', n).reaction).toBe('第一句')
|
|
62
|
+
// Per-slot counters: the cooldown refusal still opens with the built-in
|
|
63
|
+
// legacy line, not the custom pool above.
|
|
64
|
+
expect(ledger.interact('pet', n + 1).reaction).toBe('摸过头啦,让鲸鱼娘歇口气~')
|
|
65
|
+
// Feeding falls back to the built-in pool when the pet declares none.
|
|
66
|
+
expect(ledger.interact('feed', n + 2).reaction).toBe('没有小鱼干了,多陪我工作一会儿吧~')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('swaps remark pools when the selected pet changes', () => {
|
|
70
|
+
const ledger = new PetLedger(emptyPersist(), { remarks: { pet: ['旧宠物台词'] } })
|
|
71
|
+
expect(ledger.interact('pet', 1_000_000).reaction).toBe('旧宠物台词')
|
|
72
|
+
ledger.setRemarks({ pet: ['新宠物台词'] })
|
|
73
|
+
expect(ledger.interact('pet', 1_011_000).reaction).toBe('新宠物台词')
|
|
74
|
+
ledger.setRemarks(undefined)
|
|
75
|
+
expect(ledger.interact('pet', 1_022_000).reaction).toBe('咕噜咕噜~被摸摸好舒服!')
|
|
76
|
+
})
|
|
77
|
+
|
|
47
78
|
it('refuses a feed on an empty stock without burning anything', () => {
|
|
48
79
|
const ledger = new PetLedger(emptyPersist())
|
|
49
80
|
const res = ledger.interact('feed', 1_000_000)
|
package/src/ledger.ts
CHANGED
|
@@ -22,12 +22,15 @@ import {
|
|
|
22
22
|
settleTreatGrants,
|
|
23
23
|
type TreatConfig,
|
|
24
24
|
} from './treats.ts'
|
|
25
|
+
import { RemarkPicker, type PetRemarks } from './remarks.ts'
|
|
25
26
|
import type { PetDisplayConfig, PetPersist } from './persist.ts'
|
|
26
27
|
|
|
27
28
|
/** Tuning overrides for the affinity economy. */
|
|
28
29
|
export interface LedgerConfig {
|
|
29
30
|
affinity?: Partial<AffinityConfig>
|
|
30
31
|
treats?: Partial<TreatConfig>
|
|
32
|
+
/** Per-pet remark pools for the selected pet (custom slots override built-ins). */
|
|
33
|
+
remarks?: PetRemarks
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
/** Result of one ledger interaction (the shape the pet RPC returns). */
|
|
@@ -48,6 +51,8 @@ export interface LedgerInteractionResult {
|
|
|
48
51
|
export class PetLedger {
|
|
49
52
|
private readonly affinityConfig: AffinityConfig
|
|
50
53
|
private readonly treatConfig: TreatConfig
|
|
54
|
+
/** Round-robin reaction picker; rebuilt when the selected pet changes. */
|
|
55
|
+
private picker: RemarkPicker
|
|
51
56
|
private current: PetPersist
|
|
52
57
|
/** Completed turns already rewarded, per session (turn numbers are per-session). */
|
|
53
58
|
private rewardedTurns = new Map<string, number>()
|
|
@@ -57,6 +62,7 @@ export class PetLedger {
|
|
|
57
62
|
constructor(persist: PetPersist, config: LedgerConfig = {}) {
|
|
58
63
|
this.affinityConfig = { ...defaultAffinityConfig, ...(config.affinity ?? {}) }
|
|
59
64
|
this.treatConfig = { ...defaultTreatConfig, ...(config.treats ?? {}) }
|
|
65
|
+
this.picker = new RemarkPicker(config.remarks)
|
|
60
66
|
this.current = persist
|
|
61
67
|
}
|
|
62
68
|
|
|
@@ -82,6 +88,14 @@ export class PetLedger {
|
|
|
82
88
|
return was
|
|
83
89
|
}
|
|
84
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Drop a session's rewarded-turn bookkeeping once that session is disposed,
|
|
93
|
+
* so the per-session map does not grow without bound.
|
|
94
|
+
*/
|
|
95
|
+
forgetSession(sessionId: string): void {
|
|
96
|
+
this.rewardedTurns.delete(sessionId)
|
|
97
|
+
}
|
|
98
|
+
|
|
85
99
|
/** Replace the display block (clamping stays a caller concern). */
|
|
86
100
|
setDisplay(display: PetDisplayConfig): void {
|
|
87
101
|
this.current = { ...this.current, display }
|
|
@@ -101,10 +115,18 @@ export class PetLedger {
|
|
|
101
115
|
this.dirty = true
|
|
102
116
|
}
|
|
103
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Swap the reaction pools to another pet's custom remarks (called on pet
|
|
120
|
+
* selection). Slots the pet does not declare fall back to built-ins.
|
|
121
|
+
*/
|
|
122
|
+
setRemarks(remarks?: PetRemarks): void {
|
|
123
|
+
this.picker = new RemarkPicker(remarks)
|
|
124
|
+
}
|
|
125
|
+
|
|
104
126
|
/**
|
|
105
127
|
* Settle the treat economy (work + time output since the last settlement).
|
|
106
128
|
* A zero-gain first settlement still starts the time clock (anchor write),
|
|
107
|
-
* which is how the
|
|
129
|
+
* which is how the time output can ever accrue. Returns true when
|
|
108
130
|
* the in-memory ledger changed and should be persisted.
|
|
109
131
|
*/
|
|
110
132
|
settleTreats(nowMs: number): boolean {
|
|
@@ -161,14 +183,17 @@ export class PetLedger {
|
|
|
161
183
|
interact(kind: PetInteraction, nowMs: number): LedgerInteractionResult {
|
|
162
184
|
if (kind === 'feed') this.settleTreats(nowMs)
|
|
163
185
|
const outcome = applyInteraction(this.current.affinity, kind, nowMs, this.affinityConfig)
|
|
186
|
+
// Reactions come from the picker pools (custom per-pet slots override
|
|
187
|
+
// the built-in library per slot); outcome.reaction stays the fallback
|
|
188
|
+
// copy for direct applyInteraction callers.
|
|
164
189
|
if (kind === 'feed' && !outcome.accepted) {
|
|
165
|
-
return { reaction:
|
|
190
|
+
return { reaction: this.picker.pick('feedCooldown'), delta: 0, affinity: this.affinityView(nowMs) }
|
|
166
191
|
}
|
|
167
192
|
if (kind === 'feed') {
|
|
168
193
|
const consume = consumeTreat(this.current.treats)
|
|
169
194
|
if (!consume.ok) {
|
|
170
195
|
return {
|
|
171
|
-
reaction: '
|
|
196
|
+
reaction: this.picker.pick('noTreats'),
|
|
172
197
|
delta: 0,
|
|
173
198
|
affinity: this.affinityView(nowMs),
|
|
174
199
|
}
|
|
@@ -180,7 +205,11 @@ export class PetLedger {
|
|
|
180
205
|
this.current = { ...this.current, affinity: outcome.affinity }
|
|
181
206
|
this.dirty = true
|
|
182
207
|
}
|
|
183
|
-
return {
|
|
208
|
+
return {
|
|
209
|
+
reaction: this.picker.pick(outcome.accepted ? kind : 'petCooldown'),
|
|
210
|
+
delta: outcome.delta,
|
|
211
|
+
affinity: this.affinityView(nowMs),
|
|
212
|
+
}
|
|
184
213
|
}
|
|
185
214
|
|
|
186
215
|
/** Current affinity view for the RPC snapshot. */
|
package/src/persist.test.ts
CHANGED
|
@@ -108,7 +108,7 @@ describe('loadPetPersist', () => {
|
|
|
108
108
|
writeFileSync(join(dir, 'pet.json'), JSON.stringify({
|
|
109
109
|
name: ' ',
|
|
110
110
|
names: { bad: ' ' },
|
|
111
|
-
affinity: { points:
|
|
111
|
+
affinity: { points: AFFINITY_MAX + 5000, lastPetAt: -5, lastFeedAt: 'x', pets: -1, feeds: 1.5, turns: 0 },
|
|
112
112
|
treats: { treats: 150, lastTreatGrantAt: -1, turnsAtLastTreatGrant: 0 },
|
|
113
113
|
display: { visible: 'yes', size: -10, right: 1e12, bottom: 20 },
|
|
114
114
|
}), 'utf8')
|
package/src/registry.test.ts
CHANGED
|
@@ -70,6 +70,33 @@ describe('resolvePetManifest', () => {
|
|
|
70
70
|
expect(resolvePetManifest({ id: 'ok', displayName: 'x', spritesheetPath: '/absolute.webp' }, '/tmp', { warnings })).toBeUndefined()
|
|
71
71
|
expect(warnings.length).toBe(3)
|
|
72
72
|
})
|
|
73
|
+
|
|
74
|
+
it('normalizes a manifest remarks block into per-pet pools', () => {
|
|
75
|
+
const entry = resolvePetManifest({
|
|
76
|
+
id: 'otter',
|
|
77
|
+
displayName: '水獭',
|
|
78
|
+
spritesheetPath: 'spritesheet.webp',
|
|
79
|
+
remarks: {
|
|
80
|
+
pet: '摸摸水獭的头',
|
|
81
|
+
feed: ['小鱼干真香', ' 再来一条 '],
|
|
82
|
+
},
|
|
83
|
+
}, join(tmpdir(), 'otter'))
|
|
84
|
+
expect(entry!.remarks).toEqual({ pet: ['摸摸水獭的头'], feed: ['小鱼干真香', '再来一条'] })
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('warns on malformed remarks slots but keeps the pet', () => {
|
|
88
|
+
const warnings: string[] = []
|
|
89
|
+
const entry = resolvePetManifest({
|
|
90
|
+
id: 'fox',
|
|
91
|
+
displayName: '狐狸',
|
|
92
|
+
spritesheetPath: 'spritesheet.webp',
|
|
93
|
+
remarks: { unknownSlot: ['x'], pet: [1, null] },
|
|
94
|
+
}, join(tmpdir(), 'fox'), { warnings })
|
|
95
|
+
expect(entry).toBeDefined()
|
|
96
|
+
expect(entry!.remarks).toBeUndefined()
|
|
97
|
+
expect(warnings.some(message => message.includes('unknown remarks slot'))).toBe(true)
|
|
98
|
+
expect(warnings.some(message => message.includes('no usable lines'))).toBe(true)
|
|
99
|
+
})
|
|
73
100
|
})
|
|
74
101
|
|
|
75
102
|
describe('loadPetRegistry', () => {
|
package/src/registry.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { homedir } from 'node:os'
|
|
|
22
22
|
import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
|
|
23
23
|
import { fileURLToPath } from 'node:url'
|
|
24
24
|
import type { PetAnimation } from './state.ts'
|
|
25
|
+
import { normalizePetRemarks, type PetRemarks, type PetRemarksManifest } from './remarks.ts'
|
|
25
26
|
|
|
26
27
|
/** Fixed row order of the 9-state animation contract. */
|
|
27
28
|
export const PET_ROW_ORDER: readonly PetAnimation[] = [
|
|
@@ -134,6 +135,13 @@ export interface PetManifest {
|
|
|
134
135
|
frames?: number[]
|
|
135
136
|
/** Optional per-track rhythm overrides; omitted tracks use the defaults. */
|
|
136
137
|
tracks?: Partial<Record<PetAnimation, PetTrackOverride>>
|
|
138
|
+
/**
|
|
139
|
+
* Optional witty-remark overrides the pet speaks on interactions
|
|
140
|
+
* (community contributions use this to give their pet its own voice).
|
|
141
|
+
* Each slot accepts one line or a pool of lines; a slot replaces the
|
|
142
|
+
* built-in default pool for that slot only.
|
|
143
|
+
*/
|
|
144
|
+
remarks?: PetRemarksManifest
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
/** Per-track rhythm overrides a manifest may carry. */
|
|
@@ -171,6 +179,8 @@ export interface PetEntry extends PetDefinition {
|
|
|
171
179
|
dir: string
|
|
172
180
|
/** Atlas path relative to 'dir' (declared by the manifest). */
|
|
173
181
|
spritesheetPath: string
|
|
182
|
+
/** Normalized per-pet remark pools (manifest 'remarks'), when declared. */
|
|
183
|
+
remarks?: PetRemarks
|
|
174
184
|
}
|
|
175
185
|
|
|
176
186
|
/** Registry load result: resolved entries plus load warnings. */
|
|
@@ -250,6 +260,7 @@ export function resolvePetManifest(
|
|
|
250
260
|
const value = Array.isArray(source.frames) ? source.frames[index] : undefined
|
|
251
261
|
return finiteInt(value, fallback, columns)
|
|
252
262
|
})
|
|
263
|
+
const remarks = normalizePetRemarks(source.remarks, message => warn('manifest ' + id + ': ' + message))
|
|
253
264
|
const trackOverrides = (typeof source.tracks === 'object' && source.tracks !== null ? source.tracks : {}) as Partial<Record<PetAnimation, PetTrackOverride>>
|
|
254
265
|
const tracks = {} as Record<PetAnimation, PetTrackDef>
|
|
255
266
|
for (const [row, animation] of PET_ROW_ORDER.entries()) {
|
|
@@ -289,6 +300,7 @@ export function resolvePetManifest(
|
|
|
289
300
|
manifestUrl: assetUrl(assetPrefix, id, 'pet.json'),
|
|
290
301
|
dir,
|
|
291
302
|
spritesheetPath: spritesheetPath.join('/'),
|
|
303
|
+
...(remarks === undefined ? {} : { remarks }),
|
|
292
304
|
}
|
|
293
305
|
}
|
|
294
306
|
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
BUILTIN_REMARKS,
|
|
4
|
+
REMARK_KINDS,
|
|
5
|
+
REMARK_LINE_MAX,
|
|
6
|
+
REMARK_LINES_MAX,
|
|
7
|
+
RemarkPicker,
|
|
8
|
+
builtinRemark,
|
|
9
|
+
normalizePetRemarks,
|
|
10
|
+
} from './remarks.ts'
|
|
11
|
+
|
|
12
|
+
describe('BUILTIN_REMARKS', () => {
|
|
13
|
+
it('carries a generous pool for every slot, with the legacy line first', () => {
|
|
14
|
+
for (const kind of REMARK_KINDS) {
|
|
15
|
+
const pool = BUILTIN_REMARKS[kind]
|
|
16
|
+
expect(pool.length).toBeGreaterThanOrEqual(8)
|
|
17
|
+
expect(pool.every(line => line.trim() !== '')).toBe(true)
|
|
18
|
+
}
|
|
19
|
+
// The legacy first lines keep the plugin's original wording so existing
|
|
20
|
+
// installs and tests never see a different opening reaction.
|
|
21
|
+
expect(BUILTIN_REMARKS.pet[0]).toBe('咕噜咕噜~被摸摸好舒服!')
|
|
22
|
+
expect(BUILTIN_REMARKS.petCooldown[0]).toBe('摸过头啦,让鲸鱼娘歇口气~')
|
|
23
|
+
expect(BUILTIN_REMARKS.feed[0]).toBe('呜哇!小鱼干好好吃!')
|
|
24
|
+
expect(BUILTIN_REMARKS.feedCooldown[0]).toBe('吃饱啦,晚点再喂~')
|
|
25
|
+
expect(BUILTIN_REMARKS.noTreats[0]).toBe('没有小鱼干了,多陪我工作一会儿吧~')
|
|
26
|
+
// Repo-wide ban on emoji characters: the pools must stay plain text.
|
|
27
|
+
const emoji = /[\u{1F000}-\u{1FFFF}\u{2600}-\u{27BF}\u{FE0F}]/u
|
|
28
|
+
for (const kind of REMARK_KINDS) {
|
|
29
|
+
for (const line of BUILTIN_REMARKS[kind]) expect(line).not.toMatch(emoji)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('RemarkPicker', () => {
|
|
35
|
+
it('picks the built-in pool round-robin, per slot, deterministically', () => {
|
|
36
|
+
const picker = new RemarkPicker()
|
|
37
|
+
const first = picker.pick('pet')
|
|
38
|
+
const second = picker.pick('pet')
|
|
39
|
+
expect(first).toBe(BUILTIN_REMARKS.pet[0])
|
|
40
|
+
expect(second).toBe(BUILTIN_REMARKS.pet[1])
|
|
41
|
+
// Per-slot counters: the feed slot still opens with its own first line.
|
|
42
|
+
expect(picker.pick('feed')).toBe(BUILTIN_REMARKS.feed[0])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('cycles back to the start after exhausting a pool', () => {
|
|
46
|
+
const picker = new RemarkPicker({ pet: ['A', 'B'] })
|
|
47
|
+
expect(picker.pick('pet')).toBe('A')
|
|
48
|
+
expect(picker.pick('pet')).toBe('B')
|
|
49
|
+
expect(picker.pick('pet')).toBe('A')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('lets a custom slot override only its own pool', () => {
|
|
53
|
+
const picker = new RemarkPicker({ pet: ['专属摸头台词'] })
|
|
54
|
+
expect(picker.pick('pet')).toBe('专属摸头台词')
|
|
55
|
+
expect(picker.pick('feed')).toBe(BUILTIN_REMARKS.feed[0])
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('normalizePetRemarks', () => {
|
|
60
|
+
it('accepts single lines and line pools and trims them', () => {
|
|
61
|
+
const remarks = normalizePetRemarks({
|
|
62
|
+
pet: ' 单句台词 ',
|
|
63
|
+
feed: ['第一句', ' 第二句 ', 42],
|
|
64
|
+
petCooldown: [],
|
|
65
|
+
})
|
|
66
|
+
expect(remarks).toEqual({ pet: ['单句台词'], feed: ['第一句', '第二句'] })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('rejects non-object blocks and unknown slots with warnings', () => {
|
|
70
|
+
const warnings: string[] = []
|
|
71
|
+
expect(normalizePetRemarks('nope', message => warnings.push(message))).toBeUndefined()
|
|
72
|
+
expect(normalizePetRemarks({ wat: ['x'] }, message => warnings.push(message))).toBeUndefined()
|
|
73
|
+
expect(normalizePetRemarks({ pet: [1, null] }, message => warnings.push(message))).toBeUndefined()
|
|
74
|
+
expect(warnings.length).toBe(3)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('caps pool size and line length at the contract bounds', () => {
|
|
78
|
+
const lines = Array.from({ length: REMARK_LINES_MAX + 10 }, (_, i) => 'line-' + i)
|
|
79
|
+
const remarks = normalizePetRemarks({ pet: lines, feed: 'x'.repeat(REMARK_LINE_MAX + 50) })
|
|
80
|
+
expect(remarks?.pet?.length).toBe(REMARK_LINES_MAX)
|
|
81
|
+
expect(remarks?.feed?.[0]?.length).toBe(REMARK_LINE_MAX)
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
describe('builtinRemark', () => {
|
|
86
|
+
it('returns the legacy first line of every slot', () => {
|
|
87
|
+
expect(builtinRemark('noTreats')).toBe('没有小鱼干了,多陪我工作一会儿吧~')
|
|
88
|
+
})
|
|
89
|
+
})
|
package/src/remarks.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pet remark (reaction copy) library — pure. Reaction bubbles the pet speaks
|
|
3
|
+
* on interaction events come from two layers:
|
|
4
|
+
* - the built-in default library below (a generous pool per event kind);
|
|
5
|
+
* - per-pet custom lines declared in a manifest's 'remarks' block, which
|
|
6
|
+
* community pet contributions (PRs) use to give their pet its own voice.
|
|
7
|
+
* A custom slot replaces the built-in pool for that slot only; the other
|
|
8
|
+
* slots keep the built-in lines. Picks cycle round-robin within a pool so
|
|
9
|
+
* repeated interactions stay varied while tests stay deterministic (no
|
|
10
|
+
* randomness, no clock).
|
|
11
|
+
* @module @linxin666/dsh-pet/remarks
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Interaction events a reaction line can accompany. */
|
|
15
|
+
export type RemarkKind = 'pet' | 'petCooldown' | 'feed' | 'feedCooldown' | 'noTreats'
|
|
16
|
+
|
|
17
|
+
/** Every remark slot, in a stable order. */
|
|
18
|
+
export const REMARK_KINDS: readonly RemarkKind[] = [
|
|
19
|
+
'pet',
|
|
20
|
+
'petCooldown',
|
|
21
|
+
'feed',
|
|
22
|
+
'feedCooldown',
|
|
23
|
+
'noTreats',
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
/** Per-pet remark overrides (normalized shape; each slot is a line pool). */
|
|
27
|
+
export type PetRemarks = Partial<Record<RemarkKind, string[]>>
|
|
28
|
+
|
|
29
|
+
/** Raw manifest shape: each slot accepts one line or a pool of lines. */
|
|
30
|
+
export type PetRemarksManifest = Partial<Record<RemarkKind, string | string[]>>
|
|
31
|
+
|
|
32
|
+
/** Longest accepted reaction line (characters, trimmed before slicing). */
|
|
33
|
+
export const REMARK_LINE_MAX = 120
|
|
34
|
+
/** Longest accepted pool per slot. */
|
|
35
|
+
export const REMARK_LINES_MAX = 64
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Built-in default remark library. Every pool is plain zh copy in the
|
|
39
|
+
* whale-girl voice; the first line of each pool is the legacy reaction the
|
|
40
|
+
* plugin has always spoken, so existing installs and tests keep their
|
|
41
|
+
* wording while the pool adds variety. No emoji characters anywhere (the
|
|
42
|
+
* repo bans them); ~ is the whale-girl's signature.
|
|
43
|
+
*/
|
|
44
|
+
export const BUILTIN_REMARKS: Readonly<Record<RemarkKind, readonly string[]>> = {
|
|
45
|
+
pet: [
|
|
46
|
+
'咕噜咕噜~被摸摸好舒服!',
|
|
47
|
+
'再摸摸这里,痒痒的~',
|
|
48
|
+
'头顶温度刚刚好,安心~',
|
|
49
|
+
'被摸到耳朵啦,扑通扑通!',
|
|
50
|
+
'你的手掌好温暖,舍不得你走~',
|
|
51
|
+
'呼噜呼噜~就靠在这里不走了!',
|
|
52
|
+
'今天的摸头也收货成功!',
|
|
53
|
+
'蹭蹭你的手心,这是回礼~',
|
|
54
|
+
'多摸摸我,亲密度会涨哦!',
|
|
55
|
+
'闭眼享受中,请勿打扰~',
|
|
56
|
+
],
|
|
57
|
+
petCooldown: [
|
|
58
|
+
'摸过头啦,让鲸鱼娘歇口气~',
|
|
59
|
+
'羽毛都快被摸秃啦,缓一缓~',
|
|
60
|
+
'呼……先让我喘口气嘛!',
|
|
61
|
+
'再摸就要睡着了哦~',
|
|
62
|
+
'稍微休息一下,待会儿再摸~',
|
|
63
|
+
'头顶要冒烟啦,停一停!',
|
|
64
|
+
'我知道你喜欢我,但也要节制呀~',
|
|
65
|
+
'歇一歇,摸摸的手感会更好哦~',
|
|
66
|
+
'咕……等我回个蓝~',
|
|
67
|
+
'让我先消化一下刚才的爱!',
|
|
68
|
+
],
|
|
69
|
+
feed: [
|
|
70
|
+
'呜哇!小鱼干好好吃!',
|
|
71
|
+
'咔嚓咔嚓,美味到尾巴打结~',
|
|
72
|
+
'这条小鱼干是刚晒好的,好香!',
|
|
73
|
+
'谢谢你,胃里暖暖的~',
|
|
74
|
+
'囤粮 +1,今天也有好好被爱!',
|
|
75
|
+
'好吃到想转圈圈~',
|
|
76
|
+
'小鱼干最好吃了,再来亿条!',
|
|
77
|
+
'饱餐一顿,马上满血复活~',
|
|
78
|
+
'这个味道,是幸福的味道!',
|
|
79
|
+
'吃完了还不忘舔舔爪子~',
|
|
80
|
+
],
|
|
81
|
+
feedCooldown: [
|
|
82
|
+
'吃饱啦,晚点再喂~',
|
|
83
|
+
'肚子圆滚滚的,装不下啦~',
|
|
84
|
+
'再喂就要变成球啦!',
|
|
85
|
+
'让我慢慢消化这份心意~',
|
|
86
|
+
'小鱼干的香气还没散呢~',
|
|
87
|
+
'呼……满足得动不了了~',
|
|
88
|
+
'先散步一圈再吃下一顿!',
|
|
89
|
+
'肚皮已经鼓鼓的啦~',
|
|
90
|
+
'好吃是好吃,可也得节制呀~',
|
|
91
|
+
'等我饿了会告诉你哦~',
|
|
92
|
+
],
|
|
93
|
+
noTreats: [
|
|
94
|
+
'没有小鱼干了,多陪我工作一会儿吧~',
|
|
95
|
+
'粮仓空空,陪我完成几轮任务就会有小鱼干啦~',
|
|
96
|
+
'小鱼干在路上啦,先一起加油工作!',
|
|
97
|
+
'嘴巴寂寞了……快去完成一轮任务!',
|
|
98
|
+
'陪我多工作一会儿,鱼干自动到账~',
|
|
99
|
+
'现在喂我也只会饿着肚子说谢谢哦~',
|
|
100
|
+
'粮仓见底啦,用几轮任务换一条鱼干吧~',
|
|
101
|
+
'饿着肚子等你完成下一轮任务~',
|
|
102
|
+
'小鱼干藏在你的工作里,去找找看!',
|
|
103
|
+
'先工作后干饭,我们的约定哦~',
|
|
104
|
+
],
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The legacy first line of one kind (direct callers' fallback copy). */
|
|
108
|
+
export function builtinRemark(kind: RemarkKind): string {
|
|
109
|
+
return BUILTIN_REMARKS[kind][0]!
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Normalize a manifest 'remarks' block into per-kind line pools. Unknown
|
|
114
|
+
* slots and non-string entries are skipped with a warning; empty pools are
|
|
115
|
+
* dropped so the built-in library takes the slot. Returns undefined when no
|
|
116
|
+
* usable slot remains.
|
|
117
|
+
*/
|
|
118
|
+
export function normalizePetRemarks(
|
|
119
|
+
raw: unknown,
|
|
120
|
+
onWarning: (message: string) => void = () => {},
|
|
121
|
+
): PetRemarks | undefined {
|
|
122
|
+
if (raw === undefined) return undefined
|
|
123
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
124
|
+
onWarning('remarks must be an object with pet/petCooldown/feed/feedCooldown/noTreats slots')
|
|
125
|
+
return undefined
|
|
126
|
+
}
|
|
127
|
+
const remarks: PetRemarks = {}
|
|
128
|
+
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
129
|
+
if (!REMARK_KINDS.includes(key as RemarkKind)) {
|
|
130
|
+
onWarning('unknown remarks slot ' + JSON.stringify(key))
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
const lines = (Array.isArray(value) ? value : [value])
|
|
134
|
+
.filter((line): line is string => typeof line === 'string')
|
|
135
|
+
.map(line => line.trim())
|
|
136
|
+
.filter(line => line !== '')
|
|
137
|
+
.slice(0, REMARK_LINES_MAX)
|
|
138
|
+
.map(line => line.slice(0, REMARK_LINE_MAX))
|
|
139
|
+
if (lines.length === 0) {
|
|
140
|
+
onWarning('remarks slot ' + key + ' carries no usable lines')
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
remarks[key as RemarkKind] = lines
|
|
144
|
+
}
|
|
145
|
+
return Object.keys(remarks).length === 0 ? undefined : remarks
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Round-robin reaction picker over the effective pools (per-pet custom lines
|
|
150
|
+
* override the built-in pool per slot). Counters are per slot, so each slot
|
|
151
|
+
* cycles its own list independently and picks stay deterministic for tests.
|
|
152
|
+
*/
|
|
153
|
+
export class RemarkPicker {
|
|
154
|
+
private readonly counters = new Map<RemarkKind, number>()
|
|
155
|
+
private readonly pools: Record<RemarkKind, readonly string[]>
|
|
156
|
+
|
|
157
|
+
constructor(overrides?: PetRemarks) {
|
|
158
|
+
this.pools = {} as Record<RemarkKind, readonly string[]>
|
|
159
|
+
for (const kind of REMARK_KINDS) {
|
|
160
|
+
const custom = overrides?.[kind]
|
|
161
|
+
this.pools[kind] = custom !== undefined && custom.length > 0 ? custom : BUILTIN_REMARKS[kind]
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The effective pool for one slot (custom override or built-in). */
|
|
166
|
+
pool(kind: RemarkKind): readonly string[] {
|
|
167
|
+
return this.pools[kind]
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The next line for one slot (round-robin within its pool). */
|
|
171
|
+
pick(kind: RemarkKind): string {
|
|
172
|
+
const pool = this.pools[kind]
|
|
173
|
+
const index = (this.counters.get(kind) ?? 0) % pool.length
|
|
174
|
+
this.counters.set(kind, index + 1)
|
|
175
|
+
return pool[index]!
|
|
176
|
+
}
|
|
177
|
+
}
|