def setUp(self): """ Sets up the JSON of the world state generated by the service file for testing. """ self.avatar_manager = self.DummyAvatarManager() CELLS = [ [ { 'pickup': MockPickup('b'), 'avatar': self.avatar_manager.avatars[0] }, {}, { 'generates_score': True }, ], [ {}, { 'habitable': False }, { 'pickup': 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) } test_game_state = GameState(WorldMap(grid, {}), self.avatar_manager) self.world_state_json = test_game_state.serialise()
def test_successful_move_action(self): # Move north game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) target_cell = game_state.world_map.get_cell(NORTH_OF_ORIGIN) self.assertEqual(self.avatar.location, NORTH_OF_ORIGIN) self.assertEqual(self.avatar, target_cell.avatar) self.assertEqual(self.avatar.events, [event.MovedEvent(ORIGIN, NORTH_OF_ORIGIN)]) # Move east self.setUp() game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, { "x": 1, "y": 0 }).process(game_state.world_map) self.assertEqual(self.avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.avatar.events, [event.MovedEvent(ORIGIN, EAST_OF_ORIGIN)])
def test_add_avatar(self): state = GameState(AvatarMap(None), DummyAvatarManager()) state.add_avatar(7, "") self.assertIn(7, state.avatar_manager.avatars_by_id) avatar = state.avatar_manager.avatars_by_id[7] self.assertEqual(avatar.location.x, 10) self.assertEqual(avatar.location.y, 10)
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 construct_turn_manager(self, avatars, locations): self.avatar_manager = DummyAvatarManager(avatars) self.game_state = GameState(InfiniteMap(), self.avatar_manager) self.turn_manager = ConcurrentTurnManager( game_state=self.game_state, end_turn_callback=lambda: None) for index, location in enumerate(locations): self.game_state.add_avatar(index, "", location) return self.turn_manager
def test_updates_map_with_correct_num_avatars(self): map = InfiniteMap() manager = DummyAvatarManager() manager.add_avatar(1, '', None) state = GameState(map, manager) state.update_environment() self.assertEqual(map.num_avatars, 1) manager.add_avatar(2, '', None) manager.add_avatar(3, '', None) state.update_environment() self.assertEqual(map.num_avatars, 3)
def test_is_complete_calls_lambda(self): class LambdaTest(object): def __init__(self, return_value): self.return_value = return_value def __call__(self, game_state): self.game_state = game_state return self.return_value test = LambdaTest(True) game_state = GameState(EmptyMap(), DummyAvatarManager(), test) self.assertTrue(game_state.is_complete()) test.return_value = False self.assertFalse(game_state.is_complete())
def test_successful_attack_action(self): game_state = GameState(AvatarMap(self.other_avatar), self.avatar_manager) action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) target_location = NORTH_OF_ORIGIN damage_dealt = 1 self.assertEqual(self.avatar.location, ORIGIN) self.assertEqual(self.other_avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.other_avatar.times_died, 0) self.assertEqual(self.other_avatar.health, 4) self.assertEqual( self.avatar.events, [ event.PerformedAttackEvent(self.other_avatar, target_location, damage_dealt) ], ) self.assertEqual( self.other_avatar.events, [event.ReceivedAttackEvent(self.avatar, damage_dealt)], )
def test_successful_multiple_attack_actions(self): game_state = GameState(AvatarMap(self.other_avatar), self.avatar_manager) action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) self.assertEqual(self.other_avatar.events, [event.ReceivedAttackEvent(self.avatar, 1)]) action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) self.assertEqual( self.other_avatar.events, [ event.ReceivedAttackEvent(self.avatar, 1), event.ReceivedAttackEvent(self.avatar, 1), ], ) self.assertEqual(self.avatar.location, ORIGIN) self.assertEqual(self.other_avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.other_avatar.times_died, 0) self.assertEqual(self.other_avatar.health, 3)
def setup_world(self): avatar_manager = SimpleAvatarManager() CELLS = [ [ { 'pickup': MockPickup('b'), 'avatar': avatar_manager.avatars[0] }, {}, { 'generates_score': True }, ], [ {}, { 'habitable': False }, { 'pickup': MockPickup('a') }, ], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in xrange(3) for x in xrange(2) } state_provider.set_world(GameState(WorldMap(grid, {}), avatar_manager)) return service.get_world_state()
def test_successful_multiple_attack_actions(self): game_state = GameState(AvatarMap(self.other_avatar), self.avatar_manager) action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) assert self.other_avatar.events == [ event.ReceivedAttackEvent(self.avatar, 1) ] action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) assert self.other_avatar.events == [ event.ReceivedAttackEvent(self.avatar, 1), event.ReceivedAttackEvent(self.avatar, 1), ] assert self.avatar.location == ORIGIN assert self.other_avatar.location == EAST_OF_ORIGIN assert self.other_avatar.times_died == 0 assert self.other_avatar.health == 3
def construct_turn_manager(self, avatars, locations): self.avatar_manager = DummyAvatarManager(avatars) self.game_state = GameState(InfiniteMap(), self.avatar_manager) self.turn_manager = ConcurrentTurnManager(game_state=self.game_state, end_turn_callback=lambda: None) for index, location in enumerate(locations): self.game_state.add_avatar(index, "", location) return self.turn_manager
def test_failed_move_action(self): game_state = GameState(EmptyMap(), self.avatar_manager) action.MoveAction(self.avatar, {"x": 0, "y": 1}).process(game_state.world_map) self.assertEqual(self.avatar.location, ORIGIN) self.assertEqual( self.avatar.events, [event.FailedMoveEvent(ORIGIN, NORTH_OF_ORIGIN)] )
def run_game(): print("Running game...") my_map = map_generator.generate_map(15, 15, 0.1) player_manager = AvatarManager([]) game_state = GameState(my_map, player_manager) turn_manager = TurnManager(game_state) turn_manager.run_game()
def test_successful_pickup_action(self): game_state = GameState(PickupMap(Artefact), self.avatar_manager) action.PickupAction(self.avatar).process(game_state.world_map) self.assertEqual( self.avatar.events, [event.PickedUpEvent({"type": "artefact", "location": ORIGIN.serialize()})], )
def run_game(): print("Running game...") my_map = map_generator.generate_map(10, 10, 0.1) player_manager = AvatarManager() game_state = GameState(my_map, player_manager) turn_manager = TurnManager(game_state=game_state, end_turn_callback=send_world_update) worker_manager = LocalWorkerManager(game_state=game_state, users_url='http://localhost:8000/players/api/games/') worker_manager.start() turn_manager.start()
def test_failed_attack_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) AttackAction(direction.NORTH).apply(game_state, self.avatar) target_location = NORTH_OF_ORIGIN self.assertEqual(self.avatar.location, ORIGIN) self.assertEqual(self.other_avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.avatar.events, [FailedAttackEvent(target_location)]) self.assertEqual(self.other_avatar.events, [])
def test_successful_move_east_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, { 'x': 1, 'y': 0 }).process(game_state.world_map) self.assertEqual(self.avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.avatar.events, [event.MovedEvent(ORIGIN, EAST_OF_ORIGIN)])
def test_failed_attack_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.AttackAction(self.avatar, {'x': 0, 'y': 1}).process(game_state.world_map) target_location = NORTH_OF_ORIGIN self.assertEqual(self.avatar.location, ORIGIN) self.assertEqual(self.other_avatar.location, EAST_OF_ORIGIN) self.assertEqual(self.avatar.events, [event.FailedAttackEvent(target_location)]) self.assertEqual(self.other_avatar.events, [])
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()
def test_remove_avatar(self): world_map = InfiniteMap() manager = DummyAvatarManager() game_state = GameState(world_map, manager) avatar1 = DummyAvatar(1, Location(0, 0)) avatar2 = DummyAvatar(2, Location(1, 1)) avatar2.marked = True manager.add_avatar_directly(avatar1) world_map.get_cell(Location(0, 0)).avatar = avatar1 manager.add_avatar_directly(avatar2) world_map.get_cell(Location(1, 1)).avatar = avatar2 game_state.remove_avatar(1) self.assertNotIn(1, manager.avatars_by_id) self.assertEqual(world_map.get_cell((0, 0)).avatar, None) self.assertTrue(manager.avatars_by_id[2].marked) self.assertTrue(world_map.get_cell(Location(1, 1)).avatar.marked)
def test_remove_avatar(self): world_map = InfiniteMap() manager = DummyAvatarManager() game_state = GameState(world_map, manager) avatar1 = DummyAvatar(1, Location(0, 0)) avatar2 = DummyAvatar(2, Location(1, 1)) avatar2.marked = True manager.add_avatar_directly(avatar1) world_map.get_cell(Location(0, 0)).avatar = avatar1 manager.add_avatar_directly(avatar2) world_map.get_cell(Location(1, 1)).avatar = avatar2 game_state.remove_avatar(1) self.assertNotIn(1, manager.avatars_by_id) self.assertEqual(world_map.get_cell(Location(0, 0)).avatar, None) self.assertTrue(manager.avatars_by_id[2].marked) self.assertTrue(world_map.get_cell(Location(1, 1)).avatar.marked)
def test_successful_move_east_twice_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, { "x": 1, "y": 0 }).process(game_state.world_map) action.MoveAction(self.avatar, { "x": 1, "y": 0 }).process(game_state.world_map) self.assertEqual(self.avatar.location, Location(2, 0))
def test_move_action_to_score_square(self): game_state = GameState(ScoreOnOddColumnsMap(), self.avatar_manager) self.assertEqual(self.avatar.score, 0) MoveAction(direction.EAST).apply(game_state, self.avatar) self.assertEqual(self.avatar.score, 1) MoveAction(direction.EAST).apply(game_state, self.avatar) self.assertEqual(self.avatar.score, 1) MoveAction(direction.EAST).apply(game_state, self.avatar) self.assertEqual(self.avatar.score, 2)
def test_successful_move_north_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, { 'x': 0, 'y': 1 }).process(game_state.world_map) target_cell = game_state.world_map.get_cell(NORTH_OF_ORIGIN) self.assertEqual(self.avatar.location, NORTH_OF_ORIGIN) self.assertEqual(self.avatar, target_cell.avatar) self.assertEqual(self.avatar.events, [event.MovedEvent(ORIGIN, NORTH_OF_ORIGIN)])
def test_failed_attack_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.AttackAction(self.avatar, { "x": 0, "y": 1 }).process(game_state.world_map) target_location = NORTH_OF_ORIGIN assert self.avatar.location == ORIGIN assert self.other_avatar.location == EAST_OF_ORIGIN assert self.avatar.events == [event.FailedAttackEvent(target_location)] assert self.other_avatar.events == []
def game_state_with_two_avatars(self, world_map=None, avatar_manager=None): if world_map is None: world_map = EmptyMap() if avatar_manager is None: avatar_manager = DummyAvatarManager() avatar = DummyAvatar(1, (0, 0)) other_avatar = DummyAvatar(2, (0, 0)) other_avatar.marked = True avatar_manager.avatars_by_id[1] = avatar avatar_manager.avatars_by_id[2] = other_avatar game_state = GameState(world_map, avatar_manager) return (game_state, avatar, world_map, avatar_manager)
def game_runner(): async def mock_callback(): pass game_state = GameState(InfiniteMap(), AvatarManager()) game_runner = GameRunner( worker_manager_class=ConcreteWorkerManager, game_state_generator=lambda avatar_manager: game_state, port='0000', django_api_url='http://test') game_runner.communicator = MockCommunicator() game_runner.set_end_turn_callback(mock_callback) return game_runner
def game_runner(): async def mock_callback(): pass game_state = GameState(InfiniteMap(), AvatarManager()) game_runner = GameRunner( game_state_generator=lambda avatar_manager: game_state, port="0000", communicator=MockCommunicator(), worker_manager_class=MockWorkerManager, ) game_runner.set_end_turn_callback(mock_callback) return game_runner
def test_failed_pickup_action_if_backpack_full(self): game_state = GameState(PickupMap(Artefact), self.avatar_manager) game_state.world_map.setup_cell(self.avatar.location) artefact = game_state.world_map.get_cell( self.avatar.location).interactable self.avatar.backpack = [ Artefact for _ in range(self.avatar.BACKPACK_SIZE) ] action.PickupAction(self.avatar).process(game_state.world_map) self.assertEqual(self.avatar.events, [event.FailedPickupEvent()]) self.assertEquals(artefact.in_backpack, False)
def test_successful_pickup_action(self): game_state = GameState(PickupMap(Artefact), self.avatar_manager) game_state.world_map.setup_cell(self.avatar.location) artefact = game_state.world_map.get_cell( self.avatar.location).interactable self.assertEquals(artefact.in_backpack, False) action.PickupAction(self.avatar).process(game_state.world_map) self.assertEqual( self.avatar.events, [event.PickedUpEvent({"type": "artefact"})], ) self.assertEquals(artefact.in_backpack, True)
def test_successful_pickup_action(self): game_state = GameState(PickupMap(YellowOrbArtefact), self.avatar_manager) game_state.world_map.setup_cell(self.avatar.location) artefact = game_state.world_map.get_cell( self.avatar.location).interactable assert artefact.in_backpack == False action.PickupAction(self.avatar).process(game_state.world_map) assert self.avatar.events == [ event.PickedUpEvent({"type": "yellow_orb"}) ] assert artefact.in_backpack == True
class TestTurnManager(unittest.TestCase): def construct_default_avatar_appearance(self): return AvatarAppearance("#000", "#ddd", "#777", "#fff") def construct_turn_manager(self, avatars, locations): self.avatar_manager = DummyAvatarManager(avatars) self.game_state = GameState(InfiniteMap(), self.avatar_manager) self.turn_manager = ConcurrentTurnManager(game_state=self.game_state, end_turn_callback=lambda: None) for index, location in enumerate(locations): self.game_state.add_avatar(index, "", location) return self.turn_manager def assert_at(self, avatar, location): self.assertEqual(avatar.location, location) cell = self.game_state.world_map.get_cell(location) self.assertEqual(cell.avatar, avatar) def get_avatar(self, player_id): return self.avatar_manager.get_avatar(player_id) def run_turn(self): self.turn_manager.run_turn() def test_run_turn(self): ''' Given: > _ (1) Expect: _ o ''' self.construct_turn_manager([MoveEastDummy], [ORIGIN]) avatar = self.get_avatar(0) self.assert_at(avatar, ORIGIN) self.run_turn() self.assert_at(avatar, RIGHT_OF_ORIGIN) def test_run_several_turns(self): ''' Given: > _ _ _ _ _ (5) Expect: _ _ _ _ _ o ''' self.construct_turn_manager([MoveEastDummy], [ORIGIN]) avatar = self.get_avatar(0) self.assertEqual(avatar.location, ORIGIN) [self.run_turn() for _ in range(5)] self.assertEqual(avatar.location, FIVE_RIGHT_OF_ORIGIN) def test_run_several_turns_and_avatars(self): ''' Given: > _ _ _ _ _ > _ _ _ _ _ (5) Expect: _ _ _ _ _ o _ _ _ _ _ o ''' self.construct_turn_manager([MoveEastDummy, MoveEastDummy], [ORIGIN, ABOVE_ORIGIN]) avatar0 = self.get_avatar(0) avatar1 = self.get_avatar(1) self.assert_at(avatar0, ORIGIN) self.assert_at(avatar1, ABOVE_ORIGIN) [self.run_turn() for _ in range(5)] self.assert_at(avatar0, FIVE_RIGHT_OF_ORIGIN) self.assert_at(avatar1, FIVE_RIGHT_OF_ORIGIN_AND_ONE_ABOVE) def test_move_chain_succeeds(self): ''' Given: > > > > > _ Expect: _ o o o o o ''' self.construct_turn_manager([MoveEastDummy for _ in range(5)], [Location(x, 0) for x in range(5)]) avatars = [self.get_avatar(i) for i in range(5)] [self.assert_at(avatars[x], Location(x, 0)) for x in range(5)] self.run_turn() [self.assert_at(avatars[x], Location(x + 1, 0)) for x in range(5)] def test_move_chain_fails_occupied(self): ''' Given: > > x _ Expect: x x x _ ''' self.construct_turn_manager([MoveEastDummy, MoveEastDummy, WaitDummy], [Location(x, 0) for x in range(3)]) avatars = [self.get_avatar(i) for i in range(3)] [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)] self.run_turn() [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)] def test_move_chain_fails_collision(self): ''' Given: > > > _ < (1) Expect: x x x _ x ''' locations = [Location(0, 0), Location(1, 0), Location(2, 0), Location(4, 0)] self.construct_turn_manager( [MoveEastDummy, MoveEastDummy, MoveEastDummy, MoveWestDummy], locations) avatars = [self.get_avatar(i) for i in range(4)] [self.assert_at(avatars[i], locations[i]) for i in range(4)] self.run_turn() [self.assert_at(avatars[i], locations[i]) for i in range(4)] def test_move_chain_fails_cycle(self): ''' Given: > v ^ < (1) Expect: x x x x ''' locations = [Location(0, 1), Location(1, 1), Location(1, 0), Location(0, 0)] self.construct_turn_manager( [MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy], locations) avatars = [self.get_avatar(i) for i in range(4)] [self.assert_at(avatars[i], locations[i]) for i in range(4)] self.run_turn() [self.assert_at(avatars[i], locations[i]) for i in range(4)] def test_move_chain_fails_spiral(self): ''' Given: > > v ^ < (1) Expect: x x x x x ''' locations = [Location(0, 1), Location(1, 1), Location(2, 1), Location(2, 0), Location(1, 0)] self.construct_turn_manager( [MoveEastDummy, MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy], locations) avatars = [self.get_avatar(i) for i in range(5)] [self.assert_at(avatars[i], locations[i]) for i in range(5)] self.run_turn() [self.assert_at(avatars[i], locations[i]) for i in range(5)]
def test_remove_non_existant_avatar(self): state = GameState(None, DummyAvatarManager()) state.remove_avatar(10)
def test_fog_of_war(self): state = GameState(InfiniteMap(), DummyAvatarManager()) view = state.get_state_for(DummyAvatar(None, None), FogToEmpty()) self.assertEqual(len(view['world_map']['cells']), 0) self.assertEqual(view['avatar_state'], 'Dummy')
def test_updates_map(self): map = InfiniteMap() state = GameState(map, DummyAvatarManager()) state.update_environment() self.assertEqual(map.updates, 1)