From 751fc8fdb477e24414a020f6d4663010d2a01476 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Sun, 16 Oct 2022 00:19:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=88=91=E7=9A=84=E9=81=93?= =?UTF-8?q?=E5=85=B7=E4=BB=85=E6=9C=89=E8=A2=AB=E5=8A=A8=E6=88=96=E4=B8=BB?= =?UTF-8?q?=E5=8A=A8=E9=81=93=E5=85=B7=E6=97=B6=E5=9B=BE=E7=89=87=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + basic_plugins/shop/my_props/_data_source.py | 79 +++++++++++++++++---- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 14d06dd4..0b41bdd6 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,7 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能 * 新增用户使用道具,花费金币(包括插件)及用途记录 * 更细致的金币使用依赖注入 * 更多的依赖注入(包含图片获取等等.. +* 修复我的道具仅有被动或主动道具时图片显示错误 * 色图插件p站反向代理失效 [@pull/1139](https://github.com/HibiKier/zhenxun_bot/pull/1139) ### 2022/10/9 diff --git a/basic_plugins/shop/my_props/_data_source.py b/basic_plugins/shop/my_props/_data_source.py index ea142251..dec64650 100644 --- a/basic_plugins/shop/my_props/_data_source.py +++ b/basic_plugins/shop/my_props/_data_source.py @@ -1,4 +1,4 @@ -from typing import Dict, List +from typing import Dict, List, Optional from models.bag_user import BagUser from models.goods_info import GoodsInfo @@ -6,7 +6,7 @@ from utils.image_utils import BuildImage from configs.path_config import IMAGE_PATH -icon_path = IMAGE_PATH / 'shop_icon' +icon_path = IMAGE_PATH / "shop_icon" async def create_bag_image(props: Dict[str, int]): @@ -19,16 +19,59 @@ async def create_bag_image(props: Dict[str, int]): goods_list = await GoodsInfo.get_all_goods() active_props = await _init_prop(props, [x for x in goods_list if not x.is_passive]) passive_props = await _init_prop(props, [x for x in goods_list if x.is_passive]) - A = BuildImage(active_props.w + passive_props.w + 100, max(active_props.h, passive_props.h) + 100, font="CJGaoDeGuo.otf", font_size=30, color="#f9f6f2") - await A.apaste(active_props, (50, 70)) - await A.apaste(passive_props, (active_props.w + 50, 70)) - await A.aline((active_props.w + 45, 70, active_props.w + 45, A.h - 20), fill=(0, 0, 0)) - await A.atext((50, 30), "主动道具") - await A.atext((active_props.w + 55, 30), "被动道具") + active_w = 0 + active_h = 0 + passive_w = 0 + passive_h = 0 + if active_props: + img = BuildImage( + active_props.w, + active_props.h + 70, + font="CJGaoDeGuo.otf", + font_size=30, + color="#f9f6f2", + ) + await img.apaste(active_props, (0, 70)) + await img.atext((0, 30), "主动道具") + active_props = img + active_w = img.w + active_h = img.h + if passive_props: + img = BuildImage( + passive_props.w, + passive_props.h + 70, + font="CJGaoDeGuo.otf", + font_size=30, + color="#f9f6f2", + ) + await img.apaste(passive_props, (0, 70)) + await img.atext((0, 30), "被动道具") + passive_props = img + passive_w = img.w + passive_h = img.h + A = BuildImage( + active_w + passive_w + 100, + max(active_h, passive_h) + 60, + font="CJGaoDeGuo.otf", + font_size=30, + color="#f9f6f2", + ) + curr_w = 50 + if active_props: + await A.apaste(active_props, (curr_w, 0)) + curr_w += active_props.w + 10 + if passive_props: + await A.apaste(passive_props, (curr_w, 0)) + if active_props and passive_props: + await A.aline( + (active_props.w + 45, 70, active_props.w + 45, A.h - 20), fill=(0, 0, 0) + ) return A.pic2bs4() -async def _init_prop(props: Dict[str, int], _props: List[GoodsInfo]) -> BuildImage: +async def _init_prop( + props: Dict[str, int], _props: List[GoodsInfo] +) -> Optional[BuildImage]: """ 说明: 构造道具列表图片 @@ -38,18 +81,30 @@ async def _init_prop(props: Dict[str, int], _props: List[GoodsInfo]) -> BuildIma """ active_name = [x.goods_name for x in _props] name_list = [x for x in props.keys() if x in active_name] + if not name_list: + return None temp_img = BuildImage(0, 0, font_size=20) image_list = [] num_list = [] for i, name in enumerate(name_list): - img = BuildImage(temp_img.getsize(name)[0] + 50, 30, font="msyh.ttf", font_size=20, color="#f9f6f2") - await img.atext((30, 5), f'{i + 1}.{name}') + img = BuildImage( + temp_img.getsize(name)[0] + 50, + 30, + font="msyh.ttf", + font_size=20, + color="#f9f6f2", + ) + await img.atext((30, 5), f"{i + 1}.{name}") goods = [x for x in _props if x.goods_name == name][0] if goods.icon and (icon_path / goods.icon).exists(): icon = BuildImage(30, 30, background=icon_path / goods.icon) await img.apaste(icon, alpha=True) image_list.append(img) - num_list.append(BuildImage(30, 30, font_size=20, plain_text=f"×{props[name]}")) + num_list.append( + BuildImage( + 30, 30, font_size=20, font="msyh.ttf", plain_text=f"×{props[name]}" + ) + ) max_w = 0 num_max_w = 0 h = 0