Settle player level and stamina; unify strategy configuration
This commit is contained in:
@@ -1,9 +1,34 @@
|
||||
"""Portable rule sets; credentials, sessions and local paths never enter files."""
|
||||
import copy
|
||||
import math
|
||||
from dataclasses import asdict
|
||||
from .model import Policy
|
||||
from .daily import DailyPolicy
|
||||
from .quests import QuestPolicy
|
||||
from .sweeping import SweepPolicy
|
||||
|
||||
FORMAT = "ranch-helper-policies"
|
||||
VERSION = 1
|
||||
CONFIG_VERSION = 2
|
||||
|
||||
|
||||
def checked_policy(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError("策略配置必须是对象")
|
||||
for key, default in asdict(cls()).items():
|
||||
if key not in data:
|
||||
continue
|
||||
value = data[key]
|
||||
if cls.__annotations__.get(key) in (float, "float"):
|
||||
valid = type(value) in (int, float) and math.isfinite(value)
|
||||
else:
|
||||
valid = type(value) is type(default)
|
||||
if not valid:
|
||||
raise ValueError("策略字段类型错误:" + key)
|
||||
try:
|
||||
return cls(**data)
|
||||
except (AttributeError, TypeError, KeyError) as error:
|
||||
raise ValueError("策略字段格式错误") from error
|
||||
|
||||
|
||||
def portable_policy(policy, snapshot=None):
|
||||
@@ -11,6 +36,9 @@ def portable_policy(policy, snapshot=None):
|
||||
value["enabled"] = False
|
||||
for field in ("buy", "collect", "dispatch", "feed", "fill", "gift_enabled"):
|
||||
value[field] = False
|
||||
if value["feed_target"] and not value["feed_targets"]:
|
||||
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")):
|
||||
targets[kind] = []
|
||||
@@ -24,12 +52,18 @@ def portable_policy(policy, snapshot=None):
|
||||
def import_policy(value, snapshot=None):
|
||||
if not isinstance(value, dict) or value.get("format") != FORMAT or value.get("schema") != VERSION or not isinstance(value.get("policy"), dict):
|
||||
raise ValueError("不是受支持的辅助策略文件")
|
||||
policy = Policy(**value["policy"])
|
||||
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")):
|
||||
result = {}
|
||||
for rule in value.get("targetSpecies", {}).get(kind, []):
|
||||
targets = value.get("targetSpecies", {})
|
||||
if not isinstance(targets, dict) or not isinstance(targets.get(kind, []), list):
|
||||
raise ValueError("培养目标配置格式错误")
|
||||
for rule in targets.get(kind, []):
|
||||
if not isinstance(rule, dict) or not 1 <= int(rule.get("priority", 0)) <= 999:
|
||||
raise ValueError("培养目标优先级格式错误")
|
||||
key = str(rule.get("typeId", ""))
|
||||
for mon in sorted(snapshot.raw["monsters"], key=lambda m: int(m["id"])):
|
||||
if str(mon.get("typeId")) == key and mon.get("process") == 3 and int(mon["id"]) not in used[kind]:
|
||||
@@ -40,3 +74,57 @@ def import_policy(value, snapshot=None):
|
||||
else:
|
||||
policy.feed_targets, policy.gift_targets = {}, {}
|
||||
return policy
|
||||
|
||||
|
||||
def portable_config(config, snapshot=None):
|
||||
"""One portable file for all strategies, independent of session/UI state."""
|
||||
policy = Policy(**config.get("policy", {}))
|
||||
value = portable_policy(policy, snapshot)
|
||||
value["schema"] = CONFIG_VERSION
|
||||
# These are configuration choices. The GUI execution switch is deliberately
|
||||
# outside the portable document and stays stopped on import.
|
||||
for field in ("enabled", "buy", "collect", "dispatch", "feed", "fill", "gift_enabled"):
|
||||
value["policy"][field] = getattr(policy, field)
|
||||
for key, cls in (("quest_policy", QuestPolicy), ("daily_policy", DailyPolicy), ("sweep_policy", SweepPolicy)):
|
||||
value[key] = asdict(cls(**config.get(key, {})))
|
||||
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]))
|
||||
return value
|
||||
|
||||
|
||||
def import_config(value, snapshot=None):
|
||||
if not isinstance(value, dict) or value.get("format") != FORMAT:
|
||||
raise ValueError("不是受支持的辅助策略文件")
|
||||
if value.get("schema") == VERSION:
|
||||
return {"policy": asdict(import_policy(value, snapshot))}
|
||||
if value.get("schema") != CONFIG_VERSION:
|
||||
raise ValueError("不支持此策略文件版本")
|
||||
config = {"policy": asdict(import_policy(value | {"schema": VERSION}, snapshot))}
|
||||
for key, cls in (("quest_policy", QuestPolicy), ("daily_policy", DailyPolicy), ("sweep_policy", SweepPolicy)):
|
||||
if not isinstance(value.get(key), dict):
|
||||
raise ValueError("策略文件缺少 " + key)
|
||||
config[key] = asdict(checked_policy(cls, value[key]))
|
||||
interval = value.get("interval", 30)
|
||||
if interval not in (30, 60, 120, 300):
|
||||
raise ValueError("刷新间隔须为30、60、120或300秒")
|
||||
overrides, stars = value.get("overrides", {}), value.get("yield_stars", [1, 2, 3, 4, 5])
|
||||
if not isinstance(overrides, dict) or not isinstance(stars, list) or any(type(s) is not int or s not in range(1, 6) for s in stars):
|
||||
raise ValueError("收益资料配置格式错误")
|
||||
for key, row in overrides.items():
|
||||
if (not str(key).isdigit() or not isinstance(row, dict)
|
||||
or any(field not in ("sell_money", "sell_exp", "time_total") or type(number) not in (int, float)
|
||||
or not math.isfinite(number) or number < 0 for field, number in row.items())):
|
||||
raise ValueError("收益资料配置格式错误")
|
||||
for rule in config["sweep_policy"]["rules"].values():
|
||||
if (not isinstance(rule, dict) or str(rule.get("level", "3")) not in ("1", "2", "3")
|
||||
or type(rule.get("limit", 3)) is not int or not 0 <= rule.get("limit", 3) <= 999
|
||||
or type(rule.get("enabled", False)) is not bool):
|
||||
raise ValueError("扫荡规则格式错误")
|
||||
if any(not isinstance(key, str) for key in config["daily_policy"]["selected"]):
|
||||
raise ValueError("每日事务规则格式错误")
|
||||
if any(not str(key).isdigit() or type(count) is not int or count < 0
|
||||
for key, count in config["quest_policy"]["item_reserves"].items()):
|
||||
raise ValueError("委托保留物品规则格式错误")
|
||||
config.update(interval=interval, overrides=copy.deepcopy(overrides), yield_stars=list(stars))
|
||||
return config
|
||||
|
||||
Reference in New Issue
Block a user