better-dsh-pet 0.2.6 → 0.3.0

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/lib/client.js CHANGED
@@ -18,6 +18,7 @@ window.__ModuleLoader__.load({
18
18
  const { useEffect, useRef, useState } = React
19
19
  const h = React.createElement
20
20
  const CONFIG_ENDPOINT = '/plugins/better-dsh-pet/config'
21
+ const FESTIVAL_PLAY_ENDPOINT = '/plugins/better-dsh-pet/festival'
21
22
 
22
23
  const cardStyle = {
23
24
  listStyle: 'none', border: '1px solid var(--border-color, #d8d8d8)', borderRadius: 12,
@@ -37,6 +38,90 @@ window.__ModuleLoader__.load({
37
38
  display: 'grid', gridTemplateColumns: 'repeat(3, auto)', gap: '6px 14px',
38
39
  padding: '10px 12px', border: '1px solid var(--border-color, #d8d8d8)', borderRadius: 8,
39
40
  }
41
+ const festivalButtonStyle = {
42
+ minWidth: 120,
43
+ padding: '7px 12px',
44
+ border: '1px solid var(--border-color, #d8d8d8)',
45
+ borderRadius: 8,
46
+ background: 'var(--surface-color, #fff)',
47
+ cursor: 'pointer',
48
+ }
49
+ const LUNAR_DATE_FORMAT = new Intl.DateTimeFormat('zh-CN-u-ca-chinese', { month: 'numeric', day: 'numeric' })
50
+ const LUNAR_MONTH_NAMES = ['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '冬月', '腊月']
51
+ const FESTIVAL_DEFS = [
52
+ { id: 'solar-new-year', calendar: 'solar', label: '元旦', month: 1, day: 1 },
53
+ { id: 'lunar-spring-festival', calendar: 'lunar', label: '春节', month: '正月', day: 1 },
54
+ { id: 'lunar-lantern', calendar: 'lunar', label: '元宵节', month: '正月', day: 15 },
55
+ { id: 'solar-labor', calendar: 'solar', label: '劳动节', month: 5, day: 1 },
56
+ { id: 'solar-childrens', calendar: 'solar', label: '儿童节', month: 6, day: 1 },
57
+ { id: 'lunar-dragon-boat', calendar: 'lunar', label: '端午节', month: '五月', day: 5 },
58
+ { id: 'lunar-qixi', calendar: 'lunar', label: '七夕节', month: '七月', day: 7 },
59
+ { id: 'lunar-mid-autumn', calendar: 'lunar', label: '中秋节', month: '八月', day: 15 },
60
+ { id: 'lunar-double-ninth', calendar: 'lunar', label: '重阳节', month: '九月', day: 9 },
61
+ { id: 'lunar-laba', calendar: 'lunar', label: '腊八节', month: '腊月', day: 8 },
62
+ { id: 'solar-national', calendar: 'solar', label: '国庆节', month: 10, day: 1 },
63
+ { id: 'solar-christmas', calendar: 'solar', label: '圣诞节', month: 12, day: 25 },
64
+ ]
65
+
66
+ function parseLunarNumber(value) {
67
+ const text = String(value || '').replace(/[年月日\s]/g, '').replace(/^初/, '')
68
+ if (!text) return 0
69
+ if (/^\d+$/.test(text)) return Number(text)
70
+ if (text === '正') return 1
71
+ if (text === '冬') return 11
72
+ if (text === '腊') return 12
73
+ const digits = { 一: 1, 二: 2, 两: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9 }
74
+ if (text.startsWith('廿')) return 20 + (digits[text.slice(1)] || 0)
75
+ if (text.startsWith('卅')) return 30 + (digits[text.slice(1)] || 0)
76
+ const tenIndex = text.indexOf('十')
77
+ if (tenIndex >= 0) {
78
+ const tens = tenIndex === 0 ? 1 : (digits[text.slice(0, tenIndex)] || 0)
79
+ const ones = digits[text.slice(tenIndex + 1)] || 0
80
+ return tens * 10 + ones
81
+ }
82
+ return digits[text] || 0
83
+ }
84
+
85
+ function normalizeLunarMonth(value) {
86
+ const raw = String(value || '').trim()
87
+ const leap = raw.startsWith('闰')
88
+ const text = leap ? raw.slice(1) : raw
89
+ const number = parseLunarNumber(text)
90
+ const name = LUNAR_MONTH_NAMES[number - 1] || text
91
+ return leap ? `闰${name}` : name
92
+ }
93
+
94
+ function getLunarDateParts(date = new Date()) {
95
+ const parts = {}
96
+ for (const part of LUNAR_DATE_FORMAT.formatToParts(date)) {
97
+ if (part.type !== 'literal') parts[part.type] = part.value
98
+ }
99
+ return {
100
+ month: normalizeLunarMonth(parts.month || ''),
101
+ day: parseLunarNumber(parts.day || 0),
102
+ }
103
+ }
104
+
105
+ function isLunarNewYearsEve(date = new Date()) {
106
+ const today = getLunarDateParts(date)
107
+ if (today.month !== '腊月') return false
108
+ const tomorrow = new Date(date)
109
+ tomorrow.setDate(tomorrow.getDate() + 1)
110
+ const next = getLunarDateParts(tomorrow)
111
+ return next.month === '正月' && next.day === 1
112
+ }
113
+
114
+ function getTodayFestival(date = new Date()) {
115
+ const solarKey = `${date.getMonth() + 1}-${date.getDate()}`
116
+ const lunar = getLunarDateParts(date)
117
+ if (isLunarNewYearsEve(date)) {
118
+ return { id: 'lunar-new-years-eve', label: '除夕' }
119
+ }
120
+ return FESTIVAL_DEFS.find((festival) => {
121
+ if (festival.calendar === 'solar') return solarKey === `${festival.month}-${festival.day}`
122
+ return normalizeLunarMonth(festival.month) === lunar.month && festival.day === lunar.day
123
+ }) || null
124
+ }
40
125
 
41
126
  function Field({ label, hint, children }) {
42
127
  return h('label', { style: rowStyle },
@@ -74,9 +159,12 @@ window.__ModuleLoader__.load({
74
159
  const [value, setValue] = useState({})
75
160
  const [sizeDraft, setSizeDraft] = useState(null)
76
161
  const [busy, setBusy] = useState(false)
162
+ const [festivalStatus, setFestivalStatus] = useState('')
163
+ const [now, setNow] = useState(() => Date.now())
77
164
  const patchSeq = useRef(0)
78
165
  const sliderTimers = useRef(new Map())
79
166
  const writable = status === 'ready' && !busy
167
+ const todayFestival = getTodayFestival(new Date(now))
80
168
 
81
169
  useEffect(() => {
82
170
  let active = true
@@ -94,6 +182,16 @@ window.__ModuleLoader__.load({
94
182
  }
95
183
  }, [])
96
184
 
185
+ useEffect(() => {
186
+ const update = () => setNow(Date.now())
187
+ window.addEventListener('focus', update)
188
+ const timer = setInterval(update, 60 * 1000)
189
+ return () => {
190
+ window.removeEventListener('focus', update)
191
+ clearInterval(timer)
192
+ }
193
+ }, [])
194
+
97
195
  const write = async (field, next) => {
98
196
  const seq = ++patchSeq.current
99
197
  setBusy(true)
@@ -128,6 +226,26 @@ window.__ModuleLoader__.load({
128
226
  sliderTimers.current.set(field, timer)
129
227
  }
130
228
 
229
+ const playFestival = async () => {
230
+ const festival = getTodayFestival()
231
+ if (!festival || value.holidayEnabled !== true) return
232
+ setBusy(true)
233
+ setFestivalStatus('')
234
+ try {
235
+ const response = await fetch(FESTIVAL_PLAY_ENDPOINT, {
236
+ method: 'POST',
237
+ headers: { 'content-type': 'application/json' },
238
+ body: JSON.stringify({ festivalId: festival.id }),
239
+ })
240
+ if (!response.ok) throw new Error(`festival request failed: ${response.status}`)
241
+ setFestivalStatus('已发送到桌宠')
242
+ } catch {
243
+ setFestivalStatus('播放请求失败')
244
+ } finally {
245
+ setBusy(false)
246
+ }
247
+ }
248
+
131
249
  return h('li', { style: cardStyle, 'data-testid': 'better-dsh-pet-settings' },
132
250
  h('div', null,
133
251
  h('strong', { style: { fontSize: 16 } }, 'Better DSH Pet(大肥鱼增强版)'),
@@ -171,13 +289,20 @@ window.__ModuleLoader__.load({
171
289
  onChange: (event) => void writeSlider('moveChance', Number(event.target.value)),
172
290
  }),
173
291
  ),
174
- h(Field, { label: '动作切换间隔(ms)', hint: '每个动作播完后的停顿时间,0 表示立即切换。' },
292
+ h(Field, { label: '空闲动作间隔(ms)', hint: '空闲动作播完后的停顿时间,0 表示立即切换。' },
175
293
  h('input', {
176
294
  type: 'range', min: 0, max: 5000, step: 100,
177
295
  value: value.actionDelayMs ?? 0, disabled: status !== 'ready',
178
296
  onChange: (event) => void writeSlider('actionDelayMs', Number(event.target.value)),
179
297
  }),
180
298
  ),
299
+ h(Field, { label: '播放速度', hint: `${Math.round((value.playbackRate ?? 1) * 10) / 10}x,1.0~2.0。` },
300
+ h('input', {
301
+ type: 'range', min: 1, max: 2, step: 0.1,
302
+ value: value.playbackRate ?? 1, disabled: status !== 'ready',
303
+ onChange: (event) => void writeSlider('playbackRate', Number(event.target.value)),
304
+ }),
305
+ ),
181
306
  h(Field, { label: '活跃程度', hint: '控制空闲时微动作的出现频率。' },
182
307
  h('select', {
183
308
  value: value.activityLevel ?? 'normal', disabled: !writable, style: selectStyle,
@@ -232,6 +357,41 @@ window.__ModuleLoader__.load({
232
357
  onChange: (event) => void write('roastEnabled', event.target.checked),
233
358
  }),
234
359
  ),
360
+ h(Field, { label: '节日祝福', hint: '识别阳历与农历节日,在桌宠里显示祝福入口并在首次启动时自动播放。' },
361
+ h('input', {
362
+ type: 'checkbox', checked: value.holidayEnabled === true, disabled: !writable,
363
+ onChange: (event) => void write('holidayEnabled', event.target.checked),
364
+ }),
365
+ ),
366
+ h(Field, {
367
+ label: '今日节日',
368
+ hint: todayFestival ? `今天是${todayFestival.label},可手动播放祝福。` : '今天没有匹配到已配置节日。',
369
+ },
370
+ todayFestival
371
+ ? h('button', {
372
+ type: 'button',
373
+ style: Object.assign({}, festivalButtonStyle, {
374
+ opacity: value.holidayEnabled === true && writable ? 1 : 0.55,
375
+ cursor: value.holidayEnabled === true && writable ? 'pointer' : 'not-allowed',
376
+ }),
377
+ disabled: value.holidayEnabled !== true || !writable,
378
+ onClick: () => void playFestival(),
379
+ }, value.holidayEnabled === true ? `播放${todayFestival.label}祝福` : '先开启节日祝福')
380
+ : h('span', { style: { opacity: 0.72 } }, '今日无节日'),
381
+ ),
382
+ festivalStatus ? h('small', { role: 'status' }, festivalStatus) : null,
383
+ h(Field, { label: '启用语音功能', hint: '关闭后隐藏所有麦克风/语音唤醒入口,保护隐私。' },
384
+ h('input', {
385
+ type: 'checkbox', checked: value.voiceEnabled !== false, disabled: !writable,
386
+ onChange: (event) => void write('voiceEnabled', event.target.checked),
387
+ }),
388
+ ),
389
+ h(Field, { label: '启动时语音唤醒', hint: '开启后桌宠启动会自动打开麦克风听“大肥鱼+指令”。' },
390
+ h('input', {
391
+ type: 'checkbox', checked: value.voiceWakeAutoStart === true, disabled: !writable,
392
+ onChange: (event) => void write('voiceWakeAutoStart', event.target.checked),
393
+ }),
394
+ ),
235
395
  h(Field, { label: '允许行走', hint: '关闭后待机不会再走动。' },
236
396
  h('input', {
237
397
  type: 'checkbox', checked: value.walkEnabled !== false, disabled: !writable,