fix schemas
This commit is contained in:
@@ -34,5 +34,3 @@ def bootstrap():
|
||||
|
||||
# persist all the records we've created
|
||||
db.add_or_update([sabetha, bob])
|
||||
|
||||
print(f"{sabetha.intelligence.bonus = }, {sabetha.size = }")
|
||||
|
||||
@@ -117,7 +117,7 @@ class CharacterClassMap(BaseObject):
|
||||
level: Mapped[int] = mapped_column(nullable=False, info={"min": 1, "max": 20}, default=1)
|
||||
|
||||
def __repr__(self):
|
||||
return "{self.character.name}, {self.character_class.name}, level {self.level}"
|
||||
return f"{self.character.name}, {self.character_class.name}, level {self.level}"
|
||||
|
||||
|
||||
class CharacterClassAttributeMap(BaseObject):
|
||||
@@ -147,34 +147,37 @@ class Character(BaseObject, SlugMixin, SavingThrowsMixin, SkillsMixin, ModifierM
|
||||
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)
|
||||
|
||||
name: Mapped[str] = mapped_column(default="New Character", nullable=False)
|
||||
_armor_class: Mapped[int] = mapped_column(default=10, nullable=False, info={"min": 1, "max": 99, "modify": True})
|
||||
_hit_points: Mapped[int] = mapped_column(default=1, nullable=False, info={"min": 0, "max": 999, "modify": True})
|
||||
_max_hit_points: Mapped[int] = mapped_column(
|
||||
default=10, nullable=False, info={"min": 0, "max": 999, "modify": True}
|
||||
)
|
||||
hit_points: Mapped[int] = mapped_column(default=10, nullable=False, info={"min": 0, "max": 999})
|
||||
temp_hit_points: Mapped[int] = mapped_column(default=0, nullable=False, info={"min": 0, "max": 999})
|
||||
|
||||
_max_hit_points: Mapped[int] = mapped_column(
|
||||
default=10, nullable=False, info={"min": 0, "max": 999, "modifiable": True}
|
||||
)
|
||||
_armor_class: Mapped[int] = mapped_column(
|
||||
default=10, nullable=False, info={"min": 1, "max": 99, "modifiable": True}
|
||||
)
|
||||
_strength: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
_dexterity: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
_constitution: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
_intelligence: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
_wisdom: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
_charisma: Mapped[int] = mapped_column(
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modify": True, "modify_class": Stat}
|
||||
nullable=False, default=10, info={"min": 0, "max": 30, "modifiable_class": Stat}
|
||||
)
|
||||
|
||||
_vision: Mapped[int] = mapped_column(default=None, nullable=True, info={"min": 0})
|
||||
_vision: Mapped[int] = mapped_column(default=None, nullable=True, info={"min": 0, "modifiable": True})
|
||||
|
||||
proficiencies: Mapped[str] = mapped_column(nullable=False, default="")
|
||||
_proficiencies: Mapped[str] = mapped_column(nullable=False, default="")
|
||||
|
||||
class_map = relationship("CharacterClassMap", cascade="all,delete,delete-orphan")
|
||||
class_list = association_proxy("class_map", "id", creator=class_map_creator)
|
||||
@@ -204,19 +207,19 @@ class Character(BaseObject, SlugMixin, SavingThrowsMixin, SkillsMixin, ModifierM
|
||||
|
||||
@property
|
||||
def speed(self):
|
||||
return self.apply_modifiers("speed", self.ancestry.speed)
|
||||
return self._apply_modifiers("speed", self.ancestry.speed)
|
||||
|
||||
@property
|
||||
def climb_speed(self):
|
||||
return self.apply_modifiers("climb_speed", self.ancestry.climb_speed)
|
||||
return self._apply_modifiers("climb_speed", self.ancestry._climb_speed)
|
||||
|
||||
@property
|
||||
def swim_speed(self):
|
||||
return self.apply_modifiers("swim_speed", self.ancestry.swim_speed)
|
||||
return self._apply_modifiers("swim_speed", self.ancestry._swim_speed)
|
||||
|
||||
@property
|
||||
def fly_speed(self):
|
||||
return self.apply_modifiers("fly_speed", self.ancestry.fly_speed)
|
||||
return self._apply_modifiers("fly_speed", self.ancestry._fly_speed)
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
@@ -254,13 +257,15 @@ class Character(BaseObject, SlugMixin, SavingThrowsMixin, SkillsMixin, ModifierM
|
||||
self.add_class_attribute(attr, attr.options[0])
|
||||
|
||||
def remove_class(self, target):
|
||||
self.class_map = [m for m in self.class_map if m.id != target.id]
|
||||
self.class_map = [m for m in self.class_map if m.character_class != target]
|
||||
for mapping in self.character_class_attribute_map:
|
||||
if mapping.character_class.id == target.id:
|
||||
self.remove_class_attribute(mapping.class_attribute)
|
||||
|
||||
def remove_class_attribute(self, attribute):
|
||||
self.character_class_attribute_map = [m for m in self.character_class_attribute_map if m.id != attribute.id]
|
||||
self.character_class_attribute_map = [
|
||||
m for m in self.character_class_attribute_map if m.class_attribute.id != attribute.id
|
||||
]
|
||||
|
||||
def add_class_attribute(self, attribute, option):
|
||||
for thisclass in self.classes.values():
|
||||
@@ -270,7 +275,12 @@ class Character(BaseObject, SlugMixin, SavingThrowsMixin, SkillsMixin, ModifierM
|
||||
if attribute.name in self.class_attributes:
|
||||
return True
|
||||
self.attribute_list.append(
|
||||
CharacterClassAttributeMap(character_id=self.id, class_attribute=attribute, option=option)
|
||||
CharacterClassAttributeMap(
|
||||
character_id=self.id,
|
||||
class_attribute_id=attribute.id,
|
||||
option_id=option.id,
|
||||
class_attribute=attribute,
|
||||
)
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -27,6 +27,17 @@ class ClassAttribute(BaseObject):
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
options = relationship("ClassAttributeOption", cascade="all,delete,delete-orphan", lazy="immediate")
|
||||
|
||||
def add_option(self, **kwargs):
|
||||
option = ClassAttributeOption(attribute_id=self.id, **kwargs)
|
||||
if not self.options or option not in self.options:
|
||||
option.attribute_id = self.id
|
||||
if not self.options:
|
||||
self.options = [option]
|
||||
else:
|
||||
self.options.append(option)
|
||||
return True
|
||||
return False
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.id}: {self.name}"
|
||||
|
||||
@@ -35,7 +46,7 @@ class ClassAttributeOption(BaseObject):
|
||||
__tablename__ = "class_attribute_option"
|
||||
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
attribute_id: Mapped[int] = mapped_column(ForeignKey("class_attribute.id"), nullable=False)
|
||||
attribute_id: Mapped[int] = mapped_column(ForeignKey("class_attribute.id"), nullable=True)
|
||||
|
||||
|
||||
class CharacterClass(BaseObject, SavingThrowsMixin, SkillsMixin):
|
||||
@@ -47,6 +58,16 @@ class CharacterClass(BaseObject, SavingThrowsMixin, SkillsMixin):
|
||||
proficiencies: Mapped[str] = mapped_column(default="")
|
||||
attributes = relationship("ClassAttributeMap", cascade="all,delete,delete-orphan", lazy="immediate")
|
||||
|
||||
def add_attribute(self, attribute, level=1):
|
||||
if not self.attributes or attribute not in self.attributes:
|
||||
mapping = ClassAttributeMap(character_class_id=self.id, class_attribute_id=attribute.id, level=level)
|
||||
if not self.attributes:
|
||||
self.attributes = [mapping]
|
||||
else:
|
||||
self.attributes.append(mapping)
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def attributes_by_level(self):
|
||||
by_level = defaultdict(list)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from collections import defaultdict
|
||||
from typing import Any, Union
|
||||
|
||||
from sqlalchemy import ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.ext.declarative import declared_attr
|
||||
@@ -94,6 +95,9 @@ class ModifierMixin:
|
||||
|
||||
@declared_attr
|
||||
def modifier_map(cls):
|
||||
"""
|
||||
Create the join between the current model and the ModifierMap table.
|
||||
"""
|
||||
return relationship(
|
||||
"ModifierMap",
|
||||
primaryjoin=(
|
||||
@@ -111,12 +115,20 @@ class ModifierMixin:
|
||||
|
||||
@property
|
||||
def modifiers(self):
|
||||
"""
|
||||
Return all modifiers for the current instance as a dict keyed on target attribute name.
|
||||
"""
|
||||
all_modifiers = defaultdict(list)
|
||||
for mapping in self.modifier_map:
|
||||
all_modifiers[mapping.modifier.target].append(mapping.modifier)
|
||||
return all_modifiers
|
||||
|
||||
def add_modifier(self, modifier):
|
||||
def add_modifier(self, modifier: Modifier) -> bool:
|
||||
"""
|
||||
Associate a modifier to the current instance if it isn't already.
|
||||
|
||||
Returns True if the modifier was added; False if was already present.
|
||||
"""
|
||||
if modifier.absolute_value is not None and modifier.relative_value is not None and modifier.multiple_value:
|
||||
raise AttributeError(f"You must provide only one of absolute, relative, and multiple values {modifier}.")
|
||||
|
||||
@@ -131,15 +143,74 @@ class ModifierMixin:
|
||||
)
|
||||
return True
|
||||
|
||||
def remove_modifier(self, modifier):
|
||||
def remove_modifier(self, modifier: Modifier) -> bool:
|
||||
"""
|
||||
Remove a modifier from the map.
|
||||
|
||||
Returns True if it was removed and False if it wasn't present.
|
||||
"""
|
||||
if modifier not in self.modifiers[modifier.target]:
|
||||
return False
|
||||
self.modifier_map = [mapping for mapping in self.modifier_map if mapping.modifier != modifier]
|
||||
return True
|
||||
|
||||
def _apply_modifiers(self, target, initial, modify_class=None):
|
||||
if not modify_class:
|
||||
modify_class = globals()["ModifiableInt"] if isinstance(initial, int) else globals()["ModifiableStr"]
|
||||
def _modifiable_column(self, attr_name: str) -> Union[mapped_column, None]:
|
||||
"""
|
||||
Given an atttribute name, look for a column attribute with the same
|
||||
name but with an underscore prefix. If that column exists, and it
|
||||
has one or more of the expected "modifiable" keys in its info, the
|
||||
column is modifiable.
|
||||
|
||||
Returns the matching column if it was found, or None.
|
||||
"""
|
||||
col = getattr(self.__table__.columns, f"_{attr_name}", None)
|
||||
if col is None:
|
||||
return None
|
||||
for key in col.info.keys():
|
||||
if key.startswith("modifiable"):
|
||||
return col
|
||||
return None
|
||||
|
||||
def _get_modifiable_base(self, attr_name: str) -> object:
|
||||
"""
|
||||
Resolve a dottted string "foo.bar.baz" as its corresponding nested attribute.
|
||||
|
||||
This is useful for cases where a column definition includes a modifiable_base
|
||||
that is some other attribute. For example:
|
||||
|
||||
foo[int] = mapped_column(default=0, info={"modifiable_base": "ancestry.bar")
|
||||
|
||||
This will create an initial value for self.foo equal to self.ancesetry.bar.
|
||||
"""
|
||||
|
||||
def get_attr(obj, parts):
|
||||
if parts:
|
||||
name, *parts = parts
|
||||
return get_attr(getattr(obj, name), parts)
|
||||
return obj
|
||||
|
||||
return get_attr(self, attr_name.split("."))
|
||||
|
||||
def _apply_modifiers(self, target: str, initial: Any, modifiable_class: type = None) -> Modifiable:
|
||||
"""
|
||||
Apply all the modifiers for a given target and return the modified value.
|
||||
|
||||
This is mostly called from __getattr__() below to handle cases where a
|
||||
column is named self._foo but the modified value is accessible as
|
||||
self.foo. It can also be invoked directly, as, say from a property:
|
||||
|
||||
@property
|
||||
def speed(self):
|
||||
return self._apply_modifiers("speed", self.ancestry.speed)
|
||||
|
||||
Args:
|
||||
target - The name of the attribute to modify
|
||||
initial - The initial value for the target
|
||||
modifiable_class - The object type to return; inferred from the
|
||||
target attribute's type if not specified.
|
||||
"""
|
||||
if not modifiable_class:
|
||||
modifiable_class = globals()["ModifiableInt"] if isinstance(initial, int) else globals()["ModifiableStr"]
|
||||
|
||||
# get the modifiers in order from most to least recent
|
||||
modifiers = list(reversed(self.modifiers.get(target, [])))
|
||||
@@ -161,18 +232,28 @@ class ModifierMixin:
|
||||
else:
|
||||
modified = initial
|
||||
|
||||
return modify_class(base=initial, modified=modified)
|
||||
return modifiable_class(base=initial, modified=modified) if modified is not None else None
|
||||
|
||||
def __setattr__(self, attr_name, value):
|
||||
col = getattr(self.__table__.columns, f"_{attr_name}", None)
|
||||
if col is not None and col.info.get("modify", False):
|
||||
raise AttributeError(f"You cannot set .{attr_name}. Did you mean ._{attr_name}?")
|
||||
"""
|
||||
Prevent callers from setting the value of a Modifiable directly.
|
||||
"""
|
||||
col = self._modifiable_column(attr_name)
|
||||
if col is not None:
|
||||
raise AttributeError(f"You cannot modify .{attr_name}. Did you mean ._{attr_name}?")
|
||||
return super().__setattr__(attr_name, value)
|
||||
|
||||
def __getattr__(self, attr_name):
|
||||
col = getattr(self.__table__.columns, f"_{attr_name}", None)
|
||||
if col is not None and col.info.get("modify", False):
|
||||
"""
|
||||
If the instance has an attribute equal to attr_name but prefixed with an
|
||||
underscore, check to see if that attribute is a column, and modifiable.
|
||||
If it is, return a Modifiable instance corresponding to that column's value.
|
||||
"""
|
||||
col = self._modifiable_column(attr_name)
|
||||
if col is not None:
|
||||
return self._apply_modifiers(
|
||||
attr_name, getattr(self, col.name), modify_class=col.info.get("modify_class", None)
|
||||
attr_name,
|
||||
self._get_modifiable_base(col.info.get("modifiable_base", col.name)),
|
||||
modifiable_class=col.info.get("modifiable_class", None),
|
||||
)
|
||||
return super().__getattr__(attr_name)
|
||||
|
||||
Reference in New Issue
Block a user