Automatically reconcile and resume interrupted task battle clock waits

This commit is contained in:
AttilaNet
2026-11-08 20:21:35 +08:00
parent 001ed9d36d
commit 0ec95bbe65
6 changed files with 206 additions and 6 deletions
+76 -2
View File
@@ -436,9 +436,10 @@ class BattleOrchestrator:
if duration is None:
self._unknown(record, "模型未提供有效战斗时钟,未提交结算")
return False
self.journal.update(record, "waiting_for_battle_clock", battle_clock_seconds=duration,
message="等待已计算的战斗时钟;可以停止,停止后保留待核对记录")
deadline = started + duration
self.journal.update(record, "waiting_for_battle_clock", battle_clock_seconds=duration,
battle_ready_at=time.time() + max(0, deadline - self.clock()), resume_clock_ready=True,
message="等待已计算的战斗时钟;停止后保留结果,重新开启策略后自动续交")
while not cancel.is_set():
remaining = deadline - self.clock()
if remaining <= 0:
@@ -448,6 +449,79 @@ class BattleOrchestrator:
self._unknown(record, "等待战斗时钟期间已取消,开战记录保留待核对")
return False
@staticmethod
def resumable_task_clock(record):
return bool(record and record.get("kind") == "progression_battle" and record.get("task_id")
and record.get("phase") in ("unknown", "waiting_for_battle_clock")
and record.get("path") in ("", "battleService/battleStart")
and not record.get("settlement_confirmed") and not record.get("settlement_dispatched")
and (record.get("resume_clock_ready") is True or
record.get("message") == "等待战斗时钟期间已取消,开战记录保留待核对")
and record.get("execution_mode") == "experimental" and record.get("execution_status") == "completed"
and record.get("experimental_settlement_eligible") is True and record.get("terminal") is True
and record.get("approximate") is True and record.get("faithful") is False
and not record.get("anomalies") and not any(row.get("blocking") is True
for row in record.get("diagnostics") or [] if isinstance(row, dict))
and isinstance(record.get("battle_data"), dict) and record["battle_data"].get("win") is True)
def resume_task_clock(self, client, snapshot, intent, policy, *, cancel):
"""Continue a saved result only when no settlement was ever dispatched."""
with self.journal.lock:
record = self.journal.pending(snapshot)
if not policy.enabled or cancel.is_set() or not self.resumable_task_clock(record):
return ""
if str(record.get("role_id")) != str(role_id(snapshot)):
return ""
if any(str(record.get(key)) != str(value(intent, key)) for key in
("kind", "task_id", "task_type_id", "step_number")):
return ""
task = next((row for row in snapshot.raw.get("currentTasks", [])
if str(row.get("id")) == str(record["task_id"])
and str(row.get("typeId")) == str(record["task_type_id"])), None)
if not task or int(task.get("curStep", 1)) != int(record["step_number"]):
return ""
expected = {"battleType": "task", "stageId": None, "stageLevel": None,
"taskTypeId": str(record["task_type_id"]), "taskStep": int(record["step_number"]) - 1}
data = record["battle_data"]
position = data.get("battlePosition")
if not isinstance(position, dict) or {key: val for key, val in position.items() if val is not None} != {
key: val for key, val in expected.items() if val is not None}:
return ""
ready = record.get("battle_ready_at")
if ready is None:
duration = record.get("battle_clock_seconds")
stamp = record.get("updated_at")
if any(type(number) not in (int, float) or not math.isfinite(number) or number < 0
for number in (duration, stamp)):
return ""
ready = stamp + duration # Older builds: conservatively wait a full duration after interruption.
self.journal.update(record, record["phase"], battle_ready_at=ready)
if type(ready) not in (int, float) or not math.isfinite(ready):
return ""
if time.time() < ready:
return "已核对保存的战果;尚未到结算时间,2分钟内自动复查"
path = "taskService/taskCompleteStep"
payload = {"taskId": int(record["task_id"]), "taskTypeId": str(record["task_type_id"]),
"removeIds": None, "battleData": deepcopy(data)}
self.journal.update(record, "settlement_pending", path=path, settlement_dispatched=True)
try:
response = self._call(client, path, payload, cancel=cancel)
self.journal.update(record, "settlement_acknowledged", response=gameplay_response(response),
settlement_confirmed=True, reward=response["stageReward"] if response.get("stageReward") is not None else response.get("reward"))
if self.observer:
self.observer("settled", intent, response)
else:
self.journal.checkpoint(snapshot, intent, response)
self.journal.finish(snapshot, record, "win", "已自动续交保存的战果;未重新开战或重新模拟")
return record["message"]
except CancelledBeforeDispatch:
self.journal.update(record, "waiting_for_battle_clock", path="battleService/battleStart",
settlement_dispatched=False, resume_clock_ready=True)
return "策略已停止,保存的战果将在重新开启后续交"
except Exception as error:
self._unknown(record, "续交请求结果未确认,将自动查询状态,不重复发送", error_type=type(error).__name__)
raise
def run(self, client, snapshot, intent, policy, *, cancel=None, ranch=None):
if not self.journal.lock.acquire(blocking=False):
return "同账号已有任务 / 战斗正在执行"