update shop daily_limit

This commit is contained in:
HibiKier 2022-06-26 03:08:23 +08:00
parent 667c143c74
commit 1c393e5c3e
7 changed files with 87 additions and 28 deletions

View File

@ -247,6 +247,7 @@ __Docker 最新版本由 [Sakuracio](https://github.com/Sakuracio) 提供__
* 替换了bt URL * 替换了bt URL
* PIX当使用pid查询图片时会发送该pid下所有图片 * PIX当使用pid查询图片时会发送该pid下所有图片
* 商店提供了每日购买次数限制
### 2022/6/24 ### 2022/6/24

View File

@ -6,7 +6,6 @@ from models.group_info import GroupInfo
from models.friend_user import FriendUser from models.friend_user import FriendUser
from nonebot.adapters.onebot.v11 import ActionFailed from nonebot.adapters.onebot.v11 import ActionFailed
from configs.config import NICKNAME, Config from configs.config import NICKNAME, Config
from utils.manager import group_manager
from pathlib import Path from pathlib import Path
import shutil import shutil

View File

@ -112,6 +112,14 @@ async def _():
"ALTER TABLE chat_history ADD plain_text Text;", "ALTER TABLE chat_history ADD plain_text Text;",
"chat_history" "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: for sql in sql_str:
try: try:

View File

@ -77,7 +77,11 @@ async def _(event: GroupMessageEvent, arg: Message = CommandArg()):
await BagUser.get_gold(event.user_id, event.group_id) await BagUser.get_gold(event.user_id, event.group_id)
) < goods.goods_price * num * goods.goods_discount: ) < goods.goods_price * num * goods.goods_discount:
await buy.finish("您的金币好像不太够哦", at_sender=True) 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): 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( await buy.send(
f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 成功!", f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 成功!",
at_sender=True, at_sender=True,

View File

@ -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.adapters.onebot.v11 import MessageEvent, Message
from nonebot import on_command from nonebot import on_command
from configs.path_config import IMAGE_PATH from configs.path_config import IMAGE_PATH
from utils.message_builder import image from utils.message_builder import image
from nonebot.permission import SUPERUSER 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.params import CommandArg
from nonebot.plugin import export from nonebot.plugin import export
from services.log import logger 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) await shop_update_goods.send(f"修改商品 {name} 成功了...\n{text}", at_sender=True)
logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 成功") logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 成功")
else: 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} 失败") 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}")

View File

@ -3,7 +3,7 @@ from utils.image_utils import BuildImage
from models.sign_group_user import SignGroupUser from models.sign_group_user import SignGroupUser
from utils.utils import is_number from utils.utils import is_number
from configs.path_config import IMAGE_PATH from configs.path_config import IMAGE_PATH
from typing import Optional, Union from typing import Optional, Union, Tuple
from configs.config import Config from configs.config import Config
from nonebot import Driver from nonebot import Driver
from nonebot.plugin import require from nonebot.plugin import require
@ -32,6 +32,7 @@ async def init_default_shop_goods():
"下次签到双倍好感度概率 + 30%(金币才是真命天子!)(同类商品将覆盖)", "下次签到双倍好感度概率 + 30%(金币才是真命天子!)(同类商品将覆盖)",
), ),
load_status=Config.get_config("shop", "IMPORT_DEFAULT_SHOP_GOODS"), load_status=Config.get_config("shop", "IMPORT_DEFAULT_SHOP_GOODS"),
daily_limit=(10, 20, 30),
** {"好感度双倍加持卡_prob": 0.1, "好感度双倍加持卡Ⅱ_prob": 0.2, "好感度双倍加持卡Ⅲ_prob": 0.3}, ** {"好感度双倍加持卡_prob": 0.1, "好感度双倍加持卡Ⅱ_prob": 0.2, "好感度双倍加持卡Ⅲ_prob": 0.3},
) )
async def sign_card(user_id: int, group_id: int, prob: float): async def sign_card(user_id: int, group_id: int, prob: float):
@ -140,6 +141,7 @@ async def register_goods(
des: str, des: str,
discount: Optional[float] = 1, discount: Optional[float] = 1,
limit_time: Optional[int] = 0, limit_time: Optional[int] = 0,
daily_limit: Optional[int] = 0,
) -> bool: ) -> bool:
""" """
添加商品 添加商品
@ -151,6 +153,7 @@ async def register_goods(
:param des: 商品简介 :param des: 商品简介
:param discount: 商品折扣 :param discount: 商品折扣
:param limit_time: 商品限时销售时间单位为小时 :param limit_time: 商品限时销售时间单位为小时
:param daily_limit: 每日购买次数限制
:return: 是否添加成功 :return: 是否添加成功
""" """
if not await GoodsInfo.get_goods_info(name): if not await GoodsInfo.get_goods_info(name):
@ -162,7 +165,7 @@ async def register_goods(
else 0 else 0
) )
return await GoodsInfo.add_goods( 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 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 :param kwargs: kwargs
@ -202,17 +205,18 @@ async def update_goods(**kwargs) -> "str, str, int":
goods_lst = await GoodsInfo.get_all_goods() goods_lst = await GoodsInfo.get_all_goods()
if is_number(kwargs["name"]): if is_number(kwargs["name"]):
if int(kwargs["name"]) < 1 or int(kwargs["name"]) > len(goods_lst): if int(kwargs["name"]) < 1 or int(kwargs["name"]) > len(goods_lst):
return "序号错误,没有该序号的商品...", "", 999 return False, "序号错误,没有该序号的商品...", ""
goods = goods_lst[int(kwargs["name"]) - 1] goods = goods_lst[int(kwargs["name"]) - 1]
else: else:
goods = await GoodsInfo.get_goods_info(kwargs["name"]) goods = await GoodsInfo.get_goods_info(kwargs["name"])
if not goods: if not goods:
return "名称错误,没有该名称的商品...", "", 999 return False, "名称错误,没有该名称的商品...", ""
name = goods.goods_name name: str = goods.goods_name
price = goods.goods_price price = goods.goods_price
des = goods.goods_description des = goods.goods_description
discount = goods.goods_discount discount = goods.goods_discount
limit_time = goods.goods_limit_time limit_time = goods.goods_limit_time
daily_limit = goods.daily_limit
new_time = 0 new_time = 0
tmp = "" tmp = ""
if kwargs.get("price"): if kwargs.get("price"):
@ -232,21 +236,22 @@ async def update_goods(**kwargs) -> "str, str, int":
) )
tmp += f"限时至: {new_time}\n" tmp += f"限时至: {new_time}\n"
limit_time = kwargs["limit_time"] limit_time = kwargs["limit_time"]
return ( if kwargs.get("daily_limit"):
await GoodsInfo.update_goods( tmp += f'每日购买限制:{daily_limit} --> {kwargs["daily_limit"]}\n'
name, daily_limit = int(kwargs["daily_limit"])
int(price), await GoodsInfo.update_goods(
des,
float(discount),
int(
time.time() + limit_time * 60 * 60
if limit_time != 0 and new_time
else 0
),
),
name, 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]: 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] data["discount"] = sp[1]
elif sp[0] == "limit_time": elif sp[0] == "limit_time":
if not is_number(sp[1]) or float(sp[1]) < 0: if not is_number(sp[1]) or float(sp[1]) < 0:
return "limit_time参数不合法必须大于0" return "limit_time参数不合法必须为数字且大于0"
data["limit_time"] = sp[1] 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 return data

