better-dsh-pet 0.1.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/LICENSE +21 -0
- package/README.en.md +93 -0
- package/README.md +103 -0
- package/assets/alarm.mp3 +0 -0
- package/assets/thumb//344/270/234/345/274/240/350/245/277/346/234/233.webm +0 -0
- package/assets/thumb//344/270/255/347/247/213/350/265/217/346/234/210/345/220/203/346/234/210/351/245/274.webm +0 -0
- package/assets/thumb//344/274/230/351/233/205/345/245/263/344/273/206/350/210/236.webm +0 -0
- package/assets/thumb//345/201/267/345/220/203/351/233/266/351/243/237/350/242/253/346/212/223/344/275/217.webm +0 -0
- package/assets/thumb//345/206/231/344/273/243/347/240/201.webm +0 -0
- package/assets/thumb//345/212/250/347/211/251/347/216/257/347/273/225.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/344/270/223/345/277/203/347/216/251/351/255/224/346/226/271.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/345/260/217/346/206/251/346/262/211/347/234/240.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/345/267/246/350/275/254/345/245/224/350/267/221.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/346/225/262/345/207/273/346/241/214/351/235/242/344/272/222/345/212/250.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/346/274/202/346/265/256/350/270/217/346/255/245.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/350/267/263/350/267/203/346/212/223/347/242/216/345/244/264/351/241/266/347/211/251/345/223/201.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/350/271/262/344/270/213/347/216/251/347/216/251/345/205/267/346/261/275/350/275/246.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/351/207/215/345/212/233/344/270/213/350/271/262/345/216/213/347/274/251.webm +0 -0
- package/assets/thumb//345/217/257/347/210/261/345/256/205/350/210/236.webm +0 -0
- package/assets/thumb//345/220/203Token.webm +0 -0
- package/assets/thumb//345/220/203/345/206/260/346/267/207/346/267/213/350/236/215/345/214/226.webm +0 -0
- package/assets/thumb//345/220/203/345/215/210/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/346/227/251/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/346/231/232/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/347/231/275/351/245/255.webm +0 -0
- package/assets/thumb//345/220/271/346/260/224/347/220/203.webm +0 -0
- package/assets/thumb//345/223/210/346/254/240/350/277/236/345/244/251.webm +0 -0
- package/assets/thumb//345/240/206/351/233/252/344/272/272.webm +0 -0
- package/assets/thumb//345/244/247/345/217/243/345/220/203/351/233/266/351/243/237.webm +0 -0
- package/assets/thumb//345/245/263/344/273/206/345/261/210/350/206/235/347/244/274/344/273/252.webm +0 -0
- package/assets/thumb//345/260/217/345/271/205/345/272/246/345/216/237/345/234/260 360 /345/272/246/346/227/213/350/275/254/345/261/225/347/244/272.webm +0 -0
- package/assets/thumb//345/260/217/346/217/220/347/220/264/346/274/224/345/245/217.webm +0 -0
- package/assets/thumb//345/276/205/346/234/272/345/221/274/345/220/270/344/274/221/351/227/262.webm +0 -0
- package/assets/thumb//346/202/240/351/227/262/345/223/274/346/255/214.webm +0 -0
- package/assets/thumb//346/211/223/347/236/214/347/235/241/350/242/253/346/203/212/351/206/222.webm +0 -0
- package/assets/thumb//346/221/207/346/211/207/347/272/263/345/207/211.webm +0 -0
- package/assets/thumb//346/224/276/351/243/216/347/255/235.webm +0 -0
- package/assets/thumb//346/225/264/344/275/223/346/215/242/350/243/205/350/257/225/350/211/262.webm +0 -0
- package/assets/thumb//346/267/261/345/272/246/346/200/235/350/200/203/347/242/216/347/242/216/345/277/265.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/202/262/345/250/207/347/224/237/346/260/224/357/274/210/344/276/247/350/272/253/345/261/225/347/244/272/357/274/211.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/256/263/347/276/236/346/203/212/350/256/266.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/274/200/345/277/203/350/267/203/345/212/250.webm +0 -0
- package/assets/thumb//347/205/247/351/225/234/345/255/220.webm +0 -0
- package/assets/thumb//347/216/251/346/260/264/346/236/252.webm +0 -0
- package/assets/thumb//347/216/251/346/270/270/346/210/217/346/260/224/346/200/245/350/264/245/345/235/217.webm +0 -0
- package/assets/thumb//347/224/250/351/262/270/351/261/274/345/260/276/345/267/264/346/213/215/346/211/223/345/234/260/351/235/242.webm +0 -0
- package/assets/thumb//350/223/235/351/262/270/347/216/260/344/270/226.webm +0 -0
- package/assets/thumb//350/236/203/350/237/271/350/265/260/350/267/257.webm +0 -0
- package/assets/thumb//350/242/253/345/220/223/344/270/200/350/267/263/357/274/210/347/202/270/346/257/233/357/274/211.webm +0 -0
- package/assets/thumb//350/242/253/350/220/275/345/217/266/346/267/271/346/262/241.webm +0 -0
- package/assets/thumb//350/242/253/351/274/240/346/240/207/346/213/226/346/213/275/346/202/254/347/251/272/345/217/215/351/246/210.webm +0 -0
- package/assets/thumb//350/266/205/345/244/247/344/274/270/346/207/222/350/205/260.webm +0 -0
- package/assets/thumb//350/275/273/345/277/253/346/221/207/346/221/206/350/210/236.webm +0 -0
- package/assets/thumb//350/275/273/345/277/253/350/256/260/345/275/225.webm +0 -0
- package/assets/thumb//351/262/270/351/261/274/345/220/220/346/263/241/346/263/241/347/211/271/346/225/210.webm +0 -0
- package/cordis.patch.yml +36 -0
- package/lib/client.js +238 -0
- package/lib/index.js +589 -0
- package/lib/index.js.bak-20260819-104008 +483 -0
- package/lib/pet-helper-process.js +133 -0
- package/lib/pet-protocol.js +64 -0
- package/lib/pet-reducer.js +527 -0
- package/lib/pet-status-copy.js +118 -0
- package/lib/types/client/index.d.ts +43 -0
- package/lib/types/index.d.ts +65 -0
- package/package.json +85 -0
- package/runtime/electron-helper/index.html +134 -0
- package/runtime/electron-helper/main.js +191 -0
- package/runtime/electron-helper/package.json +7 -0
- package/runtime/electron-helper/preload.js +39 -0
- package/runtime/electron-helper/renderer.js +903 -0
|
@@ -0,0 +1,903 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* better-dsh-pet desktop helper renderer —— 透明窗口里的宠物本体。
|
|
3
|
+
*
|
|
4
|
+
* 使用纯 DOM + 双 video 缓冲播放 better-dsh-pet 的 WebM 动画;接收主进程转发的
|
|
5
|
+
* Companion 消息,根据 DSH 状态切换动画并显示气泡。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ---------- 配置 ----------
|
|
9
|
+
const params = new URLSearchParams(location.search)
|
|
10
|
+
const CONFIG = {
|
|
11
|
+
scale: Number(params.get('scale') || '1'),
|
|
12
|
+
bubbleScale: Number(params.get('bubbleScale') || '1'),
|
|
13
|
+
activityLevel: params.get('activityLevel') || 'normal',
|
|
14
|
+
reducedMotion: params.get('reducedMotion') === '1',
|
|
15
|
+
bubbleMode: params.get('bubbleMode') || 'always',
|
|
16
|
+
bubbleStates: (params.get('bubbleStates') || 'SUCCESS,ERROR,WAITING').split(',').filter(Boolean),
|
|
17
|
+
webuiUrl: params.get('webuiUrl') || 'http://127.0.0.1:3080/',
|
|
18
|
+
workMinutes: Number(params.get('workMinutes') || '25'),
|
|
19
|
+
breakMinutes: Number(params.get('breakMinutes') || '5'),
|
|
20
|
+
roastEnabled: params.get('roastEnabled') === '1',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ---------- 资源根 ----------
|
|
24
|
+
const ASSET_BASE = new URL('../../assets/thumb/', location.href).href
|
|
25
|
+
const ALARM_URL = new URL('../../assets/alarm.mp3', location.href).href
|
|
26
|
+
|
|
27
|
+
// ---------- 动画目录 ----------
|
|
28
|
+
const CANVAS_H = 360
|
|
29
|
+
const FEET_Y = 330
|
|
30
|
+
const HIT_BOX = { x0: 200, y0: 50, x1: 440, y1: 335 }
|
|
31
|
+
|
|
32
|
+
const IDLE = '待机呼吸休闲'
|
|
33
|
+
const TURN = '东张西望'
|
|
34
|
+
const DRAG = '被鼠标拖拽悬空反馈'
|
|
35
|
+
const ACTS = [
|
|
36
|
+
'悠闲哼歌', '超大伸懒腰', '原地专心玩魔方', '原地敲击桌面互动',
|
|
37
|
+
'原地重力下蹲压缩', '哈欠连天', '原地小憩沉眠', '原地蹲下玩玩具汽车',
|
|
38
|
+
'鲸鱼吐泡泡特效', '女仆屈膝礼仪', '被吓一跳(炸毛)', '原地跳跃抓碎头顶物品',
|
|
39
|
+
'小幅度原地 360 度旋转展示', '偷吃零食被抓住', '玩游戏气急败坏',
|
|
40
|
+
'用鲸鱼尾巴拍打地面', '打瞌睡被惊醒', '玩水枪', '小提琴演奏', '蓝鲸现世',
|
|
41
|
+
'吃白饭', '照镜子', '优雅女仆舞', '轻快摇摆舞', '可爱宅舞', '整体换装试色',
|
|
42
|
+
'大口吃零食', '吹气球', '动物环绕', '深度思考碎碎念', '轻快记录', '写代码',
|
|
43
|
+
'吃Token', '吃早餐', '吃午餐', '吃晚餐', '放风筝', '摇扇纳凉', '吃冰淇淋融化',
|
|
44
|
+
'被落叶淹没', '中秋赏月吃月饼', '堆雪人',
|
|
45
|
+
]
|
|
46
|
+
const ACTION_COPY = {
|
|
47
|
+
'东张西望': '大肥鱼东张西望,看看有没有鱼干~',
|
|
48
|
+
'悠闲哼歌': '大肥鱼悠闲地哼着小曲~',
|
|
49
|
+
'超大伸懒腰': '大肥鱼伸了个大大的懒腰~',
|
|
50
|
+
'原地专心玩魔方': '大肥鱼在专心拧魔方呢~',
|
|
51
|
+
'原地敲击桌面互动': '大肥鱼在敲桌面求关注~',
|
|
52
|
+
'原地重力下蹲压缩': '大肥鱼像弹簧一样蹲下去啦~',
|
|
53
|
+
'哈欠连天': '大肥鱼打了个大大的哈欠~',
|
|
54
|
+
'原地小憩沉眠': '大肥鱼偷偷打了个盹儿~',
|
|
55
|
+
'原地蹲下玩玩具汽车': '大肥鱼蹲在地上玩小汽车~',
|
|
56
|
+
'鲸鱼吐泡泡特效': '大肥鱼吐出一串可爱的泡泡~',
|
|
57
|
+
'女仆屈膝礼仪': '大肥鱼向你行了个女仆礼~',
|
|
58
|
+
'被吓一跳(炸毛)': '大肥鱼被吓得炸毛啦!',
|
|
59
|
+
'原地跳跃抓碎头顶物品': '大肥鱼跳起来抓碎了头顶的东西~',
|
|
60
|
+
'小幅度原地 360 度旋转展示': '大肥鱼原地转圈圈展示自己~',
|
|
61
|
+
'偷吃零食被抓住': '大肥鱼偷吃零食被抓住啦!',
|
|
62
|
+
'玩游戏气急败坏': '大肥鱼玩游戏玩到气急败坏~',
|
|
63
|
+
'用鲸鱼尾巴拍打地面': '大肥鱼用尾巴拍地面抗议~',
|
|
64
|
+
'打瞌睡被惊醒': '大肥鱼打瞌睡被吓醒了!',
|
|
65
|
+
'玩水枪': '大肥鱼拿起水枪滋水玩~',
|
|
66
|
+
'小提琴演奏': '大肥鱼拉起了小提琴~',
|
|
67
|
+
'蓝鲸现世': '蓝鲸大肥鱼现身啦!',
|
|
68
|
+
'吃白饭': '大肥鱼在专心吃白饭~',
|
|
69
|
+
'照镜子': '大肥鱼照镜子臭美中~',
|
|
70
|
+
'优雅女仆舞': '大肥鱼跳起了优雅的女仆舞~',
|
|
71
|
+
'轻快摇摆舞': '大肥鱼轻快地摇摆跳舞~',
|
|
72
|
+
'可爱宅舞': '大肥鱼跳起了可爱宅舞~',
|
|
73
|
+
'整体换装试色': '大肥鱼在试新衣服的颜色~',
|
|
74
|
+
'大口吃零食': '大肥鱼大口大口吃零食~',
|
|
75
|
+
'吹气球': '大肥鱼在努力吹气球~',
|
|
76
|
+
'动物环绕': '好多小动物围着大肥鱼转~',
|
|
77
|
+
'深度思考碎碎念': '大肥鱼陷入深度思考,碎碎念中~',
|
|
78
|
+
'轻快记录': '大肥鱼在轻快地记笔记~',
|
|
79
|
+
'写代码': '大肥鱼在认真写代码~',
|
|
80
|
+
'吃Token': '大肥鱼把 Token 当零食吃掉啦~',
|
|
81
|
+
'吃早餐': '大肥鱼在吃早餐~',
|
|
82
|
+
'吃午餐': '大肥鱼在吃午餐~',
|
|
83
|
+
'吃晚餐': '大肥鱼在吃晚餐~',
|
|
84
|
+
'放风筝': '大肥鱼在放风筝~',
|
|
85
|
+
'摇扇纳凉': '大肥鱼摇着扇子纳凉~',
|
|
86
|
+
'吃冰淇淋融化': '大肥鱼吃冰淇淋,可是化得好快~',
|
|
87
|
+
'被落叶淹没': '大肥鱼被落叶埋住啦~',
|
|
88
|
+
'中秋赏月吃月饼': '大肥鱼中秋赏月吃月饼~',
|
|
89
|
+
'堆雪人': '大肥鱼在堆雪人~',
|
|
90
|
+
}
|
|
91
|
+
const CLICKS = ['点击回应 - 开心跃动', '点击回应 - 害羞惊讶', '点击回应 - 傲娇生气(侧身展示)']
|
|
92
|
+
const CLICK_COPY = {
|
|
93
|
+
'点击回应 - 开心跃动': '大肥鱼被摸得好开心~',
|
|
94
|
+
'点击回应 - 害羞惊讶': '大肥鱼害羞地躲了一下~',
|
|
95
|
+
'点击回应 - 傲娇生气(侧身展示)': '大肥鱼傲娇地哼了一声!',
|
|
96
|
+
}
|
|
97
|
+
const EAT_ANIMS = ['吃白饭', '吃早餐', '吃午餐', '吃晚餐', '大口吃零食', '吃Token', '吃冰淇淋融化', '偷吃零食被抓住']
|
|
98
|
+
const MOVES = ['螃蟹走路', '原地漂浮踏步', '原地左转奔跑']
|
|
99
|
+
const STATE_ANIMS = {
|
|
100
|
+
IDLE: [IDLE],
|
|
101
|
+
THINKING: ['深度思考碎碎念', '原地专心玩魔方', '东张西望'],
|
|
102
|
+
WORKING: ['写代码', '轻快记录', '原地敲击桌面互动', '吃Token'],
|
|
103
|
+
WAITING: ['东张西望', '悠闲哼歌', '原地敲击桌面互动'],
|
|
104
|
+
SUCCESS: ['点击回应 - 开心跃动', '优雅女仆舞', '轻快摇摆舞', '可爱宅舞', '女仆屈膝礼仪'],
|
|
105
|
+
ERROR: ['被吓一跳(炸毛)', '玩游戏气急败坏', '偷吃零食被抓住'],
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ---------- DOM ----------
|
|
109
|
+
const rootEl = document.getElementById('pet-root')
|
|
110
|
+
const stageEl = document.getElementById('pet-stage')
|
|
111
|
+
const videoA = document.querySelectorAll('video')[0]
|
|
112
|
+
const videoB = document.querySelectorAll('video')[1]
|
|
113
|
+
const hitEl = document.getElementById('pet-hit')
|
|
114
|
+
const bubbleEl = document.getElementById('bubble')
|
|
115
|
+
const bubbleTitle = document.getElementById('bubble-title')
|
|
116
|
+
const bubbleDetail = document.getElementById('bubble-detail')
|
|
117
|
+
const menuEl = document.getElementById('menu')
|
|
118
|
+
|
|
119
|
+
// ---------- 基础尺寸 ----------
|
|
120
|
+
const size = 462 * CONFIG.scale
|
|
121
|
+
const halfW = size / 2
|
|
122
|
+
const halfH = size * 9 / 16 / 2
|
|
123
|
+
const bottomPad = (size * 9 / 16 * (CANVAS_H - FEET_Y)) / CANVAS_H
|
|
124
|
+
stageEl.style.width = size + 'px'
|
|
125
|
+
stageEl.style.height = (size * 9 / 16) + 'px'
|
|
126
|
+
stageEl.style.transform = `translateY(${bottomPad}px)`
|
|
127
|
+
hitEl.style.left = (HIT_BOX.x0 / 640 * 100) + '%'
|
|
128
|
+
hitEl.style.top = (HIT_BOX.y0 / 360 * 100) + '%'
|
|
129
|
+
hitEl.style.width = ((HIT_BOX.x1 - HIT_BOX.x0) / 640 * 100) + '%'
|
|
130
|
+
hitEl.style.height = ((HIT_BOX.y1 - HIT_BOX.y0) / 360 * 100) + '%'
|
|
131
|
+
if (CONFIG.reducedMotion) {
|
|
132
|
+
document.documentElement.style.setProperty('--pet-transition', 'none')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 全屏透明画布内,宠物初始放在右下角;拖拽时只移动这个 DOM,不移动窗口。
|
|
136
|
+
let petPos = {
|
|
137
|
+
x: window.innerWidth - size - 24,
|
|
138
|
+
y: window.innerHeight - size * 9 / 16 - 24,
|
|
139
|
+
}
|
|
140
|
+
function applyPetPosition() {
|
|
141
|
+
rootEl.style.left = petPos.x + 'px'
|
|
142
|
+
rootEl.style.top = petPos.y + 'px'
|
|
143
|
+
rootEl.style.right = 'auto'
|
|
144
|
+
rootEl.style.bottom = 'auto'
|
|
145
|
+
rootEl.style.transform = 'none'
|
|
146
|
+
}
|
|
147
|
+
applyPetPosition()
|
|
148
|
+
|
|
149
|
+
// ---------- 状态 ----------
|
|
150
|
+
let front = 0 // 0 = A, 1 = B
|
|
151
|
+
let pending = null
|
|
152
|
+
let gen = 0
|
|
153
|
+
let anim = IDLE
|
|
154
|
+
let animOnce = true
|
|
155
|
+
let animLoop = false
|
|
156
|
+
let currentMode = 'idle' // idle | state | click | drag | move | pulse
|
|
157
|
+
let currentState = 'IDLE'
|
|
158
|
+
let resumeState = 'IDLE'
|
|
159
|
+
let resumeMessage = ''
|
|
160
|
+
let resumeDetail = ''
|
|
161
|
+
let overlay = null
|
|
162
|
+
let overlayTimer = null
|
|
163
|
+
let lastPulseKey = null
|
|
164
|
+
let status = { state: 'IDLE', message: '', detail: '', project: '', task: '', progress: null }
|
|
165
|
+
let tasks = []
|
|
166
|
+
let dragging = false
|
|
167
|
+
let dragState = { active: false, dragging: false, sx: 0, sy: 0, offX: 0, offY: 0 }
|
|
168
|
+
let justDragged = false
|
|
169
|
+
let facing = 'left'
|
|
170
|
+
let lastMouse = { x: -1, y: -1 }
|
|
171
|
+
let tokenUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
|
172
|
+
let balance = null
|
|
173
|
+
let roast = null
|
|
174
|
+
let lastRoast = null
|
|
175
|
+
let manualBubble = null
|
|
176
|
+
let manualBubbleTimer = null
|
|
177
|
+
let pomodoro = null
|
|
178
|
+
let pomodoroTimer = null
|
|
179
|
+
let menuPage = 'main'
|
|
180
|
+
let lastMenuPos = { x: 0, y: 0 }
|
|
181
|
+
|
|
182
|
+
// ---------- 工具 ----------
|
|
183
|
+
const randomBetween = (min, max) => Math.floor(min + Math.random() * (max - min))
|
|
184
|
+
const pick = (pool, exclude) => {
|
|
185
|
+
const entries = exclude ? pool.filter((n) => n !== exclude) : pool
|
|
186
|
+
return entries[Math.floor(Math.random() * entries.length)]
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function assetUrl(name) {
|
|
190
|
+
return new URL(encodeURIComponent(name) + '.webm', ASSET_BASE).href
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ---------- 视频切换 ----------
|
|
194
|
+
function currentVideo() {
|
|
195
|
+
return front === 0 ? videoA : videoB
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function otherVideo() {
|
|
199
|
+
return front === 0 ? videoB : videoA
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function switchTo(next, { once = true, loop = false } = {}) {
|
|
203
|
+
if (pending && pending.anim === next && pending.once === once && pending.loop === loop) return
|
|
204
|
+
const currentGen = ++gen
|
|
205
|
+
pending = { anim: next, once, loop, gen: currentGen }
|
|
206
|
+
const target = otherVideo()
|
|
207
|
+
const el = target
|
|
208
|
+
const old = currentVideo()
|
|
209
|
+
// 旧视频可能还没播完就被切走:清掉它的 ended 回调,避免稍后触发
|
|
210
|
+
// handleEnded 打断当前正在播放的新动画。
|
|
211
|
+
if (old && old !== el) old.onended = null
|
|
212
|
+
el.src = assetUrl(next)
|
|
213
|
+
el.loop = loop
|
|
214
|
+
el.muted = true
|
|
215
|
+
el.autoplay = true
|
|
216
|
+
el.playsInline = true
|
|
217
|
+
el.onended = loop ? null : () => handleEnded()
|
|
218
|
+
el.load()
|
|
219
|
+
const onReady = () => {
|
|
220
|
+
el.removeEventListener('loadeddata', onReady)
|
|
221
|
+
if (pending?.gen !== currentGen) return
|
|
222
|
+
el.classList.add('is-front')
|
|
223
|
+
if (old !== el) {
|
|
224
|
+
old.classList.remove('is-front')
|
|
225
|
+
old.onended = null
|
|
226
|
+
}
|
|
227
|
+
front = front === 0 ? 1 : 0
|
|
228
|
+
pending = null
|
|
229
|
+
el.style.transform = facing === 'right' ? 'scaleX(-1)' : ''
|
|
230
|
+
el.play().catch(() => {})
|
|
231
|
+
}
|
|
232
|
+
el.addEventListener('loadeddata', onReady)
|
|
233
|
+
if (el.readyState >= 2) onReady()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ---------- 播放控制 ----------
|
|
237
|
+
function playIdle() {
|
|
238
|
+
currentMode = 'idle'
|
|
239
|
+
const roll = Math.random()
|
|
240
|
+
let next = IDLE
|
|
241
|
+
if (roll < 0.3) {
|
|
242
|
+
next = IDLE
|
|
243
|
+
} else if (roll < 0.4) {
|
|
244
|
+
next = TURN
|
|
245
|
+
} else if (roll < 0.8) {
|
|
246
|
+
next = pick(ACTS, anim)
|
|
247
|
+
} else {
|
|
248
|
+
next = pick(ACTS, anim) // 桌面版暂不实现自动漫游,避免窗口移动过于复杂
|
|
249
|
+
}
|
|
250
|
+
anim = next
|
|
251
|
+
animOnce = true
|
|
252
|
+
animLoop = false
|
|
253
|
+
switchTo(next, { once: true })
|
|
254
|
+
// 随机动作播放时,给出与动作匹配的可爱气泡描述。
|
|
255
|
+
if (next !== IDLE) {
|
|
256
|
+
const copy = ACTION_COPY[next] || '大肥鱼正在表演小动作~'
|
|
257
|
+
showManualBubble(copy, '大肥鱼的小剧场~', 4200)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function playState(state, { pulse = false } = {}) {
|
|
262
|
+
currentMode = pulse ? 'pulse' : 'state'
|
|
263
|
+
const pool = STATE_ANIMS[state] || STATE_ANIMS.IDLE
|
|
264
|
+
const next = state === 'IDLE' ? IDLE : pick(pool, anim)
|
|
265
|
+
anim = next
|
|
266
|
+
// 非空闲状态动画循环播放,避免播完一帧后停在原地;空闲状态仍走随机动画链。
|
|
267
|
+
animLoop = !pulse && state !== 'IDLE'
|
|
268
|
+
animOnce = !animLoop
|
|
269
|
+
switchTo(next, { once: !animLoop, loop: animLoop })
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function playClick() {
|
|
273
|
+
currentMode = 'click'
|
|
274
|
+
const next = pick(CLICKS, anim)
|
|
275
|
+
anim = next
|
|
276
|
+
animOnce = true
|
|
277
|
+
animLoop = false
|
|
278
|
+
switchTo(next, { once: true })
|
|
279
|
+
showManualBubble(CLICK_COPY[next] || '大肥鱼被戳了一下~', '大肥鱼的小剧场~', 2500)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function playDrag() {
|
|
283
|
+
currentMode = 'drag'
|
|
284
|
+
anim = DRAG
|
|
285
|
+
animOnce = true
|
|
286
|
+
animLoop = false
|
|
287
|
+
switchTo(DRAG, { once: true })
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function handleEnded() {
|
|
291
|
+
if (dragging || dragState.active) return
|
|
292
|
+
if (overlay && overlay.state) {
|
|
293
|
+
// 脉冲动画播完但 TTL 未结束:气泡保留,动画先回到 resume 状态。
|
|
294
|
+
if (!overlay.animationDone) {
|
|
295
|
+
overlay.animationDone = true
|
|
296
|
+
applyResume()
|
|
297
|
+
}
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
if (currentMode === 'click') {
|
|
301
|
+
applyResume()
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
if (currentMode === 'state') {
|
|
305
|
+
const state = overlay ? overlay.state : currentState
|
|
306
|
+
if (state && state !== 'IDLE') {
|
|
307
|
+
playState(state)
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
playIdle()
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
if (anim === TURN) {
|
|
314
|
+
facing = facing === 'left' ? 'right' : 'left'
|
|
315
|
+
}
|
|
316
|
+
playIdle()
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function applyResume() {
|
|
320
|
+
const state = resumeState || currentState || 'IDLE'
|
|
321
|
+
currentState = state
|
|
322
|
+
// 强制清掉可能卡住的切换状态,确保拖拽结束后一定切回正常动画。
|
|
323
|
+
pending = null
|
|
324
|
+
if (state === 'IDLE') {
|
|
325
|
+
// 任务刚结束/拖拽结束后先回到纯待机,避免随机链立刻播放“思考类”动画。
|
|
326
|
+
status = {
|
|
327
|
+
...status,
|
|
328
|
+
state: 'IDLE',
|
|
329
|
+
message: resumeMessage || '大肥鱼在这儿等新任务哦~',
|
|
330
|
+
detail: resumeDetail || '大肥鱼 · 等待下一次任务',
|
|
331
|
+
}
|
|
332
|
+
currentMode = 'idle'
|
|
333
|
+
anim = IDLE
|
|
334
|
+
animOnce = true
|
|
335
|
+
animLoop = false
|
|
336
|
+
switchTo(IDLE, { once: true })
|
|
337
|
+
} else {
|
|
338
|
+
status = {
|
|
339
|
+
...status,
|
|
340
|
+
state,
|
|
341
|
+
message: resumeMessage || status.message,
|
|
342
|
+
detail: resumeDetail || status.detail,
|
|
343
|
+
}
|
|
344
|
+
playState(state)
|
|
345
|
+
}
|
|
346
|
+
updateBubble()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function clearOverlay() {
|
|
350
|
+
if (overlayTimer) clearTimeout(overlayTimer)
|
|
351
|
+
overlayTimer = null
|
|
352
|
+
overlay = null
|
|
353
|
+
lastPulseKey = null
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ---------- 消息处理 ----------
|
|
357
|
+
function applyStateMessage(message) {
|
|
358
|
+
const state = message.state || 'IDLE'
|
|
359
|
+
status = {
|
|
360
|
+
state,
|
|
361
|
+
message: message.message || status.message,
|
|
362
|
+
detail: message.detail || status.detail,
|
|
363
|
+
project: message.project || status.project,
|
|
364
|
+
task: message.task || status.task,
|
|
365
|
+
progress: message.progress || status.progress,
|
|
366
|
+
}
|
|
367
|
+
currentState = state
|
|
368
|
+
resumeState = state
|
|
369
|
+
resumeMessage = message.message || ''
|
|
370
|
+
resumeDetail = message.detail || ''
|
|
371
|
+
clearOverlay()
|
|
372
|
+
if (state === 'IDLE') playIdle()
|
|
373
|
+
else playState(state)
|
|
374
|
+
updateBubble()
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function applyPulseMessage(message) {
|
|
378
|
+
const state = message.state || 'SUCCESS'
|
|
379
|
+
resumeState = message.resumeState || currentState || 'IDLE'
|
|
380
|
+
resumeMessage = message.resumeMessage || ''
|
|
381
|
+
resumeDetail = message.resumeDetail || ''
|
|
382
|
+
overlay = {
|
|
383
|
+
state,
|
|
384
|
+
message: message.message || '',
|
|
385
|
+
detail: message.detail || '',
|
|
386
|
+
ttlMs: Number(message.ttlMs || 1800),
|
|
387
|
+
animationDone: false,
|
|
388
|
+
}
|
|
389
|
+
lastPulseKey = `${state}:${message.expiresAt || message.ttlMs || 1800}`
|
|
390
|
+
currentState = state
|
|
391
|
+
if (state === 'SUCCESS') {
|
|
392
|
+
window.petBridge.beep()
|
|
393
|
+
shakePet()
|
|
394
|
+
}
|
|
395
|
+
playState(state, { pulse: true })
|
|
396
|
+
if (overlayTimer) clearTimeout(overlayTimer)
|
|
397
|
+
overlayTimer = setTimeout(() => {
|
|
398
|
+
clearOverlay()
|
|
399
|
+
applyResume()
|
|
400
|
+
}, overlay.ttlMs)
|
|
401
|
+
updateBubble()
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function applyTaskMessage(message) {
|
|
405
|
+
status.task = message.task || status.task
|
|
406
|
+
status.progress = message.progress || status.progress
|
|
407
|
+
status.project = message.project || status.project
|
|
408
|
+
status.message = message.message || status.message
|
|
409
|
+
status.detail = message.detail || status.detail
|
|
410
|
+
updateBubble()
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function applyTasksMessage(message) {
|
|
414
|
+
tasks = Array.isArray(message.tasks) ? message.tasks : []
|
|
415
|
+
updateBubble()
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// ---------- 喂食/互动反馈 ----------
|
|
419
|
+
function showManualBubble(message, detail, ttl = 2200) {
|
|
420
|
+
if (manualBubbleTimer) clearTimeout(manualBubbleTimer)
|
|
421
|
+
manualBubble = { message, detail, expiresAt: Date.now() + ttl }
|
|
422
|
+
manualBubbleTimer = setTimeout(() => {
|
|
423
|
+
manualBubble = null
|
|
424
|
+
manualBubbleTimer = null
|
|
425
|
+
updateBubble()
|
|
426
|
+
}, ttl)
|
|
427
|
+
updateBubble()
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function feedPet() {
|
|
431
|
+
const next = pick(EAT_ANIMS, anim)
|
|
432
|
+
anim = next
|
|
433
|
+
animOnce = true
|
|
434
|
+
animLoop = false
|
|
435
|
+
currentMode = 'click' // 复用“一次性动画播完回当前状态”的路径
|
|
436
|
+
switchTo(next, { once: true })
|
|
437
|
+
showManualBubble('谢谢投喂大肥鱼~', '吃饱了更有力气干活!', 2200)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function shakePet() {
|
|
441
|
+
const originX = petPos.x
|
|
442
|
+
const originY = petPos.y
|
|
443
|
+
const offsets = [[6, 0], [-6, 0], [4, 0], [-4, 0], [2, 0], [-2, 0], [0, 0]]
|
|
444
|
+
let index = 0
|
|
445
|
+
const timer = setInterval(() => {
|
|
446
|
+
if (index >= offsets.length) {
|
|
447
|
+
clearInterval(timer)
|
|
448
|
+
applyPetPosition()
|
|
449
|
+
return
|
|
450
|
+
}
|
|
451
|
+
const [dx, dy] = offsets[index++]
|
|
452
|
+
rootEl.style.left = (originX + dx) + 'px'
|
|
453
|
+
rootEl.style.top = (originY + dy) + 'px'
|
|
454
|
+
}, 30)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ---------- 番茄钟 ----------
|
|
458
|
+
function formatTime(ms) {
|
|
459
|
+
const totalSeconds = Math.max(0, Math.ceil(ms / 1000))
|
|
460
|
+
const minutes = Math.floor(totalSeconds / 60)
|
|
461
|
+
const seconds = totalSeconds % 60
|
|
462
|
+
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function playAlarm() {
|
|
466
|
+
try {
|
|
467
|
+
const audio = new Audio(ALARM_URL)
|
|
468
|
+
audio.volume = 0.8
|
|
469
|
+
audio.play().catch(() => {})
|
|
470
|
+
} catch {
|
|
471
|
+
// 音频不可用时静默失败
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function startPomodoro(mode, minutes) {
|
|
476
|
+
stopPomodoro()
|
|
477
|
+
const durationMs = Math.max(1, minutes) * 60 * 1000
|
|
478
|
+
pomodoro = { mode, endAt: Date.now() + durationMs, durationMs }
|
|
479
|
+
pomodoroTimer = setInterval(updatePomodoro, 1000)
|
|
480
|
+
updateBubble()
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function stopPomodoro() {
|
|
484
|
+
if (pomodoroTimer) clearInterval(pomodoroTimer)
|
|
485
|
+
pomodoroTimer = null
|
|
486
|
+
pomodoro = null
|
|
487
|
+
updateBubble()
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function updatePomodoro() {
|
|
491
|
+
if (!pomodoro) return
|
|
492
|
+
const remaining = pomodoro.endAt - Date.now()
|
|
493
|
+
if (remaining <= 0) {
|
|
494
|
+
completePomodoro()
|
|
495
|
+
return
|
|
496
|
+
}
|
|
497
|
+
updateBubble()
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function completePomodoro() {
|
|
501
|
+
const mode = pomodoro?.mode || 'work'
|
|
502
|
+
stopPomodoro()
|
|
503
|
+
playAlarm()
|
|
504
|
+
shakePet()
|
|
505
|
+
showManualBubble(
|
|
506
|
+
mode === 'work' ? '🍅 大肥鱼番茄钟结束啦!' : '☕ 大肥鱼休息结束啦!',
|
|
507
|
+
mode === 'work' ? '快休息一下,摸摸鱼~' : '继续加油,大肥鱼陪你!',
|
|
508
|
+
4000,
|
|
509
|
+
)
|
|
510
|
+
playClick()
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// ---------- 气泡 ----------
|
|
514
|
+
function visibleState() {
|
|
515
|
+
if (overlay) return overlay.state
|
|
516
|
+
if (tasks.length >= 2) return status.state
|
|
517
|
+
return status.state || 'IDLE'
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function updateBubble() {
|
|
521
|
+
const state = visibleState()
|
|
522
|
+
const manualActive = manualBubble && manualBubble.expiresAt > Date.now()
|
|
523
|
+
const pomodoroActive = pomodoro && pomodoro.endAt > Date.now()
|
|
524
|
+
const shouldShow = pomodoroActive || manualActive
|
|
525
|
+
? CONFIG.bubbleMode !== 'hidden'
|
|
526
|
+
: CONFIG.bubbleMode === 'always'
|
|
527
|
+
|| (CONFIG.bubbleMode === 'custom' && CONFIG.bubbleStates.includes(state))
|
|
528
|
+
|| (CONFIG.bubbleMode === 'custom' && tasks.length >= 2 && tasks.some((t) => CONFIG.bubbleStates.includes(t.state)))
|
|
529
|
+
if (CONFIG.bubbleMode === 'hidden' || !shouldShow) {
|
|
530
|
+
bubbleEl.classList.remove('visible')
|
|
531
|
+
return
|
|
532
|
+
}
|
|
533
|
+
let title = ''
|
|
534
|
+
let detail = ''
|
|
535
|
+
if (pomodoroActive) {
|
|
536
|
+
const remaining = Math.max(0, pomodoro.endAt - Date.now())
|
|
537
|
+
const label = pomodoro.mode === 'work' ? '🍅 番茄钟' : '☕ 休息'
|
|
538
|
+
title = `${label} ${formatTime(remaining)}`
|
|
539
|
+
detail = pomodoro.mode === 'work' ? '大肥鱼专注中,冲鸭~' : '大肥鱼摸鱼中,放松一下~'
|
|
540
|
+
} else if (manualActive) {
|
|
541
|
+
title = manualBubble.message
|
|
542
|
+
detail = manualBubble.detail || ''
|
|
543
|
+
} else if (tasks.length >= 2) {
|
|
544
|
+
title = `${tasks.length} 个任务进行中`
|
|
545
|
+
detail = tasks.slice(0, 3).map((t) => `${t.state || ''} · ${t.project || t.task || t.message || ''}`).join(';')
|
|
546
|
+
} else if (overlay && overlay.message) {
|
|
547
|
+
title = overlay.message
|
|
548
|
+
detail = overlay.detail || status.detail || ''
|
|
549
|
+
} else if (status.message) {
|
|
550
|
+
title = status.message
|
|
551
|
+
detail = status.detail || ''
|
|
552
|
+
} else {
|
|
553
|
+
title = '大肥鱼正在摸鱼中~'
|
|
554
|
+
detail = ''
|
|
555
|
+
}
|
|
556
|
+
// 余额显示:优先展示 DeepSeek 账户余额,不再显示 Token。
|
|
557
|
+
if (balance && balance.total !== undefined && balance.total !== null) {
|
|
558
|
+
const symbol = balance.currency === 'CNY' ? '¥' : balance.currency === 'USD' ? '$' : `${balance.currency} `
|
|
559
|
+
const balanceText = `余额 ${symbol}${balance.total}`
|
|
560
|
+
detail = detail ? `${detail} · ${balanceText}` : balanceText
|
|
561
|
+
}
|
|
562
|
+
bubbleTitle.textContent = title
|
|
563
|
+
bubbleDetail.textContent = detail
|
|
564
|
+
bubbleEl.style.transform = `translateX(-50%) scale(${CONFIG.bubbleScale})`
|
|
565
|
+
bubbleEl.classList.add('visible')
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// ---------- 交互 ----------
|
|
569
|
+
const DRAG_THRESHOLD = 5
|
|
570
|
+
|
|
571
|
+
// 让透明窗口只在宠物/菜单区域接收鼠标,其余区域点击穿透到下层应用。
|
|
572
|
+
function updateClickThrough() {
|
|
573
|
+
const rect = hitEl.getBoundingClientRect()
|
|
574
|
+
const inside = lastMouse.x >= rect.left && lastMouse.x <= rect.right
|
|
575
|
+
&& lastMouse.y >= rect.top && lastMouse.y <= rect.bottom
|
|
576
|
+
const menuVisible = menuEl.classList.contains('visible')
|
|
577
|
+
const ignore = !inside && !dragging && !dragState.active && !menuVisible
|
|
578
|
+
window.petBridge.setIgnoreMouse(ignore)
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
document.addEventListener('mousemove', (e) => {
|
|
582
|
+
lastMouse = { x: e.clientX, y: e.clientY }
|
|
583
|
+
updateClickThrough()
|
|
584
|
+
})
|
|
585
|
+
|
|
586
|
+
function startDrag(e) {
|
|
587
|
+
e.currentTarget.classList.add('dragging')
|
|
588
|
+
try { e.currentTarget.setPointerCapture(e.pointerId) } catch { /* 忽略捕获失败 */ }
|
|
589
|
+
dragState = {
|
|
590
|
+
active: true,
|
|
591
|
+
dragging: false,
|
|
592
|
+
pointerId: e.pointerId,
|
|
593
|
+
sx: e.screenX,
|
|
594
|
+
sy: e.screenY,
|
|
595
|
+
lastX: e.screenX,
|
|
596
|
+
lastY: e.screenY,
|
|
597
|
+
}
|
|
598
|
+
window.petBridge.setIgnoreMouse(false)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function moveDrag(e) {
|
|
602
|
+
const d = dragState
|
|
603
|
+
if (!d.active) return
|
|
604
|
+
const totalDx = e.screenX - d.sx
|
|
605
|
+
const totalDy = e.screenY - d.sy
|
|
606
|
+
if (!d.dragging) {
|
|
607
|
+
if (Math.hypot(totalDx, totalDy) < DRAG_THRESHOLD) return
|
|
608
|
+
d.dragging = true
|
|
609
|
+
dragging = true
|
|
610
|
+
playDrag()
|
|
611
|
+
showManualBubble('大肥鱼被拎起来啦!', '快放我下来~', 3000)
|
|
612
|
+
}
|
|
613
|
+
// 在全屏透明画布内移动宠物 DOM,避免移动窗口导致的指针事件问题。
|
|
614
|
+
const dx = e.movementX || 0
|
|
615
|
+
const dy = e.movementY || 0
|
|
616
|
+
if (dx || dy) {
|
|
617
|
+
// 按角色实际命中区域贴边,而不是按整个透明舞台贴边,
|
|
618
|
+
// 这样宠物可以真正拖到屏幕最左/最右。
|
|
619
|
+
const hitLeft = HIT_BOX.x0 / 640 * size
|
|
620
|
+
const hitRight = HIT_BOX.x1 / 640 * size
|
|
621
|
+
const minX = -hitLeft
|
|
622
|
+
const maxX = window.innerWidth - hitRight
|
|
623
|
+
petPos.x = Math.min(Math.max(petPos.x + dx, minX), maxX)
|
|
624
|
+
petPos.y = Math.min(Math.max(petPos.y + dy, 0), window.innerHeight - size * 9 / 16)
|
|
625
|
+
applyPetPosition()
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function endDrag(e) {
|
|
630
|
+
const d = dragState
|
|
631
|
+
if (!d.active) return
|
|
632
|
+
const wasDragging = d.dragging
|
|
633
|
+
dragState = { active: false, dragging: false, sx: 0, sy: 0, lastX: 0, lastY: 0 }
|
|
634
|
+
dragging = false
|
|
635
|
+
window.petBridge.endDrag()
|
|
636
|
+
if (e.currentTarget && e.currentTarget.classList) {
|
|
637
|
+
e.currentTarget.classList.remove('dragging')
|
|
638
|
+
} else {
|
|
639
|
+
hitEl.classList.remove('dragging')
|
|
640
|
+
}
|
|
641
|
+
if (wasDragging) {
|
|
642
|
+
justDragged = true
|
|
643
|
+
setTimeout(() => { justDragged = false }, 100)
|
|
644
|
+
applyResume()
|
|
645
|
+
showManualBubble('终于放我下来啦~', '', 2000)
|
|
646
|
+
}
|
|
647
|
+
updateClickThrough()
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
hitEl.addEventListener('pointerdown', startDrag)
|
|
651
|
+
hitEl.addEventListener('pointermove', moveDrag)
|
|
652
|
+
// 即使 pointerup 因为窗口移动没有落在宠物元素上,也要保证拖拽状态被重置。
|
|
653
|
+
window.addEventListener('pointerup', endDrag)
|
|
654
|
+
window.addEventListener('pointercancel', endDrag)
|
|
655
|
+
hitEl.addEventListener('lostpointercapture', endDrag)
|
|
656
|
+
|
|
657
|
+
hitEl.addEventListener('click', (e) => {
|
|
658
|
+
if (dragState.active || dragging || justDragged) return
|
|
659
|
+
playClick()
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
hitEl.addEventListener('contextmenu', (e) => {
|
|
663
|
+
e.preventDefault()
|
|
664
|
+
showMenu(e.clientX, e.clientY)
|
|
665
|
+
})
|
|
666
|
+
|
|
667
|
+
function addMenuButton(label, onClick) {
|
|
668
|
+
const button = document.createElement('button')
|
|
669
|
+
button.textContent = label
|
|
670
|
+
button.addEventListener('click', onClick)
|
|
671
|
+
menuEl.appendChild(button)
|
|
672
|
+
return button
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function showMenu(x, y) {
|
|
676
|
+
lastMenuPos = { x, y }
|
|
677
|
+
menuEl.innerHTML = ''
|
|
678
|
+
if (menuPage === 'pomodoro') {
|
|
679
|
+
renderPomodoroSettings()
|
|
680
|
+
} else {
|
|
681
|
+
renderMainMenu()
|
|
682
|
+
}
|
|
683
|
+
menuEl.style.left = Math.min(x, window.innerWidth - 180) + 'px'
|
|
684
|
+
menuEl.style.top = Math.min(y, window.innerHeight - 200) + 'px'
|
|
685
|
+
menuEl.classList.add('visible')
|
|
686
|
+
window.petBridge.setIgnoreMouse(false)
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function renderMainMenu() {
|
|
690
|
+
addMenuButton('喂食', () => {
|
|
691
|
+
menuEl.classList.remove('visible')
|
|
692
|
+
updateClickThrough()
|
|
693
|
+
feedPet()
|
|
694
|
+
})
|
|
695
|
+
addMenuButton(`开始番茄钟 ${CONFIG.workMinutes}分`, () => {
|
|
696
|
+
menuEl.classList.remove('visible')
|
|
697
|
+
updateClickThrough()
|
|
698
|
+
startPomodoro('work', CONFIG.workMinutes)
|
|
699
|
+
})
|
|
700
|
+
addMenuButton(`开始短休息 ${CONFIG.breakMinutes}分`, () => {
|
|
701
|
+
menuEl.classList.remove('visible')
|
|
702
|
+
updateClickThrough()
|
|
703
|
+
startPomodoro('break', CONFIG.breakMinutes)
|
|
704
|
+
})
|
|
705
|
+
if (pomodoro) {
|
|
706
|
+
addMenuButton('停止番茄钟', () => {
|
|
707
|
+
menuEl.classList.remove('visible')
|
|
708
|
+
updateClickThrough()
|
|
709
|
+
stopPomodoro()
|
|
710
|
+
})
|
|
711
|
+
}
|
|
712
|
+
addMenuButton('番茄钟设置', () => {
|
|
713
|
+
menuPage = 'pomodoro'
|
|
714
|
+
showMenu(lastMenuPos.x, lastMenuPos.y)
|
|
715
|
+
})
|
|
716
|
+
addMenuButton(CONFIG.roastEnabled ? '关闭自动吐槽' : '开启自动吐槽', () => {
|
|
717
|
+
const next = !CONFIG.roastEnabled
|
|
718
|
+
CONFIG.roastEnabled = next
|
|
719
|
+
window.petBridge.saveConfig({ roastEnabled: next })
|
|
720
|
+
menuEl.classList.remove('visible')
|
|
721
|
+
updateClickThrough()
|
|
722
|
+
})
|
|
723
|
+
addMenuButton('让大肥鱼吐槽一下', () => {
|
|
724
|
+
menuEl.classList.remove('visible')
|
|
725
|
+
updateClickThrough()
|
|
726
|
+
window.petBridge.requestRoast()
|
|
727
|
+
})
|
|
728
|
+
addMenuButton('打开 DSH 桌面版', () => {
|
|
729
|
+
menuEl.classList.remove('visible')
|
|
730
|
+
updateClickThrough()
|
|
731
|
+
window.petBridge.openDesktop()
|
|
732
|
+
})
|
|
733
|
+
addMenuButton('本次隐藏', () => {
|
|
734
|
+
menuEl.classList.remove('visible')
|
|
735
|
+
updateClickThrough()
|
|
736
|
+
window.petBridge.hide()
|
|
737
|
+
})
|
|
738
|
+
addMenuButton('本次关闭', () => {
|
|
739
|
+
menuEl.classList.remove('visible')
|
|
740
|
+
updateClickThrough()
|
|
741
|
+
window.petBridge.close('user')
|
|
742
|
+
})
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function renderPomodoroSettings() {
|
|
746
|
+
const workInput = document.createElement('input')
|
|
747
|
+
workInput.type = 'number'
|
|
748
|
+
workInput.min = 1
|
|
749
|
+
workInput.max = 120
|
|
750
|
+
workInput.value = String(CONFIG.workMinutes)
|
|
751
|
+
workInput.style.width = '80px'
|
|
752
|
+
|
|
753
|
+
const breakInput = document.createElement('input')
|
|
754
|
+
breakInput.type = 'number'
|
|
755
|
+
breakInput.min = 1
|
|
756
|
+
breakInput.max = 60
|
|
757
|
+
breakInput.value = String(CONFIG.breakMinutes)
|
|
758
|
+
breakInput.style.width = '80px'
|
|
759
|
+
|
|
760
|
+
const workRow = document.createElement('div')
|
|
761
|
+
workRow.style.cssText = 'display:flex;justify-content:space-between;align-items:center;gap:8px;padding:4px 0'
|
|
762
|
+
workRow.append('工作时长(分)', workInput)
|
|
763
|
+
|
|
764
|
+
const breakRow = document.createElement('div')
|
|
765
|
+
breakRow.style.cssText = 'display:flex;justify-content:space-between;align-items:center;gap:8px;padding:4px 0'
|
|
766
|
+
breakRow.append('休息时长(分)', breakInput)
|
|
767
|
+
|
|
768
|
+
menuEl.append(workRow, breakRow)
|
|
769
|
+
|
|
770
|
+
addMenuButton('保存', () => {
|
|
771
|
+
const work = Math.min(120, Math.max(1, Number(workInput.value) || 25))
|
|
772
|
+
const rest = Math.min(60, Math.max(1, Number(breakInput.value) || 5))
|
|
773
|
+
CONFIG.workMinutes = work
|
|
774
|
+
CONFIG.breakMinutes = rest
|
|
775
|
+
window.petBridge.saveConfig({ workMinutes: work, breakMinutes: rest })
|
|
776
|
+
menuPage = 'main'
|
|
777
|
+
menuEl.classList.remove('visible')
|
|
778
|
+
updateClickThrough()
|
|
779
|
+
updateBubble()
|
|
780
|
+
})
|
|
781
|
+
addMenuButton('返回', () => {
|
|
782
|
+
menuPage = 'main'
|
|
783
|
+
showMenu(lastMenuPos.x, lastMenuPos.y)
|
|
784
|
+
})
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
document.addEventListener('click', (e) => {
|
|
788
|
+
if (!menuEl.contains(e.target)) {
|
|
789
|
+
menuEl.classList.remove('visible')
|
|
790
|
+
updateClickThrough()
|
|
791
|
+
}
|
|
792
|
+
})
|
|
793
|
+
|
|
794
|
+
// ---------- 状态订阅 ----------
|
|
795
|
+
function applyStatus(incoming) {
|
|
796
|
+
if (!incoming || typeof incoming !== 'object') return
|
|
797
|
+
|
|
798
|
+
if (incoming.config) {
|
|
799
|
+
CONFIG.bubbleMode = incoming.config.bubbleMode || CONFIG.bubbleMode
|
|
800
|
+
CONFIG.bubbleStates = Array.isArray(incoming.config.bubbleStates) ? incoming.config.bubbleStates : CONFIG.bubbleStates
|
|
801
|
+
CONFIG.reducedMotion = incoming.config.reducedMotion === true
|
|
802
|
+
CONFIG.activityLevel = incoming.config.activityLevel || CONFIG.activityLevel
|
|
803
|
+
CONFIG.bubbleScale = Number(incoming.config.bubbleScale || CONFIG.bubbleScale)
|
|
804
|
+
CONFIG.workMinutes = Number(incoming.config.workMinutes) || CONFIG.workMinutes
|
|
805
|
+
CONFIG.breakMinutes = Number(incoming.config.breakMinutes) || CONFIG.breakMinutes
|
|
806
|
+
CONFIG.roastEnabled = incoming.config.roastEnabled === true
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
if (incoming.tokenUsage) {
|
|
810
|
+
tokenUsage = {
|
|
811
|
+
inputTokens: Number(incoming.tokenUsage.inputTokens) || 0,
|
|
812
|
+
outputTokens: Number(incoming.tokenUsage.outputTokens) || 0,
|
|
813
|
+
totalTokens: Number(incoming.tokenUsage.totalTokens) || 0,
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
if (incoming.balance) {
|
|
818
|
+
balance = {
|
|
819
|
+
currency: incoming.balance.currency || 'CNY',
|
|
820
|
+
total: incoming.balance.total,
|
|
821
|
+
granted: incoming.balance.granted,
|
|
822
|
+
toppedUp: incoming.balance.toppedUp,
|
|
823
|
+
isAvailable: incoming.balance.isAvailable !== false,
|
|
824
|
+
updatedAt: incoming.balance.updatedAt,
|
|
825
|
+
}
|
|
826
|
+
} else if (incoming.balance === null) {
|
|
827
|
+
balance = null
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
if (incoming.roast && incoming.roast !== lastRoast) {
|
|
831
|
+
roast = incoming.roast
|
|
832
|
+
lastRoast = incoming.roast
|
|
833
|
+
showManualBubble(roast, '大肥鱼吐槽time~', 5000)
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
if (incoming.pulse) {
|
|
837
|
+
// 同一个脉冲持续轮询时不要反复重播动画。
|
|
838
|
+
const pulseKey = `${incoming.pulse.state}:${incoming.pulse.expiresAt || incoming.pulse.ttlMs || 1800}`
|
|
839
|
+
if (lastPulseKey === pulseKey) {
|
|
840
|
+
if (overlay) {
|
|
841
|
+
overlay.message = incoming.pulse.message || overlay.message
|
|
842
|
+
overlay.detail = incoming.pulse.detail || overlay.detail
|
|
843
|
+
}
|
|
844
|
+
updateBubble()
|
|
845
|
+
return
|
|
846
|
+
}
|
|
847
|
+
applyPulseMessage(incoming.pulse)
|
|
848
|
+
return
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if (incoming.tasks && incoming.tasks.length >= 2) {
|
|
852
|
+
tasks = incoming.tasks
|
|
853
|
+
} else if (incoming.tasks) {
|
|
854
|
+
tasks = incoming.tasks
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
const nextState = incoming.state || 'IDLE'
|
|
858
|
+
if (nextState !== currentState) {
|
|
859
|
+
applyStateMessage({
|
|
860
|
+
state: nextState,
|
|
861
|
+
message: incoming.message,
|
|
862
|
+
detail: incoming.detail,
|
|
863
|
+
project: incoming.project,
|
|
864
|
+
task: incoming.task,
|
|
865
|
+
progress: incoming.progress,
|
|
866
|
+
})
|
|
867
|
+
} else {
|
|
868
|
+
// 状态没变:只更新气泡文案/任务信息,不打断当前动画。
|
|
869
|
+
status = {
|
|
870
|
+
...status,
|
|
871
|
+
state: nextState,
|
|
872
|
+
message: incoming.message ?? status.message,
|
|
873
|
+
detail: incoming.detail ?? status.detail,
|
|
874
|
+
project: incoming.project ?? status.project,
|
|
875
|
+
task: incoming.task ?? status.task,
|
|
876
|
+
progress: incoming.progress ?? status.progress,
|
|
877
|
+
}
|
|
878
|
+
updateBubble()
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
window.petBridge.onStatus((status) => {
|
|
883
|
+
applyStatus(status)
|
|
884
|
+
})
|
|
885
|
+
|
|
886
|
+
// ---------- 启动 ----------
|
|
887
|
+
playIdle()
|
|
888
|
+
updateBubble()
|
|
889
|
+
updateClickThrough()
|
|
890
|
+
|
|
891
|
+
// 看门狗:万一 video 的 ended 事件或 loop 没有按预期工作,这里兜底,
|
|
892
|
+
// 避免宠物停在最后一帧。
|
|
893
|
+
setInterval(() => {
|
|
894
|
+
if (pending) return
|
|
895
|
+
const el = currentVideo()
|
|
896
|
+
if (!el || !el.ended) return
|
|
897
|
+
if (animLoop) {
|
|
898
|
+
el.currentTime = 0
|
|
899
|
+
el.play().catch(() => {})
|
|
900
|
+
} else if (!dragging && !dragState.active) {
|
|
901
|
+
handleEnded()
|
|
902
|
+
}
|
|
903
|
+
}, 500)
|