Skip to content

Commit

Permalink
Merge pull request #1561 from HibiKier/dev
Browse files Browse the repository at this point in the history
🐛 修复使用道具后未减去道具数量
  • Loading branch information
HibiKier authored Aug 19, 2024
2 parents f09f2c3 + 09f586b commit 4d6013c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
20 changes: 14 additions & 6 deletions zhenxun/builtin_plugins/shop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from zhenxun.configs.utils import BaseBlock, PluginExtraData
from zhenxun.services.log import logger
from zhenxun.utils.enum import BlockType, PluginType
from zhenxun.utils.exception import GoodsNotFound
from zhenxun.utils.message import MessageUtils

from ._data_source import ShopManage
Expand Down Expand Up @@ -141,9 +142,16 @@ async def _(
name: str,
num: int,
):
result = await ShopManage.use(bot, event, session, message, name, num, "")
logger.info(f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session)
if isinstance(result, str):
await MessageUtils.build_message(result).send(reply_to=True)
elif isinstance(result, UniMessage):
await result.finish(reply_to=True)
try:
result = await ShopManage.use(bot, event, session, message, name, num, "")
logger.info(
f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session
)
if isinstance(result, str):
await MessageUtils.build_message(result).send(reply_to=True)
elif isinstance(result, UniMessage):
await result.finish(reply_to=True)
except GoodsNotFound:
await MessageUtils.build_message(f"没有找到道具 {name} 或道具数量不足...").send(
reply_to=True
)
3 changes: 2 additions & 1 deletion zhenxun/builtin_plugins/shop/_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ async def use(
bot, event, session, message, goods, num, text
)
if num > param.max_num_limit:
return f"{goods_name} 单次使用最大数量为{param.max_num_limit}..."
return f"{goods_info.goods_name} 单次使用最大数量为{param.max_num_limit}..."
await cls.run_before_after(goods, param, "before", **kwargs)
result = await cls.__run(goods, param, session, message, **kwargs)
await UserConsole.use_props(session.id1, goods_info.uuid, num, session.platform) # type: ignore
await cls.run_before_after(goods, param, "after", **kwargs)
if not result and param.send_success_msg:
result = f"使用道具 {goods.name} {num} 次成功!"
Expand Down
40 changes: 39 additions & 1 deletion zhenxun/models/user_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async def add_props(
async def add_props_by_name(
cls, user_id: str, name: str, num: int = 1, platform: str | None = None
):
"""添加道具
"""根据名称添加道具
参数:
user_id: 用户id
Expand All @@ -155,3 +155,41 @@ async def add_props_by_name(
if goods := await GoodsInfo.get_or_none(goods_name=name):
return await cls.add_props(user_id, goods.uuid, num, platform)
raise GoodsNotFound("未找到商品...")

@classmethod
async def use_props(
cls, user_id: str, goods_uuid: str, num: int = 1, platform: str | None = None
):
"""添加道具
参数:
user_id: 用户id
goods_uuid: 道具uuid
num: 道具数量.
platform: 平台.
"""
user, _ = await cls.get_or_create(
user_id=user_id,
defaults={"platform": platform, "uid": await cls.get_new_uid()},
)

if goods_uuid not in user.props or user.props[goods_uuid] < num:
raise GoodsNotFound("未找到商品或道具数量不足...")
user.props[goods_uuid] -= num
await user.save(update_fields=["props"])

@classmethod
async def use_props_by_name(
cls, user_id: str, name: str, num: int = 1, platform: str | None = None
):
"""根据名称添加道具
参数:
user_id: 用户id
name: 道具名称
num: 道具数量.
platform: 平台.
"""
if goods := await GoodsInfo.get_or_none(goods_name=name):
return await cls.use_props(user_id, goods.uuid, num, platform)
raise GoodsNotFound("未找到商品...")

0 comments on commit 4d6013c

Please sign in to comment.