Add hatch retention, automatic breakthroughs and exploration
This commit is contained in:
@@ -7,6 +7,7 @@ from .daily import DailyPolicy
|
||||
from .quests import QuestPolicy
|
||||
from .sweeping import SweepPolicy
|
||||
from .progression import ProgressionPolicy
|
||||
from .exploration import ExplorePolicy
|
||||
|
||||
FORMAT = "ranch-helper-policies"
|
||||
VERSION = 1
|
||||
@@ -28,6 +29,8 @@ def checked_policy(cls, data):
|
||||
raise ValueError("策略字段类型错误:" + key)
|
||||
try:
|
||||
return cls(**data)
|
||||
|
||||
|
||||
except (AttributeError, TypeError, KeyError) as error:
|
||||
raise ValueError("策略字段格式错误") from error
|
||||
|
||||
@@ -58,7 +61,7 @@ def portable_policy(policy, snapshot=None):
|
||||
value["feed_targets"] = {str(value["feed_target"]): 1}
|
||||
value["feed_target"] = value["room_id"] = 0
|
||||
targets = {}
|
||||
for kind, field in (("feed", "feed_targets"), ("gift", "gift_targets")):
|
||||
for kind, field in (("feed", "feed_targets"), ("gift", "gift_targets"), ("breakthrough", "breakthrough_targets")):
|
||||
targets[kind] = []
|
||||
for key, priority in sorted(value.pop(field).items(), key=lambda pair: pair[1]):
|
||||
mon = snapshot.monsters.get(int(key)) if snapshot else None
|
||||
@@ -73,8 +76,8 @@ def import_policy(value, snapshot=None):
|
||||
policy = checked_policy(Policy, value["policy"])
|
||||
policy.feed_target = policy.room_id = 0
|
||||
if snapshot:
|
||||
used = {"feed": set(), "gift": set()}
|
||||
for kind, field in (("feed", "feed_targets"), ("gift", "gift_targets")):
|
||||
used = {"feed": set(), "gift": set(), "breakthrough": set()}
|
||||
for kind, field in (("feed", "feed_targets"), ("gift", "gift_targets"), ("breakthrough", "breakthrough_targets")):
|
||||
result = {}
|
||||
targets = value.get("targetSpecies", {})
|
||||
if not isinstance(targets, dict) or not isinstance(targets.get(kind, []), list):
|
||||
@@ -90,7 +93,7 @@ def import_policy(value, snapshot=None):
|
||||
break
|
||||
setattr(policy, field, result)
|
||||
else:
|
||||
policy.feed_targets, policy.gift_targets = {}, {}
|
||||
policy.feed_targets, policy.gift_targets, policy.breakthrough_targets = {}, {}, {}
|
||||
return policy
|
||||
|
||||
|
||||
@@ -109,9 +112,60 @@ def portable_config(config, snapshot=None):
|
||||
value["interval"] = config.get("interval", 30)
|
||||
value["overrides"] = copy.deepcopy(config.get("overrides", {}))
|
||||
value["yield_stars"] = list(config.get("yield_stars", [1, 2, 3, 4, 5]))
|
||||
value["explore_policy"] = portable_explore_policy(ExplorePolicy.from_dict(config.get("explore_policy", {})), snapshot)
|
||||
return value
|
||||
|
||||
|
||||
def portable_explore_policy(policy, snapshot=None):
|
||||
"""Team and monster IDs are account-local; preserve positions, not identities."""
|
||||
value = policy.to_dict()
|
||||
teams = [team for team in (snapshot.raw.get("exploreTeams", []) if snapshot else [])
|
||||
if isinstance(team, dict) and str(team.get("id")) in value["teams"]]
|
||||
teams.sort(key=lambda team: int(team["id"]))
|
||||
if snapshot is None:
|
||||
teams = list(value["teams"])
|
||||
result = []
|
||||
for position, team in enumerate(teams):
|
||||
key = str(team.get("id")) if isinstance(team, dict) else str(team)
|
||||
rule = value["teams"][key]
|
||||
species = []
|
||||
if snapshot:
|
||||
for mid in rule["monsters"]:
|
||||
mon = snapshot.monsters.get(int(mid))
|
||||
species.append(str(mon.get("typeId")) if mon and mon.get("process") == 3 else "")
|
||||
result.append({"position": position, "enabled": rule["enabled"], "area": rule["area"], "memberSpecies": species})
|
||||
return {"enabled": False, "reserve_ti": value["reserve_ti"], "teams": result}
|
||||
|
||||
|
||||
def import_explore_policy(value, snapshot=None):
|
||||
if not isinstance(value, dict) or set(value) - {"enabled", "reserve_ti", "teams"} or not isinstance(value.get("teams"), list):
|
||||
raise ValueError("自动探险策略格式错误")
|
||||
teams = [team for team in (snapshot.raw.get("exploreTeams", []) if snapshot else []) if isinstance(team, dict)]
|
||||
teams.sort(key=lambda team: int(team["id"]))
|
||||
used = set()
|
||||
result = {}
|
||||
for index, rule in enumerate(value["teams"]):
|
||||
if (not isinstance(rule, dict) or set(rule) != {"position", "enabled", "area", "memberSpecies"}
|
||||
or type(rule["position"]) is not int or rule["position"] != index):
|
||||
raise ValueError("自动探险队伍顺序配置无效")
|
||||
if index >= len(teams):
|
||||
continue
|
||||
members = []
|
||||
if rule["memberSpecies"]:
|
||||
if len(rule["memberSpecies"]) != 3:
|
||||
raise ValueError("自动探险手动队员须恰好三只")
|
||||
for type_id in rule["memberSpecies"]:
|
||||
match = next((mon for mon in sorted(snapshot.raw["monsters"], key=lambda row: int(row["id"]))
|
||||
if mon.get("process") == 3 and str(mon.get("typeId")) == str(type_id) and int(mon["id"]) not in used), None)
|
||||
if match is None:
|
||||
raise ValueError("当前账号无法完整匹配探险手动队员;请重新选择")
|
||||
used.add(int(match["id"]))
|
||||
members.append(int(match["id"]))
|
||||
result[str(int(teams[index]["id"]))] = {"enabled": rule["enabled"], "area": rule["area"], "monsters": members}
|
||||
# Never copy the exported enable switch or any source-account instance IDs.
|
||||
return ExplorePolicy(enabled=False, reserve_ti=value.get("reserve_ti", 0), teams=result).to_dict()
|
||||
|
||||
|
||||
def import_config(value, snapshot=None):
|
||||
if not isinstance(value, dict) or value.get("format") != FORMAT:
|
||||
raise ValueError("不是受支持的辅助策略文件")
|
||||
@@ -120,6 +174,8 @@ def import_config(value, snapshot=None):
|
||||
if value.get("schema") != CONFIG_VERSION:
|
||||
raise ValueError("不支持此策略文件版本")
|
||||
config = {"policy": asdict(import_policy(value | {"schema": VERSION}, snapshot))}
|
||||
if "explore_policy" in value:
|
||||
config["explore_policy"] = import_explore_policy(value["explore_policy"], snapshot)
|
||||
for key, cls in (("quest_policy", QuestPolicy), ("daily_policy", DailyPolicy), ("sweep_policy", SweepPolicy)):
|
||||
if not isinstance(value.get(key), dict):
|
||||
raise ValueError("策略文件缺少 " + key)
|
||||
|
||||
Reference in New Issue
Block a user