69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
# coding=utf-8
|
|
from ..ToolsConfig import *
|
|
|
|
|
|
class ResultTimer(object):
|
|
"""
|
|
结果定时器, 在返回True时结束循环
|
|
"""
|
|
|
|
def __init__(self, func, delay=0.0, maxTimes=10, *args, **kwargs):
|
|
"""
|
|
:param func: 执行函数
|
|
:param delay: 执行延迟
|
|
:param maxTimes: 最大执行次数
|
|
"""
|
|
self.func = func
|
|
self.delay = delay
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
self.maxTimes = maxTimes
|
|
self.runTimes = 0
|
|
self.gameComp = serverCompFactory.CreateGame(LevelId) or clientCompFactory.CreateGame(LevelId)
|
|
|
|
def __timerFunc__(self):
|
|
result = self.func(*self.args, **self.kwargs)
|
|
if not result and self.runTimes < self.maxTimes: self.Start()
|
|
|
|
def Start(self):
|
|
"""
|
|
开始执行定时器
|
|
:return:
|
|
"""
|
|
self.gameComp.AddTimer(self.delay, self.__timerFunc__)
|
|
self.runTimes += 1
|
|
|
|
|
|
class TimesTimer(object):
|
|
"""
|
|
次数定时器
|
|
"""
|
|
|
|
def __init__(self, times, func, delay=0.0, *args, **kwargs):
|
|
"""
|
|
:param times: 循环次数
|
|
:param func: 执行函数
|
|
:param delay: 执行延迟
|
|
"""
|
|
self.func = func
|
|
self.delay = delay
|
|
self.times = times
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
self.gameComp = serverCompFactory.CreateGame(LevelId) or clientCompFactory.CreateGame(LevelId)
|
|
|
|
def __func(self, *args, **kwargs):
|
|
self.times -= 1
|
|
self.func(*args, **kwargs)
|
|
if self.times >= 0:
|
|
self.gameComp.AddTimer(self.delay, self.__func, *self.args, **self.kwargs)
|
|
|
|
def Start(self):
|
|
"""
|
|
开始执行定时器
|
|
:return:
|
|
"""
|
|
if self.times > 0:
|
|
self.times -= 1
|
|
self.gameComp.AddTimer(self.delay, self.__func, *self.args, **self.kwargs)
|