dnd-item-generator/dnd_item/five_e.py

90 lines
2.5 KiB
Python
Raw Normal View History

import json
from collections import defaultdict
2023-12-29 19:24:26 -08:00
from pathlib import Path
2023-12-29 19:24:26 -08:00
import yaml
2023-12-23 16:41:21 -08:00
from random_sets.datasources import DataSource
sources = Path(__file__).parent / Path("sources")
2023-12-29 19:24:26 -08:00
RARITY = {"unknown": "common", "none": "common", "": ""}
2023-12-29 19:24:26 -08:00
TYPE = {"M": "martial", "R": "ranged", "": ""}
2023-12-29 19:24:26 -08:00
DAMAGE = {"S": "Slashing", "P": "Piercing", "B": "Bludgeoning", "": ""}
PROPERTIES = {
2023-12-29 19:24:26 -08:00
"F": "finesse",
"AF": "firearm",
"A": "ammmunition",
"T": "thrown",
"L": "light",
"2H": "two-handed",
"V": "versatile",
"RLD": "reload",
"LD": "loading",
"S": "special",
"H": "heavy",
"R": "reach",
}
class Weapons(DataSource):
"""
A rolltables data source backed by a 5e.tools json data file. used to
convert the 5e.tools data to the yaml format consumed by dnd-rolltables.
"""
2023-12-29 19:24:26 -08:00
def read_source(self) -> None:
2023-12-29 19:24:26 -08:00
src = json.load(self.source)["baseitem"]
self.data = defaultdict(list)
2023-12-29 19:24:26 -08:00
headers = [
"Rarity",
"Name",
"Category",
"Type",
"Weight",
"Damage Type",
"Damage Dice",
"Range",
"Reload",
"Value",
"Properties",
]
for item in src:
2023-12-29 19:24:26 -08:00
if not item.get("weapon", False):
continue
2023-12-29 19:24:26 -08:00
if item.get("age", False):
continue
2023-12-29 19:24:26 -08:00
rarity = RARITY.get(item["rarity"], "Common").capitalize()
itype = TYPE.get(item["type"], "_unknown").capitalize()
properties = ", ".join([PROPERTIES[p] for p in item.get("property", [])])
2023-12-29 19:24:26 -08:00
self.data[rarity].append(
{
item["name"].capitalize(): [
item["weaponCategory"],
itype,
str(item.get("weight", 0)),
DAMAGE.get(item.get("dmgType", "")),
item.get("dmg1", None),
item.get("range", None),
str(item.get("reload", "")),
str(item.get("value", "")),
properties,
]
}
)
self.metadata = {"headers": headers}
@property
def as_yaml(self) -> str:
2023-12-29 19:24:26 -08:00
return yaml.dump({"metadata": self.metadata}) + yaml.dump(dict(self.data))
2023-12-29 19:24:26 -08:00
def weapons(source_path: str = "items-base.json") -> dict:
with open(sources / Path(source_path)) as filehandle:
ds = Weapons(source=filehandle)
return ds