Parsing the spell attack type and area type

This commit is contained in:
David Kruger 2025-05-20 22:13:17 -07:00
parent 48b41ca885
commit 6cab3b289a

View File

@ -55,13 +55,35 @@ class Duration(typing.NamedTuple):
) )
class SpellAttackType(enum.StrEnum):
Melee = "M"
Ranged = "R"
Other = "O"
class SpellAreaType(enum.StrEnum):
SingleTarget = "ST"
MultipleTargets = "MT"
Cube = "C"
Cone = "N"
Cylinder = "Y"
Sphere = "S"
Circle = "R"
Square = "Q"
Line = "L"
Hemisphere = "H"
Wall = "W"
class Spell(typing.NamedTuple): class Spell(typing.NamedTuple):
name: str name: str
source: str source: str
description: str description: str
ability_check: typing.Set[Ability] ability_check: typing.Set[Ability]
duration: typing.List[Duration] duration: typing.List[Duration]
# remaining: 'affectsCreatureType', 'alias', 'areaTags', 'basicRules2024', 'components', 'conditionImmune', 'conditionInflict', 'damageImmune', 'damageInflict', 'damageResist', 'damageVulnerable', 'entriesHigherLevel', 'hasFluffImages', 'level', 'meta', 'miscTags', 'page', 'range', 'savingThrow', 'scalingLevelDice', 'school', 'spellAttack', 'srd52', 'time' attack_type: typing.Set[SpellAttackType]
area_type: typing.Set[SpellAreaType]
# remaining: 'affectsCreatureType', 'alias', 'basicRules2024', 'components', 'conditionImmune', 'conditionInflict', 'damageImmune', 'damageInflict', 'damageResist', 'damageVulnerable', 'entriesHigherLevel', 'hasFluffImages', 'level', 'meta', 'miscTags', 'page', 'range', 'savingThrow', 'scalingLevelDice', 'school', 'srd52', 'time'
@classmethod @classmethod
def from_json(cls, json_data) -> typing.Self: def from_json(cls, json_data) -> typing.Self:
@ -79,19 +101,43 @@ class Spell(typing.NamedTuple):
duration = cls.duration_from_json(json_data) duration = cls.duration_from_json(json_data)
except Exception as e: except Exception as e:
raise Exception(f"Unable to parse duration for {name}: {e}") raise Exception(f"Unable to parse duration for {name}: {e}")
try:
attack_type = cls.attack_type_from_json(json_data)
except Exception as e:
raise Exception(f"Unable to parse spellAttack for {name}: {e}")
try:
area_type = cls.area_type_from_json(json_data)
except Exception as e:
raise Exception(f"Unable to parse areaTags for {name}: {e}")
return cls( return cls(
name=name, name=name,
source=source, source=source,
description=description, description=description,
ability_check=ability_check, ability_check=ability_check,
duration=duration, duration=duration,
attack_type=attack_type,
area_type=area_type,
) )
@classmethod @classmethod
def ability_check_from_json(cls, json_data) -> typing.Set[Ability]: def ability_check_from_json(cls, json_data) -> typing.Set[Ability]:
if "abilityCheck" not in json_data: return cls.json_str_enum_list(json_data, "abilityCheck", Ability)
@classmethod
def attack_type_from_json(cls, json_data) -> typing.Set[SpellAttackType]:
return cls.json_str_enum_list(json_data, "spellAttack", SpellAttackType)
@classmethod
def area_type_from_json(cls, json_data) -> typing.Set[SpellAreaType]:
return cls.json_str_enum_list(json_data, "areaTags", SpellAreaType)
@classmethod
def json_str_enum_list(
cls, json_data, key: str, enum_cls: typing.Type[enum.StrEnum]
) -> typing.Set[enum.StrEnum]:
if key not in json_data:
return set() return set()
return {Ability(c) for c in json_data["abilityCheck"]} return {enum_cls(c) for c in json_data[key]}
@classmethod @classmethod
def description_from_json(cls, json_data) -> str: def description_from_json(cls, json_data) -> str: