Skip to content

Commit

Permalink
feat: allow inspection of artefact's type and types via the world map (
Browse files Browse the repository at this point in the history
…#1494)

* allow inspection of artefact's type and types via the world map

* feat: allow inspection of artefact's type and types via the world map

* Merge branch 'moreartefacts' of https://github.com/ocadotechnology/aimmo into moreartefacts

* check backpack existence

* add test coverage for avatar state

* Merge branch 'development' into moreartefacts

* Merge branch 'development' into moreartefacts
  • Loading branch information
dionizh committed Mar 26, 2021
1 parent 271dacb commit abcd6a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aimmo-game-worker/simulation/avatar_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
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"):
# 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"]
]

return namedtuple("AvatarState", avatar_state_dict.keys())(
*avatar_state_dict.values()
)
16 changes: 16 additions & 0 deletions aimmo-game-worker/simulation/world_map.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from collections import defaultdict
from enum import Enum

from .avatar_state import create_avatar_state
from .location import Location
from typing import Dict, List
Expand All @@ -11,6 +13,18 @@
PICKUP_TYPES = ["damage_boost", "invulnerability", "health"] + ARTEFACT_TYPES


class ArtefactType(Enum):
CHEST = "chest"
KEY = "key"
YELLOW_ORB = "yellow_orb"

def __eq__(self, other):
return self.value == other

def __str__(self):
return self.value


class Cell(object):

"""
Expand Down Expand Up @@ -93,6 +107,8 @@ class WorldMap(object):
The non-player world state.
"""

artefact_types = ArtefactType

def __init__(self, cells: Dict[Location, Cell]):
self.cells = cells

Expand Down
23 changes: 23 additions & 0 deletions aimmo-game-worker/tests/tests_simulation/test_avatar_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import absolute_import

from unittest import TestCase

from simulation.location import Location
from simulation.avatar_state import create_avatar_state


class TestAvatarState(TestCase):
AVATAR = {"location": {"x": 0, "y": 0}, "backpack": [{"type": "key"}]}

def test_create_avatar_state(self):
avatar_state = create_avatar_state(self.AVATAR)
avatar_loc = self.AVATAR["location"]

self.assertEqual(
avatar_state.location,
Location(avatar_loc["x"], avatar_loc["y"]),
)

self.assertEqual(
avatar_state.backpack[0].type, self.AVATAR["backpack"][0]["type"]
)

0 comments on commit abcd6a7

Please sign in to comment.