nonebot-plugin-bugu 0.3.0__tar.gz → 0.4.0__tar.gz

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.
Files changed (32) hide show
  1. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/PKG-INFO +28 -9
  2. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/README.md +27 -8
  3. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/commands.py +3 -1
  4. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/draw.py +10 -0
  5. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/common.py +33 -1
  6. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/recipes/render.py +130 -1
  7. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/recipes/reply.py +105 -8
  8. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/starsea/render.py +275 -0
  9. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/starsea/reply.py +192 -20
  10. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/pyproject.toml +1 -1
  11. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/LICENSE +0 -0
  12. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/__init__.py +0 -0
  13. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/api.py +0 -0
  14. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/config.py +0 -0
  15. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/__init__.py +0 -0
  16. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/flowers/__init__.py +0 -0
  17. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/flowers/data.py +0 -0
  18. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/flowers/render.py +0 -0
  19. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/flowers/reply.py +0 -0
  20. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/neighbors/__init__.py +0 -0
  21. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/neighbors/data.py +0 -0
  22. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/neighbors/render.py +0 -0
  23. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/neighbors/reply.py +0 -0
  24. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/recipes/__init__.py +0 -0
  25. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/recipes/data.py +0 -0
  26. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/recipes/search.py +0 -0
  27. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/starsea/__init__.py +0 -0
  28. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/starsea/data.py +0 -0
  29. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/features/starsea/search.py +0 -0
  30. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/fonts.py +0 -0
  31. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/runtime.py +0 -0
  32. {nonebot_plugin_bugu-0.3.0 → nonebot_plugin_bugu-0.4.0}/nonebot_plugin_bugu/values.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot-plugin-bugu
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: 布谷工具箱(星布谷地小助手):菜谱、星海地图、友邻备忘与鲜花培育
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -37,8 +37,8 @@ Description-Content-Type: text/markdown
37
37
 
38
38
  | 模块 | 能干什么 |
39
39
  | --- | --- |
40
- | 🍳 **菜谱查询** | 121 道菜谱的食材 / 厨具 / 力气 / 特殊效果 / 获取途径,支持按食材、类型、厨具、效果、特征搜索 |
41
- | 🗺 **星海地图** | 3 张地图 43 个点位,底图自动编号 + 图例;点位详情是带品质色的图标墙;还能按物种反查「在哪几个岛」 |
40
+ | 🍳 **菜谱查询** | 121 道菜谱的食材 / 厨具 / 力气 / 特殊效果 / 获取途径;支持按食材、类型、厨具、效果、特征搜索,也能按目录一页页翻 |
41
+ | 🗺 **星海地图** | 3 张地图 43 个点位,底图自动编号 + 图例;点位详情是带品质色的图标墙;物种图鉴可按大类翻页;还能按物种反查「在哪几个岛」 |
42
42
  | 🧑‍🤝‍🧑 **友邻备忘** | 10 位友邻的立绘、资料、介绍和喜爱 / 好感 / 反感 / 关于词条与友邻能力 |
43
43
  | 🌷 **鲜花培育** | 13 个品种 87 种花色的杂交配方表 + 花色图鉴(品质、颜色、产地) |
44
44
 
@@ -52,6 +52,12 @@ Description-Content-Type: text/markdown
52
52
 
53
53
  ![鲜花培育](docs/flower-card.png)
54
54
 
55
+ 不知道菜名 / 物种名也能查——目录是图片,用页码翻:
56
+
57
+ ![菜谱目录](docs/recipe-catalog.png)
58
+
59
+ ![星海图鉴](docs/starsea-catalog.png)
60
+
55
61
  ## 安装
56
62
 
57
63
  需要 Python 3.9+ 和 NoneBot2(OneBot V11 适配器)。
@@ -116,12 +122,14 @@ nonebot.load_plugin("nonebot_plugin_bugu")
116
122
  | 指令 | 权限 | 说明 |
117
123
  | --- | --- | --- |
118
124
  | `/布谷` | 所有人 | 全部指令一览 |
125
+ | `/菜谱 列表 [类型/厨具/词] [页码]` | 所有人 | 菜谱目录卡片,可翻页 |
119
126
  | `/菜谱 [菜名 / 编号]` | 所有人 | 不带参数看统计;带参数出菜谱卡片 |
120
- | `/菜谱 随机` | 所有人 | 随手看一道 |
127
+ | `/菜谱 随机 [条件]` | 所有人 | 随手看一道(可按类型/厨具限定) |
121
128
  | `/菜谱查 <关键词>` | 所有人 | 搜索 / 筛选(关键词可混写) |
122
129
  | `/星海 <任意词>` | 所有人 | 智能识别区域 / 点位 / 物种 |
123
130
  | `/星海地图 [区域]` | 所有人 | 区域地图卡片,不带参数列所有区域 |
124
- | `/星海点位 <点位名>` | 所有人 | 点位详情卡片 |
131
+ | `/星海 列表 [大类] [页码]` | 所有人 | 物种图鉴卡片;不带大类先出大类索引 |
132
+ | `/星海点位 [区域 / 点位名]` | 所有人 | 给区域出点位清单卡片,给点位名出详情 |
125
133
  | `/星海查 <物种 / 大类>` | 所有人 | 跨区域搜索 |
126
134
  | `/友邻 [名字] [分区]` | 所有人 | 友邻名单 / 档案卡;分区可填 喜爱 / 好感 / 反感 / 关于 / 介绍 |
127
135
  | `/鲜花 [品种 / 花名]` | 所有人 | 品种列表 / 培育配方卡 / 单朵花详情 |
@@ -132,25 +140,36 @@ nonebot.load_plugin("nonebot_plugin_bugu")
132
140
  一个查询里可以同时混写食材、菜名、类型、厨具、效果、特征,规则和站点一致:
133
141
 
134
142
  ```
143
+ /菜谱 列表 → 目录第 1 页(每页 15 道,5×3)
144
+ /菜谱 列表 2 → 第 2 页
145
+ /菜谱 列表 简单 → 只看简单类型
146
+ /菜谱 列表 煮锅 3 → 煮锅分类的第 3 页
135
147
  /菜谱 炒鲜蔬 → 精确菜名出卡片
136
148
  /菜谱 12 → 按编号出卡片
149
+ /菜谱 随机 汤羹 → 在汤羹里随机
137
150
  /菜谱查 苹果 → 「可用苹果烹饪」+「名称含苹果」两组
138
151
  /菜谱查 煮锅 苹果 → 用苹果做的煮锅菜谱
139
152
  /菜谱查 秘制 → 只看秘制菜谱
140
153
  /菜谱查 甜点 煮锅 → 甜点特征 + 煮锅
141
154
  ```
142
155
 
156
+ 打错字也有救:没命中时会用编辑距离给几个相近的菜名。
157
+
143
158
  支持的筛选值:菜品类型(简单 / 招牌 / 宴客 / 秘制 / 奇迹盛宴)、厨具(煮锅 / 烤箱 / 榨汁机)、
144
159
  特殊效果(大力播洒 / 大力敲伐 / 钓鱼之力……)、菜品特征(荤菜 / 素菜 / 主食 / 汤羹……)。
145
160
 
146
161
  ### 星海地图
147
162
 
148
163
  ```
149
- /星海地图 → 三个区域列表
150
- /星海地图 H83 → 禾夏 H83 区地图卡片(点位 1~15 编号 + 图例)
164
+ /星海 → 星海用法
165
+ /星海 列表 → 图鉴大类索引(11 个大类 + 数量 + 代表物种)
166
+ /星海 列表 河鱼 → 河鱼图鉴(27 种,每页 32 个,带品质/特产/采集点数量)
167
+ /星海 列表 鱼 2 → 口语别名 + 翻页
168
+ /星海地图 → 三个区域列表
169
+ /星海地图 H83 → 禾夏 H83 区地图卡片(点位 1~15 编号 + 图例)
170
+ /星海点位 H83 → 这个区域的点位清单(编号/类型/物种数)
151
171
  /星海点位 迷途小屋之屿 → 点位详情(昆虫 12 / 树木 3 / 鲜花 3 / 创想卡 1 / 种子 1)
152
172
  /星海查 三色锦鲤 → 这个物种出现在哪几个点位
153
- /星海 河鱼 → 智能识别
154
173
  ```
