Skip to content

Commit

Permalink
fix: fix code not running when not near artefacts (#1736)
Browse files Browse the repository at this point in the history
* fix: fix code not running when not near artefacts

* altered the field

* added exception for types

* add indent

* adding an if statement

* extended the if statement

* fixed one test
  • Loading branch information
KamilPawel committed Jan 11, 2023
1 parent 0ed8ca5 commit 504238b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
35 changes: 19 additions & 16 deletions aimmo-game-worker/simulation/action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABCMeta, abstractmethod

from .direction import Direction
from .world_map import ArtefactType


class Action:
Expand Down Expand Up @@ -43,22 +44,24 @@ def serialise(self):
class MoveTowardsAction(Action):
def __init__(self, artefact):
self.direction = None

if not artefact:
print("MoveTowardsAction got an invalid parameter. Is it empty?")
return

if len(artefact.path) < 2:
return # not a valid path

# the first cell in the path is the starting cell
avatar_location = artefact.path[0].location
next_location = artefact.path[1].location

# calculate direction
x = next_location.x - avatar_location.x
y = next_location.y - avatar_location.y
self.direction = Direction(x, y)
if artefact:
try:
hasattr(ArtefactType, artefact.type)
except AttributeError:
print("MoveTowardsAction got an invalid parameter.")
return

if len(artefact.path) < 2:
return # not a valid path

# the first cell in the path is the starting cell
avatar_location = artefact.path[0].location
next_location = artefact.path[1].location

# calculate direction
x = next_location.x - avatar_location.x
y = next_location.y - avatar_location.y
self.direction = Direction(x, y)

def serialise(self):
if not self.direction:
Expand Down
4 changes: 1 addition & 3 deletions aimmo-game-worker/simulation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def __getitem__(self, i):
return super().__getitem__(i)
except IndexError:
if len(self) == 0:
raise NoNearbyArtefactsError(
"There aren't any nearby artefacts, you need to move closer!"
) from None
print("There aren't any nearby artefacts, you need to move closer!")
else:
raise
8 changes: 5 additions & 3 deletions aimmo-game-worker/tests/tests_simulation/test_world_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_cannot_move_to_inhabited_cell(avatar_state_json):
assert map.can_move_to(Location(-1, 0)) == False


def test_scan_nearby(avatar_state_json):
def test_scan_nearby(avatar_state_json, capsys):
cells = _generate_cells(5, 5)
cells[0]["avatar"] = avatar_state_json
cells[2]["obstacle"] = {"location": {"x": 0, "y": 0}}
Expand All @@ -163,5 +163,7 @@ def test_scan_nearby(avatar_state_json):
artefacts = map.scan_nearby(Location(5, 5), radius=1)
assert type(artefacts) == NearbyArtefactsList
assert len(artefacts) == 0
with pytest.raises(NoNearbyArtefactsError):
artefacts[0]
artefacts[0]
captured = capsys.readouterr()
# check the print statement matches
assert captured.out == "There aren't any nearby artefacts, you need to move closer!\n"

0 comments on commit 504238b

Please sign in to comment.