nonebot-plugin-bugu 0.2.0__py3-none-any.whl
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.
- nonebot_plugin_bugu/__init__.py +31 -0
- nonebot_plugin_bugu/api.py +282 -0
- nonebot_plugin_bugu/collection.py +166 -0
- nonebot_plugin_bugu/commands.py +234 -0
- nonebot_plugin_bugu/config.py +91 -0
- nonebot_plugin_bugu/draw.py +406 -0
- nonebot_plugin_bugu/features/__init__.py +0 -0
- nonebot_plugin_bugu/features/collection.py +247 -0
- nonebot_plugin_bugu/features/common.py +100 -0
- nonebot_plugin_bugu/features/flowers/__init__.py +0 -0
- nonebot_plugin_bugu/features/flowers/data.py +167 -0
- nonebot_plugin_bugu/features/flowers/render.py +300 -0
- nonebot_plugin_bugu/features/flowers/reply.py +123 -0
- nonebot_plugin_bugu/features/neighbors/__init__.py +0 -0
- nonebot_plugin_bugu/features/neighbors/data.py +143 -0
- nonebot_plugin_bugu/features/neighbors/render.py +225 -0
- nonebot_plugin_bugu/features/neighbors/reply.py +149 -0
- nonebot_plugin_bugu/features/recipes/__init__.py +0 -0
- nonebot_plugin_bugu/features/recipes/data.py +313 -0
- nonebot_plugin_bugu/features/recipes/render.py +371 -0
- nonebot_plugin_bugu/features/recipes/reply.py +219 -0
- nonebot_plugin_bugu/features/recipes/search.py +151 -0
- nonebot_plugin_bugu/features/starsea/__init__.py +0 -0
- nonebot_plugin_bugu/features/starsea/data.py +267 -0
- nonebot_plugin_bugu/features/starsea/render.py +579 -0
- nonebot_plugin_bugu/features/starsea/reply.py +356 -0
- nonebot_plugin_bugu/features/starsea/search.py +271 -0
- nonebot_plugin_bugu/fonts.py +152 -0
- nonebot_plugin_bugu/runtime.py +109 -0
- nonebot_plugin_bugu/values.py +87 -0
- nonebot_plugin_bugu-0.2.0.dist-info/METADATA +257 -0
- nonebot_plugin_bugu-0.2.0.dist-info/RECORD +35 -0
- nonebot_plugin_bugu-0.2.0.dist-info/WHEEL +4 -0
- nonebot_plugin_bugu-0.2.0.dist-info/entry_points.txt +3 -0
- nonebot_plugin_bugu-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""各功能模块共用的小东西:回复类型、运行环境、命令前缀、渲染包装。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Callable, Optional, Set, Union
|
|
8
|
+
|
|
9
|
+
import nonebot
|
|
10
|
+
from nonebot.log import logger
|
|
11
|
+
|
|
12
|
+
from ..api import BuguClient
|
|
13
|
+
from ..collection import KINDS, CollectionStore
|
|
14
|
+
from ..config import Config
|
|
15
|
+
from ..draw import DrawError
|
|
16
|
+
from ..fonts import FontError, FontSet
|
|
17
|
+
|
|
18
|
+
Reply = Union[str, bytes]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class ReplyEnv:
|
|
23
|
+
"""一次回复要用到的东西。"""
|
|
24
|
+
|
|
25
|
+
client: BuguClient
|
|
26
|
+
config: Config
|
|
27
|
+
fonts: Optional[FontSet]
|
|
28
|
+
user_id: str = ""
|
|
29
|
+
store: Optional[CollectionStore] = None
|
|
30
|
+
|
|
31
|
+
def collected(self, kind: str) -> Set[str]:
|
|
32
|
+
"""当前用户某一类的收集集合(没有登录态 / 没存过就是空集)。"""
|
|
33
|
+
if self.store is None or not self.user_id:
|
|
34
|
+
return set()
|
|
35
|
+
return self.store.items(self.user_id, kind)
|
|
36
|
+
|
|
37
|
+
def collected_ids(self, kind: str) -> Set[int]:
|
|
38
|
+
"""星海物种 / 菜谱的 key 是数字,这里直接转好。"""
|
|
39
|
+
out: Set[int] = set()
|
|
40
|
+
for value in self.collected(kind):
|
|
41
|
+
try:
|
|
42
|
+
out.add(int(value))
|
|
43
|
+
except (TypeError, ValueError):
|
|
44
|
+
continue
|
|
45
|
+
return out
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def collected_totals(self) -> dict:
|
|
49
|
+
"""各类进度条数(用于「收藏」概览)。"""
|
|
50
|
+
if self.store is None or not self.user_id:
|
|
51
|
+
return {}
|
|
52
|
+
return {kind: len(self.store.items(self.user_id, kind)) for kind in KINDS}
|
|
53
|
+
|
|
54
|
+
def is_collected(self, kind: str, key) -> bool:
|
|
55
|
+
if self.store is None or not self.user_id:
|
|
56
|
+
return False
|
|
57
|
+
return self.store.has(self.user_id, kind, key)
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def can_render(self) -> bool:
|
|
61
|
+
return bool(self.config.bugu_image_enabled and self.fonts is not None)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def command_start() -> str:
|
|
65
|
+
"""当前 bot 的命令前缀,写进帮助里。"""
|
|
66
|
+
try:
|
|
67
|
+
starts = nonebot.get_driver().config.command_start
|
|
68
|
+
except Exception: # 没初始化 driver 时(单测)
|
|
69
|
+
return "/"
|
|
70
|
+
if not starts:
|
|
71
|
+
return ""
|
|
72
|
+
if "" in starts:
|
|
73
|
+
return ""
|
|
74
|
+
return sorted(starts)[0]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def usage(*lines: str) -> str:
|
|
78
|
+
"""把若干行用法拼成一段(自动补命令前缀,空行原样保留)。"""
|
|
79
|
+
start = command_start()
|
|
80
|
+
out = []
|
|
81
|
+
for line in lines:
|
|
82
|
+
out.append(f"{start}{line}" if line and not line.startswith(" ") else line)
|
|
83
|
+
return "\n".join(out)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
async def render_card(
|
|
87
|
+
func: Callable[..., bytes],
|
|
88
|
+
*args,
|
|
89
|
+
label: str = "卡片",
|
|
90
|
+
**kwargs,
|
|
91
|
+
) -> Optional[bytes]:
|
|
92
|
+
"""在线程里跑 Pillow 渲染;失败只记日志,返回 ``None`` 让调用方降级。"""
|
|
93
|
+
try:
|
|
94
|
+
return await asyncio.to_thread(func, *args, **kwargs)
|
|
95
|
+
except (DrawError, FontError) as exc:
|
|
96
|
+
logger.warning(f"{label}渲染失败:{exc}")
|
|
97
|
+
return None
|
|
98
|
+
except Exception:
|
|
99
|
+
logger.opt(exception=True).error(f"{label}渲染异常")
|
|
100
|
+
return None
|
|
File without changes
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""鲜花培育(``/api/flowers``)的数据模型。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Any, Dict, List, Optional
|
|
7
|
+
|
|
8
|
+
from ...values import absolute_url, as_int, as_str, clean_text
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class Flower:
|
|
13
|
+
id: int
|
|
14
|
+
name: str
|
|
15
|
+
species: str
|
|
16
|
+
color: str
|
|
17
|
+
grade: str
|
|
18
|
+
area: str
|
|
19
|
+
sort: int
|
|
20
|
+
icon: str
|
|
21
|
+
has_icon: bool
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def colors(self) -> List[str]:
|
|
25
|
+
"""颜色字段可能是「白色,粉色」这种多色写法。"""
|
|
26
|
+
return [part.strip() for part in self.color.replace(",", ",").split(",") if part.strip()]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class FlowerRecipe:
|
|
31
|
+
id: int
|
|
32
|
+
species: str
|
|
33
|
+
parent1: str
|
|
34
|
+
parent2: str
|
|
35
|
+
result: str
|
|
36
|
+
grade: str
|
|
37
|
+
sort: int
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def is_self_cross(self) -> bool:
|
|
41
|
+
return self.parent1 == self.parent2
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class FlowerSpecies:
|
|
46
|
+
name: str
|
|
47
|
+
count: int
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class FlowerBook:
|
|
52
|
+
species: List[FlowerSpecies]
|
|
53
|
+
flowers: List[Flower]
|
|
54
|
+
recipes: List[FlowerRecipe]
|
|
55
|
+
grades: List[str] = field(default_factory=list)
|
|
56
|
+
|
|
57
|
+
# ------------------------------------------------------------ 查询
|
|
58
|
+
|
|
59
|
+
def flowers_of(self, species: str) -> List[Flower]:
|
|
60
|
+
return sorted(
|
|
61
|
+
(f for f in self.flowers if f.species == species),
|
|
62
|
+
key=lambda f: (f.sort, f.name),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def recipes_of(self, species: str) -> List[FlowerRecipe]:
|
|
66
|
+
return sorted(
|
|
67
|
+
(r for r in self.recipes if r.species == species),
|
|
68
|
+
key=lambda r: (r.sort, r.id),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def find_flower(self, name: str) -> Optional[Flower]:
|
|
72
|
+
for flower in self.flowers:
|
|
73
|
+
if flower.name == name:
|
|
74
|
+
return flower
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
def species_of_flower(self, name: str) -> Optional[str]:
|
|
78
|
+
flower = self.find_flower(name)
|
|
79
|
+
return flower.species if flower else None
|
|
80
|
+
|
|
81
|
+
def find_species(self, query: str) -> Optional[str]:
|
|
82
|
+
"""按品种名找;也允许直接给花名(自动归到它所属的品种)。"""
|
|
83
|
+
text = (query or "").strip()
|
|
84
|
+
if not text:
|
|
85
|
+
return None
|
|
86
|
+
for item in self.species:
|
|
87
|
+
if item.name == text:
|
|
88
|
+
return item.name
|
|
89
|
+
for item in self.species:
|
|
90
|
+
if text in item.name:
|
|
91
|
+
return item.name
|
|
92
|
+
return self.species_of_flower(text)
|
|
93
|
+
|
|
94
|
+
def parent_recipe(self, name: str) -> Optional[FlowerRecipe]:
|
|
95
|
+
for recipe in self.recipes:
|
|
96
|
+
if recipe.result == name:
|
|
97
|
+
return recipe
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
def child_recipes(self, name: str) -> List[FlowerRecipe]:
|
|
101
|
+
return [r for r in self.recipes if name in (r.parent1, r.parent2)]
|
|
102
|
+
|
|
103
|
+
def area_of(self, species: str) -> str:
|
|
104
|
+
"""品种产地:花表里的 area(如「星海花种·禾夏H83区」)。"""
|
|
105
|
+
for flower in self.flowers_of(species):
|
|
106
|
+
if flower.area:
|
|
107
|
+
return flower.area
|
|
108
|
+
return ""
|
|
109
|
+
|
|
110
|
+
def is_star_seed(self, species: str) -> bool:
|
|
111
|
+
return self.area_of(species).startswith("星海花种")
|
|
112
|
+
|
|
113
|
+
def collected_names(self, species: str) -> List[str]:
|
|
114
|
+
return [f.name for f in self.flowers_of(species)]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def parse_flowers(payload: Dict[str, Any], base: str) -> FlowerBook:
|
|
118
|
+
if not isinstance(payload, dict):
|
|
119
|
+
raise ValueError("接口返回的不是 JSON 对象")
|
|
120
|
+
if payload.get("ready") is False:
|
|
121
|
+
raise ValueError("站点上的鲜花数据还没建好")
|
|
122
|
+
raw_flowers = payload.get("flowers")
|
|
123
|
+
if not isinstance(raw_flowers, list) or not raw_flowers:
|
|
124
|
+
raise ValueError("接口没有返回任何鲜花")
|
|
125
|
+
|
|
126
|
+
species = [
|
|
127
|
+
FlowerSpecies(name=clean_text(item.get("name")), count=as_int(item.get("count")))
|
|
128
|
+
for item in payload.get("species") or []
|
|
129
|
+
if isinstance(item, dict) and clean_text(item.get("name"))
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
flowers = [
|
|
133
|
+
Flower(
|
|
134
|
+
id=as_int(item.get("id")),
|
|
135
|
+
name=clean_text(item.get("name")),
|
|
136
|
+
species=clean_text(item.get("species")),
|
|
137
|
+
color=clean_text(item.get("color")),
|
|
138
|
+
grade=as_str(item.get("grade")).upper(),
|
|
139
|
+
area=clean_text(item.get("area")),
|
|
140
|
+
sort=as_int(item.get("sort")),
|
|
141
|
+
icon=absolute_url(base, as_str(item.get("icon"))),
|
|
142
|
+
has_icon=bool(item.get("hasIcon", True)),
|
|
143
|
+
)
|
|
144
|
+
for item in raw_flowers
|
|
145
|
+
if isinstance(item, dict) and clean_text(item.get("name"))
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
recipes = [
|
|
149
|
+
FlowerRecipe(
|
|
150
|
+
id=as_int(item.get("id")),
|
|
151
|
+
species=clean_text(item.get("species")),
|
|
152
|
+
parent1=clean_text(item.get("parent1")),
|
|
153
|
+
parent2=clean_text(item.get("parent2")),
|
|
154
|
+
result=clean_text(item.get("result")),
|
|
155
|
+
grade=as_str(item.get("grade")).upper(),
|
|
156
|
+
sort=as_int(item.get("sort")),
|
|
157
|
+
)
|
|
158
|
+
for item in payload.get("recipes") or []
|
|
159
|
+
if isinstance(item, dict) and clean_text(item.get("result"))
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
return FlowerBook(
|
|
163
|
+
species=species,
|
|
164
|
+
flowers=flowers,
|
|
165
|
+
recipes=recipes,
|
|
166
|
+
grades=[as_str(g) for g in payload.get("grades") or []],
|
|
167
|
+
)
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"""鲜花培育卡:一个品种的杂交配方表 + 全部花色图鉴。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from typing import Dict, Optional
|
|
7
|
+
|
|
8
|
+
from ...draw import (
|
|
9
|
+
CREAM,
|
|
10
|
+
DIM,
|
|
11
|
+
FAINT,
|
|
12
|
+
TILE_BG,
|
|
13
|
+
TYPE_CHIP_INK,
|
|
14
|
+
YELLOW,
|
|
15
|
+
Draw,
|
|
16
|
+
bold_text,
|
|
17
|
+
check_mark,
|
|
18
|
+
chip,
|
|
19
|
+
finish,
|
|
20
|
+
fit_text,
|
|
21
|
+
grade_color,
|
|
22
|
+
icon_disc,
|
|
23
|
+
open_image,
|
|
24
|
+
require_pillow,
|
|
25
|
+
resolve_fontset,
|
|
26
|
+
text_width,
|
|
27
|
+
wrap_text,
|
|
28
|
+
)
|
|
29
|
+
from ...fonts import FontSet
|
|
30
|
+
from .data import Flower, FlowerBook, FlowerRecipe
|
|
31
|
+
|
|
32
|
+
# 花色 → 展示色(站点 flowers.js 的 COLOR_HEX)
|
|
33
|
+
COLOR_HEX: Dict[str, str] = {
|
|
34
|
+
"白色": "#FFFBD9",
|
|
35
|
+
"黑色": "#212016",
|
|
36
|
+
"红色": "#E94F4F",
|
|
37
|
+
"橙色": "#EC6941",
|
|
38
|
+
"黄色": "#EFC805",
|
|
39
|
+
"绿色": "#60A66A",
|
|
40
|
+
"青色": "#91C1E4",
|
|
41
|
+
"蓝色": "#0069B7",
|
|
42
|
+
"紫色": "#8957A1",
|
|
43
|
+
"粉色": "#F1B5CB",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _hex_rgb(value: str, default=(200, 200, 200)):
|
|
48
|
+
text = (value or "").lstrip("#")
|
|
49
|
+
if len(text) != 6:
|
|
50
|
+
return default
|
|
51
|
+
try:
|
|
52
|
+
return tuple(int(text[i : i + 2], 16) for i in (0, 2, 4))
|
|
53
|
+
except ValueError:
|
|
54
|
+
return default
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _is_light(color) -> bool:
|
|
58
|
+
r, g, b = (c / 255 for c in color)
|
|
59
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b > 0.62
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _draw_flower_node(
|
|
63
|
+
canvas,
|
|
64
|
+
draw: Draw,
|
|
65
|
+
name: str,
|
|
66
|
+
grade: str,
|
|
67
|
+
icons: Dict[str, Optional[bytes]],
|
|
68
|
+
cx: float,
|
|
69
|
+
cy: float,
|
|
70
|
+
radius: int,
|
|
71
|
+
fonts: FontSet,
|
|
72
|
+
name_width: int,
|
|
73
|
+
) -> None:
|
|
74
|
+
icon = open_image(icons.get(name), "鲜花图标")
|
|
75
|
+
icon_disc(
|
|
76
|
+
canvas,
|
|
77
|
+
draw,
|
|
78
|
+
icon,
|
|
79
|
+
cx,
|
|
80
|
+
cy,
|
|
81
|
+
radius,
|
|
82
|
+
ring=grade_color(grade),
|
|
83
|
+
fill=TILE_BG,
|
|
84
|
+
placeholder="?",
|
|
85
|
+
font=fonts.get(18, True),
|
|
86
|
+
placeholder_fill=DIM,
|
|
87
|
+
)
|
|
88
|
+
if grade:
|
|
89
|
+
bx, by = cx + radius * 0.66, cy + radius * 0.60
|
|
90
|
+
draw.ellipse(
|
|
91
|
+
[bx - 11, by - 11, bx + 11, by + 11],
|
|
92
|
+
fill=grade_color(grade),
|
|
93
|
+
outline=(11, 16, 36),
|
|
94
|
+
width=2,
|
|
95
|
+
)
|
|
96
|
+
draw.text((bx, by), grade, font=fonts.get(13, True), fill=TYPE_CHIP_INK, anchor="mm")
|
|
97
|
+
|
|
98
|
+
font = fonts.get(15)
|
|
99
|
+
for i, line in enumerate(wrap_text(draw, name, font, name_width, max_lines=1)):
|
|
100
|
+
draw.text((cx, cy + radius + 10 + i * 19), line, font=font, fill=CREAM, anchor="mt")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _draw_recipe_row(
|
|
104
|
+
canvas,
|
|
105
|
+
draw: Draw,
|
|
106
|
+
recipe: FlowerRecipe,
|
|
107
|
+
icons: Dict[str, Optional[bytes]],
|
|
108
|
+
grades: Dict[str, str],
|
|
109
|
+
x: int,
|
|
110
|
+
y: int,
|
|
111
|
+
width: int,
|
|
112
|
+
fonts: FontSet,
|
|
113
|
+
) -> int:
|
|
114
|
+
"""一行配方:父本1 × 父本2 → 产物。"""
|
|
115
|
+
slot_w = (width - 130) // 3
|
|
116
|
+
radius = 30
|
|
117
|
+
c1 = x + slot_w // 2
|
|
118
|
+
c2 = c1 + slot_w + 65
|
|
119
|
+
c3 = c2 + slot_w + 65
|
|
120
|
+
cy = y + radius + 4
|
|
121
|
+
|
|
122
|
+
_draw_flower_node(canvas, draw, recipe.parent1, grades.get(recipe.parent1, ""), icons, c1, cy, radius, fonts, slot_w - 24)
|
|
123
|
+
_draw_flower_node(canvas, draw, recipe.parent2, grades.get(recipe.parent2, ""), icons, c2, cy, radius, fonts, slot_w - 24)
|
|
124
|
+
draw.text(((c1 + c2) / 2, cy), "×", font=fonts.get(22, True), fill=FAINT, anchor="mm")
|
|
125
|
+
draw.text(((c2 + c3) / 2, cy), "→", font=fonts.get(22, True), fill=YELLOW, anchor="mm")
|
|
126
|
+
_draw_flower_node(canvas, draw, recipe.result, recipe.grade, icons, c3, cy, radius, fonts, slot_w - 24)
|
|
127
|
+
return 106
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _draw_tile(
|
|
131
|
+
canvas,
|
|
132
|
+
draw: Draw,
|
|
133
|
+
flower: Flower,
|
|
134
|
+
icon_bytes: Optional[bytes],
|
|
135
|
+
cx: float,
|
|
136
|
+
cy: float,
|
|
137
|
+
radius: int,
|
|
138
|
+
fonts: FontSet,
|
|
139
|
+
*,
|
|
140
|
+
done: bool = False,
|
|
141
|
+
) -> None:
|
|
142
|
+
icon = open_image(icon_bytes, "鲜花图标")
|
|
143
|
+
icon_disc(
|
|
144
|
+
canvas,
|
|
145
|
+
draw,
|
|
146
|
+
icon,
|
|
147
|
+
cx,
|
|
148
|
+
cy,
|
|
149
|
+
radius,
|
|
150
|
+
ring=grade_color(flower.grade),
|
|
151
|
+
fill=TILE_BG,
|
|
152
|
+
placeholder=flower.grade or "?",
|
|
153
|
+
font=fonts.get(24, True),
|
|
154
|
+
placeholder_fill=grade_color(flower.grade),
|
|
155
|
+
)
|
|
156
|
+
if flower.grade:
|
|
157
|
+
bx, by = cx + radius * 0.64, cy + radius * 0.58
|
|
158
|
+
draw.ellipse(
|
|
159
|
+
[bx - 11, by - 11, bx + 11, by + 11],
|
|
160
|
+
fill=grade_color(flower.grade),
|
|
161
|
+
outline=(11, 16, 36),
|
|
162
|
+
width=2,
|
|
163
|
+
)
|
|
164
|
+
draw.text((bx, by), flower.grade, font=fonts.get(13, True), fill=TYPE_CHIP_INK, anchor="mm")
|
|
165
|
+
|
|
166
|
+
if done:
|
|
167
|
+
check_mark(draw, cx + radius * 0.72, cy - radius * 0.72, 11)
|
|
168
|
+
|
|
169
|
+
font = fonts.get(15)
|
|
170
|
+
for i, line in enumerate(wrap_text(draw, flower.name, font, radius * 3, max_lines=1)):
|
|
171
|
+
draw.text((cx, cy + radius + 8 + i * 19), line, font=font, fill=CREAM, anchor="mt")
|
|
172
|
+
|
|
173
|
+
if flower.color:
|
|
174
|
+
color_name = flower.colors[0] if flower.colors else flower.color
|
|
175
|
+
rgb = _hex_rgb(COLOR_HEX.get(color_name, ""), (158, 154, 139))
|
|
176
|
+
text = fit_text(draw, flower.color, fonts.get(13), radius * 3)
|
|
177
|
+
width = int(text_width(draw, text, fonts.get(13))) + 14
|
|
178
|
+
top = cy + radius + 30
|
|
179
|
+
draw.rounded_rectangle([cx - width / 2, top, cx + width / 2, top + 20], 10, fill=rgb)
|
|
180
|
+
draw.text(
|
|
181
|
+
(cx, top + 10),
|
|
182
|
+
text,
|
|
183
|
+
font=fonts.get(13),
|
|
184
|
+
fill=(33, 32, 22) if _is_light(rgb) else (255, 255, 255),
|
|
185
|
+
anchor="mm",
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def render_flower_card(
|
|
190
|
+
book: FlowerBook,
|
|
191
|
+
species: str,
|
|
192
|
+
images: Dict[str, Optional[bytes]],
|
|
193
|
+
*,
|
|
194
|
+
fonts: Optional[FontSet] = None,
|
|
195
|
+
width: int = 1040,
|
|
196
|
+
collected: Optional[set] = None,
|
|
197
|
+
) -> bytes:
|
|
198
|
+
require_pillow()
|
|
199
|
+
font_set = resolve_fontset(fonts)
|
|
200
|
+
|
|
201
|
+
from PIL import Image, ImageDraw
|
|
202
|
+
|
|
203
|
+
pad = 36
|
|
204
|
+
flowers = book.flowers_of(species)
|
|
205
|
+
recipes = book.recipes_of(species)
|
|
206
|
+
area = book.area_of(species)
|
|
207
|
+
columns = 6
|
|
208
|
+
tile_w = (width - pad * 2) // columns
|
|
209
|
+
tile_h = 138
|
|
210
|
+
|
|
211
|
+
estimate = (
|
|
212
|
+
pad
|
|
213
|
+
+ 90
|
|
214
|
+
+ 40
|
|
215
|
+
+ len(recipes) * 106
|
|
216
|
+
+ 80
|
|
217
|
+
+ math.ceil(max(1, len(flowers)) / columns) * tile_h
|
|
218
|
+
+ 220
|
|
219
|
+
)
|
|
220
|
+
content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
|
|
221
|
+
draw = ImageDraw.Draw(content)
|
|
222
|
+
|
|
223
|
+
# ---- 标题
|
|
224
|
+
title_font = font_set.get(40, True)
|
|
225
|
+
bold_text(draw, (pad, pad), species, title_font, font_set)
|
|
226
|
+
chip(
|
|
227
|
+
draw,
|
|
228
|
+
pad + text_width(draw, species, title_font) + 16,
|
|
229
|
+
pad + 8,
|
|
230
|
+
f"{len(flowers)} 种花色",
|
|
231
|
+
font_set.get(16, True),
|
|
232
|
+
)
|
|
233
|
+
cursor = pad + 58
|
|
234
|
+
meta = f"{len(recipes)} 条培育配方"
|
|
235
|
+
if area:
|
|
236
|
+
meta += f" · 产地 {area}"
|
|
237
|
+
draw.text((pad, cursor), meta, font=font_set.get(17), fill=DIM)
|
|
238
|
+
cursor += 30
|
|
239
|
+
|
|
240
|
+
if book.is_star_seed(species):
|
|
241
|
+
note = "该类型花种的基础花需通过星海探索获得其花株/花种"
|
|
242
|
+
else:
|
|
243
|
+
note = "C 等级的基础花可以野外采集,其余花色靠配方培育"
|
|
244
|
+
draw.text((pad, cursor), note, font=font_set.get(15), fill=FAINT)
|
|
245
|
+
cursor += 36
|
|
246
|
+
|
|
247
|
+
# ---- 配方
|
|
248
|
+
if recipes:
|
|
249
|
+
bold_text(draw, (pad, cursor), "培育配方", font_set.get(20, True), font_set, fill=YELLOW)
|
|
250
|
+
draw.text(
|
|
251
|
+
(pad + text_width(draw, "培育配方", font_set.get(20, True)) + 10, cursor + 6),
|
|
252
|
+
str(len(recipes)),
|
|
253
|
+
font=font_set.get(14),
|
|
254
|
+
fill=FAINT,
|
|
255
|
+
)
|
|
256
|
+
cursor += 36
|
|
257
|
+
grades = {flower.name: flower.grade for flower in book.flowers}
|
|
258
|
+
for recipe in recipes:
|
|
259
|
+
_draw_recipe_row(
|
|
260
|
+
content, draw, recipe, images, grades, pad, cursor, width - pad * 2, font_set
|
|
261
|
+
)
|
|
262
|
+
cursor += 106
|
|
263
|
+
cursor += 18
|
|
264
|
+
else:
|
|
265
|
+
draw.text((pad, cursor), "这个品种暂时没有培育配方。", font=font_set.get(16), fill=DIM)
|
|
266
|
+
cursor += 42
|
|
267
|
+
|
|
268
|
+
# ---- 花色图鉴
|
|
269
|
+
bold_text(draw, (pad, cursor), "全部花色", font_set.get(20, True), font_set, fill=YELLOW)
|
|
270
|
+
draw.text(
|
|
271
|
+
(pad + text_width(draw, "全部花色", font_set.get(20, True)) + 10, cursor + 6),
|
|
272
|
+
str(len(flowers)),
|
|
273
|
+
font=font_set.get(14),
|
|
274
|
+
fill=FAINT,
|
|
275
|
+
)
|
|
276
|
+
cursor += 38
|
|
277
|
+
|
|
278
|
+
for index, flower in enumerate(flowers):
|
|
279
|
+
row, col = divmod(index, columns)
|
|
280
|
+
cx = pad + col * tile_w + tile_w // 2
|
|
281
|
+
cy = cursor + row * tile_h + 42
|
|
282
|
+
_draw_tile(
|
|
283
|
+
content,
|
|
284
|
+
draw,
|
|
285
|
+
flower,
|
|
286
|
+
images.get(flower.name),
|
|
287
|
+
cx,
|
|
288
|
+
cy,
|
|
289
|
+
38,
|
|
290
|
+
font_set,
|
|
291
|
+
done=bool(collected and flower.name in collected),
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
return finish(
|
|
295
|
+
content,
|
|
296
|
+
width,
|
|
297
|
+
fonts=font_set,
|
|
298
|
+
pad=pad,
|
|
299
|
+
right_text=f"{len(flowers)} 种 · {len(recipes)} 配方",
|
|
300
|
+
)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""鲜花培育的文字回复与命令逻辑。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import List
|
|
6
|
+
|
|
7
|
+
from ...collection import KIND_FLOWER
|
|
8
|
+
from ...values import grade_text
|
|
9
|
+
from ..common import Reply, ReplyEnv, render_card, usage
|
|
10
|
+
from .data import Flower, FlowerBook
|
|
11
|
+
from .render import render_flower_card
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def list_text(book: FlowerBook) -> str:
|
|
15
|
+
lines = [f"鲜花培育 · {len(book.species)} 个品种 / {len(book.flowers)} 种花色", ""]
|
|
16
|
+
for item in book.species:
|
|
17
|
+
area = book.area_of(item.name)
|
|
18
|
+
suffix = f" · {area}" if area else ""
|
|
19
|
+
lines.append(f"· {item.name}({item.count} 种){suffix}")
|
|
20
|
+
lines.append("")
|
|
21
|
+
lines.append(usage(
|
|
22
|
+
"鲜花 <品种> —— 培育配方卡片,例如 鲜花 百合",
|
|
23
|
+
"鲜花 <花名> —— 单朵花的配方与用途",
|
|
24
|
+
))
|
|
25
|
+
return "\n".join(lines)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def species_text(book: FlowerBook, species: str) -> str:
|
|
29
|
+
flowers = book.flowers_of(species)
|
|
30
|
+
recipes = book.recipes_of(species)
|
|
31
|
+
lines = [f"【{species}】{len(flowers)} 种花色 · {len(recipes)} 条配方"]
|
|
32
|
+
area = book.area_of(species)
|
|
33
|
+
if area:
|
|
34
|
+
lines.append(f"产地:{area}")
|
|
35
|
+
|
|
36
|
+
if recipes:
|
|
37
|
+
lines.append("")
|
|
38
|
+
lines.append("培育配方:")
|
|
39
|
+
for recipe in recipes:
|
|
40
|
+
lines.append(
|
|
41
|
+
f"· {recipe.parent1} × {recipe.parent2} → {recipe.result}({recipe.grade})"
|
|
42
|
+
)
|
|
43
|
+
if flowers:
|
|
44
|
+
lines.append("")
|
|
45
|
+
lines.append("全部花色:")
|
|
46
|
+
lines.append("、".join(f"{f.name}({f.grade})" for f in flowers))
|
|
47
|
+
return "\n".join(lines)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def flower_text(book: FlowerBook, flower: Flower) -> str:
|
|
51
|
+
lines = [f"【{flower.name}】{flower.species}"]
|
|
52
|
+
tags = []
|
|
53
|
+
if flower.grade:
|
|
54
|
+
tags.append(f"{grade_text(flower.grade)}品质")
|
|
55
|
+
if flower.color:
|
|
56
|
+
tags.append(flower.color)
|
|
57
|
+
if flower.area:
|
|
58
|
+
tags.append(flower.area)
|
|
59
|
+
if tags:
|
|
60
|
+
lines.append(" · ".join(tags))
|
|
61
|
+
|
|
62
|
+
recipe = book.parent_recipe(flower.name)
|
|
63
|
+
if recipe is not None:
|
|
64
|
+
lines.append(f"培育:{recipe.parent1} × {recipe.parent2} → {recipe.result}")
|
|
65
|
+
elif book.is_star_seed(flower.species):
|
|
66
|
+
lines.append("培育:基础花,需通过星海探索获得花株/花种")
|
|
67
|
+
else:
|
|
68
|
+
lines.append("培育:基础花,野外采集,不能培育得到")
|
|
69
|
+
|
|
70
|
+
children = book.child_recipes(flower.name)
|
|
71
|
+
if children:
|
|
72
|
+
lines.append("用于培育:")
|
|
73
|
+
for child in children:
|
|
74
|
+
if child.is_self_cross:
|
|
75
|
+
lines.append(f"· {child.result}({child.parent1} 自交)")
|
|
76
|
+
else:
|
|
77
|
+
lines.append(f"· {child.parent1} × {child.parent2} → {child.result}")
|
|
78
|
+
return "\n".join(lines)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
async def _species_reply(book: FlowerBook, species: str, env: ReplyEnv) -> Reply:
|
|
82
|
+
png = None
|
|
83
|
+
if env.can_render:
|
|
84
|
+
url_map = {flower.name: flower.icon for flower in book.flowers_of(species)}
|
|
85
|
+
fetched = await env.client.get_images(list(url_map.values()))
|
|
86
|
+
png = await render_card(
|
|
87
|
+
render_flower_card,
|
|
88
|
+
book,
|
|
89
|
+
species,
|
|
90
|
+
{name: fetched.get(url) for name, url in url_map.items()},
|
|
91
|
+
fonts=env.fonts,
|
|
92
|
+
collected=env.collected(KIND_FLOWER) or None,
|
|
93
|
+
label="鲜花培育卡片",
|
|
94
|
+
)
|
|
95
|
+
return png if png is not None else species_text(book, species)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def reply_flowers(arg: str, env: ReplyEnv) -> Reply:
|
|
99
|
+
book = await env.client.get_flowers()
|
|
100
|
+
arg = (arg or "").strip()
|
|
101
|
+
if not arg:
|
|
102
|
+
return list_text(book)
|
|
103
|
+
|
|
104
|
+
species = book.find_species(arg)
|
|
105
|
+
if species is not None and species == arg:
|
|
106
|
+
return await _species_reply(book, species, env)
|
|
107
|
+
|
|
108
|
+
flower = book.find_flower(arg)
|
|
109
|
+
if flower is not None:
|
|
110
|
+
return flower_text(book, flower)
|
|
111
|
+
|
|
112
|
+
if species is not None:
|
|
113
|
+
# 模糊命中品种(例如只打了「百」)
|
|
114
|
+
return await _species_reply(book, species, env)
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
f"没找到「{arg}」这个品种或花名。\n"
|
|
118
|
+
"现有品种:" + "、".join(item.name for item in book.species)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def species_choices(book: FlowerBook) -> List[str]:
|
|
123
|
+
return [item.name for item in book.species]
|
|
File without changes
|