Created
April 14, 2022 02:23
-
-
Save strikaco/bb5ba7a1ace2f69c5f3b0fd942c4c5a6 to your computer and use it in GitHub Desktop.
Value Container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass, field, asdict | |
| from enum import Enum, auto | |
| class Scale(Enum): | |
| NONE = 0 | |
| LOW = 25 | |
| MEDIUM = 50 | |
| HIGH = 75 | |
| MAX = 100 | |
| @dataclass | |
| class Value(object): | |
| base: float = field(default=0.0) | |
| static: float = field(default=0.0) | |
| mods: float = field(default=0.0) | |
| max_value: float = field(default=0.0, repr=False) | |
| min_value: float = field(default=0.0, repr=False) | |
| value:float = field(default=0.0) | |
| scale:Enum = field(default=None, repr=False) | |
| label:str = field(default='') | |
| at_empty:object = field(default=None, repr=False) | |
| at_full:object = field(default=None, repr=False) | |
| def __int__(self): | |
| return int(self.get_value()) | |
| def __bool__(self): | |
| return bool(max((int(self), 0))) | |
| def get_max_value(self, *args, **kwargs): | |
| return self.max_value | |
| def get_value(self, *args, **kwargs): | |
| self.value = self.base + self.mods | |
| if self.min_value and self.value <= self.min_value: | |
| self.value = self.min_value | |
| if self.at_empty: | |
| self.at_empty(*args, **kwargs) | |
| elif self.max_value and self.value >= self.max_value: | |
| self.value = self.max_value | |
| if self.at_full: | |
| self.at_full(*args, **kwargs) | |
| self.get_label(self.value) | |
| return self.value | |
| def get_label(self, value): | |
| self.label = next((x for x in self.scale if x.value >= value)) | |
| return self.label.name | |
| def incr(self, value, *args, **kwargs): | |
| previous = self.base | |
| self.base += value | |
| current = self.base | |
| delta = current - previous | |
| return self.get_value(*args, **kwargs) | |
| def decr(self, value, *args, **kwargs): | |
| return self.incr(-value, *args, **kwargs) | |
| def buff(self, value, *args, **kwargs): | |
| previous = self.mods | |
| self.mods += value | |
| current = self.mods | |
| delta = current - previous | |
| return self.get_value(*args, **kwargs) | |
| def debuff(self, value, *args, **kwargs): | |
| return self.buff(-value, *args, **kwargs) | |
| def fill(self, value:float=0.0, *args, **kwargs): | |
| value = value or self.get_max_value(*args, **kwargs) - self.get_value(*args, **kwargs) | |
| return self.incr(value, *args, **kwargs) | |
| def drain(self, *args, **kwargs): | |
| value = self.get_value(*args, **kwargs) | |
| return self.fill(-value, *args, **kwargs) | |
| def get_data(self, *args, **kwargs): | |
| minimum = self.min_value or 0 | |
| current = (self.get_value(), (self.base, self.mods)) | |
| maximum = self.get_max_value() or 1 | |
| return minimum, current, maximum | |
| b = Value(50, max_value=100, scale=Scale) | |
| b.decr(5) | |
| print(b) | |
| b.buff(500) | |
| print(b) | |
| b.fill() | |
| print(b) |
Author
strikaco
commented
Apr 15, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment