Files
myrzx_bot/tools/build_exploration_data.py

57 lines
2.3 KiB
Python

"""Extract the original normal-team exploration areas for offline planning."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import sys
import zipfile
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from tools.research_battle_v12 import recover_table
from ranch_assistant.game_data import ORIGINAL_APK_SHA256
def repair(value):
if isinstance(value, dict):
return {str(key): repair(item) for key, item in value.items()}
if isinstance(value, list):
return [repair(item) for item in value]
if isinstance(value, str):
try:
return value.encode("latin1").decode("gbk")
except (UnicodeEncodeError, UnicodeDecodeError):
pass
return value
def build(apk, output):
import hashlib
with Path(apk).open("rb") as stream:
if hashlib.file_digest(stream, "sha256").hexdigest() != ORIGINAL_APK_SHA256:
raise ValueError("Only the verified original 1.9.68 APK is accepted")
with zipfile.ZipFile(apk) as archive:
original, metadata = recover_table(archive, "explore_info")
source = original.get("datas", original)["areas"]
fields = ("id", "name", "passStage", "costTi", "costTime", "level", "team", "monExpMin", "monExpMax", "dlevel", "reward")
areas = [{key: repair(row.get(key)) for key in fields} for row in source.values() if row.get("team") == "normal"]
document = {"schema": 1, "source_apk_sha256": ORIGINAL_APK_SHA256,
"source_table_sha256": metadata.get("sha256"), "areas": areas}
Path(output).write_text(json.dumps(document, ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8", newline="\n")
return document
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--apk", type=Path)
parser.add_argument("--output", type=Path, default=ROOT / "ranch_assistant/data/exploration.json")
arguments = parser.parse_args()
if arguments.apk is None:
candidates = list(ROOT.glob("*_1.9.68.apk"))
if len(candidates) != 1:
parser.error("Pass --apk with the original 1.9.68 APK")
arguments.apk = candidates[0]
result = build(arguments.apk, arguments.output)
print(json.dumps({"areas": len(result["areas"]), "output": str(arguments.output)}, ensure_ascii=True))