222 lines
14 KiB
Python
222 lines
14 KiB
Python
import unittest
|
|
from dataclasses import asdict
|
|
from unittest.mock import patch
|
|
|
|
from ranch_assistant.cultivation import quality_rank
|
|
from ranch_assistant.model import Catalog, EggRule, Planner, Policy, Snapshot
|
|
|
|
|
|
def graded_monster(catalog, mid, grades, *, level=10, species=1001010, process=3, **values):
|
|
row = catalog.monster(species)
|
|
mon = {"id": mid, "typeId": species, "process": process, "mode": 2, "level": level,
|
|
"feel": 0, "entangleLevel": 0, "giftTime": 0, "roomId": 1, "lock": 0, **values}
|
|
for stat, grade in zip(("atk", "def", "hp", "dex"), grades):
|
|
low, high = row[stat + "_grow_min"], row[stat + "_grow_max"]
|
|
mon["g" + stat.capitalize()] = low + (high - low) * {"C": .1, "B": .375, "A": .625, "S": .9}[grade]
|
|
return mon
|
|
|
|
|
|
class RanchRetentionTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.catalog = Catalog()
|
|
room_type = next(iter(self.catalog.data["room_info"]))
|
|
self.catalog.data["room_info"][room_type]["maxNum"] = 10
|
|
self.snapshot = Snapshot({"master": {"id": 1}, "monsters": [], "teams": [], "stacks": [],
|
|
"items": [], "rooms": [{"id": 1, "typeId": int(room_type)}], "history": {}, "activityHistory": {}})
|
|
self.planner = Planner(self.catalog)
|
|
|
|
def policy(self, action="sell", count=1, **values):
|
|
values.setdefault("protect_star", 6)
|
|
return Policy(manual_eggs=True, fill=False, protect_quality=6, keep_better_quality=True,
|
|
egg_rules={"1001010": asdict(EggRule(enabled=True, action=action, keep_count=count))}, **values)
|
|
|
|
def plan(self, rows, policy=None, managed=None):
|
|
self.snapshot.raw["monsters"] = rows
|
|
self.planner.managed.update(managed if managed is not None else [row["id"] for row in rows])
|
|
self.planner.cultivation.choices["1001010"] = 1
|
|
self.planner.cultivation.signature = None
|
|
return self.planner.plan(self.snapshot, policy or self.policy())
|
|
|
|
def test_quality_uses_s_then_a_then_b_before_normalized_growth(self):
|
|
aasa, ssas = graded_monster(self.catalog, 1, "AASA", level=50), graded_monster(self.catalog, 2, "SSAS")
|
|
self.assertLess(quality_rank(self.catalog, aasa), quality_rank(self.catalog, ssas))
|
|
self.assertGreater(quality_rank(self.catalog, graded_monster(self.catalog, 3, "AAAC")),
|
|
quality_rank(self.catalog, graded_monster(self.catalog, 4, "BBAA")))
|
|
self.assertFalse(any(a.kind == "sell" for a in self.plan([aasa, ssas])))
|
|
reason = self.planner.protection_reason(ssas, self.snapshot, self.policy())
|
|
self.assertIn("SSAS", reason)
|
|
self.assertIn("S项3", reason)
|
|
|
|
def test_quota_zero_one_two_apply_to_every_enabled_action(self):
|
|
for action in ("sell", "feed", "keep"):
|
|
for quota, disposed in ((0, 3), (1, 2), (2, 1)):
|
|
with self.subTest(action=action, quota=quota):
|
|
rows = [graded_monster(self.catalog, mid, "CCCC", tpLevel=4) for mid in (1, 2, 3)]
|
|
if action == "feed":
|
|
target = graded_monster(self.catalog, 99, "SSSS", species=1002010,
|
|
level=1, exp=0, potentialLevel=2, tpLevel=4, tpExp=33)
|
|
rows.append(target)
|
|
elif action == "keep":
|
|
disposed = 0
|
|
policy = self.policy(action=action, count=quota,
|
|
feed_targets={"99": 1} if action == "feed" else {})
|
|
actions = self.plan(rows, policy)
|
|
sold = [mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]]
|
|
fed = [mid for a in actions if a.kind == "feed" for mid in a.payload["foods"] if mid != 99]
|
|
self.assertEqual(len(sold + fed), disposed)
|
|
for target in (a.payload["targetId"] for a in actions if a.kind == "feed"):
|
|
self.assertNotIn(target, fed)
|
|
|
|
def test_same_batch_and_locked_ranch_monsters_share_one_quota(self):
|
|
rows = [graded_monster(self.catalog, mid, "CCCC", roomId=mid) for mid in (1, 2)]
|
|
rows += [graded_monster(self.catalog, mid, "CCCC", process=2, roomId=None) for mid in (3, 4)]
|
|
for row in rows[:2]:
|
|
row["lock"] = 1
|
|
actions = self.plan(rows, self.policy(count=2))
|
|
self.assertEqual([mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]], [3, 4])
|
|
|
|
def test_incubator_batch_fills_quota_and_keeps_same_ids_after_collection(self):
|
|
for quota in (2, 3):
|
|
with self.subTest(quota=quota):
|
|
self.setUp()
|
|
rows = [graded_monster(self.catalog, mid, "CCCC", process=2, roomId=None) for mid in (1, 2, 3)]
|
|
self.snapshot.raw["stacks"] = [{"id": mid, "position": mid - 1, "monsterId": mid} for mid in (1, 2, 3)]
|
|
actions = self.plan(rows, self.policy(count=quota))
|
|
collected = {mid for a in actions if a.kind == "collect" for data in a.payload["datas"] for mid in data["monsterId"]}
|
|
self.assertEqual(collected, set(range(1, quota + 1)))
|
|
self.assertEqual(self.planner.retained, collected)
|
|
for row in rows:
|
|
row.update(process=3, roomId=1)
|
|
actions = self.plan(rows, self.policy(count=quota))
|
|
self.assertEqual(self.planner.retained, collected)
|
|
sold = {mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]}
|
|
self.assertEqual(sold, set(range(1, 4)) - collected)
|
|
actions = self.plan(rows, self.policy(count=quota))
|
|
self.assertEqual(self.planner.retained, collected)
|
|
self.assertTrue(all(mid not in collected for a in actions for mid in a.payload.get("foods", [])))
|
|
|
|
def test_ranch_and_incubator_share_quota_before_and_after_collection(self):
|
|
ranch = graded_monster(self.catalog, 1, "CCCC")
|
|
rows = [ranch] + [graded_monster(self.catalog, mid, "CCCC", process=2, roomId=None) for mid in (2, 3, 4)]
|
|
self.snapshot.raw["stacks"] = [{"id": mid, "position": mid - 1, "monsterId": mid} for mid in (2, 3, 4)]
|
|
actions = self.plan(rows, self.policy(count=2))
|
|
collected = {mid for a in actions if a.kind == "collect" for data in a.payload["datas"] for mid in data["monsterId"]}
|
|
self.assertEqual(collected, {2})
|
|
self.assertEqual(self.planner.retained, {1, 2})
|
|
for row in rows[1:]:
|
|
row.update(process=3, roomId=1)
|
|
actions = self.plan(rows, self.policy(count=2))
|
|
self.assertEqual(self.planner.retained, {1, 2})
|
|
self.assertEqual([mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]], [3, 4])
|
|
|
|
def test_initial_incubator_quota_prefers_best_quality_last_in_raw(self):
|
|
rows = [graded_monster(self.catalog, mid, "CCCC", process=2, roomId=None) for mid in (1, 2)]
|
|
rows.append(graded_monster(self.catalog, 3, "SSAS", process=2, roomId=None))
|
|
self.snapshot.raw["stacks"] = [{"id": mid, "position": mid - 1, "monsterId": mid} for mid in (1, 2, 3)]
|
|
actions = self.plan(rows, self.policy(count=2))
|
|
collected = {mid for a in actions if a.kind == "collect" for data in a.payload["datas"] for mid in data["monsterId"]}
|
|
self.assertEqual(collected, {1, 3})
|
|
self.assertEqual(self.planner.retained, {1, 3})
|
|
|
|
def test_incubator_quota_quality_prevents_equal_quality_extra_retention(self):
|
|
rows = [graded_monster(self.catalog, 1, "AASA")]
|
|
rows += [graded_monster(self.catalog, mid, "SSAS", process=2, roomId=None) for mid in (2, 3)]
|
|
self.snapshot.raw["stacks"] = [{"id": mid, "position": mid - 1, "monsterId": mid} for mid in (2, 3)]
|
|
actions = self.plan(rows, self.policy(count=2))
|
|
self.assertEqual(self.planner.retained, {1, 2})
|
|
collected = [mid for a in actions if a.kind == "collect" for data in a.payload["datas"] for mid in data["monsterId"]]
|
|
self.assertEqual(collected, [2])
|
|
self.assertEqual([mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]], [])
|
|
|
|
def test_zero_quota_still_protects_better_incubator_above_locked_ranch(self):
|
|
locked = graded_monster(self.catalog, 1, "AASA", lock=1)
|
|
better = graded_monster(self.catalog, 2, "SSAS", process=2, roomId=None)
|
|
self.snapshot.raw["stacks"] = [{"id": 2, "position": 0, "monsterId": 2}]
|
|
actions = self.plan([locked, better], self.policy(count=0))
|
|
self.assertEqual(self.planner.retained, {1, 2})
|
|
self.assertEqual([mid for a in actions if a.kind == "collect" for data in a.payload["datas"] for mid in data["monsterId"]], [2])
|
|
self.assertEqual([mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]], [])
|
|
|
|
def test_three_collection_rounds_keep_only_best_ssas_above_locked_aasa(self):
|
|
locked = graded_monster(self.catalog, 1, "AASA", level=50, lock=1)
|
|
first = graded_monster(self.catalog, 2, "SSAS", process=2, roomId=None)
|
|
self.snapshot.raw["stacks"] = [{"id": 1, "position": 0, "monsterId": 2}]
|
|
policy = self.policy()
|
|
self.assertEqual(self.plan([locked, first], policy)[0].kind, "collect")
|
|
first.update(process=3, roomId=1)
|
|
self.assertFalse(any(a.kind == "sell" for a in self.plan([locked, first], policy)))
|
|
second = graded_monster(self.catalog, 3, "SSAS", process=2, roomId=None)
|
|
self.snapshot.raw["stacks"][0]["monsterId"] = 3
|
|
self.assertEqual([a.payload["monsterIds"] for a in self.plan([locked, first, second], policy)
|
|
if a.kind == "sell"], [[3]])
|
|
second.update(process=3, roomId=1)
|
|
actions = self.plan([locked, first, second], policy)
|
|
self.assertEqual([mid for a in actions if a.kind == "sell" for mid in a.payload["monsterIds"]], [3])
|
|
self.assertTrue(all(2 not in a.payload.get("monsterIds", []) and 2 not in a.payload.get("foods", []) for a in actions))
|
|
|
|
def test_same_species_tp_batch_ignores_potential_cap_and_never_swaps_representative(self):
|
|
target = graded_monster(self.catalog, 1, "SCCC", level=10, tpLevel=2, tpExp=5, potentialLevel=1)
|
|
rows = [target, graded_monster(self.catalog, 2, "CCCC"), graded_monster(self.catalog, 3, "CCCC")]
|
|
self.assertEqual(self.plan(rows, self.policy(action="feed"))[0].payload, {"targetId": 1, "foods": [2, 3]})
|
|
target.update(tpLevel=4, tpExp=self.catalog.data["game_setting"]["tpExp"]["1"]["4"])
|
|
self.assertFalse(any(a.kind == "feed" and 1 in a.payload["foods"] for a in self.plan(rows, self.policy(action="feed"))))
|
|
|
|
def test_normal_cap_still_allows_tp_food_until_cumulative_exp_is_full(self):
|
|
target = graded_monster(self.catalog, 1, "SCCC", level=10, tpLevel=2, tpExp=6,
|
|
potentialLevel=1, exp=self.catalog.data["monster_level"]["10"])
|
|
food = graded_monster(self.catalog, 2, "CCCC")
|
|
self.assertEqual(self.plan([target, food], self.policy(action="feed"))[0].payload["foods"], [2])
|
|
target["tpExp"] = self.catalog.data["game_setting"]["tpExp"]["1"]["4"] - 1
|
|
self.assertEqual(self.plan([target, food], self.policy(action="feed"))[0].payload["foods"], [2])
|
|
target["tpExp"] = self.catalog.data["game_setting"]["tpExp"]["1"]["4"]
|
|
self.assertFalse(any(a.kind == "feed" for a in self.plan([target, food], self.policy(action="feed"))))
|
|
|
|
def test_unmanaged_protected_and_other_feed_targets_are_not_materials(self):
|
|
target = graded_monster(self.catalog, 1, "SSSS", tpLevel=0, tpExp=0)
|
|
other = graded_monster(self.catalog, 2, "SSSA", tpLevel=0, tpExp=0)
|
|
food = graded_monster(self.catalog, 3, "SSSB")
|
|
action = self.plan([target, other, food], self.policy(action="feed", count=0), managed=[3])[0]
|
|
self.assertEqual(action.payload, {"targetId": 1, "foods": [3]})
|
|
self.planner.managed.discard(2)
|
|
other["lock"] = 1
|
|
action = self.plan([target, other, food], self.policy(action="feed", count=0))[0]
|
|
self.assertEqual(action.payload["foods"], [3])
|
|
|
|
def test_direct_protected_and_disposition_prepare_quota_before_first_call(self):
|
|
rows = [graded_monster(self.catalog, 1, "CCCC"), graded_monster(self.catalog, 2, "CCCC")]
|
|
self.snapshot.raw["monsters"] = rows
|
|
policy = self.policy()
|
|
self.planner.cultivation.choices["1001010"] = 1
|
|
self.planner.cultivation.signature = None
|
|
self.planner.managed.update((1, 2))
|
|
with patch.object(self.planner, "prepare_retention", wraps=self.planner.prepare_retention) as prepare:
|
|
protected = [self.planner.protected(row, self.snapshot, policy) for row in rows]
|
|
self.assertEqual(protected.count(True), 1)
|
|
self.assertEqual(self.planner.disposition(rows[0], self.snapshot, policy), "keep" if protected[0] else "sell")
|
|
self.assertEqual(self.planner.disposition(rows[1], self.snapshot, policy), "keep" if protected[1] else "sell")
|
|
self.assertEqual(prepare.call_count, 1)
|
|
|
|
def test_missing_tp_fields_pause_special_feed_but_not_legacy_experience(self):
|
|
target = graded_monster(self.catalog, 1, "SCCC", level=10,
|
|
potentialLevel=1, exp=self.catalog.data["monster_level"]["10"])
|
|
food = graded_monster(self.catalog, 2, "CCCC")
|
|
self.assertFalse(any(a.kind == "feed" for a in self.plan([target, food], self.policy(action="feed"))))
|
|
target.update(tpLevel=0, tpExp=0, level=1, exp=0)
|
|
policy = Policy(strategy="experience", feed_target=1, fill=False, protect_star=6, protect_quality=6)
|
|
self.assertEqual(self.plan([target, food], policy)[0].payload, {"targetId": 1, "foods": [2]})
|
|
|
|
def test_special_food_tp_count_matches_client_rules(self):
|
|
target = graded_monster(self.catalog, 1, "SSSS", tpLevel=0, tpExp=0)
|
|
settings = self.catalog.data["game_setting"]
|
|
special = graded_monster(self.catalog, 2, "CCCC", species=int(settings["specialFood"]))
|
|
special14 = graded_monster(self.catalog, 3, "CCCC", species=int(settings["specialFood14"]))
|
|
self.assertEqual((self.planner.tp_food_value(target, special), self.planner.tp_food_value(target, special14)), (1, 1))
|
|
five_star = next(int(key) for key, row in self.catalog.monsters.items()
|
|
if int(row.get("star") or 0) == 5 and all(row.get(stat + "_grow_min") is not None for stat in ("atk", "def", "hp", "dex")))
|
|
target = graded_monster(self.catalog, 1, "SSSS", species=five_star, tpLevel=0, tpExp=0)
|
|
self.assertEqual((self.planner.tp_food_value(target, special), self.planner.tp_food_value(target, special14)), (1, 0))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|