From c0727b65187a7533c0ee5c5801be05196b63cc46 Mon Sep 17 00:00:00 2001 From: Gleb Minaev <43728100+lounres@users.noreply.github.com> Date: Fri, 14 Jun 2019 23:00:58 -0400 Subject: [PATCH 1/2] Made the decorator for unique classes. --- labyrinth_engine/__init__.py | 1 + labyrinth_engine/labyrinth_object.py | 14 ++++++++++++++ labyrinth_engine/uniqueness.py | 21 +++++++++++++++++++++ labyrinth_objects/Vanilla/ammo.py | 5 ++--- labyrinth_objects/Vanilla/death.py | 5 ++--- labyrinth_objects/Vanilla/health.py | 5 ++--- 6 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 labyrinth_engine/uniqueness.py 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 f79c5a0..0ff2980 100644 --- a/labyrinth_engine/labyrinth_object.py +++ b/labyrinth_engine/labyrinth_object.py @@ -1,5 +1,6 @@ from labyrinth_engine.ui_buttons import CommonButton, DirectionButton, ListButton from labyrinth_engine.ui_status_bars import StringBar +from copy import copy class LabyrinthObject: @@ -9,6 +10,7 @@ class LabyrinthObject: labyrinth = None _lrtype = 'labyrinth_object' + _metadata = {} def __init__(self): self.turn_set = {} @@ -87,6 +89,18 @@ def get_children(self, lrtype=['location', 'item', 'player', 'creature'], class_ def lrtype(self): return self._lrtype + @property + def metadata(self): + return copy(self._metadata) + + @metadata.setter + def metadata(self, *args, **kwargs): + for key in args: + self._metadata[key] = True + self._metadata.update(kwargs) + + add_meta = metadata.setter + def main(self): """ Основная функция объекта. Определяется здесь, чтобы потом не было ошибки при её вызове. 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 5105698..ce0dbd4 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): From 5c9bd923b331c7a85a4a9c3ba8456c8065bbd5b0 Mon Sep 17 00:00:00 2001 From: Gleb Minaev <43728100+lounres@users.noreply.github.com> Date: Sat, 22 Jun 2019 12:59:59 -0400 Subject: [PATCH 2/2] ORC --- labyrinth_engine/labyrinth_object.py | 12 ++++-------- labyrinth_engine/labyrinth_object.pyi | 17 ++++++++++++----- labyrinth_engine/metadata.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 labyrinth_engine/metadata.py diff --git a/labyrinth_engine/labyrinth_object.py b/labyrinth_engine/labyrinth_object.py index 3f02ab6..a53a8ef 100644 --- a/labyrinth_engine/labyrinth_object.py +++ b/labyrinth_engine/labyrinth_object.py @@ -1,6 +1,5 @@ from labyrinth_engine.ui_buttons import CommonButton, DirectionButton, ListButton from labyrinth_engine.ui_status_bars import StringBar -from copy import copy class LabyrinthObject: @@ -90,15 +89,12 @@ def lrtype(self): @property def metadata(self): - return copy(self._metadata) + return self._metadata - @metadata.setter - def metadata(self, *args, **kwargs): - for key in args: - self._metadata[key] = True + def add_meta(self, *args, **kwargs): + self._metadata.update({key: True for key in args}) self._metadata.update(kwargs) - - add_meta = metadata.setter + 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