155
174
 
156
175
  搜索支持物种名、大类(`鱼`→河鱼/海鱼,`生物`→昆虫/河鱼/海鱼/海岸生物,也认 `树`、`花`、`矿`、`特产`)和点位名。
@@ -200,7 +219,7 @@ nonebot_plugin_bugu/
200
219
  ├── config.py # 配置项
201
220
  ├── api.py # 四个接口的客户端 + 内存 / 磁盘缓存
202
221
  ├── values.py # 字段兜底与通用小工具
203
- ├── draw.py # 共用绘制:画布、字体、折行、胶囊、页脚
222
+ ├── draw.py # 共用绘制:画布、字体、折行、胶囊、页脚、分页卡片
204
223
  ├── fonts.py # 中文字体探测
205
224
  ├── runtime.py # 单例:客户端 / 字体 / 进度
206
225
  ├── commands.py # 命令注册与分发
@@ -9,8 +9,8 @@
9
9
 
10
10
  | 模块 | 能干什么 |
11
11
  | --- | --- |
12
- | 🍳 **菜谱查询** | 121 道菜谱的食材 / 厨具 / 力气 / 特殊效果 / 获取途径,支持按食材、类型、厨具、效果、特征搜索 |
13
- | 🗺 **星海地图** | 3 张地图 43 个点位,底图自动编号 + 图例;点位详情是带品质色的图标墙;还能按物种反查「在哪几个岛」 |
12
+ | 🍳 **菜谱查询** | 121 道菜谱的食材 / 厨具 / 力气 / 特殊效果 / 获取途径;支持按食材、类型、厨具、效果、特征搜索,也能按目录一页页翻 |
13
+ | 🗺 **星海地图** | 3 张地图 43 个点位,底图自动编号 + 图例;点位详情是带品质色的图标墙;物种图鉴可按大类翻页;还能按物种反查「在哪几个岛」 |
14
14
  | 🧑‍🤝‍🧑 **友邻备忘** | 10 位友邻的立绘、资料、介绍和喜爱 / 好感 / 反感 / 关于词条与友邻能力 |
15
15
  | 🌷 **鲜花培育** | 13 个品种 87 种花色的杂交配方表 + 花色图鉴(品质、颜色、产地) |
16
16
 
@@ -24,6 +24,12 @@
24
24
 
25
25
  ![鲜花培育](docs/flower-card.png)
26
26
 
27
+ 不知道菜名 / 物种名也能查——目录是图片,用页码翻:
28
+
29
+ ![菜谱目录](docs/recipe-catalog.png)
30
+
31
+ ![星海图鉴](docs/starsea-catalog.png)
32
+
27
33
  ## 安装
28
34
 
29
35
  需要 Python 3.9+ 和 NoneBot2(OneBot V11 适配器)。
@@ -88,12 +94,14 @@ nonebot.load_plugin("nonebot_plugin_bugu")
88
94
  | 指令 | 权限 | 说明 |
89
95
  | --- | --- | --- |
90
96
  | `/布谷` | 所有人 | 全部指令一览 |
97
+ | `/菜谱 列表 [类型/厨具/词] [页码]` | 所有人 | 菜谱目录卡片,可翻页 |
91
98
  | `/菜谱 [菜名 / 编号]` | 所有人 | 不带参数看统计;带参数出菜谱卡片 |
92
- | `/菜谱 随机` | 所有人 | 随手看一道 |
99
+ | `/菜谱 随机 [条件]` | 所有人 | 随手看一道(可按类型/厨具限定) |
93
100
  | `/菜谱查 <关键词>` | 所有人 | 搜索 / 筛选(关键词可混写) |
94
101
  | `/星海 <任意词>` | 所有人 | 智能识别区域 / 点位 / 物种 |
95
102
  | `/星海地图 [区域]` | 所有人 | 区域地图卡片,不带参数列所有区域 |
96
- | `/星海点位 <点位名>` | 所有人 | 点位详情卡片 |
103
+ | `/星海 列表 [大类] [页码]` | 所有人 | 物种图鉴卡片;不带大类先出大类索引 |
104
+ | `/星海点位 [区域 / 点位名]` | 所有人 | 给区域出点位清单卡片,给点位名出详情 |
97
105
  | `/星海查 <物种 / 大类>` | 所有人 | 跨区域搜索 |
98
106
  | `/友邻 [名字] [分区]` | 所有人 | 友邻名单 / 档案卡;分区可填 喜爱 / 好感 / 反感 / 关于 / 介绍 |
99
107
  | `/鲜花 [品种 / 花名]` | 所有人 | 品种列表 / 培育配方卡 / 单朵花详情 |
@@ -104,25 +112,36 @@ nonebot.load_plugin("nonebot_plugin_bugu")
104
112
  一个查询里可以同时混写食材、菜名、类型、厨具、效果、特征,规则和站点一致:
105
113
 
106
114
  ```
115
+ /菜谱 列表 → 目录第 1 页(每页 15 道,5×3)
116
+ /菜谱 列表 2 → 第 2 页
117
+ /菜谱 列表 简单 → 只看简单类型
118
+ /菜谱 列表 煮锅 3 → 煮锅分类的第 3 页
107
119
  /菜谱 炒鲜蔬 → 精确菜名出卡片
108
120
  /菜谱 12 → 按编号出卡片
121
+ /菜谱 随机 汤羹 → 在汤羹里随机
109
122
  /菜谱查 苹果 → 「可用苹果烹饪」+「名称含苹果」两组
110
123
  /菜谱查 煮锅 苹果 → 用苹果做的煮锅菜谱
111
124
  /菜谱查 秘制 → 只看秘制菜谱
112
125
  /菜谱查 甜点 煮锅 → 甜点特征 + 煮锅
113
126
  ```
114
127
 
128
+ 打错字也有救:没命中时会用编辑距离给几个相近的菜名。
129
+
115
130
  支持的筛选值:菜品类型(简单 / 招牌 / 宴客 / 秘制 / 奇迹盛宴)、厨具(煮锅 / 烤箱 / 榨汁机)、
116
131
  特殊效果(大力播洒 / 大力敲伐 / 钓鱼之力……)、菜品特征(荤菜 / 素菜 / 主食 / 汤羹……)。
117
132
 
118
133
  ### 星海地图
119
134
 
120
135
  ```
121
- /星海地图 → 三个区域列表
122
- /星海地图 H83 → 禾夏 H83 区地图卡片(点位 1~15 编号 + 图例)
136
+ /星海 → 星海用法
137
+ /星海 列表 → 图鉴大类索引(11 个大类 + 数量 + 代表物种)
138
+ /星海 列表 河鱼 → 河鱼图鉴(27 种,每页 32 个,带品质/特产/采集点数量)
139
+ /星海 列表 鱼 2 → 口语别名 + 翻页
140
+ /星海地图 → 三个区域列表
141
+ /星海地图 H83 → 禾夏 H83 区地图卡片(点位 1~15 编号 + 图例)
142
+ /星海点位 H83 → 这个区域的点位清单(编号/类型/物种数)
123
143
  /星海点位 迷途小屋之屿 → 点位详情(昆虫 12 / 树木 3 / 鲜花 3 / 创想卡 1 / 种子 1)
124
144
  /星海查 三色锦鲤 → 这个物种出现在哪几个点位
125
- /星海 河鱼 → 智能识别
126
145
  ```
127
146
 
128
147
  搜索支持物种名、大类(`鱼`→河鱼/海鱼,`生物`→昆虫/河鱼/海鱼/海岸生物,也认 `树`、`花`、`矿`、`特产`)和点位名。
@@ -172,7 +191,7 @@ nonebot_plugin_bugu/
172
191
  ├── config.py # 配置项
173
192
  ├── api.py # 四个接口的客户端 + 内存 / 磁盘缓存
174
193
  ├── values.py # 字段兜底与通用小工具
175
- ├── draw.py # 共用绘制:画布、字体、折行、胶囊、页脚
194
+ ├── draw.py # 共用绘制:画布、字体、折行、胶囊、页脚、分页卡片
176
195
  ├── fonts.py # 中文字体探测
177
196
  ├── runtime.py # 单例:客户端 / 字体 / 进度
178
197
  ├── commands.py # 命令注册与分发
@@ -91,11 +91,13 @@ def help_text() -> str:
91
91
  "",
92
92
  "【星海地图】",
93
93
  f" {start}星海 <词> 智能识别区域 / 点位 / 物种",
94
+ f" {start}星海 列表 [大类] [页码] 物种图鉴(出图)",
94
95
  f" {start}星海地图 [区域] 区域地图卡片",
95
- f" {start}星海点位 <点位名> 点位详情",
96
+ f" {start}星海点位 [区域 / 点位名] 区域点位清单 / 点位详情",
96
97
  f" {start}星海查 <物种 / 大类> 跨区域搜索",
97
98
  "",
98
99
  "【菜谱】",
100
+ f" {start}菜谱 列表 [类型 / 厨具 / 词] [页码] 翻目录(出图)",
99
101
  f" {start}菜谱 <菜名 / 编号> 菜谱卡片",
100
102
  f" {start}菜谱查 <食材 / 类型 / 厨具> 搜索筛选",
101
103
  f" {start}菜谱 随机 随手看一道",
@@ -353,6 +353,16 @@ def encode(image: Image.Image) -> bytes:
353
353
  return buffer.getvalue()
354
354
 
355
355
 
