def test_fleet_transport(): game_state = init_state_complex() game = Game(game_state) game.update(100) fleet = game_state.players["max"].fleets[0] fleet_comp = fleet.components[FleetComponent] MissionSystem.order_mission( fleet, "TRANSPORT", PlanetLocation(1, 1, 4), {Resources.Metal: 2, Resources.Cristal: 0.5}, ) game.update(10) assert fleet_comp.travelling_to == PlanetLocation(1, 1, 4) assert fleet_comp.cargo[Resources.Metal] == 2 game.update(100), assert fleet_comp.mission == "RETURN" assert fleet_comp.travelling_to == PlanetLocation(1, 1, 3) assert fleet_comp.cargo[Resources.Metal] == 0
def test_upgrade_building(): game_state = initialise_gamestate() game = Game(game_state) upgrade_success = game.action_upgrade_building( "max", PlanetLocation(1, 1, 3), "metal_mine" ) assert not upgrade_success game.update(3600) upgrade_success = game.action_upgrade_building( "max", PlanetLocation(1, 1, 3), "metal_mine" ) planet_component = game_state.world.planets[PlanetLocation(1, 1, 3)].components[ PlanetComponent ] assert upgrade_success assert planet_component.resources[Resources.Metal] < 30 * game_state.world.speed assert ( int(planet_component.resources[Resources.Cristal]) < 20 * game_state.world.speed ) with pytest.raises(AssertionError) as excinfo: game.action_upgrade_building("max", "ploup", "metal_mine") assert "unkwnown planet" in str(excinfo.value).lower() with pytest.raises(AssertionError) as excinfo: game.action_upgrade_building("ploup", PlanetLocation(1, 1, 3), "metal_mine") assert "unknown player" in str(excinfo.value).lower()
def test_initialise_gamestate(): game_state = initialise_gamestate() assert len(game_state.players) == 1 assert "max" in game_state.players assert game_state.players["max"].components[PlayerComponent].name == "Max" assert PlanetLocation(1, 1, 3) in game_state.world.planets earth = game_state.world.planets[PlanetLocation(1, 1, 3)] assert earth.components[PlanetComponent].name == "Earth" metal_mine = earth.buildings["metal_mine"] assert metal_mine.components[BuildingComponent].level == 0
def test_combat(): game_state = init_state_complex() game = Game(game_state) earth = PlanetLocation(1, 1, 3) jupiter = PlanetLocation(1, 1, 5) game_state.world.planets[jupiter].buildings["metal_mine"].components[ BuildingComponent ].level = 0 game.update(360) game.action_send_mission("max", earth, "ATTACK", jupiter) attacking_fleet = game_state.players["max"].fleets[0] assert attacking_fleet.components[FleetComponent].mission == "ATTACK" assert attacking_fleet.ships[0].components[ShipComponent].number == 10 assert attacking_fleet.components[FleetComponent].cargo == { Resources.Metal: 0, Resources.Cristal: 0, Resources.Deuterium: 0, } travelling_time = attacking_fleet.components[FleetComponent].travel_time_left game.update(travelling_time - 1) assert ( game_state.world.planets[jupiter] .components[PlanetComponent] .resources[Resources.Metal] > 0 ) game.update(1) assert game_state.world.planets[jupiter].components[PlanetComponent].resources == { Resources.Metal: 0, Resources.Cristal: 0, Resources.Deuterium: 0, } assert len(game_state.players["bob"].fleets) == 0 assert attacking_fleet.ships[0].components[ShipComponent].number == 7 assert attacking_fleet.components[FleetComponent].cargo[Resources.Metal] > 0 assert attacking_fleet.components[FleetComponent].cargo[Resources.Cristal] > 0 assert attacking_fleet.components[FleetComponent].cargo[Resources.Deuterium] > 0 game.update(20)
def test_build_ship(): game_state = initialise_gamestate() game = Game(game_state) game_state.world.speed = 1000000 earth = PlanetLocation(1, 1, 3) outcome = game.action_build_ship("max", earth, "light_fighter") assert not outcome.success game.update(24 * 3600) success = game.action_upgrade_building("max", earth, "shipyard") assert success outcome = game.action_build_ship("max", earth, "light_fighter") assert not outcome.success assert game.action_upgrade_building("max", earth, "research_lab") assert game.action_upgrade_research("max", earth, "energy") assert game.action_upgrade_research("max", earth, "combustion_drive") outcome = game.action_build_ship("max", earth, "light_fighter") assert outcome.success assert len(game_state.players["max"].fleets) == 1 assert ( game_state.players["max"] .fleets[0] .light_fighter.components[ShipComponent] .number == 1 )
def test_upgrade_research(): game_state = initialise_gamestate() game = Game(game_state) game.update(24 * 3600) earth = PlanetLocation(1, 1, 3) upgrade_success = game.action_upgrade_research("max", earth, "energy") assert not upgrade_success upgrade_success = game.action_upgrade_building("max", earth, "research_lab") assert upgrade_success upgrade_success = game.action_upgrade_research("max", earth, "laser") assert not upgrade_success upgrade_success = game.action_upgrade_research("max", earth, "energy") assert upgrade_success upgrade_success = game.action_upgrade_research("max", earth, "laser") assert not upgrade_success upgrade_success = game.action_upgrade_building("max", earth, "research_lab") upgrade_sucess = game.action_upgrade_research("max", earth, "energy") upgrade_sucess = game.action_upgrade_research("max", earth, "laser") assert upgrade_success
def initialise_gamestate() -> GameState: game_state = GameState( world=World( planets={ PlanetLocation(1, 1, 3): Planet.new( name="Earth", planet_id=PlanetLocation(1, 1, 3), size=16, location=PlanetLocation(position=4, system=1, galaxy=1), owner_id="max", ) }), players={"max": Player.new(id="max", name="Max")}, ) return game_state
def test_universe_speed(): game_state = initialise_gamestate() game = Game(game_state) game_state.world.speed = 10000000 earth = game_state.world.planets[PlanetLocation(1, 1, 3)] game.update(1) assert earth.components[PlanetComponent].resources[Resources.Metal] == 10000
def test_ids(): game_state = initialise_gamestate() metal_mine = game_state.world.planets[PlanetLocation(1, 1, 3)].buildings[ "metal_mine" ] assert metal_mine.components[ProducerComponent]._entity_id == metal_mine.id assert metal_mine.id in EntityCatalog.entities_index
def test_fleet_transport_from_game(): game_state = init_state_complex() game = Game(game_state) game.update(3600) earth = PlanetLocation(1, 1, 3) assert ( game_state.world.planets[earth] .components[PlanetComponent] .resources[Resources.Metal] == 30 * game_state.world.speed ) game.action_send_mission( "max", PlanetLocation(1, 1, 3), "TRANSPORT", PlanetLocation(1, 1, 4), {Resources.Metal: 2, Resources.Cristal: 2}, ) assert ( game_state.world.planets[earth] .components[PlanetComponent] .resources[Resources.Metal] == 30 * game_state.world.speed - 2 ) game.update(5) fleets = game.get_player_fleets("max") fleet_comp = fleets[0].components[FleetComponent] assert fleet_comp.travelling_to == PlanetLocation(1, 1, 4) assert fleet_comp.cargo[Resources.Metal] == 2 game.update(100) assert fleet_comp.mission == "RETURN" assert fleet_comp.travelling_to == PlanetLocation(1, 1, 3) assert fleet_comp.cargo[Resources.Metal] == 0
def send_mission(player_id: str, planet_id: str, mission: str): content = request.json assert "destination_id" in content destination_id = content["destination_id"] if "cargo" in content: assert isinstance(content["cargo"], dict) cargo = { Resources(res): quantity for res, quantity in content["cargo"].items() } else: cargo = None action_outcome = game_thread.action_send_mission( player_id, PlanetLocation.from_str(planet_id), mission, PlanetLocation.from_str(destination_id), cargo, ) return action_outcome.to_json()
def get_random_free_location(self, galaxy_range=5, system_range=500, position_range=9) -> PlanetLocation: all_locations = set((g + 1, s + 1, p + 1) for g in range(galaxy_range) for s in range(system_range) for p in range(position_range)) taken_locations = set(loc for loc in self.planets_index) free_locations = all_locations - taken_locations random_free_location = random.choice(list(free_locations)) return PlanetLocation(*random_free_location)
def test_colonize_planet(): game_state = init_state_complex() game = Game(game_state) earth = PlanetLocation(1, 1, 3) game.update(100) game.action_upgrade_building("max", earth, "shipyard") game.update(100) game.action_build_ship("max", earth, "colony_ship") game.action_send_mission( "max", earth, "COLONIZE", PlanetLocation(1, 1, 6), cargo={Resources.Metal: 5} ) game.update(25) game.update(25) assert len(game_state.world.planets) == 4 new_planet = game_state.world.planets[PlanetLocation(1, 1, 6)] new_planet_comp = new_planet.components[PlanetComponent] assert new_planet.buildings["metal_mine"].components[BuildingComponent].level == 0
def test_game_update(): game_state = initialise_gamestate() game = Game(game_state) earth = game.game_state.world.planets[PlanetLocation(1, 1, 3)] game.update(3600) assert ( earth.components[PlanetComponent].resources[Resources.Metal] == 30 * game_state.world.speed ) earth.buildings["metal_mine"].components[BuildingComponent].level += 1 earth.buildings["solar_plant"].components[BuildingComponent].level += 1 # assert earth.components[PlanetComponent].production_per_second == 800 game.update(3600) assert ( earth.components[PlanetComponent].resources[Resources.Metal] > 60 * game_state.world.speed )
def upgrade_building(player_id: str, planet_id: str, building_id: str): return str( game_thread.action_upgrade_building(player_id, PlanetLocation.from_str(planet_id), building_id))
def build_ship(player_id: str, planet_id: str, ship_id: str): return str( game_thread.action_build_ship(player_id, PlanetLocation.from_str(planet_id), ship_id))
def init_state_complex() -> GameState: game_state = GameState( world=World( planets={ PlanetLocation(1, 1, 3): Planet.new( name="Earth", planet_id=PlanetLocation(1, 1, 3), size=250, location=PlanetLocation(position=3, system=1, galaxy=1), owner_id="max", ), PlanetLocation(1, 1, 4): Planet.new( name="Mars", planet_id=PlanetLocation(1, 1, 4), size=200, location=PlanetLocation(position=4, system=1, galaxy=1), owner_id="max", ), PlanetLocation(1, 1, 5): Planet.new( name="Jupiter", planet_id=PlanetLocation(1, 1, 5), size=300, location=PlanetLocation(1, 1, 5), owner_id="bob", ), }), players={ "max": Player.new(id="max", name="Max"), "bob": Player.new(id="bob", name="Bob"), }, ) earth_fleet = Fleet.new("max", PlanetLocation(1, 1, 3)) earth_fleet.light_fighter.components[ShipComponent].number = 10 mars_fleet = Fleet.new("max", PlanetLocation(1, 1, 4)) mars_fleet.heavy_fighter.components[ShipComponent].number = 5 game_state.players["max"].fleets = [earth_fleet, mars_fleet] jupiter_fleet = Fleet.new("bob", PlanetLocation(1, 1, 5)) jupiter_fleet.heavy_fighter.components[ShipComponent].number = 5 game_state.players["bob"].fleets = [jupiter_fleet] return game_state
def upgrade_research(player_id: str, planet_id: str, research_id: str): return str( game_thread.action_upgrade_research(player_id, PlanetLocation.from_str(planet_id), research_id))