Skip to content

Commit

Permalink
NOISSUE Rename 'walkable' tag to 'tile' (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-szweda authored Sep 16, 2023
1 parent 029eafb commit 289acc1
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 82 deletions.
4 changes: 2 additions & 2 deletions lynx/common/actions/common_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lynx/common/actions/drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions lynx/common/actions/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lynx/common/actions/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lynx/common/actions/user_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
14 changes: 7 additions & 7 deletions lynx/common/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
37 changes: 18 additions & 19 deletions lynx/common/square.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 11 additions & 11 deletions tests/common_requirements_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions tests/drop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,27 @@ 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

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

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)
Expand Down
6 changes: 3 additions & 3 deletions tests/move_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions tests/push_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
40 changes: 20 additions & 20 deletions tests/square_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/user_helper_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

2 comments on commit 289acc1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
lynx/common
   enums.py16850%17–24
   object.py24196%35
   scene.py881978%53, 71–72, 75, 78, 81–85, 88–94, 100, 106
lynx/common/actions
   common_requirements.py54394%18–19, 32
   create_object.py13285%20–22
   error_log.py10280%15, 18
   message_log.py10280%15, 18
   print.py10280%15, 18
   remove_object.py12192%21
   update_resources.py12283%17, 20
TOTAL5424292% 

Tests Skipped Failures Errors Time
82 0 💤 0 ❌ 0 🔥 0.342s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
lynx/common
   enums.py16850%17–24
   object.py24196%35
   scene.py881978%53, 71–72, 75, 78, 81–85, 88–94, 100, 106
lynx/common/actions
   common_requirements.py54394%18–19, 32
   create_object.py13285%20–22
   error_log.py10280%15, 18
   message_log.py10280%15, 18
   print.py10280%15, 18
   remove_object.py12192%21
   update_resources.py12283%17, 20
TOTAL5424292% 

Tests Skipped Failures Errors Time
82 0 💤 0 ❌ 0 🔥 0.342s ⏱️

Please sign in to comment.