Add hatch retention, automatic breakthroughs and exploration
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
import copy
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ranch_assistant.breakthrough import (
|
||||
BreakthroughPlanner, PATH, POTENTIAL_PATH, plan_breakthrough,
|
||||
)
|
||||
from ranch_assistant.model import Catalog, Policy, Snapshot
|
||||
from ranch_assistant.tests.test_core import workspace_temp
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
def snapshot_with(**monster):
|
||||
mon = {"id": 900, "typeId": 1001010, "process": 3, "mode": 2,
|
||||
"potentialLevel": 1, "level": 1, "pAtk": 4, "pDef": 8,
|
||||
"pHp": 105, "pDex": 1}
|
||||
mon.update(monster)
|
||||
return Snapshot({"master": {"id": 1}, "monsters": [mon], "items": [
|
||||
{"id": 600, "typeId": 30058, "num": 100}], "stacks": [],
|
||||
"rooms": [{"id": 1, "typeId": 1000}], "teams": []})
|
||||
|
||||
|
||||
class BreakthroughTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.catalog = Catalog()
|
||||
|
||||
def setUp(self):
|
||||
self.snapshot = snapshot_with()
|
||||
self.policy = Policy(feed_targets={"900": 1})
|
||||
self.planner = BreakthroughPlanner(self.catalog)
|
||||
|
||||
def test_original_first_breakthrough_payload_and_chinese_description(self):
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.path, PATH)
|
||||
self.assertEqual(action.payload, {"monId": 900, "itemIds": [600]})
|
||||
self.assertEqual(action.kind, "breakthrough")
|
||||
self.assertIn("突破", action.description)
|
||||
self.assertIn("2 阶段", action.description)
|
||||
self.assertNotIn("monsterPotential", action.description)
|
||||
|
||||
def test_incomplete_potential_first_consumes_one_item_on_original_path(self):
|
||||
self.snapshot.monsters[900]["pAtk"] = 2
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.kind, "potential")
|
||||
self.assertEqual(action.path, POTENTIAL_PATH)
|
||||
self.assertEqual(action.payload, {"monId": 900, "itemId": 600, "monsterAttr": "atk",
|
||||
"traceInfo": {"itemTypeId": 600, "itemNum": 100}})
|
||||
self.assertIn("消耗", action.description)
|
||||
self.assertIn("重新读取", action.description)
|
||||
|
||||
def test_potential_trace_info_uses_selected_stack_instance_id(self):
|
||||
self.snapshot.monsters[900]["pAtk"] = 2
|
||||
self.assertEqual(self.snapshot.raw["items"], [{"id": 600, "typeId": 30058, "num": 100}])
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload["itemId"], 600)
|
||||
self.assertEqual(action.payload["traceInfo"], {"itemTypeId": 600, "itemNum": 100})
|
||||
|
||||
def test_potential_reserve_allows_partial_consumption_of_one_stack(self):
|
||||
self.snapshot.monsters[900]["pAtk"] = 2
|
||||
self.policy.gift_items = {"30058": {"enabled": False, "reserve": 10}}
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload["itemId"], 600)
|
||||
self.policy.gift_items["30058"]["reserve"] = 100
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
|
||||
def test_potential_proceeds_even_at_ordinary_level_cap(self):
|
||||
self.snapshot.monsters[900].update(pAtk=3, level=10)
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.kind, "potential")
|
||||
|
||||
def test_ordinary_level_below_cap_does_not_block_full_potential(self):
|
||||
self.assertEqual(self.snapshot.monsters[900]["level"], 1)
|
||||
self.assertIsNotNone(self.planner.plan(self.snapshot, self.policy))
|
||||
|
||||
def test_stage_reserve_uses_total_stock_and_one_sufficient_stack(self):
|
||||
self.policy.gift_items = {"30058": {"enabled": False, "reserve": 96}}
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload["itemIds"], [600])
|
||||
self.policy.gift_items["30058"]["reserve"] = 97
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.assertIn("单堆", " ".join(self.planner.notes))
|
||||
|
||||
def test_missing_or_insufficient_material_never_returns_action(self):
|
||||
self.snapshot.raw["items"][0]["num"] = 3
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.assertIn("3/4", self.planner.notes[0])
|
||||
self.snapshot.raw["items"] = []
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
|
||||
def test_multiple_stacks_choose_a_sufficient_id_not_aggregate_ids(self):
|
||||
self.snapshot.raw["items"] = [
|
||||
{"id": 600, "typeId": 30058, "num": 1},
|
||||
{"id": 601, "typeId": 30058, "num": 2},
|
||||
{"id": 602, "typeId": 30058, "num": 8},
|
||||
]
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload["itemIds"], [602])
|
||||
|
||||
def test_locked_stock_is_not_counted_toward_reserves_or_requests(self):
|
||||
self.snapshot.raw["items"] = [{"id": 600, "typeId": 30058, "num": 100, "lock": 1}]
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.snapshot.raw["items"][0].pop("lock")
|
||||
self.assertIsNotNone(self.planner.plan(self.snapshot, self.policy))
|
||||
|
||||
def test_priority_skips_blocked_candidate_and_uses_next(self):
|
||||
other = copy.deepcopy(self.snapshot.monsters[900])
|
||||
other.update(id=901, pAtk=3)
|
||||
self.snapshot.raw["monsters"].append(other)
|
||||
self.assertEqual(self.planner.plan(self.snapshot, self.policy, targets={"901": 2, "900": 1}).payload["monId"], 900)
|
||||
|
||||
def test_independent_targets_replace_feed_targets_and_empty_is_empty(self):
|
||||
other = copy.deepcopy(self.snapshot.monsters[900])
|
||||
other["id"] = 901
|
||||
self.snapshot.raw["monsters"].append(other)
|
||||
action = self.planner.plan(self.snapshot, self.policy, targets={"901": 1})
|
||||
self.assertEqual(action.payload["monId"], 901)
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy, targets={}))
|
||||
|
||||
def test_exploration_build_non_ranch_disabled_and_maximum_do_not_plan(self):
|
||||
for flags in ({"process": 1}, {"process": 2}, {"inExploreTeam": True}, {"inBuild": True}):
|
||||
self.snapshot.monsters[900].update(flags)
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.snapshot.monsters[900].update(process=3, inExploreTeam=False, inBuild=False)
|
||||
self.snapshot.monsters[900]["potentialLevel"] = 7
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy, enabled=False))
|
||||
self.policy.enabled = False
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
|
||||
def test_tp_level_and_exp_do_not_gate_material_breakthrough(self):
|
||||
self.snapshot.monsters[900].update(tpLevel=4, tpExp=33, potentialLevel=1)
|
||||
self.assertEqual(self.planner.plan(self.snapshot, self.policy).kind, "breakthrough")
|
||||
self.snapshot.monsters[900].update(tpLevel=0, tpExp=0, pAtk=0)
|
||||
self.assertEqual(self.planner.plan(self.snapshot, self.policy).kind, "potential")
|
||||
|
||||
def test_invalid_and_incomplete_snapshots_are_safe(self):
|
||||
for field in ("potentialLevel", "pAtk", "pDef", "pHp", "pDex"):
|
||||
snapshot = snapshot_with()
|
||||
snapshot.monsters[900][field] = None
|
||||
self.assertIsNone(BreakthroughPlanner(self.catalog).plan(snapshot, self.policy))
|
||||
snapshot = snapshot_with()
|
||||
snapshot.monsters[900]["typeId"] = 999999999
|
||||
self.assertIsNone(BreakthroughPlanner(self.catalog).plan(snapshot, self.policy))
|
||||
|
||||
def test_duplicate_item_instances_block_instead_of_double_spending(self):
|
||||
self.snapshot.raw["items"] = [
|
||||
{"id": 600, "typeId": 30058, "num": 2},
|
||||
{"id": 600, "typeId": 30058, "num": 2},
|
||||
]
|
||||
self.assertIsNone(self.planner.plan(self.snapshot, self.policy))
|
||||
self.assertIn("实例重复", self.planner.notes[0])
|
||||
|
||||
def test_second_stage_matches_original_element_table(self):
|
||||
self.snapshot.monsters[900].update(potentialLevel=2, pAtk=16, pDef=28, pHp=345, pDex=4)
|
||||
self.snapshot.raw["items"] = [{"id": 600, "typeId": 30058, "num": 4},
|
||||
{"id": 601, "typeId": 30041, "num": 1}]
|
||||
action = self.planner.plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload, {"monId": 900, "itemIds": [600, 601]})
|
||||
|
||||
def test_same_type_requirements_are_combined_and_instance_sent_once(self):
|
||||
info = copy.deepcopy(self.catalog.data["potential_info"])
|
||||
info["elementItem"]["4"]["lv2"] = 30058
|
||||
catalog = copy.copy(self.catalog)
|
||||
catalog.data = copy.deepcopy(self.catalog.data) | {"potential_info": info}
|
||||
self.snapshot.monsters[900].update(potentialLevel=2, pAtk=16, pDef=28, pHp=345, pDex=4)
|
||||
self.snapshot.raw["items"] = [{"id": 600, "typeId": 30058, "num": 5}]
|
||||
action = BreakthroughPlanner(catalog).plan(self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload, {"monId": 900, "itemIds": [600]})
|
||||
self.snapshot.raw["items"][0]["num"] = 4
|
||||
self.assertIsNone(BreakthroughPlanner(catalog).plan(self.snapshot, self.policy))
|
||||
|
||||
def test_chain_replans_fresh_snapshot_without_mutating_state(self):
|
||||
self.snapshot.monsters[900]["pAtk"] = 0
|
||||
original = copy.deepcopy(self.snapshot.raw)
|
||||
first = self.planner.plan(self.snapshot, self.policy, targets={"900": 1})
|
||||
self.assertEqual(first.kind, "potential")
|
||||
self.assertEqual(self.snapshot.raw, original)
|
||||
# Simulate a subsequent server read, not client-side success inference.
|
||||
fresh = snapshot_with(pAtk=2)
|
||||
second = self.planner.plan(fresh, self.policy, targets={"900": 1})
|
||||
self.assertEqual(second.kind, "potential")
|
||||
final = self.planner.plan(snapshot_with(), self.policy, targets={"900": 1})
|
||||
self.assertEqual(final.kind, "breakthrough")
|
||||
self.assertEqual(original["monsters"][0]["pAtk"], 0)
|
||||
|
||||
def test_functional_integration_is_pure_and_makes_no_client_call(self):
|
||||
with patch("ranch_assistant.api.Client.call") as call:
|
||||
action = plan_breakthrough(self.catalog, self.snapshot, self.policy)
|
||||
self.assertEqual(action.payload["monId"], 900)
|
||||
call.assert_not_called()
|
||||
|
||||
|
||||
class CatalogBuildTests(unittest.TestCase):
|
||||
def test_local_build_is_valid_and_preserves_unrelated_top_level_data(self):
|
||||
if not list(ROOT.glob("*_1.9.68.apk")):
|
||||
self.skipTest("Local source APK is not present")
|
||||
before = json.loads(subprocess.check_output(
|
||||
["git", "show", "HEAD:ranch_assistant/data/catalog.json"], cwd=ROOT).decode("utf-8"))
|
||||
with workspace_temp() as directory:
|
||||
destination = directory / "catalog.json"
|
||||
destination.write_text(json.dumps(before, ensure_ascii=False), encoding="utf-8")
|
||||
source_apk = next(ROOT.glob("*_1.9.68.apk"))
|
||||
live_catalog = ROOT / "ranch_assistant" / "data" / "catalog.json"
|
||||
live_bytes = live_catalog.read_bytes()
|
||||
result = subprocess.run([sys.executable, str(ROOT / "tools" / "extract_ranch_catalog.py"),
|
||||
"--apk", str(source_apk), "--output", str(destination)],
|
||||
cwd=ROOT, capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(live_catalog.read_bytes(), live_bytes)
|
||||
after = json.loads(destination.read_text(encoding="utf-8"))
|
||||
changed = {"monster_info", "potential_info", "game_setting", "client_constants"}
|
||||
self.assertEqual({key: value for key, value in after.items() if key not in changed},
|
||||
{key: value for key, value in before.items() if key not in changed})
|
||||
self.assertTrue(all({"job", "element"} <= set(row)
|
||||
for row in after["monster_info"].values() if isinstance(row, dict)))
|
||||
self.assertEqual(after["client_constants"], {"maxTpLevel": 4})
|
||||
self.assertTrue(after["game_setting"]["tpExp"])
|
||||
self.assertIn("specialFood", after["game_setting"])
|
||||
self.assertIn("specialFood14", after["game_setting"])
|
||||
self.assertNotIn("maxTpLevel", after["game_setting"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user