guildhall/tests/test_game.py

343 lines
15 KiB
Python
Raw Normal View History

import random
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import game # noqa: E402
T0 = 1_800_000_000.0
def world_with(*players):
world = game.new_world(T0)
for uid, cls in players:
world["players"][uid] = game.new_player(uid, uid.title(), cls, T0)
return world
class XpTest(unittest.TestCase):
def test_curve_matches_runescape(self):
self.assertEqual(game.XP_TABLE[2], 83)
self.assertEqual(game.XP_TABLE[99], 13034431)
self.assertEqual(game.level_for(0), 1)
self.assertEqual(game.level_for(83), 2)
self.assertEqual(game.level_for(10**9), 99)
class ActionTest(unittest.TestCase):
def test_gathering_runs_while_away(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
self.assertIsNone(game.start_action(world, p, "mine_stone", T0))
summary = game.process_player(world, p, T0 + 200, random.Random(1))
self.assertEqual(summary["done"], 10)
self.assertEqual(p["bank"]["stone"], 10)
self.assertEqual(p["xp"]["mining"], 50)
self.assertEqual(p["action"]["start"], T0 + 200)
def test_class_speed_and_lucky_yield(self):
world = world_with(("pia", "pioneer"))
p = world["players"]["pia"]
game.start_action(world, p, "mine_stone", T0)
summary = game.process_player(world, p, T0 + 1600, random.Random(3))
self.assertEqual(summary["done"], 100) # 16s instead of 20s
self.assertGreater(p["bank"]["stone"], 100)
self.assertEqual(p["bank"]["stone"], 100 + summary["lucky"])
def test_offline_cap(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
game.start_action(world, p, "cut_logs", T0)
summary = game.process_player(world, p, T0 + 3 * game.DAY, random.Random(1))
self.assertEqual(summary["done"], game.OFFLINE_CAP // 20)
self.assertEqual(p["action"]["start"], T0 + 3 * game.DAY)
def test_crafting_stops_when_inputs_run_out(self):
world = world_with(("ann", "warrior"))
p = world["players"]["ann"]
p["bank"] = {"copper_ore": 3, "logs": 10}
game.start_action(world, p, "smith_copper_bar", T0)
summary = game.process_player(world, p, T0 + 1000, random.Random(1))
self.assertEqual(p["bank"]["copper_bar"], 3)
self.assertNotIn("copper_ore", p["bank"])
self.assertIsNone(p["action"])
self.assertEqual(summary["stopped"], "Out of copper ore")
def test_level_and_city_requirements(self):
world = world_with(("ann", "warrior"))
p = world["players"]["ann"]
self.assertEqual(game.start_action(world, p, "mine_iron", T0), "Needs level 15")
p["xp"]["smithing"] = game.XP_TABLE[30]
p["bank"] = {"iron_bar": 10}
self.assertEqual(game.start_action(world, p, "smith_iron_sword", T0), "Needs Smithy level 1")
self.assertEqual(game.start_action(world, p, "recon_roads", T0), "Only scouts can do that")
def test_full_bank_overflows_to_city(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
p["bank"] = {"stone": game.BASE_STACK - 2}
game.start_action(world, p, "mine_stone", T0)
summary = game.process_player(world, p, T0 + 100, random.Random(1))
self.assertEqual(p["bank"]["stone"], game.BASE_STACK)
self.assertEqual(summary["overflow"], {"stone": 3})
self.assertEqual(world["city"]["stockpile"]["stone"], 3)
self.assertEqual(p["favour"], 3)
def test_full_bank_can_stop_instead(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
p["overflow"] = "stop"
p["bank"] = {f"x{i}": 1 for i in range(game.BASE_SLOTS)}
game.start_action(world, p, "mine_stone", T0)
summary = game.process_player(world, p, T0 + 100, random.Random(1))
self.assertEqual(summary["stopped"], "Bank full")
self.assertNotIn("stone", p["bank"])
def test_combat_pays_and_loses(self):
world = world_with(("wil", "warrior"))
p = world["players"]["wil"]
game.start_action(world, p, "fight_wolves", T0)
summary = game.process_player(world, p, T0 + 48 * 100, random.Random(5))
self.assertEqual(summary["done"], 100)
self.assertEqual(summary["wins"] + summary["losses"], 100)
self.assertEqual(p["gold"], summary["wins"] * 2)
class GiftTest(unittest.TestCase):
def test_give_moves_items_within_bank_room(self):
world = world_with(("fred", "pioneer"), ("kay", "artisan"))
fred, kay = world["players"]["fred"], world["players"]["kay"]
fred["bank"] = {"copper_ore": 300}
self.assertIsNone(game.give(world, fred, kay, "copper_ore", 200, T0))
self.assertEqual(fred["bank"], {"copper_ore": 100})
self.assertEqual(kay["bank"], {"copper_ore": 200})
self.assertEqual(world["ledger"][-1]["text"], "Fred gave 200 copper ore to Kay")
kay["bank"] = {f"x{i}": 1 for i in range(game.BASE_SLOTS)}
self.assertEqual(game.give(world, fred, kay, "copper_ore", 1, T0), "Kay only has room for 0")
self.assertEqual(game.give(world, fred, fred, "copper_ore", 1, T0), "You can't give to yourself")
def test_amount_pluralises(self):
self.assertEqual(game.amount("copper_sword", 179), "179 copper swords")
self.assertEqual(game.amount("copper_sword", 1), "1 copper sword")
self.assertEqual(game.amount("stone", 1200), "1,200 stone")
class CityTest(unittest.TestCase):
def test_fund_from_bank_and_stores_then_builds(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
city = world["city"]
p["bank"] = {"stone": 1500}
city["stockpile"] = {"bricks": 600}
city["treasury"] = 300
self.assertIsNone(game.fund(world, p, "walls", "stone", "all", "bank", T0))
self.assertEqual(p["bank"], {"stone": 300}) # only what the fund needed
self.assertEqual(p["favour"], 1200)
self.assertIsNone(game.fund(world, p, "walls", "bricks", 500, "city", T0))
self.assertEqual(city["stockpile"], {"bricks": 100})
self.assertIsNone(city["construction"])
self.assertEqual(game.fund(world, p, "walls", "stone", 1, "bank", T0), "The fund doesn't need that")
self.assertIsNone(game.fund(world, p, "walls", "gold", "all", "city", T0))
self.assertEqual(city["construction"]["building"], "walls")
self.assertEqual(city["buildings"]["walls"]["fund"], {})
game.tick(world, T0 + 3 * game.HOUR, random.Random(1))
self.assertEqual(city["buildings"]["walls"]["level"], 1)
self.assertEqual(game.defence(world)["parts"]["walls"], 20)
def test_funded_buildings_queue_behind_the_builders(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
city = world["city"]
for key in ("walls", "houses"):
city["buildings"][key]["fund"] = {k: v for k, v in game.build_cost(key, 1).items() if k != "gold"}
city["treasury"] += game.build_cost(key, 1)["gold"]
self.assertIsNone(game.fund(world, p, key, "gold", "all", "city", T0))
self.assertEqual(city["construction"]["building"], "walls")
self.assertEqual(city["queue"], ["houses"])
game.tick(world, T0 + 3 * game.HOUR, random.Random(1))
self.assertEqual(city["construction"]["building"], "houses")
self.assertEqual(city["queue"], [])
def test_raids_never_touch_funds(self):
world = world_with(("fred", "warrior"))
city = world["city"]
city["buildings"]["walls"]["fund"] = {"stone": 1000}
city["stockpile"] = {"stone": 1000}
world["next_event_at"] = T0
game.tick(world, T0, random.Random(2))
raid = game.upcoming_raid(world)
raid["strength"] = 500
game.tick(world, raid["fire_at"], random.Random(1))
self.assertLess(city["stockpile"]["stone"], 1000)
self.assertEqual(city["buildings"]["walls"]["fund"], {"stone": 1000})
def test_tier_caps_levels_and_promotes_for_good(self):
world = world_with(("fred", "warrior"))
city = world["city"]
city["buildings"]["walls"]["level"] = 1
self.assertIsNone(game.fund_target(world, "walls")) # hamlet: level 1 only
self.assertIsNotNone(game.fund_target(world, "houses")) # houses go to 2
slots = game.bank_slots(world, world["players"]["fred"])
city["population"] = 41
world["next_event_at"] = None
game.tick(world, T0 + 1, random.Random(1))
self.assertEqual(city["tier"], 1)
self.assertIsNotNone(game.fund_target(world, "walls"))
self.assertEqual(game.bank_slots(world, world["players"]["fred"]), slots + game.TIER_SLOTS)
city["population"] = 10
game.tick(world, T0 + 2, random.Random(1))
self.assertEqual(city["tier"], 1)
def test_damage_turns_the_fund_towards_repairs_and_keeps_the_rest(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
city = world["city"]
city["buildings"]["walls"].update(level=1, damaged=True, fund={"stone": 2000})
repair = game.repair_cost("walls", 1)
self.assertEqual(game.fund_target(world, "walls"), repair)
for item, qty in repair.items():
if item == "gold":
city["treasury"] = qty
game.fund(world, p, "walls", "gold", "all", "city", T0)
elif item != "stone":
city["stockpile"][item] = qty
game.fund(world, p, "walls", item, "all", "city", T0)
self.assertTrue(city["construction"]["repair"])
self.assertEqual(city["buildings"]["walls"]["fund"], {"stone": 2000 - repair["stone"]})
def test_old_worlds_migrate(self):
world = game.new_world(T0)
world["version"] = 1
for building in world["city"]["buildings"].values():
del building["fund"]
del world["city"]["queue"], world["city"]["tier"]
game.migrate(world)
self.assertEqual(world["version"], game.WORLD_VERSION)
self.assertEqual(world["city"]["queue"], [])
self.assertEqual(world["city"]["buildings"]["walls"]["fund"], {})
def test_population_view_explains_growth(self):
world = world_with()
view = game.population_view(world)
self.assertEqual(view["tier_name"], "Hamlet")
self.assertEqual(view["next_tier"], {"name": "Village", "pop": 40})
self.assertGreater(view["growth"], 0)
self.assertEqual(view["food_days"], 4.0) # 40 bread for 20 people eating 0.5 a day
def test_people_eat_and_go_hungry(self):
world = world_with()
city = world["city"]
city["stockpile"] = {"bread": 5}
game.tick(world, T0 + 4 * game.DAY, random.Random(1))
self.assertTrue(city["starving"])
self.assertNotIn("bread", city["stockpile"])
self.assertLess(city["population"], 20)
def test_fed_city_grows_to_housing_and_pays_tax(self):
world = world_with()
city = world["city"]
city["stockpile"] = {"bread": 1000}
world["next_event_at"] = None
game.tick(world, T0 + 10 * game.DAY, random.Random(1))
self.assertFalse(city["starving"])
self.assertEqual(city["population"], 25)
self.assertGreater(city["treasury"], 100)
def test_speed_scales_time(self):
world = game.new_world(T0, speed=60)
world["players"]["fred"] = game.new_player("fred", "Fred", "warrior", T0)
p = world["players"]["fred"]
game.start_action(world, p, "mine_stone", T0)
game.process_player(world, p, T0 + 10, random.Random(1))
self.assertEqual(p["bank"]["stone"], 30)
class EventTest(unittest.TestCase):
def announce(self, world):
world["next_event_at"] = T0
game.tick(world, T0, random.Random(2))
return game.upcoming_raid(world)
def test_first_event_is_a_raid_with_vague_intel(self):
world = world_with()
raid = self.announce(world)
self.assertIsNotNone(raid)
view = game.event_view(world, raid, T0)
self.assertEqual(view["intel"], 0)
self.assertNotIn("strength", view)
self.assertNotIn("group", view)
self.assertLessEqual(view["earliest"], raid["fire_at"])
self.assertGreaterEqual(view["latest"], raid["fire_at"])
def test_spymaster_and_recon_sharpen_intel(self):
world = world_with(("sam", "scout"))
raid = self.announce(world)
world["city"]["buildings"]["spymaster"]["level"] = 1
self.assertIn("size", game.event_view(world, raid, T0))
p = world["players"]["sam"]
p["xp"]["reconnaissance"] = game.XP_TABLE[20]
game.start_action(world, p, "recon_camps", T0)
game.process_player(world, p, T0 + 8 * 72, random.Random(1))
view = game.event_view(world, raid, T0)
self.assertEqual(view["intel"], 3)
self.assertEqual(view["exact"], raid["fire_at"])
self.assertIn("strength", view)
self.assertTrue(view["camp"])
def test_victory_rewards_garrison(self):
world = world_with(("wil", "warrior"))
raid = self.announce(world)
p = world["players"]["wil"]
p["xp"]["combat"] = game.XP_TABLE[60]
p["garrison"] = True
game.tick(world, raid["fire_at"], random.Random(1))
self.assertEqual(raid["status"], "done")
self.assertTrue(raid["report"]["title"].startswith("Victory"))
self.assertEqual(world["city"]["repelled"], 1)
self.assertEqual(p["favour"], 25)
self.assertFalse(p["garrison"])
def test_defeat_culls_and_loots_but_spares_banks(self):
world = world_with(("fred", "warrior"))
p = world["players"]["fred"]
p["bank"] = {"stone": 500}
city = world["city"]
city["stockpile"] = {"bread": 200, "stone": 1000}
city["treasury"] = 500
city["buildings"]["walls"]["level"] = 1
raid = self.announce(world)
raid["strength"] = 500
game.tick(world, raid["fire_at"], random.Random(1))
self.assertTrue(raid["report"]["title"].startswith("Defeat"))
self.assertLess(city["population"], 20)
self.assertLess(city["stockpile"]["stone"], 1000)
self.assertLess(city["treasury"], 500)
self.assertTrue(city["buildings"]["walls"]["damaged"])
self.assertEqual(city["buildings"]["walls"]["level"], 1)
self.assertEqual(p["bank"], {"stone": 500})
self.assertEqual(game.defence(world)["parts"]["walls"], 0)
def test_repair_is_cheaper_than_building(self):
cost = game.repair_cost("walls", 2)
full = game.build_cost("walls", 2)
for item, qty in cost.items():
self.assertLess(qty, full[item])
def test_sabotage_needs_a_located_camp(self):
world = world_with(("wil", "warrior"))
p = world["players"]["wil"]
p["xp"]["combat"] = game.XP_TABLE[40]
raid = self.announce(world)
self.assertEqual(game.start_action(world, p, "sabotage", T0), "No bandit camp has been located")
raid["recon"] = 3 * game.RECON_PER_INTEL
self.assertIsNone(game.start_action(world, p, "sabotage", T0))
game.process_player(world, p, T0 + 96 * 20, random.Random(1))
self.assertGreater(raid["sabotage"], 0)
self.assertLessEqual(raid["sabotage"], 0.3)
if __name__ == "__main__":
unittest.main()