Use main-window menus, cache maps, and handle settled sweep events

This commit is contained in:
AttilaNet
2026-11-08 11:10:07 +08:00
parent 2498f1f98f
commit dba667f21d
13 changed files with 578 additions and 33 deletions
+76 -1
View File
@@ -7,6 +7,13 @@ from .daily import day
from .maps import stages
def settled_reward_event(response):
event = response.get("stageEvent")
return (response.get("triggerEvent") in (True, 1) and isinstance(event, dict)
and event.get("resCode", 0) in (0, "0") and event.get("hideStageId") is None
and isinstance(event.get("result"), dict))
@dataclass
class SweepPolicy:
enabled: bool = False
@@ -20,6 +27,60 @@ class SweepRunner:
self.catalog = catalog
self.path = Path(path) if path else None
self.state = json.loads(self.path.read_text(encoding="utf-8")) if self.path and self.path.exists() else {"counts": {}, "pending": None}
self.last_outcome = ""
self.recovery_checked = False
self.recovered_event = False
def recover_reward_event(self, snapshot, trace_path):
"""Repair only old reward-event blocks with a role-matched log reply.
This never replays a start or settlement request. Missing/ambiguous
evidence leaves the pending record intact.
"""
pending = self.state.get("pending")
if (self.recovery_checked or not trace_path or not pending
or pending.get("phase") != "遇到事件或响应缺少事件状态,请在游戏内处理"):
return False
self.recovery_checked = True
role, requests, last_reply = None, {}, None
owner = snapshot.raw["master"]["id"]
path = Path(trace_path)
try:
for file in (path.with_name("network_debug.previous.jsonl"), path):
if not file.exists():
continue
with file.open(encoding="utf-8") as stream:
for line in stream:
try:
event = json.loads(line)
except ValueError:
continue
body = event.get("body")
if not isinstance(body, dict):
continue
if event.get("operation") == "读取角色" and event.get("kind") == "response":
role = (body.get("master") or {}).get("id")
if event.get("operation") != "stageService/stageStart":
continue
identifier = event.get("request_id")
if not isinstance(identifier, str) or not identifier:
continue
if event.get("kind") == "request":
matched = (str(role) == str(owner) and str(body.get("stageTypeId")) == pending["stage"]
and str(body.get("stageLevel")) == str(pending["level"]))
requests[identifier] = matched
if matched:
last_reply = None
elif event.get("kind") == "response" and requests.get(identifier):
last_reply = body if event.get("http_status") == 200 and body.get("resCode") in (0, "0") else None
except (OSError, ValueError, TypeError, AttributeError):
return False
if last_reply and settled_reward_event(last_reply):
self.state["pending"] = None
self.save()
self.recovered_event = True
return True
return False
def save(self):
if self.path:
@@ -78,6 +139,7 @@ class SweepRunner:
return None
def run(self, client, snapshot, stage, policy):
self.last_outcome = ""
rule = policy.rules.get(stage.stage_id) or {}
if not rule.get("enabled") or str(rule.get("level")) != stage.level or self.used(snapshot, stage) >= int(rule.get("limit") or 0):
raise ValueError("此关卡未勾选或已达本日次数上限")
@@ -89,9 +151,21 @@ class SweepRunner:
self.state["pending"] = {"stage": stage.stage_id, "level": stage.level, "phase": "开始请求待确认"}
self.save() # Reserve before the first non-retryable write, including timeouts.
started = client.call("stageService/stageStart", {"stageTypeId": stage.stage_id, "stageLevel": stage.level})
if settled_reward_event(started):
# StageCenter.startStageHandler applies stageEvent.result directly;
# StageStartPanel shows it and returns, without battleEnd.
self.state["pending"] = None
self.save()
self.last_outcome = "event"
return started
flag = started.get("triggerEvent")
if flag is not False and not (type(flag) is int and flag == 0):
self.state["pending"]["phase"] = "遇到事件或响应缺少事件状态,请在游戏内处理"
event = started.get("stageEvent")
if isinstance(event, dict) and event.get("hideStageId") is not None:
phase = "遭遇隐藏关卡,请在游戏内手动战斗"
else:
phase = "事件结果或开始状态尚未确认,请在游戏内检查"
self.state["pending"]["phase"] = phase
self.save()
raise ValueError(self.state["pending"]["phase"])
# Exactly the official doSaodangBattle data; only for its eligible stages.
@@ -104,4 +178,5 @@ class SweepRunner:
self.state["pending"] = None
self.save()
client.call("battleService/recordBattleResult", {"stageId": stage.stage_id, "cityId": stage.stage_id[1:2], "result": 1})
self.last_outcome = "sweep"
return result