mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-06 04:10:02 +08:00
update ShopRegister
This commit is contained in:
+110
-60
@@ -1,60 +1,110 @@
|
||||
#
|
||||
# from functools import wraps
|
||||
# from typing import Union, List, Callable
|
||||
# from nonebot.plugin import require
|
||||
# from nonebot.adapters.onebot.v11 import Bot
|
||||
# import asyncio
|
||||
# import nonebot
|
||||
#
|
||||
# driver = nonebot.get_driver()
|
||||
#
|
||||
# use = require("use")
|
||||
# shop = require("shop_handle")
|
||||
#
|
||||
# flag = False
|
||||
#
|
||||
# name_list = []
|
||||
#
|
||||
# func_list = []
|
||||
#
|
||||
#
|
||||
# def shop_register(
|
||||
# name: Union[str, List[str]],
|
||||
# price: Union[int, List[int]],
|
||||
# des: Union[str, List[str]],
|
||||
# discount: Union[float, List[float]] = 1,
|
||||
# limit_time: Union[int, List[int]] = 0,
|
||||
# status: bool = True,
|
||||
# **kwargs_
|
||||
# ):
|
||||
# print("---------")
|
||||
# print("name:", name)
|
||||
# print("price:", price)
|
||||
# print("des:", des)
|
||||
# print("discount:", discount)
|
||||
# print("limit_time:", limit_time)
|
||||
# print("status:", status)
|
||||
# print("kwargs:", kwargs_)
|
||||
# asyncio.run(shop.register_goods(
|
||||
# name, 30, price, discount, limit_time
|
||||
# ))
|
||||
#
|
||||
# def _register_use(goods_func: Callable):
|
||||
# def _wrapper(**kwargs):
|
||||
# # print(*args)
|
||||
# print(**kwargs)
|
||||
# print(1111111111111111)
|
||||
# use.register_use(name, goods_func, **kwargs)
|
||||
# # func_list.append({"name": name, "func": goods_func, "args": args, "kwargs": kwargs})
|
||||
# return _wrapper
|
||||
#
|
||||
# return _register_use
|
||||
#
|
||||
#
|
||||
# @driver.on_bot_connect
|
||||
# async def do_something(bot: Bot):
|
||||
# for func in func_list:
|
||||
# if asyncio.iscoroutinefunction(func):
|
||||
# await func()
|
||||
# else:
|
||||
# func()
|
||||
from typing import Callable, Union, Tuple
|
||||
from nonebot.plugin import require
|
||||
|
||||
|
||||
use = require("use")
|
||||
shop = require("shop_handle")
|
||||
|
||||
|
||||
class ShopRegister(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ShopRegister, self).__init__(*args, **kwargs)
|
||||
self._data = {}
|
||||
self._flag = True
|
||||
|
||||
def register(
|
||||
self,
|
||||
name: Tuple[str, ...],
|
||||
price: Tuple[float, ...],
|
||||
des: Tuple[str, ...],
|
||||
load_status: Tuple[bool, ...],
|
||||
**kwargs,
|
||||
):
|
||||
def add_register_item(func: Callable):
|
||||
if name in self._data.keys():
|
||||
raise ValueError("该商品已注册,请替换其他名称!")
|
||||
for n, p, d, s in zip(name, price, des, load_status):
|
||||
if s:
|
||||
_temp_kwargs = {}
|
||||
for key, value in kwargs.items():
|
||||
if key.startswith(f"{n}_"):
|
||||
_temp_kwargs[key.split("_", maxsplit=1)[-1]] = value
|
||||
self._data[n] = {
|
||||
"price": p,
|
||||
"des": d,
|
||||
"func": func,
|
||||
"kwargs": _temp_kwargs,
|
||||
}
|
||||
return func
|
||||
|
||||
return lambda func: add_register_item(func)
|
||||
|
||||
async def load_register(self):
|
||||
# 统一进行注册
|
||||
if self._flag:
|
||||
# 只进行一次注册
|
||||
self._flag = False
|
||||
for name in self._data.keys():
|
||||
await shop.register_goods(
|
||||
name, self._data[name]["price"], self._data[name]["des"]
|
||||
)
|
||||
use.register_use(
|
||||
name, self._data[name]["func"], **self._data[name]["kwargs"]
|
||||
)
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
name: Union[str, Tuple[str, ...]],
|
||||
price: Union[float, Tuple[float, ...]],
|
||||
des: Union[str, Tuple[str, ...]],
|
||||
load_status: Union[bool, Tuple[bool, ...]] = True,
|
||||
**kwargs,
|
||||
):
|
||||
_tuple_list = []
|
||||
_current_len = -1
|
||||
for x in [name, price, des, load_status]:
|
||||
if isinstance(x, tuple):
|
||||
if _current_len == -1:
|
||||
_current_len = len(x)
|
||||
if _current_len != len(x):
|
||||
raise ValueError(f"注册商品 {name} 中 name,price,des,load_status 数量不符!")
|
||||
_current_len = _current_len if _current_len > -1 else 1
|
||||
_name = name if isinstance(name, tuple) else tuple(name)
|
||||
_price = (
|
||||
price
|
||||
if isinstance(price, tuple)
|
||||
else tuple([price for _ in range(_current_len)])
|
||||
)
|
||||
_des = (
|
||||
des if isinstance(des, tuple) else tuple([des for _ in range(_current_len)])
|
||||
)
|
||||
_load_status = (
|
||||
load_status
|
||||
if isinstance(load_status, tuple)
|
||||
else tuple([load_status for _ in range(_current_len)])
|
||||
)
|
||||
return self.register(_name, _price, _des, _load_status, **kwargs)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._data[key] = value
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._data[key]
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self._data
|
||||
|
||||
def __str__(self):
|
||||
return str(self._data)
|
||||
|
||||
def keys(self):
|
||||
return self._data.keys()
|
||||
|
||||
def values(self):
|
||||
return self._data.values()
|
||||
|
||||
def items(self):
|
||||
return self._data.items()
|
||||
|
||||
|
||||
shop_register = ShopRegister()
|
||||
|
||||
@@ -11,4 +11,4 @@ class ShopParam(BaseModel):
|
||||
event: MessageEvent
|
||||
num: int # 道具单次使用数量
|
||||
send_success_msg: bool = True # 是否发送使用成功信息
|
||||
_max_num_limit: int = 1 # 单次使用最大次数
|
||||
max_num_limit: int = 1 # 单次使用最大次数
|
||||
|
||||
Reference in New Issue
Block a user