f | | f | |
| | | |
| class Potion: | | class Potion: |
| def __init__(effects, duration): | | def __init__(effects, duration): |
| self.__effects__ = {} | | self.__effects__ = {} |
| self.__duration__ = duration | | self.__duration__ = duration |
| self.__is_used__ = False | | self.__is_used__ = False |
| | | |
| for key, effect in effects.items(): | | for key, effect in effects.items(): |
| self.__effects__[key] = (1, effect) | | self.__effects__[key] = (1, effect) |
| | | |
| setattr(self, key, lambda effect_name=key: self.apply_effect(effect_name)) | | setattr(self, key, lambda effect_name=key: self.apply_effect(effect_name)) |
| | | |
| def apply_effect(self, effect_key): | | def apply_effect(self, effect_key): |
| if self.__is_used__: | | if self.__is_used__: |
| Potion.__raise_exception__() | | Potion.__raise_exception__() |
| | | |
| intensity, lambda_effect = self.__effects__.get(effect_key, (0, None)) | | intensity, lambda_effect = self.__effects__.get(effect_key, (0, None)) |
| | | |
| for _ in range(intensity): | | for _ in range(intensity): |
| lambda_effect() | | lambda_effect() |
| | | |
| @staticmethod | | @staticmethod |
| def __raise_exception__(): | | def __raise_exception__(): |
| raise TypeError("Potion is now part of something bigger than itself.") | | raise TypeError("Potion is now part of something bigger than itself.") |
| | | |
| def set_used(self): | | def set_used(self): |
| self.__is_used__ = True | | self.__is_used__ = True |
| | | |
| for key in self.__effects__.keys(): | | for key in self.__effects__.keys(): |
| self.__effects__[key] = Potion.__raise_exception__ | | self.__effects__[key] = Potion.__raise_exception__ |
| | | |
| @staticmethod | | @staticmethod |
| def check_and_set_used(*args): | | def check_and_set_used(*args): |
| for obj in args: | | for obj in args: |
| if obj.__is_used__: | | if obj.__is_used__: |
| Potion.__raise_exception() | | Potion.__raise_exception() |
| | | |
| obj.set_used() | | obj.set_used() |
| | | |
| @staticmethod | | @staticmethod |
| def __combine_effects__(effects1, effects2): | | def __combine_effects__(effects1, effects2): |
| combined_effects = {} | | combined_effects = {} |
| | | |
| for key, (first_intensity, first_effect) in effects1.items(): | | for key, (first_intensity, first_effect) in effects1.items(): |
| second_intensity = effects2[key][0] if key in effects2 else 0 | | second_intensity = effects2[key][0] if key in effects2 else 0 |
| | | |
| combined_effects[key] = (first_intensity + second_intensity, first_effect) | | combined_effects[key] = (first_intensity + second_intensity, first_effect) |
| | | |
| for key, (second_intensity, second_effect) in effects2.items(): | | for key, (second_intensity, second_effect) in effects2.items(): |
| if key not in effects1: | | if key not in effects1: |
| combined_effects[key] = (second_intensity, second_effect) | | combined_effects[key] = (second_intensity, second_effect) |
| | | |
| return combined_effects | | return combined_effects |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| Potion.check_and_set_used(self, other) | | Potion.check_and_set_used(self, other) |
| | | |
| return Potion(Potion.__combine_effects__(self.__effects__, other.__effects__), max(self.__duration__, other.__duration__)) | | return Potion(Potion.__combine_effects__(self.__effects__, other.__effects__), max(self.__duration__, other.__duration__)) |
| | | |
t | # да го духате, защото 0.5 трябва да се закръгля до 1, а не до 0. | t | # тъпо, защото 0.5 трябва да се закръгля до 1, а не до 0. |
| @staticmethod | | @staticmethod |
| def __custom_round__(x): | | def __custom_round__(x): |
| if x - int(x) >= 0.5: | | if x - int(x) >= 0.5: |
| return int(x) + 1 | | return int(x) + 1 |
| else: | | else: |
| return int(x) | | return int(x) |
| | | |
| def __mul__(self, multiplier): | | def __mul__(self, multiplier): |
| Potion.check_and_set_used(self) | | Potion.check_and_set_used(self) |
| | | |
| for key, (intensity, effect) in self.__effects__.items(): | | for key, (intensity, effect) in self.__effects__.items(): |
| self.__effects__[key] = (Potion.__custom_round__(intensity * multiplier), effect) | | self.__effects__[key] = (Potion.__custom_round__(intensity * multiplier), effect) |
| | | |
| @staticmethod | | @staticmethod |
| def __remove_effects__(effects1, effects2): | | def __remove_effects__(effects1, effects2): |
| removed_effects = {} | | removed_effects = {} |
| | | |
| if set(dict2.keys()) - set(dict1.keys()): | | if set(dict2.keys()) - set(dict1.keys()): |
| raise TypeError("Мамка му, аз нямам такъв ефект.") | | raise TypeError("Мамка му, аз нямам такъв ефект.") |
| | | |
| for key, (first_intensity, effect) in effects1.items(): | | for key, (first_intensity, effect) in effects1.items(): |
| second_intensity = effects2.get(key, (None, None))[0] | | second_intensity = effects2.get(key, (None, None))[0] |
| | | |
| if second_intensity is None: | | if second_intensity is None: |
| removed_effects[key] = (first_intensity, effect) | | removed_effects[key] = (first_intensity, effect) |
| else if first_intensity > second_intensity: | | else if first_intensity > second_intensity: |
| removed_effects[key] = (first_intensity - second_intensity, effect) | | removed_effects[key] = (first_intensity - second_intensity, effect) |
| | | |
| return removed_effects | | return removed_effects |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| Potion.check_and_set_used(self, other) | | Potion.check_and_set_used(self, other) |
| | | |
| return Potion(Potion.__remove_effects__(self.__effects__, other.__effects__), self.__duration__) | | return Potion(Potion.__remove_effects__(self.__effects__, other.__effects__), self.__duration__) |
| | | |
| def __truediv__(self, divider): | | def __truediv__(self, divider): |
| Potion.check_and_set_used(self) | | Potion.check_and_set_used(self) |
| | | |
| result_potion = self * (1 / divider) | | result_potion = self * (1 / divider) |
| | | |
| return (result_potion, ) * divider | | return (result_potion, ) * divider |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| # effects = {'grow': lambda target: setattr(target, 'size', target.size*2)} | | # effects = {'grow': lambda target: setattr(target, 'size', target.size*2)} |
08.12.2023 14:15
08.12.2023 14:19
08.12.2023 14:21
08.12.2023 14:19
08.12.2023 14:23