✨ 新增在线查漏补缺功能
This commit is contained in:
+81
-60
@@ -1,102 +1,123 @@
|
||||
import asyncio
|
||||
import inspect
|
||||
import time
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class Signal:
|
||||
def __set_name__(self, owner, name):
|
||||
self.name = name
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
bound = instance.__dict__.get(self.name)
|
||||
if bound is None:
|
||||
bound = _SignalBound()
|
||||
instance.__dict__[self.name] = bound
|
||||
return bound
|
||||
|
||||
|
||||
class _SignalBound:
|
||||
def __init__(self):
|
||||
self._slots = [] # 绑定的槽函数列表
|
||||
self._onceSlots = [] # 只触发一次的槽函数列表
|
||||
self._slots = []
|
||||
self._onceSlots = []
|
||||
|
||||
def connect(self, slot, priority=0):
|
||||
if callable(slot) and not any(s[0] == slot for s in self._slots):
|
||||
self._slots.append((slot, priority))
|
||||
def connect(self, func=None, *, priority=0):
|
||||
if func is None:
|
||||
return lambda f: self.connect(f, priority=priority)
|
||||
if callable(func) and not any(s[0] == func for s in self._slots):
|
||||
self._slots.append((func, priority))
|
||||
self._slots.sort(key=lambda x: -x[1])
|
||||
return func
|
||||
|
||||
def connectOnce(self, slot, priority=0):
|
||||
if callable(slot) and not any(s[0] == slot for s in self._onceSlots):
|
||||
self._onceSlots.append((slot, priority))
|
||||
def connect_once(self, func=None, *, priority=0):
|
||||
if func is None:
|
||||
return lambda f: self.connect_once(f, priority=priority)
|
||||
if callable(func) and not any(s[0] == func for s in self._onceSlots):
|
||||
self._onceSlots.append((func, priority))
|
||||
self._onceSlots.sort(key=lambda x: -x[1])
|
||||
return func
|
||||
|
||||
def disconnect(self, slot):
|
||||
self._slots = [s for s in self._slots if s[0] != slot]
|
||||
self._onceSlots = [s for s in self._onceSlots if s[0] != slot]
|
||||
def disconnect(self, func):
|
||||
self._slots = [s for s in self._slots if s[0] != func]
|
||||
self._onceSlots = [s for s in self._onceSlots if s[0] != func]
|
||||
|
||||
async def emit(self, *args, **kwargs):
|
||||
slots = list(self._slots)
|
||||
onceSlots = list(self._onceSlots)
|
||||
self._onceSlots.clear()
|
||||
|
||||
for slot, _ in slots + onceSlots:
|
||||
startTime = time.time()
|
||||
start = time.time()
|
||||
try:
|
||||
if asyncio.iscoroutinefunction(slot):
|
||||
if inspect.iscoroutinefunction(slot):
|
||||
await slot(*args, **kwargs)
|
||||
else:
|
||||
slot(*args, **kwargs)
|
||||
duration = (time.time() - startTime) * 1000
|
||||
logger.debug(f"事件槽 {slot.__name__} 执行完成,耗时 {duration:.2f} ms")
|
||||
logger.debug(
|
||||
f"【真寻农场】事件槽 {slot.__name__} 执行完成,耗时 {(time.time() - start) * 1000:.2f} ms"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"事件槽 {slot.__name__} 触发异常: {e}")
|
||||
|
||||
|
||||
class FarmEventManager:
|
||||
def __init__(self):
|
||||
self.m_beforePlant = Signal()
|
||||
"""播种前信号
|
||||
m_beforePlant = Signal()
|
||||
"""播种前信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 播种种子名称
|
||||
num (int): 播种数量
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 播种种子名称
|
||||
num (int): 播种数量
|
||||
"""
|
||||
|
||||
self.m_afterPlant = Signal()
|
||||
"""播种后信号 每块地播种都会触发该信号
|
||||
m_afterPlant = Signal()
|
||||
"""播种后信号 每块地播种都会触发该信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 播种种子名称
|
||||
soilIndex (int): 播种地块索引 从1开始
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 播种种子名称
|
||||
soilIndex (int): 播种地块索引 从1开始
|
||||
"""
|
||||
|
||||
self.m_beforeHarvest = Signal()
|
||||
"""收获前信号
|
||||
m_beforeHarvest = Signal()
|
||||
"""收获前信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
"""
|
||||
|
||||
self.m_afterHarvest = Signal()
|
||||
"""收获后信号 每块地收获都会触发该信号
|
||||
m_afterHarvest = Signal()
|
||||
"""收获后信号 每块地收获都会触发该信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 收获作物名称
|
||||
num (int): 收获数量
|
||||
soilIndex (int): 收获地块索引 从1开始
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 收获作物名称
|
||||
num (int): 收获数量
|
||||
soilIndex (int): 收获地块索引 从1开始
|
||||
"""
|
||||
|
||||
self.m_beforeEradicate = Signal()
|
||||
"""铲除前信号
|
||||
m_beforeEradicate = Signal()
|
||||
"""铲除前信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
"""
|
||||
|
||||
self.m_afterEradicate = Signal()
|
||||
"""铲除后信号 每块地铲除都会触发该信号
|
||||
m_afterEradicate = Signal()
|
||||
"""铲除后信号 每块地铲除都会触发该信号
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
soilIndex (index): 铲除地块索引 从1开始
|
||||
"""
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
soilIndex (index): 铲除地块索引 从1开始
|
||||
"""
|
||||
|
||||
self.m_beforeExpand = Signal()
|
||||
self.m_afterExpand = Signal()
|
||||
self.m_beforeSteal = Signal()
|
||||
self.m_afterSteal = Signal()
|
||||
m_beforeExpand = Signal()
|
||||
m_afterExpand = Signal()
|
||||
m_beforeSteal = Signal()
|
||||
m_afterSteal = Signal()
|
||||
|
||||
m_dit = Signal()
|
||||
|
||||
|
||||
g_pEventManager = FarmEventManager()
|
||||
|
||||
Reference in New Issue
Block a user