From 289acc1998791c83b0a96c8e39b311dddb0cbd7c Mon Sep 17 00:00:00 2001 From: Filip Szweda <68189467+filip-szweda@users.noreply.github.com> Date: Sun, 17 Sep 2023 01:00:31 +0200 Subject: [PATCH] NOISSUE Rename 'walkable' tag to 'tile' (#29) --- lynx/common/actions/common_requirements.py | 4 +- lynx/common/actions/drop.py | 2 +- lynx/common/actions/move.py | 4 +- lynx/common/actions/push.py | 2 +- lynx/common/actions/user_helper_functions.py | 8 ++-- lynx/common/scene.py | 14 +++---- lynx/common/square.py | 37 +++++++++--------- tests/common_requirements_test.py | 22 +++++------ tests/drop_test.py | 8 ++-- tests/move_test.py | 6 +-- tests/push_test.py | 10 ++--- tests/square_test.py | 40 ++++++++++---------- tests/user_helper_functions_test.py | 6 +-- 13 files changed, 81 insertions(+), 82 deletions(-) diff --git a/lynx/common/actions/common_requirements.py b/lynx/common/actions/common_requirements.py index 9e0a82b..589dd24 100644 --- a/lynx/common/actions/common_requirements.py +++ b/lynx/common/actions/common_requirements.py @@ -9,9 +9,9 @@ class CommonRequirements: @staticmethod - def is_walkable(scene: 'Scene', position: Vector) -> bool: + def is_tile(scene: 'Scene', position: Vector) -> bool: square_at_destination: Square = scene.get_square(position) - return square_at_destination.walkable() + return square_at_destination.tile() @staticmethod def is_on_square(scene: 'Scene', position: Vector, name: str) -> bool: diff --git a/lynx/common/actions/drop.py b/lynx/common/actions/drop.py index f7a3bd9..0c35e8f 100644 --- a/lynx/common/actions/drop.py +++ b/lynx/common/actions/drop.py @@ -49,6 +49,6 @@ def satisfies_requirements(self, scene: 'Scene') -> bool: agent: Object = scene.get_object_by_id(self.object_id) return CommonRequirements.is_in_range(scene, self.object_id, self.target_position, 1) \ - and (CommonRequirements.is_walkable(scene, self.target_position) + and (CommonRequirements.is_tile(scene, self.target_position) or scene.get_drop_area_of_a_player(agent.owner) == self.target_position) \ and CommonRequirements.has_something_in_inventory(scene, self.object_id) diff --git a/lynx/common/actions/move.py b/lynx/common/actions/move.py index 04f5a8b..5f9544b 100644 --- a/lynx/common/actions/move.py +++ b/lynx/common/actions/move.py @@ -12,14 +12,14 @@ class Move(Action): """ Simple action for changing position of `Object`. It does not log anything - in case the movement was not possible(destination is not walkable etc). + in case the movement was not possible (destination is not a tile etc). """ object_id: int = -1 direction: Vector = Direction.NORTH.value def satisfies_requirements(self, scene: 'Scene') -> bool: destination_position = scene.get_object_by_id(self.object_id).position + self.direction - return Req.is_walkable(scene, destination_position) and Req.is_proper_direction(self.direction) + return Req.is_tile(scene, destination_position) and Req.is_proper_direction(self.direction) def apply(self, scene: 'Scene') -> NoReturn: object: Object = scene.get_object_by_id(self.object_id) diff --git a/lynx/common/actions/push.py b/lynx/common/actions/push.py index 40e6131..0148c03 100644 --- a/lynx/common/actions/push.py +++ b/lynx/common/actions/push.py @@ -23,7 +23,7 @@ def satisfies_requirements(self, scene: 'Scene') -> bool: objects_on_square = scene.get_objects_by_position(pushed_position) for pushable_object in list(filter(lambda x: 'pushable' in x.tags, objects_on_square)): - if Req.is_walkable(scene, destination_position) or Req.can_be_stacked(scene, destination_position, pushable_object.name): + if Req.is_tile(scene, destination_position) or Req.can_be_stacked(scene, destination_position, pushable_object.name): self.pushed_object_ids.append(pushable_object.id) return Req.is_in_range(scene, self.object_id, pushed_position, 1) and Req.is_proper_direction(self.direction) and self.pushed_object_ids diff --git a/lynx/common/actions/user_helper_functions.py b/lynx/common/actions/user_helper_functions.py index 6705e10..23821a4 100644 --- a/lynx/common/actions/user_helper_functions.py +++ b/lynx/common/actions/user_helper_functions.py @@ -41,17 +41,17 @@ def get_position(scene: 'Scene', object_id: int) -> Vector: def can_i_stand(scene: 'Scene', position: Vector) -> bool: - """Returns True if the position is walkable. + """Returns True if the position is a tile. Args: scene: The scene. position: The position. Returns: - True if the position is walkable. + True if the position is a tile. """ - return Req.is_walkable(scene, position) + return Req.is_tile(scene, position) def get_type(scene: 'Scene', object_id: int) -> str: @@ -113,6 +113,6 @@ def random_direction(scene: 'Scene', object_id: int) -> Vector: positions: List[Vector] = [Vector(0, 1), Vector(1, 0), Vector(-1, 0), Vector(0, -1)] object_position: Vector = scene.get_object_by_id(object_id).position available_positions = list( - filter(lambda position: Req.is_walkable(scene, object_position + position), positions) + filter(lambda position: Req.is_tile(scene, object_position + position), positions) ) return random.choice(available_positions) diff --git a/lynx/common/scene.py b/lynx/common/scene.py index 1b6769f..b550047 100644 --- a/lynx/common/scene.py +++ b/lynx/common/scene.py @@ -77,16 +77,16 @@ def add_player(self, player: str) -> None: def is_world_created(self) -> bool: return bool(self.entities) - def get_walkable_positions(self) -> List[Vector]: - walkable_positions = [] + def get_tile_positions(self) -> List[Vector]: + tile_positions = [] for position in self._square_position_map.keys(): - if self._square_position_map[position].walkable(): - walkable_positions.append(position) - return walkable_positions + if self._square_position_map[position].tile(): + tile_positions.append(position) + return tile_positions def generate_drop_area(self, player: str) -> None: - walkable_positions = self.get_walkable_positions() - position = random.choice(walkable_positions) + tile_positions = self.get_tile_positions() + position = random.choice(tile_positions) drop_area = Object(name="DropArea", id=self.generate_id(), position=position, owner=player) create_drop_area = CreateObject(drop_area.serialize()) self.add_to_pending_actions(create_drop_area.serialize()) diff --git a/lynx/common/square.py b/lynx/common/square.py index 8e8eec3..33035ca 100644 --- a/lynx/common/square.py +++ b/lynx/common/square.py @@ -1,38 +1,37 @@ from dataclasses import dataclass, field -from typing import List, NoReturn +from typing import List, Optional from lynx.common.object import Object -# Class wraping logic of ground and objects placed on it +# class wrapping logic of a tile and objects placed on it @dataclass class Square: - ground: Object = None + tile_object: Optional[Object] = None objects: List[Object] = field(default_factory=list) - def append(self, object: Object) -> NoReturn: - # We might rename the walkable property into sth like ground - if object.has_tags(['walkable']) and self.ground != None: - # In the future when we add logger we should log this event! - raise Exception("Cannot put more than a one ground in a square!") + def append(self, object: Object) -> None: + if object.has_tags(['tile']) and self.tile_object is not None: + # TODO: in the future when we add logger we should log this event + raise Exception("Cannot put more than one tile in a square!") - if object.has_tags(['walkable']): - self.ground = object + if object.has_tags(['tile']): + self.tile_object = object self.objects.append(object) - def remove(self, object: Object) -> NoReturn: - if self.ground == object: - self.ground = None + def remove(self, object: Object) -> None: + if self.tile_object == object: + self.tile_object = None self.objects.remove(object) - def walkable(self) -> bool: - objects_tags = [object_on_ground.tags for object_on_ground in self.objects] - contains_walkable = True + def tile(self) -> bool: + objects_tags = [object_on_tile.tags for object_on_tile in self.objects] + contains_tile = True for object_tags in objects_tags: - if 'walkable' not in object_tags: - contains_walkable = False + if 'tile' not in object_tags: + contains_tile = False - return self.ground is not None and contains_walkable + return self.tile_object is not None and contains_tile diff --git a/tests/common_requirements_test.py b/tests/common_requirements_test.py index 5277a6c..07180c4 100644 --- a/tests/common_requirements_test.py +++ b/tests/common_requirements_test.py @@ -9,24 +9,24 @@ from lynx.common.enums import Direction -class TestIsWalkable: +class TestIsTile: @patch('lynx.common.scene.Scene') def test_success(self, mock_scene) -> NoReturn: mock_square_at_destination = Square() - mock_square_at_destination.walkable = MagicMock(return_value=True) + mock_square_at_destination.tile = MagicMock(return_value=True) mock_scene.get_square.return_value = mock_square_at_destination - result: bool = CommonRequirements.is_walkable(mock_scene, Vector(1, 0)) + result: bool = CommonRequirements.is_tile(mock_scene, Vector(1, 0)) assert result @patch('lynx.common.scene.Scene') def test_failure(self, mock_scene) -> NoReturn: mock_square_at_destination = Square() - mock_square_at_destination.walkable = MagicMock(return_value=False) + mock_square_at_destination.tile = MagicMock(return_value=False) mock_scene.get_square.return_value = mock_square_at_destination - result: bool = CommonRequirements.is_walkable(mock_scene, Vector(1, 0)) + result: bool = CommonRequirements.is_tile(mock_scene, Vector(1, 0)) assert not result @@ -59,20 +59,20 @@ def test_huge_vector_improper(self) -> NoReturn: class TestContainTags: scene = Scene() - dummy_object1 = Object(id=123, name="dummy", position=Vector(0, 0), tags=['walkable', 'pushable']) + dummy_object1 = Object(id=123, name="dummy", position=Vector(0, 0), tags=['tile', 'pushable']) scene.add_entity(dummy_object1) def test_has_proper_tag_positive(self) -> NoReturn: - assert CommonRequirements.has_given_tags(self.scene, 123, ['walkable']) + assert CommonRequirements.has_given_tags(self.scene, 123, ['tile']) def test_has_improper_tag_negative(self) -> NoReturn: assert not CommonRequirements.has_given_tags(self.scene, 123, ['insertable']) def test_has_proper_tags_positive(self) -> NoReturn: - assert CommonRequirements.has_given_tags(self.scene, 123, ['walkable', 'pushable']) + assert CommonRequirements.has_given_tags(self.scene, 123, ['tile', 'pushable']) def test_has_improper_tags_negative(self) -> NoReturn: - assert not CommonRequirements.has_given_tags(self.scene, 123, ['walkable', 'pushable', 'insertable']) + assert not CommonRequirements.has_given_tags(self.scene, 123, ['tile', 'pushable', 'insertable']) class TestIsAnyOnSquare: scene = Scene() @@ -90,8 +90,8 @@ def test_nonexistent_object_on_square_negative(self) -> NoReturn: class TestIsAnyObjectOnSquareHasAllGivenTags: scene = Scene() - dummy_object1 = Object(id=123, name="dummy", position=Vector(0, 0), tags=['walkable', 'pushable']) - dummy_object2 = Object(id=1233, name="dummy", position=Vector(0, 1), tags=['walkable']) + dummy_object1 = Object(id=123, name="dummy", position=Vector(0, 0), tags=['tile', 'pushable']) + dummy_object2 = Object(id=1233, name="dummy", position=Vector(0, 1), tags=['tile']) scene.add_entity(dummy_object1) scene.add_entity(dummy_object2) diff --git a/tests/drop_test.py b/tests/drop_test.py index ae9deb5..63fc659 100644 --- a/tests/drop_test.py +++ b/tests/drop_test.py @@ -107,7 +107,7 @@ def test_all_requirements_satisified_positive(self) -> None: scene = Scene(players=[Player(player_id="dummy", player_resources={"Wood": 0, "Stone": 0}, drop_area=Vector(5, 5))]) dummy_object = Object(id=1, name="dummy", owner="dummy", position=Vector(5, 5), inventory={"Wood": 1}) dummy_drop = Drop(target_position=Vector(5, 6), object_id=1) - scene.add_entity(Object(id=3, name="Grass", position=Vector(5, 6), tags=['walkable'])) + scene.add_entity(Object(id=3, name="Grass", position=Vector(5, 6), tags=['tile'])) scene.add_entity(dummy_object) assert dummy_drop.satisfies_requirements(scene) is True @@ -115,7 +115,7 @@ def test_requirements_agent_too_far_fail(self) -> None: scene = Scene(players=[Player(player_id="dummy", player_resources={"Wood": 0, "Stone": 0}, drop_area=Vector(5, 5))]) dummy_object = Object(id=1, name="dummy", owner="dummy", position=Vector(5, 5), inventory={"Wood": 1}) dummy_drop = Drop(target_position=Vector(6, 6), object_id=1) - scene.add_entity(Object(id=3, name="Grass", position=Vector(6, 6), tags=['walkable'])) + scene.add_entity(Object(id=3, name="Grass", position=Vector(6, 6), tags=['tile'])) scene.add_entity(dummy_object) assert dummy_drop.satisfies_requirements(scene) is not True @@ -123,11 +123,11 @@ def test_requirements_empty_inventory_fail(self) -> None: scene = Scene(players=[Player(player_id="dummy", player_resources={"Wood": 0, "Stone": 0}, drop_area=Vector(5, 5))]) dummy_object = Object(id=1, name="dummy", owner="dummy", position=Vector(5, 5)) dummy_drop = Drop(target_position=Vector(5, 6), object_id=1) - scene.add_entity(Object(id=3, name="Grass", position=Vector(5, 6), tags=['walkable'])) + scene.add_entity(Object(id=3, name="Grass", position=Vector(5, 6), tags=['tile'])) scene.add_entity(dummy_object) assert dummy_drop.satisfies_requirements(scene) is not True - def test_requirements_no_walkable_tile_fail(self) -> None: + def test_requirements_no_tile_fail(self) -> None: scene = Scene(players=[Player(player_id="dummy", player_resources={"Wood": 0, "Stone": 0}, drop_area=Vector(5, 5))]) dummy_object = Object(id=1, name="dummy", owner="dummy", position=Vector(5, 5), inventory={"Wood": 1}) dummy_drop = Drop(target_position=Vector(5, 6), object_id=1) diff --git a/tests/move_test.py b/tests/move_test.py index f5bc213..5081f40 100644 --- a/tests/move_test.py +++ b/tests/move_test.py @@ -48,9 +48,9 @@ def test_success(self, mock_scene): object_id=123, direction=Vector(1, 0) ) - walkable_square = Square() - walkable_square.walkable = MagicMock(return_value=True) - mock_scene.get_square.return_value = walkable_square + tile_square = Square() + tile_square.tile = MagicMock(return_value=True) + mock_scene.get_square.return_value = tile_square result: bool = dummy_action.satisfies_requirements(mock_scene) diff --git a/tests/push_test.py b/tests/push_test.py index cee80fd..60f9153 100644 --- a/tests/push_test.py +++ b/tests/push_test.py @@ -44,20 +44,20 @@ def test_failure(self) -> NoReturn: class TestPushSatisfiesRequirements: - def test_square_walkable_satisfies(self) -> NoReturn: + def test_square_tile_satisfies(self) -> NoReturn: scene = Scene() pushable_object = Object(id=123, name="dummy", position=Vector(1, 1), tags=['pushable']) pusher_object = Object(id=456, name="dummy", position=Vector(2, 1)) - walkable_object = Object(id=789, name="dummy123", position=Vector(0, 1), tags=['walkable']) + tile_object = Object(id=789, name="dummy123", position=Vector(0, 1), tags=['tile']) dummy_action = Push(object_id=456, direction=Vector(-1, 0)) scene.add_entity(pushable_object) scene.add_entity(pusher_object) - scene.add_entity(walkable_object) + scene.add_entity(tile_object) scene.add_entity(dummy_action) assert dummy_action.satisfies_requirements(scene) - def test_square_not_walkable_taken_by_same_object_satisfies(self) -> NoReturn: + def test_square_not_tile_taken_by_same_object_satisfies(self) -> NoReturn: scene = Scene() pushable_object = Object(id=123, name="dummy", position=Vector(1, 1), tags=['pushable']) pusher_object = Object(id=456, name="dummy", position=Vector(2, 1)) @@ -70,7 +70,7 @@ def test_square_not_walkable_taken_by_same_object_satisfies(self) -> NoReturn: assert dummy_action.satisfies_requirements(scene) - def test_square_not_walkable_taken_by_different_object_does_not_satisfy(self) -> NoReturn: + def test_square_not_tile_taken_by_different_object_does_not_satisfy(self) -> NoReturn: scene = Scene() pushable_object = Object(id=123, name="dummy", position=Vector(1, 1), tags=['pushable']) pusher_object = Object(id=456, name="dummy", position=Vector(2, 1)) diff --git a/tests/square_test.py b/tests/square_test.py index 9a8661c..3dd1d83 100644 --- a/tests/square_test.py +++ b/tests/square_test.py @@ -11,34 +11,34 @@ def test_object_success(self): square.append(object) assert square.objects == [object] - assert square.ground is None + assert square.tile_object is None - def test_ground_success(self): - object = Object(name="Ondrej", tags=['walkable']) + def test_tile_object_success(self): + object = Object(name="Ondrej", tags=['tile']) square = Square() square.append(object) assert square.objects == [object] - assert square.ground == object + assert square.tile_object == object - def test_object_ground_success(self): + def test_object_and_tile_object_success(self): object = Object(name="Ondrej") - ground = Object(name="Groundriej", tags=['walkable']) + tile_object = Object(name="tile_objectriej", tags=['tile']) square = Square() square.append(object) - square.append(ground) + square.append(tile_object) - assert square.objects == [object, ground] - assert square.ground == ground + assert square.objects == [object, tile_object] + assert square.tile_object == tile_object - def test_ground_failure(self): - object = Object(name="Ondrej", tags=['walkable']) - ground = Object(name="Groundriej", tags=['walkable']) + def test_tile_object_failure(self): + object = Object(name="Ondrej", tags=['tile']) + tile_object = Object(name="tile_objectriej", tags=['tile']) square = Square() square.append(object) with pytest.raises(Exception): - square.append(ground) + square.append(tile_object) class TestSquareRemove: @@ -49,20 +49,20 @@ def test_object_success(self): square.remove(object) assert square.objects == [] - assert square.ground is None + assert square.tile_object is None - def test_ground_success(self): + def test_tile_object_success(self): object = Object(name="Ondrej") - ground = Object(name="Groundriej", tags=['walkable']) + tile_object = Object(name="tile_objectriej", tags=['tile']) square = Square() square.append(object) - square.append(ground) - square.remove(ground) + square.append(tile_object) + square.remove(tile_object) assert square.objects == [object] - assert square.ground is None + assert square.tile_object is None - def test_ground_failure(self): + def test_tile_object_failure(self): object = Object(name="Ondrej") object2 = Object(name="Undriej") square = Square() diff --git a/tests/user_helper_functions_test.py b/tests/user_helper_functions_test.py index d27b6a0..c9561f3 100644 --- a/tests/user_helper_functions_test.py +++ b/tests/user_helper_functions_test.py @@ -11,7 +11,7 @@ class TestUserHelperFunctions: scene = Scene() dummy_agent = Object(id=1, name="agent", position=Vector(0, 0)) scene.add_entity(dummy_agent) - dummy_object1 = Object(id=2, name="dummy", position=Vector(5, 5), tags=["walkable"]) + dummy_object1 = Object(id=2, name="dummy", position=Vector(5, 5), tags=["tile"]) scene.add_entity(dummy_object1) dummy_object2 = Object(id=3, name="dummy", position=Vector(3, 5)) scene.add_entity(dummy_object2) @@ -50,8 +50,8 @@ def test_filter_objects_failure(self) -> NoReturn: def test_random_direction_success(self) -> NoReturn: random.seed(12) - self.scene.add_entity(Object(id=10, name="Grass", position=Vector(-1, 0), tags=["walkable"])) - self.scene.add_entity(Object(id=11, name="Grass", position=Vector(0, 1), tags=["walkable"])) + self.scene.add_entity(Object(id=10, name="Grass", position=Vector(-1, 0), tags=["tile"])) + self.scene.add_entity(Object(id=11, name="Grass", position=Vector(0, 1), tags=["tile"])) assert random_direction(self.scene, 1) == Vector(-1, 0) def test_random_direction_failure(self) -> NoReturn: