Skip to content

Commit

Permalink
feat: add backpack with find method (#1657)
Browse files Browse the repository at this point in the history
* feat: add backpack with find method

* code review
  • Loading branch information
razvan-pro committed May 16, 2022
1 parent 1e59903 commit d79a354
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
17 changes: 8 additions & 9 deletions aimmo-game-worker/simulation/avatar_state.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from collections import namedtuple
from simulation.location import Location
from typing import Dict

from .backpack import Backpack
from .location import Location


def create_avatar_state(avatar_state_json: Dict):
avatar_state_dict = avatar_state_json.copy()
avatar_state_dict["location"] = Location(**avatar_state_json["location"])

# check backpack as it doesn't always exist (i.e. worksheet 1)
if avatar_state_json.get("backpack"):
if "backpack" in avatar_state_json:
# use namedtuple for artefacts to allow accessing fields by name
avatar_state_dict["backpack"] = [
namedtuple("Artefact", artefact.keys())(*artefact.values())
for artefact in avatar_state_json["backpack"]
]
avatar_state_dict["backpack"] = Backpack(
[namedtuple("Artefact", artefact.keys())(*artefact.values()) for artefact in avatar_state_json["backpack"]]
)

return namedtuple("AvatarState", avatar_state_dict.keys())(
*avatar_state_dict.values()
)
return namedtuple("AvatarState", avatar_state_dict.keys())(*avatar_state_dict.values())
24 changes: 24 additions & 0 deletions aimmo-game-worker/simulation/backpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from collections import namedtuple
from multiprocessing.sharedctypes import Value
from typing import TypeVar, List

T = TypeVar("T")


class Backpack(List[T]):
"""
A list of artefacts that has a `find` method to find the first index of an artefact type.
"""

def find(self, artefact_type: str) -> int:
"""
Finds the first artefact of type `artefact_type` and returns its index or -1 if there's no artefact of that
type.
Args:
artefact_type: The type of artefact to find.
Returns:
The index of the artefact or -1 if it doesn't exist.
"""
return next((i for i, artefact in enumerate(self) if artefact.type == artefact_type), -1)
14 changes: 13 additions & 1 deletion aimmo-game-worker/tests/tests_simulation/test_avatar_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ def avatar_state_json():
return {"location": {"x": 0, "y": 0}, "backpack": [{"type": "key"}]}


@pytest.fixture
def avatar_state_json_no_backpack():
return {"location": {"x": 1, "y": 1}}


def test_create_avatar_state(avatar_state_json):
avatar_state = create_avatar_state(avatar_state_json)
avatar_loc = avatar_state_json["location"]

assert avatar_state.location == Location(avatar_loc["x"], avatar_loc["y"])

assert avatar_state.backpack[0].type == avatar_state_json["backpack"][0]["type"]


def test_create_avatar_state_no_backpack(avatar_state_json_no_backpack):
avatar_state = create_avatar_state(avatar_state_json_no_backpack)
avatar_loc = avatar_state_json_no_backpack["location"]

assert avatar_state.location == Location(avatar_loc["x"], avatar_loc["y"])
assert not hasattr(avatar_state, "backpack")
46 changes: 46 additions & 0 deletions aimmo-game-worker/tests/tests_simulation/test_backpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from collections import namedtuple
import pytest

from simulation.backpack import Backpack

Artefact = namedtuple("Artefact", "type")


@pytest.fixture
def keyboard_artefact():
return Artefact("keyboard")


@pytest.fixture
def phone_artefact():
return Artefact("phone")


@pytest.fixture
def coins_artefact():
return Artefact("coins")


@pytest.fixture
def orb_artefact():
return Artefact("orb")


@pytest.fixture
def empty_backpack():
return Backpack()


@pytest.fixture
def backpack(keyboard_artefact, phone_artefact, coins_artefact):
return Backpack(
[keyboard_artefact, keyboard_artefact, phone_artefact, coins_artefact, phone_artefact, coins_artefact]
)


def test_backpack_find(empty_backpack, backpack, keyboard_artefact, phone_artefact, coins_artefact, orb_artefact):
assert empty_backpack.find("test") == -1
assert backpack.find(keyboard_artefact.type) == 0
assert backpack.find(phone_artefact.type) == 2
assert backpack.find(coins_artefact.type) == 3
assert backpack.find(orb_artefact.type) == -1

0 comments on commit d79a354

Please sign in to comment.