StarRailCopilot/module/base/timer.py

135 lines
3.4 KiB
Python
Raw Normal View History

2020-03-28 17:22:46 +00:00
import time
2020-04-06 11:01:22 +00:00
from datetime import datetime, timedelta
2020-03-28 17:22:46 +00:00
from functools import wraps
from module.logger import logger
def timer(function):
@wraps(function)
def function_timer(*args, **kwargs):
t0 = time.time()
result = function(*args, **kwargs)
t1 = time.time()
print('%s: %s s' % (function.__name__, str(round(t1 - t0, 10))))
return result
return function_timer
2020-04-06 11:01:22 +00:00
def future_time(string):
"""
Args:
string (str): Such as 14:59.
Returns:
datetime.datetime: Time with given hour, minute in the future.
2020-04-06 11:01:22 +00:00
"""
hour, minute = [int(x) for x in string.split(':')]
future = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
future = future + timedelta(days=1) if future < datetime.now() else future
return future
def past_time(string):
"""
Args:
string (str): Such as 14:59.
Returns:
datetime.datetime: Time with given hour, minute in the past.
"""
hour, minute = [int(x) for x in string.split(':')]
past = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
past = past - timedelta(days=1) if past > datetime.now() else past
return past
def future_time_range(string):
"""
Args:
string (str): Such as 23:30-06:30.
Returns:
tuple(datetime.datetime): (time start, time end).
"""
start, end = [future_time(s) for s in string.split('-')]
if start > end:
start = start - timedelta(days=1)
return start, end
def time_range_active(time_range):
"""
Args:
time_range(tuple(datetime.datetime)): (time start, time end).
Returns:
bool:
"""
return time_range[0] < datetime.now() < time_range[1]
2020-03-28 17:22:46 +00:00
class Timer:
def __init__(self, limit, count=0):
"""
Args:
limit (int, float): Timer limit
count (int): Timer reach confirm count. Default to 0.
When using a structure like this, must set a count.
Otherwise it goes wrong, if screenshot time cost greater than limit.
if self.appear(MAIN_CHECK):
if confirm_timer.reached():
pass
else:
confirm_timer.reset()
2020-06-02 16:14:30 +00:00
Also, It's a good idea to set `count`, to make alas run more stable on slow computers.
Expected speed is 0.35 second / screenshot.
"""
2020-03-28 17:22:46 +00:00
self.limit = limit
self.count = count
2020-03-28 17:22:46 +00:00
self._current = 0
self._reach_count = count
2020-03-28 17:22:46 +00:00
def start(self):
if not self.started():
self._current = time.time()
self._reach_count = 0
return self
2020-03-28 17:22:46 +00:00
def started(self):
return bool(self._current)
def current(self):
"""
Returns:
float
"""
return time.time() - self._current
def reached(self):
"""
Returns:
bool
"""
self._reach_count += 1
return time.time() - self._current > self.limit and self._reach_count > self.count
2020-03-28 17:22:46 +00:00
def reset(self):
self._current = time.time()
self._reach_count = 0
2020-03-28 17:22:46 +00:00
def wait(self):
"""
Wait until timer reached.
"""
diff = self._current + self.limit - time.time()
if diff > 0:
time.sleep(diff)
2020-03-28 17:22:46 +00:00
def show(self):
logger.info('%s s' % str(self.current()))