Add offline battle estimates and passive phone sample analysis
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
from contextlib import redirect_stdout
|
||||
from dataclasses import asdict, replace
|
||||
import io
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from tools.battle_estimate_v12 import Fighter, Rules, demo_player, hit_damage, main, simulate
|
||||
from ranch_assistant.tests.test_core import workspace_temp
|
||||
|
||||
|
||||
class MidpointRandom:
|
||||
def __init__(self, critical=False):
|
||||
self.critical = critical
|
||||
|
||||
def random(self):
|
||||
return 0 if self.critical else .99
|
||||
|
||||
def uniform(self, low, high):
|
||||
return (low + high) / 2
|
||||
|
||||
|
||||
def fighter(**changes):
|
||||
return replace(Fighter(1001010, "fixture", 100, 100, 20, 10, 1000), **changes)
|
||||
|
||||
|
||||
RULES = Rules(.95, .25, 1.5, 1.15, {1: 2, 2: 4, 3: 1, 4: 3, 5: 0})
|
||||
|
||||
|
||||
class BattleEstimateTests(unittest.TestCase):
|
||||
def test_base_damage_matches_source_formula_at_midpoint(self):
|
||||
# 100 * .95 - 20 * (.25 * .75 + .25 * .125) = 90.625.
|
||||
self.assertEqual(hit_damage(fighter(), fighter(), RULES, MidpointRandom()), 91)
|
||||
|
||||
def test_critical_attack_is_applied_before_the_defense_roll(self):
|
||||
self.assertEqual(hit_damage(fighter(), fighter(), RULES, MidpointRandom(True)), 138)
|
||||
|
||||
def test_dex_advantage_and_disadvantage_change_the_damage_range(self):
|
||||
self.assertEqual(hit_damage(fighter(dex=50), fighter(), RULES, MidpointRandom()), 98)
|
||||
self.assertEqual(hit_damage(fighter(dex=0), fighter(dex=40), RULES, MidpointRandom()), 84)
|
||||
|
||||
def test_extreme_dex_difference_is_capped(self):
|
||||
rng = MidpointRandom()
|
||||
self.assertEqual(hit_damage(fighter(dex=90), fighter(), RULES, rng),
|
||||
hit_damage(fighter(dex=900), fighter(), RULES, rng))
|
||||
|
||||
def test_high_defense_cannot_reduce_base_damage_below_one(self):
|
||||
self.assertEqual(hit_damage(fighter(atk=1), fighter(defense=100000), RULES, MidpointRandom()), 1)
|
||||
|
||||
def test_element_counter_uses_the_attacker_element(self):
|
||||
attack = fighter(element=1)
|
||||
self.assertGreater(hit_damage(attack, fighter(element=2), RULES, MidpointRandom()),
|
||||
hit_damage(attack, fighter(element=3), RULES, MidpointRandom()))
|
||||
|
||||
def test_win_is_derived_from_kills_and_credits_only_actual_hp_lost(self):
|
||||
strong = fighter(hp=10000, atk=10000, attack_ms=100)
|
||||
result = simulate([strong], [fighter()], RULES, seed=1, timeout_ms=10000, enemy_capacity=1)
|
||||
self.assertEqual(result["predicted_outcome"], "win")
|
||||
self.assertEqual(result["enemy_kills"], 1)
|
||||
self.assertEqual(result["player_damage"], 100)
|
||||
self.assertEqual(result["model_duration_seconds"], .1)
|
||||
|
||||
def test_a_weak_roster_can_lose(self):
|
||||
result = simulate([fighter(hp=1, atk=1)], [fighter(hp=10000)], RULES,
|
||||
seed=1, timeout_ms=10000, enemy_capacity=1)
|
||||
self.assertEqual(result["predicted_outcome"], "loss")
|
||||
self.assertEqual(result["player_survivors"], 0)
|
||||
|
||||
def test_timeout_happens_before_an_attack_at_the_same_deadline(self):
|
||||
result = simulate([fighter(atk=100000)], [fighter()], RULES,
|
||||
seed=1, timeout_ms=1000, enemy_capacity=1)
|
||||
self.assertEqual(result["predicted_outcome"], "timeout")
|
||||
self.assertEqual(result["resolved_hits"], 0)
|
||||
|
||||
def test_wave_gap_and_enemy_capacity_are_respected(self):
|
||||
result = simulate([fighter(hp=10000, atk=10000, attack_ms=100)],
|
||||
[fighter(), fighter(), fighter(wave=2)], RULES, seed=1, timeout_ms=10000, enemy_capacity=1)
|
||||
self.assertEqual(result["enemy_kills"], 3)
|
||||
self.assertEqual(result["model_duration_seconds"], 1.0)
|
||||
|
||||
def test_fixed_seed_is_reproducible_and_does_not_mutate_roster(self):
|
||||
team, enemies = [fighter()], [fighter()]
|
||||
before = [asdict(x) for x in team + enemies]
|
||||
first = simulate(team, enemies, RULES, seed=12, timeout_ms=10000, enemy_capacity=1)
|
||||
self.assertEqual(first, simulate(team, enemies, RULES, seed=12, timeout_ms=10000, enemy_capacity=1))
|
||||
self.assertEqual([asdict(x) for x in team + enemies], before)
|
||||
|
||||
def test_invalid_units_and_empty_teams_are_rejected(self):
|
||||
for field, value in (("hp", 0), ("attack_ms", 0), ("atk", float("nan")), ("crit", float("inf"))):
|
||||
with self.assertRaises(ValueError):
|
||||
fighter(**{field: value})
|
||||
with self.assertRaises(ValueError):
|
||||
simulate([], [fighter()], RULES, seed=1, timeout_ms=1000, enemy_capacity=1)
|
||||
|
||||
def test_catalog_example_uses_mean_growth_without_changing_source(self):
|
||||
row = {"type": 1, "name": "fixture", "element": 5, "spd_atk": 1600,
|
||||
**{key: 10 for key in ("atk", "def", "hp", "dex")},
|
||||
**{key + suffix: value for key in ("atk", "def", "hp", "dex")
|
||||
for suffix, value in (("_grow_min", 2), ("_grow_max", 4))}}
|
||||
before = dict(row)
|
||||
unit = demo_player(row, 3)
|
||||
self.assertEqual((unit.atk, unit.defense, unit.hp, unit.dex), (16, 16, 16, 16))
|
||||
self.assertEqual(unit.attack_ms, 1588)
|
||||
self.assertEqual(row, before)
|
||||
|
||||
def test_cli_cannot_call_network_and_labels_results_as_uncalibrated_estimates(self):
|
||||
tables = {"monster_info": {"1": {"type": 1, "name": "fixture", "element": 5, "spd_atk": 1000, "skill_1": 3,
|
||||
**{key: 100 for key in ("atk", "def", "hp", "dex")},
|
||||
**{key + suffix: 1 for key in ("atk", "def", "hp", "dex") for suffix in ("_grow_min", "_grow_max")}}}}
|
||||
scenario = (tables, {}, {"name": "fixture"}, [fighter()], RULES, 10000, 1, set())
|
||||
with workspace_temp() as folder, patch("tools.battle_estimate_v12.load_scenario", return_value=scenario), \
|
||||
patch("socket.socket", side_effect=AssertionError("Offline model attempted a socket")), redirect_stdout(io.StringIO()):
|
||||
output = folder / "estimate.json"
|
||||
self.assertEqual(main(["--apk", "fixture.apk", "--species", "1", "--trials", "2", "--output", str(output)]), 0)
|
||||
report = json.loads(output.read_text(encoding="utf-8"))
|
||||
self.assertTrue(report["approximate"])
|
||||
self.assertFalse(report["calibrated_against_original_runtime"])
|
||||
self.assertNotIn("battleData", report)
|
||||
self.assertNotIn("token", report)
|
||||
self.assertEqual(sum(v["count"] for v in report["duration_seconds_by_predicted_outcome"].values()), 2)
|
||||
team = folder / "team.json"
|
||||
team.write_text(json.dumps([asdict(fighter(species=1))]), encoding="utf-8")
|
||||
self.assertEqual(main(["--apk", "fixture.apk", "--team", str(team), "--trials", "2", "--output", str(output)]), 0)
|
||||
report = json.loads(output.read_text(encoding="utf-8"))
|
||||
self.assertEqual(report["ignored_skill_ids_from_catalog"], [3])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user