Schedule hatch filling independently every 1-2 seconds

This commit is contained in:
AttilaNet
2026-11-08 10:16:06 +08:00
parent e191226ca0
commit aa27b1673d
5 changed files with 225 additions and 12 deletions
+36 -6
View File
@@ -149,6 +149,11 @@ class Window(ActivityPages, W.QMainWindow):
self.latest_render = None
self.prepared_views = None
self.next_poll = 0
self.next_hatch = float("inf")
self.hatch_timer = C.QTimer(self)
self.hatch_timer.setSingleShot(True)
self.hatch_timer.setTimerType(C.Qt.TimerType.PreciseTimer)
self.hatch_timer.timeout.connect(self.schedule)
self.client_pending = None
self.password_login_result = None
self.egg_rules = {}
@@ -163,7 +168,7 @@ class Window(ActivityPages, W.QMainWindow):
self.summary = W.QLabel()
self.data_summary = W.QLabel("等待读取培育室、背包和牧场状态")
self.interval = combo([("随机30秒", 30), ("随机60秒", 60), ("随机2分钟", 120), ("随机5分钟", 300)])
self.interval.setToolTip("每轮完成后,从0到所选上限随机等待;请求串行执行。孵化/成长到期会单独同步。")
self.interval.setToolTip("整体刷新完成后,从0到所选上限随机等待。空坑装填独立随机1~2秒;孵化/成长到期会单独同步。")
self.start_monitor = W.QPushButton("开始监控")
self.start_monitor.setCheckable(True)
self.start_monitor.toggled.connect(self.toggle_monitor)
@@ -519,7 +524,7 @@ class Window(ActivityPages, W.QMainWindow):
self.draw_status = W.QLabel("最近抽蛋:尚无记录")
self.draw_status.setWordWrap(True)
settings_layout.addWidget(self.draw_status)
note = W.QLabel("先在“背包魔物蛋”勾选并配置规则,再开始自动孵蛋。按手动优先级装填,成熟后按各行选择派遣、喂养或保留。\n锁定、星级、资质和等级上限保护仍生效;只处理本次培育的魔物。")
note = W.QLabel("先在“背包魔物蛋”勾选并配置规则,再开始自动孵蛋。空坑按手动优先级独立随机1~2秒装填,不受整体刷新间隔影响。\n成熟后按各行选择派遣、喂养或保留;锁定、星级、资质和等级上限保护仍生效,只处理本次培育的魔物。")
note.setWordWrap(True)
settings_layout.addWidget(note)
settings_layout.addStretch()
@@ -550,6 +555,8 @@ class Window(ActivityPages, W.QMainWindow):
self.daily_buy_money.valueChanged.connect(self.update_workflow_status)
for widget in (self.strategy, self.protect_quality, self.hatch_max_star):
widget.currentIndexChanged.connect(self.update_workflow_status)
self.fill.toggled.connect(self.arm_hatching)
self.ranch_enabled.toggled.connect(self.arm_hatching)
self.protect_star.valueChanged.connect(self.update_workflow_status)
self.update_workflow_status()
@@ -557,6 +564,7 @@ class Window(ActivityPages, W.QMainWindow):
if not value:
self.engine.auto = False
self.pending_execute = False
self.arm_hatching()
self.trace.emit("info", "自动执行状态", scope="local", enabled=value, monitoring=self.monitoring)
self.update_workflow_status()
@@ -1077,7 +1085,7 @@ class Window(ActivityPages, W.QMainWindow):
def background():
started = time.perf_counter()
value = function()
if kind in ("poll", "monster_lock", "manual_monsters", "expansion", "growth_sync", "daily_claim", "connected"):
if kind in ("poll", "hatch", "monster_lock", "manual_monsters", "expansion", "growth_sync", "daily_claim", "connected"):
snapshot = value[1] if kind == "connected" else value[0]
managed = set() if kind == "connected" else set(engine.planner.managed)
preparing = time.perf_counter()
@@ -1124,7 +1132,7 @@ class Window(ActivityPages, W.QMainWindow):
note="worker总耗时包含网络;大列表在后续短批次更新")
def apply_result(self, kind, value):
if kind in ("poll", "monster_lock", "manual_monsters", "expansion", "growth_sync", "daily_claim"):
if kind in ("poll", "hatch", "monster_lock", "manual_monsters", "expansion", "growth_sync", "daily_claim"):
snapshot, plans, message = value
self.render(snapshot, plans)
self.report(message)
@@ -1133,6 +1141,7 @@ class Window(ActivityPages, W.QMainWindow):
self.next_poll = time.monotonic() + self.poll_delay()
if "策略暂不可用" in message:
self.execute.setChecked(False)
self.arm_hatching()
elif kind == "heartbeat":
self.report(value)
elif kind == "connected":
@@ -1256,7 +1265,7 @@ class Window(ActivityPages, W.QMainWindow):
allow_items=self.quest_items.isChecked(), allow_monsters=self.quest_monsters.isChecked(), max_money=self.quest_money.value(),
battle_tasks=self.quest_battles.isChecked(), item_reserves=reserves)
def refresh(self, checked=False, *, automated=False):
def refresh(self, checked=False, *, automated=False, hatch_only=False):
if self.busy or self.closed:
return
try:
@@ -1265,7 +1274,10 @@ class Window(ActivityPages, W.QMainWindow):
self.engine.daily_policy = self.read_daily_policy()
self.engine.sweep_policy = self.read_sweep_policy()
self.engine.auto = self.execute.isChecked() and automated and self.monitoring
self.submit("poll", self.engine.poll)
if hatch_only:
self.hatch_timer.stop()
self.next_hatch = float("inf")
self.submit("hatch" if hatch_only else "poll", lambda: self.engine.poll(hatch_only=hatch_only, defer_hatch=not hatch_only))
except ValueError:
if automated:
self.error("请核对策略中的魔物编号和保留数量")
@@ -1292,6 +1304,20 @@ class Window(ActivityPages, W.QMainWindow):
self.next_poll = time.monotonic() + self.poll_delay()
self.save_config(quiet=True)
def arm_hatching(self):
if (self.engine.pending_hatches and self.monitoring and self.execute.isChecked()
and self.ranch_enabled.isChecked() and self.fill.isChecked() and not self.closed):
if self.next_hatch < float("inf"):
if not self.hatch_timer.isActive():
self.hatch_timer.start(max(0, round((self.next_hatch - time.monotonic()) * 1000)))
return
delay = random.uniform(1, 2)
self.next_hatch = time.monotonic() + delay
self.hatch_timer.start(round(delay * 1000))
else:
self.hatch_timer.stop()
self.next_hatch = float("inf")
def schedule(self):
if self.busy or self.closed:
return
@@ -1300,6 +1326,9 @@ class Window(ActivityPages, W.QMainWindow):
self.submit("heartbeat", self.engine.keep_alive)
elif not self.engine.demo and self.engine.heartbeat_enabled and self.engine.growth_sync_due():
self.submit("growth_sync", self.engine.sync_growth)
elif (self.monitoring and self.execute.isChecked() and self.fill.isChecked()
and self.ranch_enabled.isChecked() and now >= self.next_hatch):
self.refresh(automated=True, hatch_only=True)
elif self.monitoring and now >= self.next_poll:
self.refresh(automated=True)
@@ -1705,6 +1734,7 @@ class Window(ActivityPages, W.QMainWindow):
def closeEvent(self, event):
self.closed = True
self.timer.stop()
self.hatch_timer.stop()
if hasattr(self, "activity_save_timer"):
self.activity_save_timer.stop()
if hasattr(self.eggs, "row_batch"):