From c54cc2d668f3bbe6a3eb0ce2fe75c1150fb830e3 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Sun, 7 Nov 2021 21:55:43 -0500 Subject: [PATCH 01/10] feat: add some basic classes - WIP --- src/classes/Dependency.py | 31 ++++++++++++++++++ src/classes/Modifiers.py | 24 ++++++++++++++ src/classes/Player.py | 69 +++++++++++++++++++++++++++++++++++++++ src/classes/__init__.py | 0 4 files changed, 124 insertions(+) create mode 100644 src/classes/Dependency.py create mode 100644 src/classes/Modifiers.py create mode 100644 src/classes/Player.py create mode 100644 src/classes/__init__.py diff --git a/src/classes/Dependency.py b/src/classes/Dependency.py new file mode 100644 index 0000000..c9a800e --- /dev/null +++ b/src/classes/Dependency.py @@ -0,0 +1,31 @@ +# + +from itertools import chain + +_dependencies = {} +_dependent_vars = set() + +def addDependency(dep1: str, dep2: str): + global _dependencies, _dependent_vars + if dep1 in _dependencies.keys(): + if not dep2 in _dependencies[dep1]: + _dependencies[dep1].append(dep2) + else: + _dependencies[dep1] = [dep2] + _dependent_vars = set(chain.from_iterable(_dependencies.values())) + +class Dependency: + def _reset_dependent_vars(self, name): + for var in _dependencies[name]: + super().__delattr__(f"{var}") + if var in _dependencies: + self._reset_dependent_vars(var) + + def __setattr__(self, name, value): + global _dependencies, _dependent_vars + if name in _dependent_vars: + raise AttributeError("Cannot set this value.") + if name in _dependencies: + self._reset_dependent_vars(name) + name = f"_{name}" + super().__setattr__(name, value) \ No newline at end of file diff --git a/src/classes/Modifiers.py b/src/classes/Modifiers.py new file mode 100644 index 0000000..8ddec35 --- /dev/null +++ b/src/classes/Modifiers.py @@ -0,0 +1,24 @@ +# + +from Dependency import addDependency + +class Mod: + def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): + self.name = name + self.type = type + self.value = value + self.source = source + self.tags = tags + self.process() + + def process(self): + if 'type' in self.tags.keys() and self.tags['type'] == "Multiplier": + addDependency(self.tags['var'].lower(), f"{self.type.lower()}_{self.name.lower()}") + +def test(): + Mod("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) + +if __name__ == "__main__": + from Dependency import _dependencies + test() + print(_dependencies) diff --git a/src/classes/Player.py b/src/classes/Player.py new file mode 100644 index 0000000..fa2d710 --- /dev/null +++ b/src/classes/Player.py @@ -0,0 +1,69 @@ +# + +from functools import cached_property +from math import floor + +from Dependency import Dependency, addDependency + +class Player(Dependency): + def __init__(self, level, strength): + self._level = level + self._strength = strength + self._flat_life = 0 + self._more_life = 0 + self._inc_life = 0 + self._base_health = None + self._max_health = None + + addDependency("level", "base_health") + addDependency("strength", "base_health") + addDependency("flat_life", "base_health") + addDependency("base_health", "max_health") + addDependency("more_life", "max_health") + addDependency("inc_life", "max_health") + + @cached_property + def base_health(self): + print("Base Health calculated") + return 38 + self.level * 12 + floor(self.strength / 2) + self.flat_life + + @cached_property + def max_health(self): + print("Max Health calculated") + return self.base_health * (1 + self.inc_life / 100) * (1 + self.more_life / 100) + + @property + def level(self): + return self._level + + @property + def strength(self): + return self._strength + + @property + def flat_life(self): + return self._flat_life + + @property + def more_life(self): + return self._more_life + + @property + def inc_life(self): + return self._inc_life + +def test(): + player = Player(1, 20) + print(f"{player.max_health}") + + player.level = 5 + print(f"{player.max_health}") + + player.more_life = 100 + print(f"{player.max_health}") + + player.flat_life = 100 + print(f"{player.max_health}") + +if __name__ == "__main__": + test() \ No newline at end of file diff --git a/src/classes/__init__.py b/src/classes/__init__.py new file mode 100644 index 0000000..e69de29 From 9a3d356737d38575135ce697ee0b7e6e521dd89b Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Mon, 8 Nov 2021 08:23:32 -0500 Subject: [PATCH 02/10] feat: added more strength variables --- src/classes/Player.py | 44 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/classes/Player.py b/src/classes/Player.py index fa2d710..2e29257 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -8,15 +8,25 @@ class Player(Dependency): def __init__(self, level, strength): self._level = level - self._strength = strength + self._start_strength = strength + self._flat_strength = 0 + self._more_strength = 0 + self._inc_strength = 0 self._flat_life = 0 self._more_life = 0 self._inc_life = 0 self._base_health = None self._max_health = None + self._base_strength = None + self._max_strength = None addDependency("level", "base_health") - addDependency("strength", "base_health") + addDependency("start_strength", "base_strength") + addDependency("flat_strength", "base_strength") + addDependency("base_strength", "max_strength") + addDependency("more_strength", "max_strength") + addDependency("inc_strength", "max_strength") + addDependency("max_strength", "base_health") addDependency("flat_life", "base_health") addDependency("base_health", "max_health") addDependency("more_life", "max_health") @@ -25,20 +35,30 @@ def __init__(self, level, strength): @cached_property def base_health(self): print("Base Health calculated") - return 38 + self.level * 12 + floor(self.strength / 2) + self.flat_life + return 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_life @cached_property def max_health(self): print("Max Health calculated") return self.base_health * (1 + self.inc_life / 100) * (1 + self.more_life / 100) + @cached_property + def base_strength(self): + print("Base Strength calculated") + return self._start_strength + self._flat_strength + + @cached_property + def max_strength(self): + print("Max Strength calculated") + return self.base_strength * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) + @property def level(self): return self._level @property - def strength(self): - return self._strength + def start_strength(self): + return self._start_strength @property def flat_life(self): @@ -52,6 +72,18 @@ def more_life(self): def inc_life(self): return self._inc_life + @property + def flat_strength(self): + return self._flat_strength + + @property + def more_strength(self): + return self._more_strength + + @property + def inc_strength(self): + return self._inc_strength + def test(): player = Player(1, 20) print(f"{player.max_health}") @@ -62,7 +94,7 @@ def test(): player.more_life = 100 print(f"{player.max_health}") - player.flat_life = 100 + player.flat_strength = 100 print(f"{player.max_health}") if __name__ == "__main__": From 2ae83b7d56065a7c86e01476dc0b52c272c6ab55 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Mon, 8 Nov 2021 12:28:53 -0500 Subject: [PATCH 03/10] fix: added Modifier Database class --- src/classes/ModDB.py | 67 ++++++++++++++++++++++++++++++++++++++++ src/classes/Modifiers.py | 7 +++-- src/classes/Player.py | 5 +-- 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/classes/ModDB.py diff --git a/src/classes/ModDB.py b/src/classes/ModDB.py new file mode 100644 index 0000000..9405570 --- /dev/null +++ b/src/classes/ModDB.py @@ -0,0 +1,67 @@ +# + +from Modifiers import Modifier + +class ModifierDatabase: + def __init__(self): + self.clear() + + def addEntry(self, entry: Modifier): + name = entry.name + type = entry.type + if name not in self.db.keys(): + self.db[name] = dict() + if type not in self.db[name].keys(): + self.db[name][type] = [entry] + else: + if not entry in self.db[name][type]: + self.db[name][type].append(entry) + + def getBase(self, name: str): + retVal = 0 + if name in self.db.keys() and "BASE" in self.db[name].keys(): + for entry in self.db[name]["BASE"]: + retVal += entry.getValue() + return retVal + return 1 + + def getFlat(self, name: str): + retVal = 0 + if name in self.db.keys() and "FLAT" in self.db[name].keys(): + for entry in self.db[name]["FLAT"]: + retVal += entry.getValue() + return retVal + return 1 + + def getInc(self, name: str): + retVal = 0 + if name in self.db.keys() and "INC" in self.db[name].keys(): + for entry in self.db[name]["INC"]: + retVal += entry.getValue() + return retVal + return 1 + + def getMore(self, name: str): + retVal = 0 + if name in self.db.keys() and "MORE" in self.db[name].keys(): + for entry in self.db[name]["MORE"]: + retVal += entry.getValue() + return retVal + return 1 + + def clear(self): + self.db = dict() + +def test(): + db = ModifierDatabase() + mod1 = Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) + mod2 = Modifier("Health", "BASE", 13, "") + db.addEntry(mod1) + db.addEntry(mod2) + import pprint + pprint.pprint(db.db) + + print("BASE: " + str(db.getBase("Health"))) + +if __name__ == "__main__": + test() diff --git a/src/classes/Modifiers.py b/src/classes/Modifiers.py index 8ddec35..d794be1 100644 --- a/src/classes/Modifiers.py +++ b/src/classes/Modifiers.py @@ -2,7 +2,7 @@ from Dependency import addDependency -class Mod: +class Modifier: def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): self.name = name self.type = type @@ -15,8 +15,11 @@ def process(self): if 'type' in self.tags.keys() and self.tags['type'] == "Multiplier": addDependency(self.tags['var'].lower(), f"{self.type.lower()}_{self.name.lower()}") + def getValue(self): + return self.value + def test(): - Mod("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) + Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) if __name__ == "__main__": from Dependency import _dependencies diff --git a/src/classes/Player.py b/src/classes/Player.py index 2e29257..8ef77ba 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -7,6 +7,7 @@ class Player(Dependency): def __init__(self, level, strength): + self.modDB = dict() self._level = level self._start_strength = strength self._flat_strength = 0 @@ -90,7 +91,7 @@ def test(): player.level = 5 print(f"{player.max_health}") - + player.more_life = 100 print(f"{player.max_health}") @@ -98,4 +99,4 @@ def test(): print(f"{player.max_health}") if __name__ == "__main__": - test() \ No newline at end of file + test() From 90223bc644a18225f9c0b349ec78ee4554c6e33d Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Mon, 8 Nov 2021 21:08:21 -0500 Subject: [PATCH 04/10] wip: dependency debugging --- src/classes/Dependency.py | 23 ++++++++++++--- src/classes/ModDB.py | 8 +++--- src/classes/Modifiers.py | 5 ++-- src/classes/Player.py | 59 +++++++++++++++++++++++---------------- 4 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/classes/Dependency.py b/src/classes/Dependency.py index c9a800e..762420c 100644 --- a/src/classes/Dependency.py +++ b/src/classes/Dependency.py @@ -10,22 +10,37 @@ def addDependency(dep1: str, dep2: str): if dep1 in _dependencies.keys(): if not dep2 in _dependencies[dep1]: _dependencies[dep1].append(dep2) + #print(f"Appending DEP: {dep1} -> {dep2}") else: _dependencies[dep1] = [dep2] + #print(f"Adding DEP: {dep1} -> {dep2}") _dependent_vars = set(chain.from_iterable(_dependencies.values())) + #print("DV: \n", _dependent_vars) + +def getDependentVars(): + return _dependent_vars + +def getDependencies(): + return _dependencies class Dependency: - def _reset_dependent_vars(self, name): + def _reset_dependent_vars(self, name: str): + #print("REMOVING: " + name) + print(f"DEP\n{_dependencies}\n") for var in _dependencies[name]: - super().__delattr__(f"{var}") + print(f"{name} FOR: " + f"{var}") + try: + super().__delattr__(f"{var}") + except: + pass if var in _dependencies: self._reset_dependent_vars(var) - def __setattr__(self, name, value): + def __setattr__(self, name: str, value): global _dependencies, _dependent_vars if name in _dependent_vars: raise AttributeError("Cannot set this value.") if name in _dependencies: self._reset_dependent_vars(name) name = f"_{name}" - super().__setattr__(name, value) \ No newline at end of file + super().__setattr__(name, value) diff --git a/src/classes/ModDB.py b/src/classes/ModDB.py index 9405570..05c7d62 100644 --- a/src/classes/ModDB.py +++ b/src/classes/ModDB.py @@ -23,7 +23,7 @@ def getBase(self, name: str): for entry in self.db[name]["BASE"]: retVal += entry.getValue() return retVal - return 1 + return 0 def getFlat(self, name: str): retVal = 0 @@ -31,7 +31,7 @@ def getFlat(self, name: str): for entry in self.db[name]["FLAT"]: retVal += entry.getValue() return retVal - return 1 + return 0 def getInc(self, name: str): retVal = 0 @@ -39,7 +39,7 @@ def getInc(self, name: str): for entry in self.db[name]["INC"]: retVal += entry.getValue() return retVal - return 1 + return retVal def getMore(self, name: str): retVal = 0 @@ -47,7 +47,7 @@ def getMore(self, name: str): for entry in self.db[name]["MORE"]: retVal += entry.getValue() return retVal - return 1 + return retVal def clear(self): self.db = dict() diff --git a/src/classes/Modifiers.py b/src/classes/Modifiers.py index d794be1..eb654c1 100644 --- a/src/classes/Modifiers.py +++ b/src/classes/Modifiers.py @@ -1,6 +1,6 @@ # -from Dependency import addDependency +from Dependency import addDependency, _dependencies class Modifier: def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): @@ -14,6 +14,7 @@ def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): def process(self): if 'type' in self.tags.keys() and self.tags['type'] == "Multiplier": addDependency(self.tags['var'].lower(), f"{self.type.lower()}_{self.name.lower()}") + addDependency(f"{self.type.lower()}_{self.name.lower()}", f"max_{self.name.lower()}") def getValue(self): return self.value @@ -22,6 +23,4 @@ def test(): Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) if __name__ == "__main__": - from Dependency import _dependencies test() - print(_dependencies) diff --git a/src/classes/Player.py b/src/classes/Player.py index 8ef77ba..e20e2b1 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -3,24 +3,20 @@ from functools import cached_property from math import floor -from Dependency import Dependency, addDependency +from Dependency import Dependency, addDependency, getDependencies +from ModDB import ModifierDatabase +from Modifiers import Modifier class Player(Dependency): def __init__(self, level, strength): - self.modDB = dict() + self.modDB = ModifierDatabase() self._level = level - self._start_strength = strength - self._flat_strength = 0 - self._more_strength = 0 - self._inc_strength = 0 - self._flat_life = 0 - self._more_life = 0 - self._inc_life = 0 self._base_health = None self._max_health = None self._base_strength = None self._max_strength = None + ''' addDependency("level", "base_health") addDependency("start_strength", "base_strength") addDependency("flat_strength", "base_strength") @@ -32,6 +28,17 @@ def __init__(self, level, strength): addDependency("base_health", "max_health") addDependency("more_life", "max_health") addDependency("inc_life", "max_health") + ''' + + self.addMod(Modifier("Health", "BASE", 12, "Base Per Level", { "type": "Multiplier", "var": "Level" })) + self.addMod(Modifier("Health", "BASE", 0.5, "Base Per Strength", { "type": "Multiplier", "var": "Max_Strength"})) + self.addMod(Modifier("Strength", "BASE", strength, "Starting")) + + def addMod(self, mod: Modifier): + #print("REMOVING: " + f"{mod.type.lower()}_{mod.name.lower()}") + self._reset_dependent_vars(f"{mod.type.lower()}_{mod.name.lower()}") + self.modDB.addEntry(mod) + #print(getDependencies()) @cached_property def base_health(self): @@ -43,59 +50,63 @@ def max_health(self): print("Max Health calculated") return self.base_health * (1 + self.inc_life / 100) * (1 + self.more_life / 100) - @cached_property + @property def base_strength(self): print("Base Strength calculated") - return self._start_strength + self._flat_strength + return self.modDB.getBase("Strength") @cached_property def max_strength(self): print("Max Strength calculated") - return self.base_strength * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) + return (self.base_strength + self.flat_strength) * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) @property def level(self): return self._level - @property - def start_strength(self): - return self._start_strength - @property def flat_life(self): - return self._flat_life + print("Flat Health calculated") + return self.modDB.getFlat("Health") @property def more_life(self): - return self._more_life + print("More Health calculated") + return self.modDB.getMore("Health") @property def inc_life(self): - return self._inc_life + print("Inc Health calculated") + return self.modDB.getInc("Health") @property def flat_strength(self): - return self._flat_strength + print("Flat Strength calculated") + return self.modDB.getFlat("Strength") @property def more_strength(self): - return self._more_strength + print("More Strength calculated") + return self.modDB.getMore("Strength") @property def inc_strength(self): - return self._inc_strength + print("Inc Strength calculated") + return self.modDB.getInc("Strength") def test(): player = Player(1, 20) + #print(f"{player.base_health}") print(f"{player.max_health}") player.level = 5 print(f"{player.max_health}") - player.more_life = 100 + player.addMod(Modifier("Health", "MORE", 100, "")) print(f"{player.max_health}") - player.flat_strength = 100 + player.addMod(Modifier("Strength", "FLAT", 100, "")) + print(f"{player.max_strength}") print(f"{player.max_health}") if __name__ == "__main__": From 078158bf7d613d217b286b4d01e5cb6b17e99a26 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Mon, 8 Nov 2021 22:00:25 -0500 Subject: [PATCH 05/10] fix: dependency resets --- src/classes/Dependency.py | 7 ++++--- src/classes/Player.py | 43 ++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/classes/Dependency.py b/src/classes/Dependency.py index 762420c..378981a 100644 --- a/src/classes/Dependency.py +++ b/src/classes/Dependency.py @@ -25,13 +25,13 @@ def getDependencies(): class Dependency: def _reset_dependent_vars(self, name: str): - #print("REMOVING: " + name) - print(f"DEP\n{_dependencies}\n") + #print(f"DEP\n{_dependencies}") for var in _dependencies[name]: - print(f"{name} FOR: " + f"{var}") + #print(f"Resetting: {var} (due to {name})") try: super().__delattr__(f"{var}") except: + #print(f"FAIL") pass if var in _dependencies: self._reset_dependent_vars(var) @@ -43,4 +43,5 @@ def __setattr__(self, name: str, value): if name in _dependencies: self._reset_dependent_vars(name) name = f"_{name}" + #print(f"Setting {name} to {value}") super().__setattr__(name, value) diff --git a/src/classes/Player.py b/src/classes/Player.py index e20e2b1..79ddf77 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -3,7 +3,7 @@ from functools import cached_property from math import floor -from Dependency import Dependency, addDependency, getDependencies +from Dependency import Dependency, getDependencies from ModDB import ModifierDatabase from Modifiers import Modifier @@ -11,10 +11,6 @@ class Player(Dependency): def __init__(self, level, strength): self.modDB = ModifierDatabase() self._level = level - self._base_health = None - self._max_health = None - self._base_strength = None - self._max_strength = None ''' addDependency("level", "base_health") @@ -35,22 +31,27 @@ def __init__(self, level, strength): self.addMod(Modifier("Strength", "BASE", strength, "Starting")) def addMod(self, mod: Modifier): - #print("REMOVING: " + f"{mod.type.lower()}_{mod.name.lower()}") - self._reset_dependent_vars(f"{mod.type.lower()}_{mod.name.lower()}") self.modDB.addEntry(mod) - #print(getDependencies()) + attr = f"{mod.type.lower()}_{mod.name.lower()}" + try: + self.__delattr__(f"{attr}") + except: + #print("FAIL") + pass + if attr in getDependencies(): + self._reset_dependent_vars(attr) @cached_property def base_health(self): print("Base Health calculated") - return 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_life + return 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_health @cached_property def max_health(self): print("Max Health calculated") - return self.base_health * (1 + self.inc_life / 100) * (1 + self.more_life / 100) + return self.base_health * (1 + self.inc_health / 100) * (1 + self.more_health / 100) - @property + @cached_property def base_strength(self): print("Base Strength calculated") return self.modDB.getBase("Strength") @@ -64,32 +65,32 @@ def max_strength(self): def level(self): return self._level - @property - def flat_life(self): + @cached_property + def flat_health(self): print("Flat Health calculated") return self.modDB.getFlat("Health") - @property - def more_life(self): + @cached_property + def more_health(self): print("More Health calculated") return self.modDB.getMore("Health") - @property - def inc_life(self): + @cached_property + def inc_health(self): print("Inc Health calculated") return self.modDB.getInc("Health") - @property + @cached_property def flat_strength(self): print("Flat Strength calculated") return self.modDB.getFlat("Strength") - @property + @cached_property def more_strength(self): print("More Strength calculated") return self.modDB.getMore("Strength") - @property + @cached_property def inc_strength(self): print("Inc Strength calculated") return self.modDB.getInc("Strength") @@ -106,7 +107,7 @@ def test(): print(f"{player.max_health}") player.addMod(Modifier("Strength", "FLAT", 100, "")) - print(f"{player.max_strength}") + #print(f"{player.max_strength}") print(f"{player.max_health}") if __name__ == "__main__": From 99413f344c527c22fa543b5a07409816bfe97c15 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Mon, 8 Nov 2021 22:16:43 -0500 Subject: [PATCH 06/10] fix: cleaned up print out --- src/classes/Player.py | 62 +++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/classes/Player.py b/src/classes/Player.py index 79ddf77..f0543d0 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -43,23 +43,27 @@ def addMod(self, mod: Modifier): @cached_property def base_health(self): - print("Base Health calculated") - return 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_health + ret = 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_health + print(f"BASE Health calculated: {ret}") + return ret @cached_property def max_health(self): - print("Max Health calculated") - return self.base_health * (1 + self.inc_health / 100) * (1 + self.more_health / 100) + ret = self.base_health * (1 + self.inc_health / 100) * (1 + self.more_health / 100) + print(f"Total Health calculated: {ret}") + return ret @cached_property def base_strength(self): - print("Base Strength calculated") - return self.modDB.getBase("Strength") + ret = self.modDB.getBase("Strength") + print(f"BASE Strength calculated: {ret}") + return ret @cached_property def max_strength(self): - print("Max Strength calculated") - return (self.base_strength + self.flat_strength) * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) + ret = (self.base_strength + self.flat_strength) * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) + print(f"Total Strength calculated: {ret}") + return ret @property def level(self): @@ -67,48 +71,54 @@ def level(self): @cached_property def flat_health(self): - print("Flat Health calculated") - return self.modDB.getFlat("Health") + ret = self.modDB.getFlat("Health") + print(f"FLAT Health calculated: {ret}") + return ret @cached_property def more_health(self): - print("More Health calculated") - return self.modDB.getMore("Health") + ret = self.modDB.getMore("Health") + print(f"MORE Health calculated: {ret}") + return ret @cached_property def inc_health(self): - print("Inc Health calculated") - return self.modDB.getInc("Health") + ret = self.modDB.getInc("Health") + print(f"INC Health calculated: {ret}") + return ret @cached_property def flat_strength(self): - print("Flat Strength calculated") - return self.modDB.getFlat("Strength") + ret = self.modDB.getFlat("Strength") + print(f"FLAT Strength calculated: {ret}") + return ret @cached_property def more_strength(self): - print("More Strength calculated") - return self.modDB.getMore("Strength") + ret = self.modDB.getMore("Strength") + print(f"MORE Strength calculated: {ret}") + return ret @cached_property def inc_strength(self): - print("Inc Strength calculated") - return self.modDB.getInc("Strength") + ret = self.modDB.getInc("Strength") + print(f"INC Strength calculated: {ret}") + return ret def test(): player = Player(1, 20) - #print(f"{player.base_health}") - print(f"{player.max_health}") + #print(f"{player.base_health}\n") + print(f"{player.max_health}\n") player.level = 5 - print(f"{player.max_health}") + print(f"{player.max_health}\n") player.addMod(Modifier("Health", "MORE", 100, "")) - print(f"{player.max_health}") + print(f"{player.max_health}\n") player.addMod(Modifier("Strength", "FLAT", 100, "")) - #print(f"{player.max_strength}") - print(f"{player.max_health}") + #print(f"{player.max_strength}\n") + print(f"{player.max_health}\n") if __name__ == "__main__": test() From d7683415d46533e648cd3e9a54e0b3928de1bc84 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Tue, 9 Nov 2021 08:23:24 -0500 Subject: [PATCH 07/10] fix: added math.floor() to some calcs to make them accurate --- src/classes/Player.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/classes/Player.py b/src/classes/Player.py index f0543d0..34f426d 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -49,7 +49,7 @@ def base_health(self): @cached_property def max_health(self): - ret = self.base_health * (1 + self.inc_health / 100) * (1 + self.more_health / 100) + ret = floor(self.base_health * (1 + self.inc_health / 100) * (1 + self.more_health / 100)) print(f"Total Health calculated: {ret}") return ret @@ -61,7 +61,7 @@ def base_strength(self): @cached_property def max_strength(self): - ret = (self.base_strength + self.flat_strength) * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100) + ret = floor((self.base_strength + self.flat_strength) * (1 + self.inc_strength / 100) * (1 + self.more_strength / 100)) print(f"Total Strength calculated: {ret}") return ret @@ -117,6 +117,9 @@ def test(): print(f"{player.max_health}\n") player.addMod(Modifier("Strength", "FLAT", 100, "")) + player.addMod(Modifier("Strength", "INC", 30, "")) + player.addMod(Modifier("Strength", "MORE", 15, "")) + player.addMod(Modifier("Health", "FLAT", 500, "")) #print(f"{player.max_strength}\n") print(f"{player.max_health}\n") From 732a873f69e693e803a00486017f14741a2b3f27 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Tue, 9 Nov 2021 21:31:56 -0500 Subject: [PATCH 08/10] feat: implemented using multiplier with caller reflection --- src/classes/ModDB.py | 11 ++++++----- src/classes/Modifiers.py | 16 +++++++++++++++- src/classes/Player.py | 19 +++---------------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/classes/ModDB.py b/src/classes/ModDB.py index 05c7d62..4f87396 100644 --- a/src/classes/ModDB.py +++ b/src/classes/ModDB.py @@ -3,7 +3,8 @@ from Modifiers import Modifier class ModifierDatabase: - def __init__(self): + def __init__(self, owner = None): + self.owner = owner self.clear() def addEntry(self, entry: Modifier): @@ -21,7 +22,7 @@ def getBase(self, name: str): retVal = 0 if name in self.db.keys() and "BASE" in self.db[name].keys(): for entry in self.db[name]["BASE"]: - retVal += entry.getValue() + retVal += entry.getValue(self.owner) return retVal return 0 @@ -29,7 +30,7 @@ def getFlat(self, name: str): retVal = 0 if name in self.db.keys() and "FLAT" in self.db[name].keys(): for entry in self.db[name]["FLAT"]: - retVal += entry.getValue() + retVal += entry.getValue(self.owner) return retVal return 0 @@ -37,7 +38,7 @@ def getInc(self, name: str): retVal = 0 if name in self.db.keys() and "INC" in self.db[name].keys(): for entry in self.db[name]["INC"]: - retVal += entry.getValue() + retVal += entry.getValue(self.owner) return retVal return retVal @@ -45,7 +46,7 @@ def getMore(self, name: str): retVal = 0 if name in self.db.keys() and "MORE" in self.db[name].keys(): for entry in self.db[name]["MORE"]: - retVal += entry.getValue() + retVal += entry.getValue(self.owner) return retVal return retVal diff --git a/src/classes/Modifiers.py b/src/classes/Modifiers.py index eb654c1..c1784c1 100644 --- a/src/classes/Modifiers.py +++ b/src/classes/Modifiers.py @@ -1,6 +1,7 @@ # from Dependency import addDependency, _dependencies +from math import floor class Modifier: def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): @@ -15,10 +16,23 @@ def process(self): if 'type' in self.tags.keys() and self.tags['type'] == "Multiplier": addDependency(self.tags['var'].lower(), f"{self.type.lower()}_{self.name.lower()}") addDependency(f"{self.type.lower()}_{self.name.lower()}", f"max_{self.name.lower()}") + #print(self) - def getValue(self): + def getValue(self, caller = None): + if caller: + #print(f"getting var: {self.name}:{self.type}") + if 'type' in self.tags.keys() and self.tags['type'] == "Multiplier": + var = f"{self.tags['var'].lower()}" + l = caller.__getattribute__(var) + #print(f"{var}: {l}") + return floor(self.value * l) + return self.value return self.value + def __repr__(self): + ret = f"{self.tags} -- {self.source}" + return ret + def test(): Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) diff --git a/src/classes/Player.py b/src/classes/Player.py index 34f426d..87c91ad 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -9,23 +9,9 @@ class Player(Dependency): def __init__(self, level, strength): - self.modDB = ModifierDatabase() + self.modDB = ModifierDatabase(self) self._level = level - ''' - addDependency("level", "base_health") - addDependency("start_strength", "base_strength") - addDependency("flat_strength", "base_strength") - addDependency("base_strength", "max_strength") - addDependency("more_strength", "max_strength") - addDependency("inc_strength", "max_strength") - addDependency("max_strength", "base_health") - addDependency("flat_life", "base_health") - addDependency("base_health", "max_health") - addDependency("more_life", "max_health") - addDependency("inc_life", "max_health") - ''' - self.addMod(Modifier("Health", "BASE", 12, "Base Per Level", { "type": "Multiplier", "var": "Level" })) self.addMod(Modifier("Health", "BASE", 0.5, "Base Per Strength", { "type": "Multiplier", "var": "Max_Strength"})) self.addMod(Modifier("Strength", "BASE", strength, "Starting")) @@ -43,7 +29,8 @@ def addMod(self, mod: Modifier): @cached_property def base_health(self): - ret = 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_health + #ret = 38 + self.level * 12 + floor(self.max_strength / 2) + self.flat_health + ret = 38 + self.modDB.getBase("Health") + self.flat_health print(f"BASE Health calculated: {ret}") return ret From c93dc56591c672d541be2bcc698cdf2899bd2305 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Wed, 10 Nov 2021 08:50:50 -0500 Subject: [PATCH 09/10] feat: added conditionals set parameter --- src/classes/Modifiers.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/classes/Modifiers.py b/src/classes/Modifiers.py index c1784c1..fe10884 100644 --- a/src/classes/Modifiers.py +++ b/src/classes/Modifiers.py @@ -4,11 +4,12 @@ from math import floor class Modifier: - def __init__(self, name: str, type: str, value, source: str, tags: dict = {}): + def __init__(self, name: str, type: str, value, source: str, conditionals: set = set(), tags: dict = {}): self.name = name self.type = type self.value = value self.source = source + self.conditionals = conditionals self.tags = tags self.process() @@ -30,11 +31,14 @@ def getValue(self, caller = None): return self.value def __repr__(self): - ret = f"{self.tags} -- {self.source}" + ret = f"{self.name}:{self.type}\n" + ret += f"Value: {self.value}\n" + ret += f"{self.tags} -- {self.source}" return ret def test(): - Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) + m = Modifier("Health", "BASE", 12, "", tags = { "type": "Multiplier", "var": "Level" }) + print(m) if __name__ == "__main__": test() From 26c31cfdd8aa21583f29811096cac03586c176e8 Mon Sep 17 00:00:00 2001 From: Nostrademous Date: Wed, 10 Nov 2021 08:53:25 -0500 Subject: [PATCH 10/10] fix: updated other uses of Modifiers to account for conditionals --- src/classes/ModDB.py | 2 +- src/classes/Player.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/classes/ModDB.py b/src/classes/ModDB.py index 4f87396..00bf0b2 100644 --- a/src/classes/ModDB.py +++ b/src/classes/ModDB.py @@ -55,7 +55,7 @@ def clear(self): def test(): db = ModifierDatabase() - mod1 = Modifier("Health", "BASE", 12, "", { "type": "Multiplier", "var": "Level" }) + mod1 = Modifier("Health", "BASE", 12, "", tags = { "type": "Multiplier", "var": "Level" }) mod2 = Modifier("Health", "BASE", 13, "") db.addEntry(mod1) db.addEntry(mod2) diff --git a/src/classes/Player.py b/src/classes/Player.py index 87c91ad..cad8f39 100644 --- a/src/classes/Player.py +++ b/src/classes/Player.py @@ -12,8 +12,8 @@ def __init__(self, level, strength): self.modDB = ModifierDatabase(self) self._level = level - self.addMod(Modifier("Health", "BASE", 12, "Base Per Level", { "type": "Multiplier", "var": "Level" })) - self.addMod(Modifier("Health", "BASE", 0.5, "Base Per Strength", { "type": "Multiplier", "var": "Max_Strength"})) + self.addMod(Modifier("Health", "BASE", 12, "Base Per Level", tags = { "type": "Multiplier", "var": "Level" })) + self.addMod(Modifier("Health", "BASE", 0.5, "Base Per Strength", tags = { "type": "Multiplier", "var": "Max_Strength"})) self.addMod(Modifier("Strength", "BASE", strength, "Starting")) def addMod(self, mod: Modifier):