feat: add configurable delayed password relogin

This commit is contained in:
AttilaNet
2026-11-11 11:48:52 +08:00
parent 4a1b06ee9d
commit 5b1ef25048
12 changed files with 1068 additions and 24 deletions
+31 -1
View File
@@ -13,8 +13,9 @@ FAST_KINDS = ("hatch", "gift", "explore", "breakthrough")
class AccountSchedule:
"""Deadlines belong to one worker and are inspected only on its thread."""
def __init__(self, *, clock=time.monotonic, uniform=random.uniform):
def __init__(self, *, clock=time.monotonic, uniform=random.uniform, wall_clock=time.time):
self.clock, self.uniform = clock, uniform
self.wall_clock = wall_clock
self.next_poll = math.inf
self.next_fast = {kind: math.inf for kind in FAST_KINDS}
self.monitoring = False
@@ -22,6 +23,31 @@ class AccountSchedule:
self.game_paused = False
self.retry_deadline = None
self.retry_attempt = 0
self.disconnected_at = None
self.disconnected_wall_at = None
self.auto_relogin_failed_at = None
def disconnected(self):
# Read retries and settings changes must not move the first failure.
if self.disconnected_at is None:
self.disconnected_at = self.clock()
self.disconnected_wall_at = self.wall_clock()
def clear_disconnection(self):
self.disconnected_at = self.disconnected_wall_at = None
self.auto_relogin_failed_at = None
def auto_relogin_deadline(self, config):
preference = config.get("auto_relogin", {})
if not preference.get("enabled") or self.disconnected_at is None:
return None
origin = max(self.disconnected_at, self.auto_relogin_failed_at
if self.auto_relogin_failed_at is not None else self.disconnected_at)
return origin + preference["delay_minutes"] * 60
def auto_relogin_failed(self):
# Back off from completion, including time spent authenticating.
self.auto_relogin_failed_at = self.clock()
def game_failed(self, *, retry=False):
self.game_paused = True
@@ -37,6 +63,7 @@ class AccountSchedule:
self.game_paused = False
self.retry_deadline = None
self.retry_attempt = 0
self.clear_disconnection()
def monitor(self, enabled=True):
self.monitoring = bool(enabled)
@@ -87,6 +114,9 @@ class AccountSchedule:
def due(self, engine, config, running):
now = self.clock()
deadline = self.auto_relogin_deadline(config)
if deadline is not None and now >= deadline:
return "auto_relogin"
if self.game_paused:
return "retry" if self.retry_deadline is not None and now >= self.retry_deadline else None