Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 修复商店购买 #1834

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions zhenxun/builtin_plugins/shop/_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from nonebot_plugin_alconna import UniMessage, UniMsg
from nonebot_plugin_uninfo import Uninfo
from pydantic import BaseModel, create_model
from tortoise.expressions import Q

from zhenxun.models.friend_user import FriendUser
from zhenxun.models.goods_info import GoodsInfo
Expand Down Expand Up @@ -120,7 +121,7 @@ async def gold_rank(
)
data_list.append(
[
f"{i+1}",
f"{i + 1}",
(ava_bytes, 30, 30) if platform == "qq" else "",
uid2name.get(user[0]),
user[1],
Expand Down Expand Up @@ -405,17 +406,19 @@ async def buy_prop(
返回:
str: 返回小
"""
if name == "神秘药水":
return "你们看看就好啦,这是不可能卖给你们的~"
if num < 0:
return "购买的数量要大于0!"
goods_list = await GoodsInfo.annotate().order_by("id").all()
goods_list = [
goods
for goods in goods_list
if goods.goods_limit_time > time.time() or goods.goods_limit_time == 0
]
goods_list = (
await GoodsInfo.filter(
Q(goods_limit_time__gte=time.time()) | Q(goods_limit_time=0)
)
.annotate()
.order_by("id")
.all()
)
if name.isdigit():
if int(name) > len(goods_list) or int(name) <= 0:
return "道具编号不存在..."
goods = goods_list[int(name) - 1]
elif filter_goods := [g for g in goods_list if g.goods_name == name]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议: 为不存在的物品名称添加错误处理

当 filter_goods 列表为空(物品名称不存在)时,我们应该返回一个适当的错误消息。

Original comment in English

suggestion: Add error handling for non-existent item names

When the filter_goods list is empty (item name doesn't exist), we should return an appropriate error message.

goods = filter_goods[0]
Expand Down
62 changes: 43 additions & 19 deletions zhenxun/builtin_plugins/shop/html_image.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
import time

from nonebot_plugin_htmlrender import template_to_pic
from pydantic import BaseModel
from tortoise.expressions import Q

from zhenxun.configs.config import BotConfig
from zhenxun.configs.path_config import TEMPLATE_PATH
Expand All @@ -18,35 +20,57 @@ class GoodsItem(BaseModel):
"""分区名称"""


def get_limit_time(end_time: int):
now = int(time.time())
if now > end_time:
return None
current_datetime = datetime.fromtimestamp(now)
end_datetime = datetime.fromtimestamp(end_time)
time_difference = end_datetime - current_datetime
total_seconds = time_difference.total_seconds()
hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
return f"{hours}:{minutes}"


def get_discount(price: int, discount: float):
return None if discount == 1.0 else int(price * discount)


async def html_image() -> bytes:
"""构建图片"""
goods_list: list[tuple[int, GoodsInfo]] = [
(i + 1, goods)
for i, goods in enumerate(await GoodsInfo.get_all_goods())
if goods.goods_limit_time == 0 or time.time() < goods.goods_limit_time
]
goods_list = (
await GoodsInfo.filter(
Q(goods_limit_time__gte=time.time()) | Q(goods_limit_time=0)
)
.annotate()
.order_by("id")
.all()
)
partition_dict: dict[str, list[dict]] = {}
for goods in goods_list:
if not goods[1].partition:
goods[1].partition = "默认分区"
if goods[1].partition not in partition_dict:
partition_dict[goods[1].partition] = []
for idx, goods in enumerate(goods_list):
if not goods.partition:
goods.partition = "默认分区"
if goods.partition not in partition_dict:
partition_dict[goods.partition] = []
icon = None
if goods[1].icon:
path = ICON_PATH / goods[1].icon
if goods.icon:
path = ICON_PATH / goods.icon
if path.exists():
icon = (
"data:image/png;base64,"
f"{BuildImage.open(ICON_PATH / goods[1].icon).pic2bs4()[9:]}"
f"{BuildImage.open(ICON_PATH / goods.icon).pic2bs4()[9:]}"
)
partition_dict[goods[1].partition].append(
partition_dict[goods.partition].append(
{
"id": goods[0],
"price": goods[1].goods_price,
"daily_limit": goods[1].daily_limit or "∞",
"name": goods[1].goods_name,
"id": idx + 1,
"price": goods.goods_price,
"discount_price": get_discount(goods.goods_price, goods.goods_discount),
"limit_time": get_limit_time(goods.goods_limit_time),
"daily_limit": goods.daily_limit or "∞",
"name": goods.goods_name,
"icon": icon,
"description": goods[1].goods_description,
"description": goods.goods_description,
}
)
data_list = [
Expand Down
18 changes: 11 additions & 7 deletions zhenxun/builtin_plugins/shop/normal_image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time

from tortoise.expressions import Q

from zhenxun.configs.path_config import IMAGE_PATH
from zhenxun.models.goods_info import GoodsInfo
from zhenxun.utils._build_image import BuildImage
Expand All @@ -14,17 +16,19 @@ async def normal_image() -> bytes:
返回:
BuildImage: 商店图片
"""
goods_lst = await GoodsInfo.get_all_goods()
h = 10
_list: list[GoodsInfo] = [
goods
for goods in goods_lst
if goods.goods_limit_time == 0 or time.time() < goods.goods_limit_time
]
goods_list = (
await GoodsInfo.filter(
Q(goods_limit_time__gte=time.time()) | Q(goods_limit_time=0)
)
.annotate()
.order_by("id")
.all()
)
# A = BuildImage(1100, h, color="#f9f6f2")
total_n = 0
image_list = []
for idx, goods in enumerate(_list):
for idx, goods in enumerate(goods_list):
name_image = BuildImage(
580, 40, font_size=25, color="#e67b6b", font="CJGaoDeGuo.otf"
)
Expand Down
8 changes: 5 additions & 3 deletions zhenxun/utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from nonebot_plugin_alconna import (
At,
AtAll,
Button,
CustomNode,
Image,
Reference,
Expand Down Expand Up @@ -37,6 +38,7 @@
| Text
| Voice
| Video
| Button
)


Expand All @@ -58,9 +60,7 @@ def __build_message(cls, msg_list: list[MESSAGE_TYPE]) -> list[Text | Image]:
config = nonebot.get_plugin_config(Config)
message_list = []
for msg in msg_list:
if isinstance(msg, Image | Text | At | AtAll | Video | Voice):
message_list.append(msg)
elif isinstance(msg, str):
if isinstance(msg, str):
if msg.startswith("base64://"):
message_list.append(Image(raw=BytesIO(base64.b64decode(msg[9:]))))
elif msg.startswith("http://"):
Expand All @@ -85,6 +85,8 @@ def __build_message(cls, msg_list: list[MESSAGE_TYPE]) -> list[Text | Image]:
message_list.append(Image(raw=msg))
elif isinstance(msg, BuildImage):
message_list.append(Image(raw=msg.pic2bytes()))
else:
message_list.append(msg)
return message_list

@classmethod
Expand Down
Loading