def test_score_cells(self): score_cell1 = MockCell() score_cell1.interactable = ScoreLocation(score_cell1) score_cell2 = MockCell() score_cell2.interactable = ScoreLocation(score_cell2) no_score_cell = MockCell() grid = self._grid_from_list([[score_cell1, no_score_cell], [no_score_cell, score_cell2]]) world_map = WorldMap(grid, self.settings) cells = list(world_map.score_cells()) self.assertIn(score_cell1, cells) self.assertIn(score_cell2, cells) self.assertEqual(len(cells), 2, "Non-scoring cells present")
def setUp(self): """ Sets up the JSON of the world state generated by the service file for testing. """ self.avatar_manager = self.DummyAvatarManager() CELLS = [ [ { "interactable": MockPickup("b"), "avatar": self.avatar_manager.avatars[0], }, {}, {}, ], [{}, { "habitable": False }, { "interactable": MockPickup("a") }], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in range(3) for x in range(2) } grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) test_game_state = GameState(WorldMap(grid, {}), self.avatar_manager) self.world_state_json = test_game_state.serialize()
def test_score_location_increase_score_of_avatar(test_data): """ Avatar spawns at the origin (0,0) and should have a score of 0. Moves EAST to (1,0) and should automatically then receive an effect that will increase the avatars score. """ game, cell = test_data cell.interactable = ScoreLocation(cell) assert game.avatar_manager.get_avatar(1).score is 0 assert cell.interactable.serialize() == { "type": "score", "location": { "x": cell.location.x, "y": cell.location.y }, } loop = asyncio.get_event_loop() loop.run_until_complete( game.simulation_runner.run_single_turn( game.avatar_manager.get_player_id_to_serialized_action())) assert cell.avatar is game.avatar_manager.get_avatar(1) assert cell.avatar.score is 1 assert len(cell.avatar.effects) is 1
def get_map(self): world_map = WorldMap.generate_empty_map(1, 5, self.settings) world_map = WorldMapStaticSpawnDecorator(world_map, Location(-2, 0)) world_map.get_cell(Location(2, 0)).interactable = ScoreLocation( world_map.get_cell(Location(2, 0)) ) return world_map
def test_scores_applied(self): self.construct_simulation_runner([], []) grid = self._generate_grid() avatar = DummyAvatar() grid[Location(1, 1)].interactable = ScoreLocation(grid[Location(1, 1)]) grid[Location(1, 1)].avatar = avatar self.simulation_runner.game_state.world_map = WorldMap(grid, SETTINGS) self.simulation_runner.update(1, self.simulation_runner.game_state) assert avatar.score == 1
def test_get_all_score_locations(self): score_cell = MockCell() score_cell.interactable = ScoreLocation(score_cell) grid = self._grid_from_list([[score_cell, MockCell()], [MockCell(), MockCell()]]) world_map = WorldMap(grid, self.settings) score_list = list(world_map.score_cells()) self.assertEqual(len(score_list), 1) self.assertTrue(isinstance(score_list[0].interactable, ScoreLocation))
def test_not_enough_pickup_space(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["TARGET_NUM_PICKUPS_PER_AVATAR"] = 1 grid = self._generate_grid(1, 1) grid[Location(0, 0)].interactable = ScoreLocation(grid[Location(0, 0)]) self.simulation_runner.game_state.world_map = WorldMap(grid, settings) self.simulation_runner.update(1, self.simulation_runner.game_state) assert (len( list(self.simulation_runner.game_state.world_map.pickup_cells())) == 0)
def test_scores_removed(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["SCORE_DESPAWN_CHANCE"] = 1 grid = self._generate_grid() grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) self.simulation_runner.game_state.world_map = WorldMap(grid, settings) self.simulation_runner.update(1, self.simulation_runner.game_state) self.assertEqual( len(list( self.simulation_runner.game_state.world_map.score_cells())), 0)
def test_scores_not_added_when_at_target(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR"] = 1 grid = self._generate_grid() grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) self.simulation_runner.game_state.world_map = WorldMap(grid, settings) self.simulation_runner.update(1, self.simulation_runner.game_state) assert len( list(self.simulation_runner.game_state.world_map.score_cells()) ) == 1 assert (grid[Location(0, 1)] in self.simulation_runner.game_state.world_map.score_cells())
def test_score_despawn_chance(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR"] = 0 grid = self._generate_grid() grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) self.simulation_runner.game_state.world_map = WorldMap(grid, settings) self.simulation_runner.update(1, self.simulation_runner.game_state) self.assertIn( grid[Location(0, 1)], self.simulation_runner.game_state.world_map.score_cells(), ) self.assertEqual( len(list( self.simulation_runner.game_state.world_map.score_cells())), 1)
def update(self, world_map, context): for cell in world_map.score_cells(): if random.random() < world_map.settings["SCORE_DESPAWN_CHANCE"]: cell.interactable.delete() new_num_score_locations = len(list(world_map.score_cells())) target_num_score_locations = int( math.ceil( context.num_avatars * world_map.settings["TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR"])) num_score_locations_to_add = (target_num_score_locations - new_num_score_locations) locations = world_map._spawn_location_finder.get_random_spawn_locations( num_score_locations_to_add) for cell in locations: cell.interactable = ScoreLocation(cell)
def test_potential_spawns(self): spawnable1 = MockCell() spawnable2 = MockCell() score_cell = MockCell() score_cell.interactable = ScoreLocation(score_cell) unhabitable = MockCell(habitable=False) filled = MockCell(avatar="avatar") grid = self._grid_from_list( [[spawnable1, score_cell, unhabitable], [unhabitable, spawnable2, filled]] ) world_map = WorldMap(grid, self.settings) spawn_location_finder = SpawnLocationFinder(world_map) cells = list(spawn_location_finder.potential_spawn_locations()) self.assertIn(spawnable1, cells) self.assertIn(spawnable2, cells) self.assertNotIn(score_cell, cells, "Score cells should not be spawns") self.assertNotIn(unhabitable, cells, "Unhabitable cells should not be spawns") self.assertNotIn(filled, cells, "Cells with avatars should not be spawns") self.assertEqual(len(cells), 2)
def test_score_locations_persist_and_keep_giving_score_effects(test_data): """ Checks if score can be increased more than once. First moved from ORIGIN to 1,0 -> then is given some score, remains in place due to an obstacle, then next turn it's score should increase again. """ game, cell = test_data loop = asyncio.get_event_loop() cell.interactable = ScoreLocation(cell) loop.run_until_complete( game.simulation_runner.run_single_turn( game.avatar_manager.get_player_id_to_serialized_action())) assert cell.avatar is game.avatar_manager.get_avatar(1) assert cell.avatar.score is 1 loop.run_until_complete( game.simulation_runner.run_single_turn( game.avatar_manager.get_player_id_to_serialized_action())) assert cell.avatar.score is 2 assert cell.avatar is game.avatar_manager.get_avatar(1)
def world_state_json(avatar_manager): CELLS = [ [ { "interactable": MockPickup("b"), "avatar": avatar_manager.avatars[0], }, {}, {}, ], [{}, {"habitable": False}, {"interactable": MockPickup("a")}], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in range(3) for x in range(2) } grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) test_game_state = GameState(WorldMap(grid, {}), avatar_manager) return test_game_state.serialize()
async def test_score_location_increase_score_of_avatar(test_data, loop): """ Avatar spawns at the origin (0,0) and should have a score of 0. Moves EAST to (1,0) and should automatically then receive an effect that will increase the avatars score. """ game, cell = test_data cell.interactable = ScoreLocation(cell) assert game.avatar_manager.get_avatar(1).score is 0 assert cell.interactable.serialize() == { "type": "score", "location": { "x": cell.location.x, "y": cell.location.y }, } await game.simulation_runner.run_single_turn( game.turn_collector.collected_turn_actions) assert cell.avatar is game.avatar_manager.get_avatar(1) assert cell.avatar.score is 1 assert len(cell.avatar.effects) is 1
def get_cell(self, location): default_cell = Cell(location) if location.x % 2 == 1: default_cell.interactable = ScoreLocation(default_cell) return self._cell_cache.setdefault(location, default_cell)