🐛 修复使用道具后未减去道具数量

This commit is contained in:
HibiKier 2024-08-19 18:57:21 +08:00
parent 862c977eda
commit 09f586be25
3 changed files with 55 additions and 8 deletions

View File

@ -15,6 +15,7 @@ from nonebot_plugin_userinfo import EventUserInfo, UserInfo
from zhenxun.configs.utils import BaseBlock, PluginExtraData from zhenxun.configs.utils import BaseBlock, PluginExtraData
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.utils.enum import BlockType, PluginType from zhenxun.utils.enum import BlockType, PluginType
from zhenxun.utils.exception import GoodsNotFound
from zhenxun.utils.message import MessageUtils from zhenxun.utils.message import MessageUtils
from ._data_source import ShopManage from ._data_source import ShopManage
@ -141,9 +142,16 @@ async def _(
name: str, name: str,
num: int, num: int,
): ):
result = await ShopManage.use(bot, event, session, message, name, num, "") try:
logger.info(f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session) result = await ShopManage.use(bot, event, session, message, name, num, "")
if isinstance(result, str): logger.info(
await MessageUtils.build_message(result).send(reply_to=True) f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session
elif isinstance(result, UniMessage): )
await result.finish(reply_to=True) 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
)

View File

@ -260,9 +260,10 @@ class ShopManage:
bot, event, session, message, goods, num, text bot, event, session, message, goods, num, text
) )
if num > param.max_num_limit: 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) await cls.run_before_after(goods, param, "before", **kwargs)
result = await cls.__run(goods, param, session, message, **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) await cls.run_before_after(goods, param, "after", **kwargs)
if not result and param.send_success_msg: if not result and param.send_success_msg:
result = f"使用道具 {goods.name} {num} 次成功!" result = f"使用道具 {goods.name} {num} 次成功!"

View File

@ -144,7 +144,7 @@ class UserConsole(Model):
async def add_props_by_name( async def add_props_by_name(
cls, user_id: str, name: str, num: int = 1, platform: str | None = None cls, user_id: str, name: str, num: int = 1, platform: str | None = None
): ):
"""添加道具 """根据名称添加道具
参数: 参数:
user_id: 用户id user_id: 用户id
@ -155,3 +155,41 @@ class UserConsole(Model):
if goods := await GoodsInfo.get_or_none(goods_name=name): if goods := await GoodsInfo.get_or_none(goods_name=name):
return await cls.add_props(user_id, goods.uuid, num, platform) return await cls.add_props(user_id, goods.uuid, num, platform)
raise GoodsNotFound("未找到商品...") 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("未找到商品...")