feat: add configurable battle loss cooldown in Web 2.0.13
Web image / image (push) Waiting to run

This commit is contained in:
AttilaNet
2026-11-17 00:17:52 +08:00
parent 598bdf5c1c
commit b976151263
23 changed files with 297 additions and 43 deletions
+19 -7
View File
@@ -45,8 +45,9 @@ import uuid
from .game_data import ProgressionData, load_progression_data
from .model import Planner, seconds
from .quests import commission_priority, commission_selection, commission_reason_kind, indexed, stage_has_level
from .quests import commission_priority, commission_selection, commission_reason_kind, commission_quota_reason, indexed, stage_has_level
from .procurement import ResourceProcurement, resource_kind
from .battle_retry import loss_retry_reason
CATEGORIES = {1: ("mainline", "主线"), 2: ("sidequest", "支线"),
@@ -96,6 +97,7 @@ class ProgressionPolicy:
allow_monsters: bool = False
item_reserves: dict[str, int] = field(default_factory=dict)
stop_after_loss: int = 1
loss_retry_hours: int = 3
repair_chain: bool = True
battle_mode: str = "original"
auto_farm: bool = True
@@ -105,13 +107,15 @@ class ProgressionPolicy:
"allow_sweep", "allow_items", "allow_monsters", "repair_chain", "auto_farm"):
if not isinstance(getattr(self, name), bool):
raise ValueError(f"{name} must be a boolean")
for name in ("reserve_ti", "max_money", "stop_after_loss"):
for name in ("reserve_ti", "max_money", "stop_after_loss", "loss_retry_hours"):
value = getattr(self, name)
if type(value) is not int or value < 0:
raise ValueError(f"{name} must be a nonnegative integer")
setattr(self, name, _integer(value))
if self.stop_after_loss < 1:
raise ValueError("stop_after_loss must be at least 1")
if not 1 <= self.loss_retry_hours <= 168:
raise ValueError("loss_retry_hours must be between 1 and 168")
if (not isinstance(self.team, str) or not 1 <= len(self.team) <= 64
or self.team.strip() != self.team or not self.team.isprintable()):
raise ValueError("team must be a nonempty printable name of at most 64 characters")
@@ -203,11 +207,15 @@ class ProgressionPlanner:
"position": str(snapshot.raw.get("position") or ""), "tasks": {}, "received": {},
"unlocked": [], "finished": [], "stage_unlocks": [], "city_unlocks": [],
"stage_passes": [], "stage_levels": [], "stage_locks": [],
"pending": None, "losses": 0, "applied": [],
"pending": None, "losses": 0, "last_loss_at": None, "applied": [],
"commissions": {"day": None, "remaining": None, "offers": None, "accepted": [], "expired_ids": [], "finished": []},
})
return scope, account
def battle_retry_reason(self, snapshot, policy):
_, account = self._account(snapshot)
return loss_retry_reason(account, snapshot.now, policy)
def _tasks(self, snapshot, account):
received = {key: task for key, task in account["received"].items()
if not account["tasks"].get(f"{_integer(task.get('id'))}:{key}", {}).get("removed")}
@@ -626,8 +634,8 @@ class ProgressionPlanner:
"""Reject an unusable battle before navigation or its before-story."""
if _integer(snapshot.raw["master"].get("ti")) < policy.reserve_ti:
return "当前体力低于保留体力"
if account["losses"] >= policy.stop_after_loss:
return "达到任务战败停止次数"
if reason := loss_retry_reason(account, snapshot.now, policy):
return reason
if kind == "battle" and not step.get("battle"):
return "任务战斗配置缺失"
if kind == "passStage":
@@ -742,7 +750,7 @@ class ProgressionPlanner:
else:
return []
scope, account = self._account(snapshot)
if account["losses"] >= policy.stop_after_loss:
if loss_retry_reason(account, snapshot.now, policy):
return []
ranked = []
seen = set()
@@ -1008,7 +1016,7 @@ class ProgressionPlanner:
city_info = self.city_diagnostic(city, snapshot)
supply = None
reason = ("城市编号无效" if city <= 0 else
"当日接取次数已用完(剩余0次),等待次日刷新" if remaining <= 0 else
commission_quota_reason(snapshot) if remaining <= 0 else
f"策略限定城市 {quest_policy.city}" if quest_policy.city and city != quest_policy.city else
city_info["city_reason"] if not city_info["city_unlocked"] else
"委托列表已跨日,等待刷新确认" if recorded is not None and recorded != today and account["commissions"]["day"] != today else
@@ -1335,6 +1343,7 @@ class ProgressionPlanner:
record["next_value"] = response.get("taskStepValue")
if intent.kind == "progression_battle" and not _from_snapshot:
account["losses"] = 0
account["last_loss_at"] = None
if intent.battle_kind == "passStage":
# Exact TaskStartPanel.step2WinHandler -> passLocalStage
# effects, only after the real step settlement succeeded.
@@ -1492,6 +1501,7 @@ class ProgressionPlanner:
if intent.account_key != scope or intent.kind != "progression_battle":
raise ValueError("Battle intent/account mismatch")
account["losses"] += 1
account["last_loss_at"] = snapshot.now
if account["pending"] and account["pending"]["token"] == intent.token:
account["pending"] = None
@@ -1518,10 +1528,12 @@ class ProgressionPlanner:
for scope, account in restored.items():
if not re.fullmatch(r"[0-9a-f]{64}", scope) or not isinstance(account, dict):
raise ValueError("Invalid local progression account")
account.setdefault("last_loss_at", None)
for key in ("stage_passes", "stage_levels", "stage_locks"):
account.setdefault(key, [])
account.setdefault("commissions", {"day": None, "remaining": None, "offers": None, "accepted": [], "expired_ids": [], "finished": []})
required = {"position", "tasks", "received", "unlocked", "finished", "stage_unlocks", "city_unlocks", "pending", "losses", "applied", "stage_passes", "stage_levels", "stage_locks", "commissions"}
required.add("last_loss_at")
if set(account) != required or not isinstance(account["tasks"], dict) or not isinstance(account["received"], dict):
raise ValueError("Invalid local progression state fields")
if account["pending"]: