From 1c393e5c3e036de234df64d4bc3df3d3e5698c5c Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Sun, 26 Jun 2022 03:08:23 +0800 Subject: [PATCH] update shop daily_limit --- README.md | 1 + basic_plugins/apscheduler/__init__.py | 1 - basic_plugins/scripts.py | 8 +++ basic_plugins/shop/buy.py | 4 ++ basic_plugins/shop/shop_handle/__init__.py | 18 +++++-- basic_plugins/shop/shop_handle/data_source.py | 49 +++++++++++-------- utils/decorator/shop.py | 34 +++++++++++-- 7 files changed, 87 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 729392c0..80b78f65 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,7 @@ __Docker 最新版本由 [Sakuracio](https://github.com/Sakuracio) 提供__ * 替换了bt URL * PIX当使用pid查询图片时,会发送该pid下所有图片 +* 商店提供了每日购买次数限制 ### 2022/6/24 diff --git a/basic_plugins/apscheduler/__init__.py b/basic_plugins/apscheduler/__init__.py index 66a9844d..5844beeb 100755 --- a/basic_plugins/apscheduler/__init__.py +++ b/basic_plugins/apscheduler/__init__.py @@ -6,7 +6,6 @@ from models.group_info import GroupInfo from models.friend_user import FriendUser from nonebot.adapters.onebot.v11 import ActionFailed from configs.config import NICKNAME, Config -from utils.manager import group_manager from pathlib import Path import shutil diff --git a/basic_plugins/scripts.py b/basic_plugins/scripts.py index b9fa13b3..eb592dd2 100755 --- a/basic_plugins/scripts.py +++ b/basic_plugins/scripts.py @@ -112,6 +112,14 @@ async def _(): "ALTER TABLE chat_history ADD plain_text Text;", "chat_history" ), # 新增纯文本 + ( + "ALTER TABLE goods_info ADD daily_limit Integer DEFAULT 0;", + "goods_info" + ), # 新增纯文本 + ( + "ALTER TABLE goods_info ADD daily_purchase_limit Json DEFAULT '{}';", + "goods_info" + ), # 新增纯文本 ] for sql in sql_str: try: diff --git a/basic_plugins/shop/buy.py b/basic_plugins/shop/buy.py index 0f723fbc..e33e54e6 100644 --- a/basic_plugins/shop/buy.py +++ b/basic_plugins/shop/buy.py @@ -77,7 +77,11 @@ async def _(event: GroupMessageEvent, arg: Message = CommandArg()): await BagUser.get_gold(event.user_id, event.group_id) ) < goods.goods_price * num * goods.goods_discount: await buy.finish("您的金币好像不太够哦", at_sender=True) + flag, n = await GoodsInfo.check_user_daily_purchase(goods, event.user_id, event.group_id, num) + if flag: + await buy.finish(f"该次购买将超过每日次数限制,目前该道具还可以购买{n}次哦", at_sender=True) if await BagUser.buy_property(event.user_id, event.group_id, goods, num): + await GoodsInfo.add_user_daily_purchase(goods, event.user_id, event.group_id, num) await buy.send( f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 成功!", at_sender=True, diff --git a/basic_plugins/shop/shop_handle/__init__.py b/basic_plugins/shop/shop_handle/__init__.py index 7e680229..0fd06280 100644 --- a/basic_plugins/shop/shop_handle/__init__.py +++ b/basic_plugins/shop/shop_handle/__init__.py @@ -1,10 +1,10 @@ -from .data_source import create_shop_help, delete_goods, update_goods, register_goods, parse_goods_info +from .data_source import create_shop_help, delete_goods, update_goods, register_goods, parse_goods_info, GoodsInfo from nonebot.adapters.onebot.v11 import MessageEvent, Message from nonebot import on_command from configs.path_config import IMAGE_PATH from utils.message_builder import image from nonebot.permission import SUPERUSER -from utils.utils import is_number +from utils.utils import is_number, scheduler from nonebot.params import CommandArg from nonebot.plugin import export from services.log import logger @@ -128,6 +128,18 @@ async def _(event: MessageEvent, arg: Message = CommandArg()): await shop_update_goods.send(f"修改商品 {name} 成功了...\n{text}", at_sender=True) logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 成功") else: - await shop_update_goods.send(f"修改商品 {name} 失败了...", at_sender=True) + await shop_update_goods.send(name, at_sender=True) logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 失败") + +@scheduler.scheduled_job( + "cron", + hour=0, + minute=0, +) +async def _(): + try: + await GoodsInfo.reset_daily_purchase() + logger.info("商品每日限购次数重置成功...") + except Exception as e: + logger.error(f"商品每日限购次数重置发生错误 {type(e)}:{e}") diff --git a/basic_plugins/shop/shop_handle/data_source.py b/basic_plugins/shop/shop_handle/data_source.py index 9d911b61..4cd416c8 100644 --- a/basic_plugins/shop/shop_handle/data_source.py +++ b/basic_plugins/shop/shop_handle/data_source.py @@ -3,7 +3,7 @@ from utils.image_utils import BuildImage from models.sign_group_user import SignGroupUser from utils.utils import is_number from configs.path_config import IMAGE_PATH -from typing import Optional, Union +from typing import Optional, Union, Tuple from configs.config import Config from nonebot import Driver from nonebot.plugin import require @@ -32,6 +32,7 @@ async def init_default_shop_goods(): "下次签到双倍好感度概率 + 30%(金币才是真命天子!)(同类商品将覆盖)", ), load_status=Config.get_config("shop", "IMPORT_DEFAULT_SHOP_GOODS"), + daily_limit=(10, 20, 30), ** {"好感度双倍加持卡Ⅰ_prob": 0.1, "好感度双倍加持卡Ⅱ_prob": 0.2, "好感度双倍加持卡Ⅲ_prob": 0.3}, ) async def sign_card(user_id: int, group_id: int, prob: float): @@ -140,6 +141,7 @@ async def register_goods( des: str, discount: Optional[float] = 1, limit_time: Optional[int] = 0, + daily_limit: Optional[int] = 0, ) -> bool: """ 添加商品 @@ -151,6 +153,7 @@ async def register_goods( :param des: 商品简介 :param discount: 商品折扣 :param limit_time: 商品限时销售时间,单位为小时 + :param daily_limit: 每日购买次数限制 :return: 是否添加成功 """ if not await GoodsInfo.get_goods_info(name): @@ -162,7 +165,7 @@ async def register_goods( else 0 ) return await GoodsInfo.add_goods( - name, int(price), des, float(discount), limit_time + name, int(price), des, float(discount), limit_time, daily_limit ) return False @@ -192,7 +195,7 @@ async def delete_goods(name: str, id_: int) -> "str, str, int": # 更新商品信息 -async def update_goods(**kwargs) -> "str, str, int": +async def update_goods(**kwargs) -> Tuple[bool, str, str]: """ 更新商品信息 :param kwargs: kwargs @@ -202,17 +205,18 @@ async def update_goods(**kwargs) -> "str, str, int": goods_lst = await GoodsInfo.get_all_goods() if is_number(kwargs["name"]): if int(kwargs["name"]) < 1 or int(kwargs["name"]) > len(goods_lst): - return "序号错误,没有该序号的商品...", "", 999 + return False, "序号错误,没有该序号的商品...", "" goods = goods_lst[int(kwargs["name"]) - 1] else: goods = await GoodsInfo.get_goods_info(kwargs["name"]) if not goods: - return "名称错误,没有该名称的商品...", "", 999 - name = goods.goods_name + return False, "名称错误,没有该名称的商品...", "" + name: str = goods.goods_name price = goods.goods_price des = goods.goods_description discount = goods.goods_discount limit_time = goods.goods_limit_time + daily_limit = goods.daily_limit new_time = 0 tmp = "" if kwargs.get("price"): @@ -232,21 +236,22 @@ async def update_goods(**kwargs) -> "str, str, int": ) tmp += f"限时至: {new_time}\n" limit_time = kwargs["limit_time"] - return ( - await GoodsInfo.update_goods( - name, - int(price), - des, - float(discount), - int( - time.time() + limit_time * 60 * 60 - if limit_time != 0 and new_time - else 0 - ), - ), + if kwargs.get("daily_limit"): + tmp += f'每日购买限制:{daily_limit} --> {kwargs["daily_limit"]}\n' + daily_limit = int(kwargs["daily_limit"]) + await GoodsInfo.update_goods( name, - tmp[:-1], + int(price), + des, + float(discount), + int( + time.time() + limit_time * 60 * 60 + if limit_time != 0 and new_time + else 0 + ), + daily_limit ) + return True, name, tmp[:-1], def parse_goods_info(msg: str) -> Union[dict, str]: @@ -276,6 +281,10 @@ def parse_goods_info(msg: str) -> Union[dict, str]: data["discount"] = sp[1] elif sp[0] == "limit_time": if not is_number(sp[1]) or float(sp[1]) < 0: - return "limit_time参数不合法,必须大于0!" + return "limit_time参数不合法,必须为数字且大于0!" data["limit_time"] = sp[1] + elif sp[0] == "daily_limit": + if not is_number(sp[1]) or float(sp[1]) < 0: + return "daily_limit参数不合法,必须为数字且大于0!" + data["daily_limit"] = sp[1] return data diff --git a/utils/decorator/shop.py b/utils/decorator/shop.py index 154feb87..e03ea290 100644 --- a/utils/decorator/shop.py +++ b/utils/decorator/shop.py @@ -20,12 +20,15 @@ class ShopRegister(dict): discount: Tuple[float, ...], limit_time: Tuple[int, ...], load_status: Tuple[bool, ...], + daily_limit: Tuple[int, ...], **kwargs, ): def add_register_item(func: Callable): if name in self._data.keys(): raise ValueError("该商品已注册,请替换其他名称!") - for n, p, d, dd, l, s in zip(name, price, des, discount, limit_time, load_status): + for n, p, d, dd, l, s, dl in zip( + name, price, des, discount, limit_time, load_status, daily_limit + ): if s: _temp_kwargs = {} for key, value in kwargs.items(): @@ -38,6 +41,7 @@ class ShopRegister(dict): "des": d, "discount": dd, "limit_time": l, + "daily_limit": dl, "func": func, "kwargs": _temp_kwargs, } @@ -52,7 +56,12 @@ class ShopRegister(dict): self._flag = False for name in self._data.keys(): await shop.register_goods( - name, self._data[name]["price"], self._data[name]["des"], self._data[name]["discount"], self._data[name]["limit_time"] + name, + self._data[name]["price"], + self._data[name]["des"], + self._data[name]["discount"], + self._data[name]["limit_time"], + self._data[name]["daily_limit"], ) use.register_use( name, self._data[name]["func"], **self._data[name]["kwargs"] @@ -66,6 +75,7 @@ class ShopRegister(dict): discount: Union[float, Tuple[float, ...]] = 1, limit_time: Union[int, Tuple[int, ...]] = 0, load_status: Union[bool, Tuple[bool, ...]] = True, + daily_limit: Union[int, Tuple[int, ...]] = 0, **kwargs, ): _tuple_list = [] @@ -75,7 +85,9 @@ class ShopRegister(dict): if _current_len == -1: _current_len = len(x) if _current_len != len(x): - raise ValueError(f"注册商品 {name} 中 name,price,des,discount,limit_time,load_status 数量不符!") + raise ValueError( + f"注册商品 {name} 中 name,price,des,discount,limit_time,load_status,daily_limit 数量不符!" + ) _current_len = _current_len if _current_len > -1 else 1 _name = name if isinstance(name, tuple) else (name,) _price = ( @@ -101,7 +113,21 @@ class ShopRegister(dict): if isinstance(load_status, tuple) else tuple([load_status for _ in range(_current_len)]) ) - return self.register(_name, _price, _des, _discount, _limit_time, _load_status, **kwargs) + _daily_limit = ( + daily_limit + if isinstance(daily_limit, tuple) + else tuple([daily_limit for _ in range(_current_len)]) + ) + return self.register( + _name, + _price, + _des, + _discount, + _limit_time, + _load_status, + _daily_limit, + **kwargs, + ) def __setitem__(self, key, value): self._data[key] = value