356
+ def flatten(content: Image.Image, width: int) -> bytes:
357
+ """把透明内容层贴到渐变底上并编码(目录类卡片没有额外页脚时用)。"""
358
+ bbox = content.getbbox()
359
+ height = bbox[3] if bbox else 1
360
+ height = max(1, min(height, content.height))
361
+ canvas = background(width, height)
362
+ canvas.alpha_composite(content.crop((0, 0, width, height)))
363
+ return encode(canvas)
364
+
365
+
356
366
  def finish(
357
367
  content: Image.Image,
358
368
  width: int,
@@ -3,8 +3,9 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import asyncio
6
+ import math
6
7
  from dataclasses import dataclass
7
- from typing import Callable, Optional, Union
8
+ from typing import Callable, List, Optional, Sequence, Tuple, Union
8
9
 
9
10
  import nonebot
10
11
  from nonebot.log import logger
@@ -67,3 +68,34 @@ async def render_card(
67
68
  except Exception:
68
69
  logger.opt(exception=True).error(f"{label}渲染异常")
69
70
  return None
71
+
72
+
73
+ # ------------------------------------------------------------------ 分页
74
+
75
+
76
+ def paginate(
77
+ items: Sequence,
78
+ page: int,
79
+ per_page: int = 15,
80
+ ) -> Tuple[List, int, int]:
81
+ """按页切片,返回 ``(当前页内容, 实际页码, 总页数)``。"""
82
+ total_pages = max(1, math.ceil(len(items) / max(1, per_page)))
83
+ page = min(max(1, int(page or 1)), total_pages)
84
+ start = (page - 1) * per_page
85
+ return list(items[start : start + per_page]), page, total_pages
86
+
87
+
88
+ def split_page(tokens: Sequence[str]) -> Tuple[List[str], int]:
89
+ """最后一个纯数字 token 当作页码:``["简单", "2"]`` → ``(["简单"], 2)``。"""
90
+ items = [token for token in tokens if token]
91
+ if items and items[-1].isdigit():
92
+ return items[:-1], int(items[-1])
93
+ return items, 1
94
+
95
+
96
+ def page_footer(current: int, total: int, command: str, spec: str = "") -> str:
97
+ """分页页脚:告诉用户怎么翻页 / 看详情。"""
98
+ suffix = f" {spec}" if spec else ""
99
+ if current < total:
100
+ return usage(f"{command}{suffix} {current + 1} 看下一页")
101
+ return usage(f"{command}{suffix} 1 回到第一页")
@@ -6,7 +6,8 @@
6
6
 
7
7
  from __future__ import annotations
8
8
 
9
- from typing import Dict, List, Optional, Tuple
9
+ import math
10
+ from typing import Dict, List, Optional, Sequence, Tuple
10
11
 
11
12
  from ...draw import (
12
13
  BG_BOTTOM,
@@ -23,12 +24,14 @@ from ...draw import (
23
24
  draw_paragraph,
24
25
  finish,
25
26
  fit_text,
27
+ flatten,
26
28
  icon_disc,
27
29
  open_image,
28
30
  require_pillow,
29
31
  resize,
30
32
  resolve_fontset,
31
33
  text_width,
34
+ wrap_text,
32
35
  )
33
36
  from ...fonts import FontSet
34
37
  from ...values import grade_text
@@ -353,3 +356,129 @@ def render_recipe_card(
353
356
 
354
357
  right = f"#{recipe.number_text}" + (f" · {recipe.type}" if recipe.type else "")
355
358
  return finish(content, width, fonts=font_set, pad=pad, right_text=right)
359
+
360
+
361
+ # ------------------------------------------------------------------ 目录卡片
362
+
363
+
364
+ def render_recipe_catalog(
365
+ book: RecipeBook,
366
+ recipes: Sequence[Recipe],
367
+ images: Dict[str, Optional[bytes]],
368
+ *,
369
+ page: int = 1,
370
+ total_pages: int = 1,
371
+ total_items: Optional[int] = None,
372
+ spec: str = "",
373
+ fonts: Optional[FontSet] = None,
374
+ width: int = 1200,
375
+ columns: int = 5,
376
+ ) -> bytes:
377
+ """菜谱目录:一页若干道菜,带菜图、编号、名称和类型。
378
+
379
+ 不知道菜名的人可以一页页翻,也可以按类型 / 厨具 / 关键词过滤后再翻。
380
+ """
381
+ require_pillow()
382
+ font_set = resolve_fontset(fonts)
383
+
384
+ from PIL import Image, ImageDraw
385
+
386
+ pad = 40
387
+ gap = 16
388
+ cell_w = (width - pad * 2 - gap * (columns - 1)) // columns
389
+ art = cell_w - 12
390
+ cell_h = art + 92
391
+ rows = max(1, math.ceil(max(1, len(recipes)) / columns))
392
+
393
+ estimate = pad + 76 + rows * (cell_h + 16) + 90
394
+ content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
395
+ draw = ImageDraw.Draw(content)
396
+
397
+ title_font = font_set.get(36, True)
398
+ title = "菜谱目录" + (f" · {spec}" if spec else "")
399
+ bold_text(draw, (pad, pad), fit_text(draw, title, title_font, width - pad * 2 - 260), title_font, font_set)
400
+ page_text = f"共 {total_items if total_items is not None else len(recipes)} 道 · 第 {page}/{total_pages} 页"
401
+ draw.text((width - pad, pad + 14), page_text, font=font_set.get(17), fill=DIM, anchor="rt")
402
+
403
+ top = pad + 66
404
+ for index, recipe in enumerate(recipes):
405
+ row, col = divmod(index, columns)
406
+ x = pad + col * (cell_w + gap)
407
+ y = top + row * (cell_h + 16)
408
+ _draw_catalog_cell(content, draw, recipe, images, x, y, cell_w, art, font_set)
409
+
410
+ footer_y = top + rows * (cell_h + 16) + 10
411
+ draw.line([(pad, footer_y), (width - pad, footer_y)], fill=LINE, width=1)
412
+ footer_hint = (
413
+ f"下一页:/菜谱 列表 {spec + ' ' if spec else ''}{page + 1}"
414
+ if page < total_pages
415
+ else "已到最后一页"
416
+ )
417
+ draw.text((pad, footer_y + 14), "数据来源:布谷工具箱 · bugu.nsun200512.cc", font=font_set.get(15), fill=FAINT)
418
+ draw.text((width - pad, footer_y + 14), footer_hint, font=font_set.get(15), fill=FAINT, anchor="rt")
419
+ return flatten(content, width)
420
+
421
+
422
+ def _draw_catalog_cell(
423
+ canvas,
424
+ draw: Draw,
425
+ recipe: Recipe,
426
+ images: Dict[str, Optional[bytes]],
427
+ x: int,
428
+ y: int,
429
+ cell_w: int,
430
+ art: int,
431
+ fonts: FontSet,
432
+ ) -> None:
433
+ quality = hex_to_rgb(recipe.panel_color, (156, 194, 194))
434
+ draw.rounded_rectangle(
435
+ [x, y, x + cell_w, y + art],
436
+ 14,
437
+ fill=mix(quality, BG_BOTTOM, 0.80),
438
+ outline=quality,
439
+ width=2,
440
+ )
441
+ dish = open_image(images.get(recipe.dish) if recipe.dish else None, "菜品图")
442
+ if dish is not None:
443
+ box = art - 26
444
+ scale = min(box / dish.width, box / dish.height)
445
+ dish = resize(dish, (max(1, round(dish.width * scale)), max(1, round(dish.height * scale))))
446
+ canvas.alpha_composite(dish, (x + (cell_w - dish.width) // 2, y + (art - dish.height) // 2))
447
+ else:
448
+ draw.text(
449
+ (x + cell_w / 2, y + art / 2),
450
+ "???" if recipe.incomplete else "菜",
451
+ font=fonts.get(30, True),
452
+ fill=DIM,
453
+ anchor="mm",
454
+ )
455
+
456
+ # 编号 + 品质角标
457
+ draw.rounded_rectangle([x + 8, y + 8, x + 62, y + 34], 13, fill=(18, 24, 50))
458
+ draw.text((x + 35, y + 21), f"#{recipe.number_text}", font=fonts.get(14), fill=CREAM, anchor="mm")
459
+ if recipe.quality:
460
+ color = hex_to_rgb(recipe.panel_color, (156, 194, 194))
461
+ draw.ellipse([x + cell_w - 34, y + 8, x + cell_w - 8, y + 34], fill=color)
462
+ draw.text(
463
+ (x + cell_w - 21, y + 21), recipe.quality, font=fonts.get(15, True), fill=(33, 32, 26), anchor="mm"
464
+ )
465
+
466
+ # 名称
467
+ name_font = fonts.get(18, True)
468
+ lines = wrap_text(draw, recipe.display_name, name_font, cell_w - 8, max_lines=2)
469
+ for i, line in enumerate(lines):
470
+ draw.text(
471
+ (x + cell_w / 2, y + art + 12 + i * 24), line, font=name_font, fill=CREAM, anchor="mt"
472
+ )
473
+
474
+ # 类型 / 厨具
475
+ meta = [recipe.type or "未分类"]
476
+ if recipe.kitchen is not None and recipe.kitchen.specified:
477
+ meta.append(recipe.kitchen.name)
478
+ draw.text(
479
+ (x + cell_w / 2, y + art + 14 + len(lines) * 24),
480
+ fit_text(draw, " · ".join(meta), fonts.get(14), cell_w - 8),
481
+ font=fonts.get(14),
482
+ fill=DIM,
483
+ anchor="mt",
484
+ )
@@ -3,12 +3,13 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import random
6
+ from difflib import get_close_matches
6
7
  from typing import Dict, List, Sequence, Set, Tuple
7
8
 
8
9
  from ...values import grade_text
9
- from ..common import Reply, ReplyEnv, render_card, usage
10
+ from ..common import Reply, ReplyEnv, page_footer, paginate, render_card, split_page, usage
10
11
  from .data import Recipe, RecipeBook
11
- from .render import render_recipe_card
12
+ from .render import render_recipe_card, render_recipe_catalog
12
13
  from .search import (
13
14
  FILTER_LABELS,
14
15
  RecipeHit,
@@ -32,10 +33,11 @@ def stats_text(book: RecipeBook) -> str:
32
33
  lines.append("按厨具:" + "、".join(f"{o.value} {o.count}" for o in kitchen_options))
33
34
  lines.append("")
34
35
  lines.append(usage(
36
+ "菜谱 列表 [类型 / 厨具 / 关键词] [页码] —— 翻目录,不知道菜名也能挑",
35
37
  "菜谱 <菜名 / 编号> —— 查看菜谱卡片",
36
38
  "菜谱查 <食材 / 菜名 / 类型 / 厨具> —— 搜索与筛选",
37
- "菜谱 随机 —— 随机看一道",
38
- "例:菜谱 炒鲜蔬 / 菜谱 12 / 菜谱查 苹果 / 菜谱查 煮锅 苹果",
39
+ "菜谱 随机 [条件] —— 随机看一道",
40
+ "例:菜谱 列表 简单 / 菜谱 炒鲜蔬 / 菜谱查 煮锅 苹果",
39
41
  ))
40
42
  return "\n".join(lines)
41
43
 
@@ -131,6 +133,78 @@ def list_text(
131
133
  return "\n".join(lines)
132
134
 
133
135
 
136
+ CATALOG_WORDS = ("列表", "目录", "浏览", "全部")
137
+ RANDOM_WORDS = ("随机", "随便", "来一个", "random")
138
+
139
+
140
+ def suggest_recipes(book: RecipeBook, text: str, limit: int = 5) -> List[str]:
141
+ """没找到时给几个相近的菜名。"""
142
+ names = [recipe.display_name for recipe in book.recipes]
143
+ return get_close_matches(text, names, n=limit, cutoff=0.4)
144
+
145
+
146
+ def catalog_text(book: RecipeBook, spec: str = "", page: int = 1, per_page: int = 15) -> str:
147
+ """分页目录:不知道菜名也能一页页翻,或按类型 / 厨具 / 关键词过滤。"""
148
+ spec = (spec or "").strip()
149
+ if spec:
150
+ hits, _ = search(book, spec)
151
+ pool = [hit.recipe for hit in hits]
152
+ title = f"菜谱列表 · {spec}"
153
+ else:
154
+ pool = list(book.recipes)
155
+ title = "菜谱列表"
156
+
157
+ if not pool:
158
+ suggestions = suggest_recipes(book, spec)
159
+ lines = [f"没找到和「{spec}」有关的菜谱。"]
160
+ if suggestions:
161
+ lines.append("是不是在找:" + "、".join(suggestions))
162
+ lines.append(usage("菜谱 列表 简单 看看有什么"))
163
+ return "\n".join(lines)
164
+
165
+ items, page, total = paginate(pool, page, per_page)
166
+ lines = [f"{title} · 共 {len(pool)} 道(第 {page}/{total} 页)", ""]
167
+ for recipe in items:
168
+ tags = [recipe.type or "未分类"]
169
+ if recipe.quality:
170
+ tags.append(recipe.quality)
171
+ if recipe.kitchen is not None and recipe.kitchen.specified:
172
+ tags.append(recipe.kitchen.name)
173
+ lines.append(f"#{recipe.number_text} {recipe.display_name} · " + " · ".join(tags))
174
+ lines.append("")
175
+ lines.append(page_footer(page, total, "菜谱 列表", spec))
176
+ return "\n".join(lines)
177
+
178
+
179
+ async def catalog_reply(book: RecipeBook, spec: str, page: int, env: ReplyEnv) -> Reply:
180
+ """菜谱目录的分页卡片(图片版,失败退回文字)。"""
181
+ spec = (spec or "").strip()
182
+ if spec:
183
+ hits, _ = search(book, spec)
184
+ pool = [hit.recipe for hit in hits]
185
+ else:
186
+ pool = list(book.recipes)
187
+ if not pool:
188
+ return catalog_text(book, spec, page)
189
+ items, page, total = paginate(pool, page, 15)
190
+ png = None
191
+ if env.can_render:
192
+ images = await env.client.get_images([recipe.dish for recipe in items if recipe.dish])
193
+ png = await render_card(
194
+ render_recipe_catalog,
195
+ book,
196
+ items,
197
+ images,
198
+ page=page,
199
+ total_pages=total,
200
+ total_items=len(pool),
201
+ spec=spec,
202
+ fonts=env.fonts,
203
+ label="菜谱目录卡片",
204
+ )
205
+ return png if png is not None else catalog_text(book, spec, page)
206
+
207
+
134
208
  def filter_text(book: RecipeBook, key: str, value: str, limit: int = 30) -> str:
135
209
  recipes = list_by_filter(book, key, value)
136
210
  label = FILTER_LABELS.get(key, key)
@@ -173,8 +247,19 @@ async def reply_recipes(arg: str, env: ReplyEnv) -> Reply:
173
247
  arg = (arg or "").strip()
174
248
  if not arg:
175
249
  return stats_text(book)
176
- if arg in {"随机", "随便", "来一个", "random"}:
177
- return await _recipe_reply(book, random.choice(book.recipes), env)
250
+
251
+ catalog_word = next((word for word in CATALOG_WORDS if arg.startswith(word)), "")
252
+ if catalog_word:
253
+ tokens, page = split_page(arg[len(catalog_word) :].split())
254
+ return await catalog_reply(book, " ".join(tokens), page, env)
255
+
256
+ random_word = next((word for word in RANDOM_WORDS if arg.startswith(word)), "")
257
+ if random_word:
258
+ spec = arg[len(random_word) :].strip()
259
+ pool = [hit.recipe for hit in search(book, spec)[0]] if spec else list(book.recipes)
260
+ if not pool:
261
+ return f"没找到符合「{spec}」的菜谱,换个条件试试。"
262
+ return await _recipe_reply(book, random.choice(pool), env)
178
263
 
179
264
  exact = book.find_exact(arg)
180
265
  if exact is not None:
@@ -189,6 +274,13 @@ async def reply_recipes(arg: str, env: ReplyEnv) -> Reply:
189
274
  hits, _ = search(book, arg)
190
275
  if len(hits) == 1:
191
276
  return await _recipe_reply(book, hits[0].recipe, env)
277
+ if not hits:
278
+ suggestions = suggest_recipes(book, arg)
279
+ lines = [f"没找到和「{arg}」有关的菜谱。"]
280
+ if suggestions:
281
+ lines.append("是不是在找:" + "、".join(suggestions))
282
+ lines.append(usage("菜谱 列表 简单 翻翻目录"))
283
+ return "\n".join(lines)
192
284
  return list_text(book, hits, arg, limit=env.config.bugu_search_limit)
193
285
 
194
286
 
@@ -206,8 +298,13 @@ async def reply_search(arg: str, env: ReplyEnv) -> Reply:
206
298
  hits, parsed = search(book, arg)
207
299
  if len(hits) == 1:
208
300
  return await _recipe_reply(book, hits[0].recipe, env)
209
- if not hits and parsed.is_empty:
210
- return f"没找到「{arg}」。试试菜名、编号、食材(苹果 / 海鲜),或者类型(简单 / 招牌 / 宴客 / 秘制)。"
301
+ if not hits:
302
+ suggestions = suggest_recipes(book, arg)
303
+ lines = [f"没找到和「{arg}」有关的菜谱。"]
304
+ if suggestions:
305
+ lines.append("是不是在找:" + "、".join(suggestions))
306
+ lines.append("可以按食材查(苹果 / 海鲜),或者按类型翻目录(菜谱 列表 简单)。")
307
+ return "\n".join(lines)
211
308
  return list_text(book, hits, arg, limit=env.config.bugu_search_limit)
212
309
 
213
310
 
@@ -14,6 +14,7 @@ from ...draw import (
14
14
  CREAM,
15
15
  DIM,
16
16
  FAINT,
17
+ GRADE_UNKNOWN,
17
18
  LINE,
18
19
  SPECIALTY_BLUE,
19
20
  TILE_BG,
@@ -26,6 +27,7 @@ from ...draw import (
26
27
  draw_paragraph,
27
28
  finish,
28
29
  fit_text,
30
+ flatten,
29
31
  grade_color,
30
32
  icon_disc,
31
33
  number_badge,
@@ -558,3 +560,276 @@ def _draw_fallback_tags(
558
560
  draw.text((x, y), "这个点位还没有填写采集物。", font=font_set.get(16), fill=DIM)
559
561
  cursor += 30
560
562
  return cursor
563
+
564
+
565
+ # ------------------------------------------------------------------ 图鉴 / 点位清单
566
+
567
+
568
+ def render_species_catalog(
569
+ data: StarSeaData,
570
+ species_list: Sequence[Species],
571
+ icon_bytes: Dict[int, Optional[bytes]],
572
+ *,
573
+ page: int = 1,
574
+ total_pages: int = 1,
575
+ total_items: Optional[int] = None,
576
+ spec: str = "",
577
+ fonts: Optional[FontSet] = None,
578
+ width: int = 1200,
579
+ columns: int = 8,
580
+ ) -> bytes:
581
+ """星海图鉴:一页若干物种,按品质配色,标出大类、特产与采集点数量。"""
582
+ require_pillow()
583
+ font_set = resolve_fontset(fonts)
584
+
585
+ from PIL import Image, ImageDraw
586
+
587
+ pad = 40
588
+ gap = 10
589
+ cell_w = (width - pad * 2 - gap * (columns - 1)) // columns
590
+ icon_r = 40
591
+ cell_h = 170
592
+ rows = max(1, math.ceil(max(1, len(species_list)) / columns))
593
+
594
+ estimate = pad + 76 + rows * (cell_h + 10) + 90
595
+ content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
596
+ draw = ImageDraw.Draw(content)
597
+
598
+ title_font = font_set.get(36, True)
599
+ title = "星海图鉴" + (f" · {spec}" if spec else "")
600
+ bold_text(draw, (pad, pad), fit_text(draw, title, title_font, width - pad * 2 - 280), title_font, font_set)
601
+ draw.text(
602
+ (width - pad, pad + 14),
603
+ f"共 {total_items if total_items is not None else len(species_list)} 种 · 第 {page}/{total_pages} 页",
604
+ font=font_set.get(17),
605
+ fill=DIM,
606
+ anchor="rt",
607
+ )
608
+
609
+ top = pad + 66
610
+ for index, species in enumerate(species_list):
611
+ row, col = divmod(index, columns)
612
+ cx = pad + col * (cell_w + gap) + cell_w // 2
613
+ cy = top + row * (cell_h + 10) + icon_r + 6
614
+ _draw_catalog_tile(
615
+ content, draw, data, species, icon_bytes.get(species.id), cx, cy, icon_r, cell_w, font_set
616
+ )
617
+
618
+ footer_y = top + rows * (cell_h + 10) + 8
619
+ draw.line([(pad, footer_y), (width - pad, footer_y)], fill=LINE, width=1)
620
+ footer_hint = (
621
+ f"下一页:/星海 列表 {spec + ' ' if spec else ''}{page + 1}"
622
+ if page < total_pages
623
+ else "已到最后一页"
624
+ )
625
+ draw.text((pad, footer_y + 14), "数据来源:布谷工具箱 · bugu.nsun200512.cc", font=font_set.get(15), fill=FAINT)
626
+ draw.text((width - pad, footer_y + 14), footer_hint, font=font_set.get(15), fill=FAINT, anchor="rt")
627
+ return flatten(content, width)
628
+
629
+
630
+ def _draw_catalog_tile(
631
+ canvas,
632
+ draw: Draw,
633
+ data: StarSeaData,
634
+ species: Species,
635
+ icon_bytes: Optional[bytes],
636
+ cx: float,
637
+ cy: float,
638
+ radius: int,
639
+ cell_w: int,
640
+ fonts: FontSet,
641
+ ) -> None:
642
+ color = grade_color(species.grade)
643
+ icon = open_image(icon_bytes, species.label or "物种")
644
+ icon_disc(
645
+ canvas,
646
+ draw,
647
+ icon,
648
+ cx,
649
+ cy,
650
+ radius,
651
+ ring=color,
652
+ fill=TILE_BG,
653
+ placeholder=species.grade or "?",
654
+ font=fonts.get(24, True),
655
+ placeholder_fill=color,
656
+ )
657
+ if species.grade:
658
+ bx, by = cx + radius * 0.66, cy + radius * 0.62
659
+ draw.ellipse([bx - 11, by - 11, bx + 11, by + 11], fill=color, outline=BG_BOTTOM, width=2)
660
+ draw.text((bx, by), species.grade, font=fonts.get(13, True), fill=TYPE_CHIP_INK, anchor="mm")
661
+ if species.islet_specialty:
662
+ sx, sy = cx - radius * 0.68, cy - radius * 0.52
663
+ draw.ellipse([sx - 10, sy - 10, sx + 10, sy + 10], fill=SPECIALTY_BLUE, outline=BG_BOTTOM, width=2)
664
+ star(draw, sx, sy, 6, (255, 255, 255))
665
+
666
+ name_font = fonts.get(14)
667
+ lines = wrap_text(draw, species.label, name_font, cell_w - 6, max_lines=2)
668
+ for i, line in enumerate(lines):
669
+ draw.text(
670
+ (cx, cy + radius + 8 + i * 18), line, font=name_font, fill=CREAM, anchor="mt"
671
+ )
672
+ places = len(data.points_with_species(species.id))
673
+ meta = f"{species.kind_name} · {places} 处"
674
+ draw.text(
675
+ (cx, cy + radius + 10 + len(lines) * 18),
676
+ fit_text(draw, meta, fonts.get(12), cell_w - 4),
677
+ font=fonts.get(12),
678
+ fill=DIM,
679
+ anchor="mt",
680
+ )
681
+
682
+
683
+ def render_points_card(
684
+ data: StarSeaData,
685
+ sea_map: SeaMap,
686
+ *,
687
+ fonts: Optional[FontSet] = None,
688
+ width: int = 1000,
689
+ ) -> bytes:
690
+ """一个区域的点位清单:编号、名字、类型、物种数和主要大类。"""
691
+ require_pillow()
692
+ font_set = resolve_fontset(fonts)
693
+
694
+ from PIL import Image, ImageDraw
695
+
696
+ pad = 36
697
+ points = sea_map.ordered_points()
698
+ row_h = 62
699
+ estimate = pad + 84 + max(1, len(points)) * row_h + 80
700
+ content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
701
+ draw = ImageDraw.Draw(content)
702
+
703
+ title_font = font_set.get(36, True)
704
+ bold_text(draw, (pad, pad), fit_text(draw, sea_map.display_name, title_font, width - pad * 2 - 200), title_font, font_set)
705
+ draw.text(
706
+ (width - pad, pad + 12),
707
+ f"{len(points)} 个点位 · {sea_map.code}",
708
+ font=font_set.get(17),
709
+ fill=DIM,
710
+ anchor="rt",
711
+ )
712
+
713
+ cursor = pad + 74
714
+ name_font = font_set.get(20, True)
715
+ meta_font = font_set.get(14)
716
+ for index, point in enumerate(points, start=1):
717
+ if index % 2 == 1:
718
+ draw.rounded_rectangle(
719
+ [pad - 8, cursor - 6, width - pad + 8, cursor + row_h - 14],
720
+ 10,
721
+ fill=(22, 30, 62),
722
+ )
723
+ number_badge(draw, pad + 14, cursor + 18, index, font_set.get(14, True), special=point.special, radius=14)
724
+ bold_text(draw, (pad + 40, cursor + 2), fit_text(draw, point.name, name_font, width - pad * 2 - 320), name_font, font_set)
725
+
726
+ species = data.species_of(point)
727
+ if species:
728
+ kinds: List[str] = []
729
+ for sp in species:
730
+ if sp.kind_name not in kinds:
731
+ kinds.append(sp.kind_name)
732
+ meta = f"{'/'.join(kinds[:3])} · {len(species)} 种"
733
+ else:
734
+ meta = point.type_name or "特殊点位"
735
+ draw.text((pad + 40, cursor + 28), fit_text(draw, meta, meta_font, width - pad * 2 - 240), font=meta_font, fill=DIM)
736
+
737
+ if point.type_name:
738
+ label_w = int(text_width(draw, point.type_name, meta_font)) + 22
739
+ draw.rounded_rectangle(
740
+ [width - pad - label_w, cursor + 4, width - pad, cursor + 30],
741
+ 13,
742
+ fill=(44, 58, 96),
743
+ )
744
+ draw.text(
745
+ (width - pad - label_w / 2, cursor + 17),
746
+ point.type_name,
747
+ font=meta_font,
748
+ fill=CREAM,
749
+ anchor="mm",
750
+ )
751
+ cursor += row_h
752
+
753
+ footer_y = cursor + 6
754
+ draw.line([(pad, footer_y), (width - pad, footer_y)], fill=LINE, width=1)
755
+ draw.text((pad, footer_y + 14), "数据来源:布谷工具箱 · bugu.nsun200512.cc", font=font_set.get(15), fill=FAINT)
756
+ draw.text(
757
+ (width - pad, footer_y + 14),
758
+ f"点位详情:/星海点位 {(next((pt for pt in points if pt.species_ids), points[0]).name if points else '')}",
759
+
760
+ font=font_set.get(15),
761
+ fill=FAINT,
762
+ anchor="rt",
763
+ )
764
+ return flatten(content, width)
765
+
766
+
767
+ def render_kind_index(
768
+ data: StarSeaData,
769
+ counts: Dict[str, int],
770
+ icon_bytes: Dict[int, Optional[bytes]],
771
+ *,
772
+ fonts: Optional[FontSet] = None,
773
+ width: int = 1000,
774
+ ) -> bytes:
775
+ """图鉴索引:列出所有大类、数量,以及每个大类的几个代表物种。"""
776
+ require_pillow()
777
+ font_set = resolve_fontset(fonts)
778
+
779
+ from PIL import Image, ImageDraw
780
+
781
+ pad = 36
782
+ row_h = 86
783
+ kinds = list(counts.items())
784
+ estimate = pad + 96 + max(1, len(kinds)) * row_h + 80
785
+ content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
786
+ draw = ImageDraw.Draw(content)
787
+
788
+ title_font = font_set.get(38, True)
789
+ bold_text(draw, (pad, pad), "星海图鉴", title_font, font_set)
790
+ draw.text(
791
+ (pad, pad + 56),
792
+ f"{len(kinds)} 个大类 · {sum(counts.values())} 种,发送「星海 列表 <大类>」翻具体物种",
793
+ font=font_set.get(16),
794
+ fill=DIM,
795
+ )
796
+
797
+ top = pad + 104
798
+ name_font = font_set.get(22, True)
799
+ count_font = font_set.get(15)
800
+ for index, (kind_name, count) in enumerate(kinds):
801
+ y = top + index * row_h
802
+ if index % 2 == 0:
803
+ draw.rounded_rectangle([pad - 8, y - 6, width - pad + 8, y + row_h - 14], 12, fill=(22, 30, 62))
804
+ bold_text(draw, (pad + 8, y + 8), kind_name, name_font, font_set)
805
+ draw.text(
806
+ (pad + 8 + text_width(draw, kind_name, name_font) + 12, y + 16),
807
+ f"{count} 种",
808
+ font=count_font,
809
+ fill=FAINT,
810
+ )
811
+
812
+ samples = [sp for sp in data.species.values() if (sp.kind_name or sp.kind) == kind_name]
813
+ samples.sort(key=lambda sp: (grade_color(sp.grade) == GRADE_UNKNOWN, sp.sort))
814
+ samples = samples[:4]
815
+ for i, species in enumerate(samples):
816
+ cx = width - pad - 34 - (len(samples) - 1 - i) * 56
817
+ icon = open_image(icon_bytes.get(species.id), species.label or "物种")
818
+ icon_disc(
819
+ content,
820
+ draw,
821
+ icon,
822
+ cx,
823
+ y + 36,
824
+ 24,
825
+ ring=grade_color(species.grade),
826
+ fill=TILE_BG,
827
+ placeholder=species.grade or "?",
828
+ font=font_set.get(14, True),
829
+ placeholder_fill=grade_color(species.grade),
830
+ )
831
+
832
+ footer_y = top + len(kinds) * row_h + 4
833
+ draw.line([(pad, footer_y), (width - pad, footer_y)], fill=LINE, width=1)
834
+ draw.text((pad, footer_y + 14), "数据来源:布谷工具箱 · bugu.nsun200512.cc", font=font_set.get(15), fill=FAINT)
835
+ return flatten(content, width)
@@ -2,13 +2,28 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import List, Sequence, Tuple
6
-
7
- from ...values import grade_text
8
- from ..common import Reply, ReplyEnv, command_start, render_card, usage
5
+ from typing import Dict, List, Sequence, Tuple
6
+
7
+ from ...values import grade_rank, grade_text
8
+ from ..common import (
9
+ Reply,
10
+ ReplyEnv,
11
+ command_start,
12
+ page_footer,
13
+ paginate,
14
+ render_card,
15
+ split_page,
16
+ usage,
17
+ )
9
18
  from .data import SeaMap, SeaPoint, Species, StarSeaData
10
- from .render import render_map_card, render_point_card
11
- from .search import PointHit, find_maps, find_points, resolve_query, search_points
19
+ from .render import (
20
+ render_kind_index,
21
+ render_map_card,
22
+ render_point_card,
23
+ render_points_card,
24
+ render_species_catalog,
25
+ )
26
+ from .search import PointHit, find_maps, find_points, match_kinds, resolve_query, search_points
12
27
 
13
28
  SPECIES_TAG_KINDS = ("S", "A", "B", "C")
14
29
 
@@ -19,8 +34,9 @@ SPECIES_TAG_KINDS = ("S", "A", "B", "C")
19
34
  def helper_text() -> str:
20
35
  return usage(
21
36
  "星海 [词] —— 智能识别区域 / 点位 / 物种",
37
+ "星海 列表 [大类] [页码] —— 按大类翻物种图鉴,不知道名字也能挑",
22
38
  "星海地图 [区域] —— 区域地图卡片",
23
- "星海点位 <点位名> —— 点位详情卡片",
39
+ "星海点位 [区域 / 点位名] —— 区域点位清单 / 点位详情卡片",
24
40
  "星海查 <物种 / 大类 / 点位名> —— 跨区域搜索",
25
41
  )
26
42
 
@@ -32,7 +48,10 @@ def map_list_text(data: StarSeaData) -> str:
32
48
  for sea_map in data.maps:
33
49
  lines.append(f"· {sea_map.display_name}({sea_map.code}){len(sea_map.points)} 个点位")
34
50
  lines.append("")
35
- lines.append(usage("星海地图 <区域代号> 查看地图,例如 星海地图 " + data.maps[0].code))
51
+ lines.append(usage(
52
+ "星海地图 " + data.maps[0].code + " 看地图卡片",
53
+ "星海点位 " + data.maps[0].code + " 看这个区域有哪些点位",
54
+ ))
36
55
  return "\n".join(lines)
37
56
 
38
57
 
@@ -122,6 +141,136 @@ def point_choices_text(points: Sequence[Tuple[SeaMap, SeaPoint]], query: str) ->
122
141
  return "\n".join(lines)
123
142
 
124
143
 
144
+ def _kind_counts(data: StarSeaData) -> List[Tuple[str, int]]:
145
+ counts: Dict[str, int] = {}
146
+ for species in data.species.values():
147
+ name = species.kind_name or species.kind
148
+ counts[name] = counts.get(name, 0) + 1
149
+ return list(counts.items())
150
+
151
+
152
+ def catalog_pool(data: StarSeaData, spec: str = "") -> List[Species]:
153
+ """按大类 / 别名 / 名称筛物种,统一排序(品质 → 大类 → 站内顺序)。"""
154
+ spec = (spec or "").strip()
155
+ if not spec:
156
+ pool = list(data.species.values())
157
+ else:
158
+ kinds = match_kinds(data, spec)
159
+ if kinds:
160
+ pool = [sp for sp in data.species.values() if sp.kind in kinds]
161
+ else:
162
+ pool = [sp for sp in data.species.values() if spec.casefold() in sp.label.casefold()]
163
+ pool.sort(key=lambda sp: (grade_rank(sp.grade), sp.kind, sp.sort))
164
+ return pool
165
+
166
+
167
+ def species_catalog_text(
168
+ data: StarSeaData, spec: str = "", page: int = 1, per_page: int = 20
169
+ ) -> str:
170
+ """物种图鉴的文字版(图片渲染失败时用)。"""
171
+ spec = (spec or "").strip()
172
+ if not spec:
173
+ kinds = _kind_counts(data)
174
+ lines = [f"星海图鉴 · {len(kinds)} 个大类 / {len(data.species)} 种", ""]
175
+ for name, count in kinds:
176
+ lines.append(f"· {name} {count} 种")
177
+ lines.append("")
178
+ lines.append(usage(f"星海 列表 {kinds[0][0]} 看具体物种"))
179
+ return "\n".join(lines)
180
+
181
+ pool = catalog_pool(data, spec)
182
+ if not pool:
183
+ lines = [f"没找到和「{spec}」有关的物种。"]
184
+ lines.append("大类有:" + "、".join(name for name, _ in _kind_counts(data)))
185
+ lines.append(usage(f"星海 列表 {_kind_counts(data)[0][0]} 看看"))
186
+ return "\n".join(lines)
187
+
188
+ title = f"星海图鉴 · {spec}"
189
+ items, page, total = paginate(pool, page, per_page)
190
+ lines = [f"{title} · 共 {len(pool)} 种(第 {page}/{total} 页)", ""]
191
+ for species in items:
192
+ places = len(data.points_with_species(species.id))
193
+ flags = [species.grade or "?", species.kind_name or species.kind]
194
+ if species.islet_specialty:
195
+ flags.append("特产")
196
+ flags.append(f"{places} 处")
197
+ lines.append(f"· {species.label}({' · '.join(flags)})")
198
+ lines.append("")
199
+ lines.append(page_footer(page, total, "星海 列表", spec))
200
+ return "\n".join(lines)
201
+
202
+
203
+ def points_text(data: StarSeaData, sea_map: SeaMap) -> str:
204
+ """一个区域的点位清单(不需要记点位名)。"""
205
+ lines = [f"【{sea_map.display_name}】{len(sea_map.points)} 个点位", ""]
206
+ for point in sea_map.ordered_points():
207
+ species = data.species_of(point)
208
+ meta = point.type_name or ""
209
+ if species:
210
+ meta = f"{meta} · {len(species)} 种" if meta else f"{len(species)} 种"
211
+ lines.append(f"· {point.name}" + (f"({meta})" if meta else ""))
212
+ lines.append("")
213
+ first = sea_map.ordered_points()[0].name if sea_map.points else ""
214
+ lines.append(usage(f"星海点位 {first} 看详情"))
215
+ return "\n".join(lines)
216
+
217
+
218
+ async def kind_index_reply(data: StarSeaData, env: ReplyEnv) -> Reply:
219
+ """图鉴首页:大类索引(图片版,失败退回文字)。"""
220
+ counts = dict(_kind_counts(data))
221
+ png = None
222
+ if env.can_render:
223
+ samples: Dict[int, Species] = {}
224
+ for kind_name in counts:
225
+ picked = [sp for sp in data.species.values() if (sp.kind_name or sp.kind) == kind_name][:4]
226
+ for sp in picked:
227
+ samples[sp.id] = sp
228
+ images = await env.client.get_images([sp.icon for sp in samples.values()])
229
+ png = await render_card(
230
+ render_kind_index,
231
+ data,
232
+ counts,
233
+ {sp.id: images.get(sp.icon) for sp in samples.values()},
234
+ fonts=env.fonts,
235
+ label="图鉴索引卡片",
236
+ )
237
+ return png if png is not None else species_catalog_text(data, "")
238
+
239
+
240
+ async def catalog_reply(data: StarSeaData, spec: str, page: int, env: ReplyEnv) -> Reply:
241
+ """图鉴分页卡片。"""
242
+ pool = catalog_pool(data, spec)
243
+ if not pool:
244
+ return species_catalog_text(data, spec, page)
245
+ items, page, total = paginate(pool, page, 32)
246
+ png = None
247
+ if env.can_render:
248
+ images = await env.client.get_images([sp.icon for sp in items if sp.has_icon])
249
+ png = await render_card(
250
+ render_species_catalog,
251
+ data,
252
+ items,
253
+ {sp.id: images.get(sp.icon) for sp in items},
254
+ page=page,
255
+ total_pages=total,
256
+ total_items=len(pool),
257
+ spec=spec,
258
+ fonts=env.fonts,
259
+ label="星海图鉴卡片",
260
+ )
261
+ return png if png is not None else species_catalog_text(data, spec, page)
262
+
263
+
264
+ async def points_reply(data: StarSeaData, sea_map: SeaMap, env: ReplyEnv) -> Reply:
265
+ """区域点位清单(图片版,失败退回文字)。"""
266
+ png = None
267
+ if env.can_render:
268
+ png = await render_card(
269
+ render_points_card, data, sea_map, fonts=env.fonts, label="点位清单卡片"
270
+ )
271
+ return png if png is not None else points_text(data, sea_map)
272
+
273
+
125
274
  def species_text(data: StarSeaData, species: Species, limit: int = 10) -> str:
126
275
  tags = [species.kind_name or species.kind, f"{grade_text(species.grade)}品质"]
127
276
  if species.islet_specialty:
@@ -171,7 +320,10 @@ def not_found_text(query: str, suggestions: Sequence[str]) -> str:
171
320
  lines = [f"没找到和「{query}」有关的区域 / 点位 / 物种。"]
172
321
  if suggestions:
173
322
  lines.append("是不是在找:" + "、".join(suggestions))
174
- lines.append(usage("星海地图 看看有哪些区域"))
323
+ lines.append(usage(
324
+ "星海 列表 按大类翻图鉴",
325
+ "星海点位 先看区域有哪些点位",
326
+ ))
175
327
  return "\n".join(lines)
176
328
 
177
329
 
@@ -231,20 +383,30 @@ async def _map_command(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
231
383
 
232
384
  async def _point_command(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
233
385
  if not arg:
234
- return usage(
235
- "星海点位 <点位名>",
236
- "例:星海点位 迷途小屋之屿",
237
- "不知道点位名?先发 星海地图 看看",
238
- )
386
+ return map_list_text(data)
239
387
 
240
388
  points = find_points(data, arg)
389
+ exact = [item for item in points if item[1].name == arg.strip()]
390
+ if len(exact) == 1:
391
+ return await _point_reply(data, exact[0][0], exact[0][1], env)
392
+
393
+ # 给的是区域代号 / 区域名 → 列这个区域的点位
394
+ maps = find_maps(data, arg)
395
+ if len(maps) == 1:
396
+ return await points_reply(data, maps[0], env)
397
+ if len(maps) > 1:
398
+ lines = [f"「{arg}」匹配到 {len(maps)} 个区域,想看哪个的点位?"]
399
+ for sea_map in maps:
400
+ lines.append(f"· {sea_map.display_name}({sea_map.code})")
401
+ lines.append(usage(f"星海点位 {maps[0].code}"))
402
+ return "\n".join(lines)
403
+
241
404
  if not points:
242
405
  suggestions = resolve_query(data, arg).suggestions
243
- return not_found_text(arg, suggestions)
406
+ lines = [not_found_text(arg, suggestions)]
407
+ lines.append(usage("星海点位 " + data.maps[0].code + " 先看区域有哪些点位"))
408
+ return "\n".join(lines)
244
409
  if len(points) > 1:
245
- exact = [item for item in points if item[1].name == arg.strip()]
246
- if len(exact) == 1:
247
- return await _point_reply(data, exact[0][0], exact[0][1], env)
248
410
  return point_choices_text(points, arg)
249
411
  return await _point_reply(data, points[0][0], points[0][1], env)
250
412
 
@@ -292,12 +454,20 @@ async def reply_map(arg: str, env: ReplyEnv) -> Reply:
292
454
 
293
455
 
294
456
  async def reply_point(arg: str, env: ReplyEnv) -> Reply:
295
- if not arg:
296
- return await _point_command(None, arg, env) # type: ignore[arg-type]
297
457
  data = await env.client.get_starsea()
298
458
  return await _point_command(data, arg, env)
299
459
 
300
460
 
461
+ async def reply_catalog(arg: str, env: ReplyEnv) -> Reply:
462
+ """``星海 列表``:按大类翻物种图鉴。"""
463
+ data = await env.client.get_starsea()
464
+ tokens, page = split_page((arg or "").split())
465
+ spec = " ".join(tokens)
466
+ if not spec:
467
+ return await kind_index_reply(data, env)
468
+ return await catalog_reply(data, spec, page, env)
469
+
470
+
301
471
  async def reply_search(arg: str, env: ReplyEnv) -> Reply:
302
472
  if not arg:
303
473
  return await _search_command(None, arg, env) # type: ignore[arg-type]
@@ -319,6 +489,8 @@ async def reply_smart(arg: str, env: ReplyEnv) -> Reply:
319
489
  ("点位", reply_point),
320
490
  ("查", reply_search),
321
491
  ("搜索", reply_search),
492
+ ("图鉴", reply_catalog),
493
+ ("列表", reply_catalog),
322
494
  ):
323
495
  if arg.startswith(keyword):
324
496
  return await handler(arg[len(keyword) :].strip(), env)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-bugu"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  description = "布谷工具箱(星布谷地小助手):菜谱、星海地图、友邻备忘与鲜花培育"
5
5
  authors = [{ name = "TonyLiangP2010405" }]
6
6
  license = "MIT"