Parse spell duration
This commit is contained in:
parent
b992873072
commit
48b41ca885
@ -14,12 +14,54 @@ class Ability(enum.StrEnum):
|
|||||||
Charisma = "charisma"
|
Charisma = "charisma"
|
||||||
|
|
||||||
|
|
||||||
|
class DurationType(enum.StrEnum):
|
||||||
|
Timed = "timed"
|
||||||
|
Instant = "instant"
|
||||||
|
Permanent = "permanent"
|
||||||
|
Special = "special"
|
||||||
|
|
||||||
|
|
||||||
|
class DurationTimeType(enum.StrEnum):
|
||||||
|
Round = "round"
|
||||||
|
Minute = "minute"
|
||||||
|
Hour = "hour"
|
||||||
|
Day = "day"
|
||||||
|
|
||||||
|
|
||||||
|
class Duration(typing.NamedTuple):
|
||||||
|
type: DurationType
|
||||||
|
time_type: typing.Optional[DurationTimeType]
|
||||||
|
time_value: typing.Optional[int]
|
||||||
|
concentration: bool
|
||||||
|
end_condition: typing.Set[str]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_json(cls, json_data) -> typing.Self:
|
||||||
|
dt = DurationType(json_data["type"])
|
||||||
|
return cls(
|
||||||
|
type=dt,
|
||||||
|
time_type=(
|
||||||
|
DurationTimeType(json_data["duration"]["type"])
|
||||||
|
if dt == DurationType.Timed
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
time_value=(
|
||||||
|
int(json_data["duration"]["amount"])
|
||||||
|
if dt == DurationType.Timed
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
concentration=json_data.get("concentration", False),
|
||||||
|
end_condition=set(json_data.get("end", [])),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
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]
|
||||||
# remaining: 'affectsCreatureType', 'alias', 'areaTags', 'basicRules2024', 'components', 'conditionImmune', 'conditionInflict', 'damageImmune', 'damageInflict', 'damageResist', 'damageVulnerable', 'duration', 'entriesHigherLevel', 'hasFluffImages', 'level', 'meta', 'miscTags', 'page', 'range', 'savingThrow', 'scalingLevelDice', 'school', 'spellAttack', 'srd52', 'time'
|
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'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, json_data) -> typing.Self:
|
def from_json(cls, json_data) -> typing.Self:
|
||||||
@ -33,11 +75,16 @@ class Spell(typing.NamedTuple):
|
|||||||
description = cls.description_from_json(json_data)
|
description = cls.description_from_json(json_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Unable to parse description for {name}: {e}")
|
raise Exception(f"Unable to parse description for {name}: {e}")
|
||||||
|
try:
|
||||||
|
duration = cls.duration_from_json(json_data)
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"Unable to parse duration 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,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -50,6 +97,10 @@ class Spell(typing.NamedTuple):
|
|||||||
def description_from_json(cls, json_data) -> str:
|
def description_from_json(cls, json_data) -> str:
|
||||||
return " ".join([str(e) for e in json_data["entries"]])
|
return " ".join([str(e) for e in json_data["entries"]])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def duration_from_json(cls, json_data) -> typing.List[Duration]:
|
||||||
|
return [Duration.from_json(d) for d in json_data["duration"]]
|
||||||
|
|
||||||
|
|
||||||
class SpellList:
|
class SpellList:
|
||||||
def __init__(self, code: str, filepath: pathlib.Path) -> None:
|
def __init__(self, code: str, filepath: pathlib.Path) -> None:
|
||||||
@ -65,6 +116,7 @@ class SpellList:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Failed to read spells file {filepath}: {e}")
|
raise Exception(f"Failed to read spells file {filepath}: {e}")
|
||||||
spells: typing.List[Spell] = []
|
spells: typing.List[Spell] = []
|
||||||
|
dbg_duration_type = set()
|
||||||
for spell_data in spell_list_data["spell"]:
|
for spell_data in spell_list_data["spell"]:
|
||||||
if "_copy" in spell_data:
|
if "_copy" in spell_data:
|
||||||
continue # Ignore spells that are just tweaks or reprints
|
continue # Ignore spells that are just tweaks or reprints
|
||||||
|
@ -14,4 +14,5 @@ def main():
|
|||||||
codes = args.source_code
|
codes = args.source_code
|
||||||
for code in codes:
|
for code in codes:
|
||||||
spell_list = db.get_spell_list(code)
|
spell_list = db.get_spell_list(code)
|
||||||
pprint.pprint(spell_list.spells)
|
# pprint.pprint(spell_list.spells)
|
||||||
|
print(f"Found {len(spell_list.spells)} spells")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user