View File

@ -20,12 +20,15 @@ class ShopRegister(dict):
discount: Tuple[float, ...], discount: Tuple[float, ...],
limit_time: Tuple[int, ...], limit_time: Tuple[int, ...],
load_status: Tuple[bool, ...], load_status: Tuple[bool, ...],
daily_limit: Tuple[int, ...],
**kwargs, **kwargs,
): ):
def add_register_item(func: Callable): def add_register_item(func: Callable):
if name in self._data.keys(): if name in self._data.keys():
raise ValueError("该商品已注册,请替换其他名称!") 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: if s:
_temp_kwargs = {} _temp_kwargs = {}
for key, value in kwargs.items(): for key, value in kwargs.items():
@ -38,6 +41,7 @@ class ShopRegister(dict):
"des": d, "des": d,
"discount": dd, "discount": dd,
"limit_time": l, "limit_time": l,
"daily_limit": dl,
"func": func, "func": func,
"kwargs": _temp_kwargs, "kwargs": _temp_kwargs,
} }
@ -52,7 +56,12 @@ class ShopRegister(dict):
self._flag = False self._flag = False
for name in self._data.keys(): for name in self._data.keys():
await shop.register_goods( 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( use.register_use(
name, self._data[name]["func"], **self._data[name]["kwargs"] name, self._data[name]["func"], **self._data[name]["kwargs"]
@ -66,6 +75,7 @@ class ShopRegister(dict):
discount: Union[float, Tuple[float, ...]] = 1, discount: Union[float, Tuple[float, ...]] = 1,
limit_time: Union[int, Tuple[int, ...]] = 0, limit_time: Union[int, Tuple[int, ...]] = 0,
load_status: Union[bool, Tuple[bool, ...]] = True, load_status: Union[bool, Tuple[bool, ...]] = True,
daily_limit: Union[int, Tuple[int, ...]] = 0,
**kwargs, **kwargs,
): ):
_tuple_list = [] _tuple_list = []
@ -75,7 +85,9 @@ class ShopRegister(dict):
if _current_len == -1: if _current_len == -1:
_current_len = len(x) _current_len = len(x)
if _current_len != len(x): if _current_len != len(x):
raise ValueError(f"注册商品 {name} 中 namepricedesdiscountlimit_timeload_status 数量不符!") raise ValueError(
f"注册商品 {name} 中 namepricedesdiscountlimit_timeload_statusdaily_limit 数量不符!"
)
_current_len = _current_len if _current_len > -1 else 1 _current_len = _current_len if _current_len > -1 else 1
_name = name if isinstance(name, tuple) else (name,) _name = name if isinstance(name, tuple) else (name,)
_price = ( _price = (
@ -101,7 +113,21 @@ class ShopRegister(dict):
if isinstance(load_status, tuple) if isinstance(load_status, tuple)
else tuple([load_status for _ in range(_current_len)]) 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): def __setitem__(self, key, value):
self._data[key] = value self._data[key] = value