diff --git a/labyrinth_engine/__init__.py b/labyrinth_engine/__init__.py index 68afe43..a3bea84 100644 --- a/labyrinth_engine/__init__.py +++ b/labyrinth_engine/__init__.py @@ -2,3 +2,4 @@ from labyrinth_engine.labyrinth import Labyrinth from labyrinth_engine.labyrinth_object import LabyrinthObject from labyrinth_engine.lr_types import Location, Item, Player, Creature +from labyrinth_engine.uniqueness import unique diff --git a/labyrinth_engine/labyrinth_object.py b/labyrinth_engine/labyrinth_object.py index 65ccc9b..a53a8ef 100644 --- a/labyrinth_engine/labyrinth_object.py +++ b/labyrinth_engine/labyrinth_object.py @@ -8,6 +8,7 @@ class LabyrinthObject: """ _lrtype = 'labyrinth_object' + _metadata = {} def __init__(self): self.turn_set = {} @@ -86,6 +87,15 @@ def get_children(self, lrtype=['location', 'item', 'player', 'creature'], class_ def lrtype(self): return self._lrtype + @property + def metadata(self): + return self._metadata + + def add_meta(self, *args, **kwargs): + self._metadata.update({key: True for key in args}) + self._metadata.update(kwargs) + return self._metadata + def main(self): """ Основная функция объекта. Определяется здесь, чтобы потом не было ошибки при её вызове. diff --git a/labyrinth_engine/labyrinth_object.pyi b/labyrinth_engine/labyrinth_object.pyi index a16f675..281b540 100644 --- a/labyrinth_engine/labyrinth_object.pyi +++ b/labyrinth_engine/labyrinth_object.pyi @@ -7,7 +7,10 @@ from typing import Any, Dict, Union, List, Callable, TypeVar class LabyrinthObject: labyrinth: Labyrinth + _lrtype: str + _metadata: Dict[Any, Any] + turn_set: Dict[str, Dict[str, Callable[[], Any]]] flags: Dict[str: Any] button_set: List[Button] @@ -28,9 +31,9 @@ class LabyrinthObject: def get_flag(self, flag_name: str, default: Any = ...) -> Any: ... # Кнопки. - def new_button(self, turn: str, image: str): ... - def new_dbutton(self, turns: List[str], image: str): ... - def new_lbutton(self, turns: List[str], image: str, turn_images): ... + def new_button(self, turn: str, image: str) -> None: ... + def new_dbutton(self, turns: List[str], image: str) -> None: ... + def new_lbutton(self, turns: List[str], image: str, turn_images) -> None: ... def get_buttons(self) -> List[Button]: ... # Бары. @@ -47,8 +50,12 @@ class LabyrinthObject: key: Callable[[AnyLO], bool] = ...): ... @property - def lrtype(self) -> str: - return self._lrtype + def lrtype(self) -> str: ... + + @property + def metadata(self) -> Dict[Any, Any]: ... + + def add_meta(self, *args, **kwargs) -> Dict[Any, Any]: ... def main(self) -> None: ... diff --git a/labyrinth_engine/metadata.py b/labyrinth_engine/metadata.py new file mode 100644 index 0000000..5e62c46 --- /dev/null +++ b/labyrinth_engine/metadata.py @@ -0,0 +1,12 @@ +def add_meta(obj, *args, **kwargs): + if '_metadata' not in obj.__dict__: + obj._metadata = {} + obj._metadata.update({key: True for key in args}) + obj._metadata.update(kwargs) + return obj._metadata + + +def get_meta(obj): + if '_metadata' not in obj.__dict__: + return {} + return obj._metadata diff --git a/labyrinth_engine/uniqueness.py b/labyrinth_engine/uniqueness.py new file mode 100644 index 0000000..39627ae --- /dev/null +++ b/labyrinth_engine/uniqueness.py @@ -0,0 +1,21 @@ +# декоратор для класса описывающего уникальный предмет +def unique(key): + """ + key - ключ, который будет установлен + этому уникальному объекту. + """ + def unique_decorator(cls): + old_function = cls.set_settings + + def set_settings(self, *args, **kwargs): + """ + Новая функция дополнительно будет устанавливать + указанный уникальный ключ. + """ + self.labyrinth.set_unique(self, key) + return old_function(self, *args, **kwargs) + # изменяем функцию в классе. + cls.set_settings = set_settings + cls.add_meta('unique_object') + return cls + return unique_decorator diff --git a/labyrinth_objects/Vanilla/ammo.py b/labyrinth_objects/Vanilla/ammo.py index 51e7782..419a8cc 100644 --- a/labyrinth_objects/Vanilla/ammo.py +++ b/labyrinth_objects/Vanilla/ammo.py @@ -1,6 +1,7 @@ -from labyrinth_engine import Item +from labyrinth_engine import Item, unique +@unique('ammo') class Ammo(Item): def __init__(self): # def resetallself(): @@ -27,8 +28,6 @@ def set_settings(self, settings, locations, items, creatures, players): self.bombs = {player: self.INIT_BOMBS_COUNT for player in players} self.update_bars() - self.labyrinth.set_unique(self, 'ammo') - def spend(self, ammo_type, player): if ammo_type == 'bullet': self.bullets[player] -= 1 diff --git a/labyrinth_objects/Vanilla/death.py b/labyrinth_objects/Vanilla/death.py index 2c010f4..fa57f18 100644 --- a/labyrinth_objects/Vanilla/death.py +++ b/labyrinth_objects/Vanilla/death.py @@ -1,6 +1,7 @@ -from labyrinth_engine import Item +from labyrinth_engine import Item, unique +@unique('death') class Death(Item): def set_settings(self, settings, locations, items, creatures, players): self.DEATH_MSG = settings['death_msg']['ru'] @@ -12,8 +13,6 @@ def set_settings(self, settings, locations, items, creatures, players): self.death = {player: False for player in players} self.crt_death = {creature: False for creature in creatures} - self.labyrinth.set_unique(self, 'death') - def revive(self, body, revival_msg=None): if body.lrtype == 'player': self.death[body] = False diff --git a/labyrinth_objects/Vanilla/health.py b/labyrinth_objects/Vanilla/health.py index f3ffc63..c1156c4 100644 --- a/labyrinth_objects/Vanilla/health.py +++ b/labyrinth_objects/Vanilla/health.py @@ -1,6 +1,7 @@ -from labyrinth_engine import Item +from labyrinth_engine import Item, unique +@unique('health') class Health(Item): def __init__(self): super().__init__() @@ -23,8 +24,6 @@ def set_settings(self, settings, locations, items, creatures, players): self.hp = {player: self.MAX_PLAYER_HEALTH for player in players} self.creature_hp = {creature: self.MAX_CREATURE_HEALTH for creature in creatures} - self.labyrinth.set_unique(self, 'health') - self.update_health_bar() def hurt(self, body, death_msg=None):