Foundry JSON file now resolves magic item names.

This commit is contained in:
Mark Wolfman
2022-06-07 22:16:09 -05:00
parent 82aa9cdf24
commit 89154bef3b
2 changed files with 40 additions and 4 deletions
+28 -3
View File
@@ -9,6 +9,8 @@ from typing import Union
from pathlib import Path from pathlib import Path
from dungeonsheets import exceptions from dungeonsheets import exceptions
from dungeonsheets.magic_items import MagicItem
from dungeonsheets.content_registry import find_content
log = logging.getLogger(__file__) log = logging.getLogger(__file__)
@@ -431,6 +433,29 @@ class FoundryCharacterReader(JSONCharacterReader):
item_name += f"({quantity})" item_name += f"({quantity})"
yield item_name.lower() yield item_name.lower()
def magic_items(self):
"""Loads magic items. If not defined, try to figure out its
properties.
"""
item_types = ["weapon", "armor", "equipment"]
items = [item for item in self.json_data()['items']
if item['type'] in item_types]
from pprint import pprint
magic_items = [item for item in items if item['data']['rarity'] not in ["Common", ""]]
# Convert magic items into classes
def make_magic_item(data):
try:
item = find_content(data['name'], valid_classes=[MagicItem])
except exceptions.ContentNotFound:
# Make a generic version based on the JSON attributes
item_name = data['name'].replace(' ', '')
item = type(item_name, (MagicItem,), {})
return item
magic_items = [make_magic_item(item) for item in magic_items]
return magic_items
def class_levels(self): def class_levels(self):
for item in self.json_data()["items"]: for item in self.json_data()["items"]:
if item["type"] == "class": if item["type"] == "class":
@@ -544,6 +569,8 @@ class FoundryCharacterReader(JSONCharacterReader):
char_props["equipment"] = ", ".join(self.equipment()) char_props["equipment"] = ", ".join(self.equipment())
char_props["armor"] = self.armor() char_props["armor"] = self.armor()
char_props["shield"] = self.shield() char_props["shield"] = self.shield()
# Magic items
char_props["magic_items"] = self.magic_items()
# Personality, etc # Personality, etc
char_props["personality_traits"] = details["trait"].strip() char_props["personality_traits"] = details["trait"].strip()
char_props["flaws"] = details["flaw"].strip() char_props["flaws"] = details["flaw"].strip()
@@ -555,12 +582,10 @@ class FoundryCharacterReader(JSONCharacterReader):
# Some unused values # Some unused values
warn_msg = ( warn_msg = (
"Importing the following traits from JSON is not yet supported: " "Importing the following traits from JSON is not yet supported: "
"magic_items, attacks_and_spellcasting, " "attacks_and_spellcasting, infusions, wild_shapes."
"infusions, wild_shapes."
) )
warnings.warn(warn_msg) warnings.warn(warn_msg)
log.warning(warn_msg) log.warning(warn_msg)
char_props["magic_items"] = ()
char_props["attacks_and_spellcasting"] = "" char_props["attacks_and_spellcasting"] = ""
char_props["infusions"] = [] char_props["infusions"] = []
char_props["wild_shapes"] = [] char_props["wild_shapes"] = []
+12 -1
View File
@@ -226,7 +226,7 @@ class FoundryReaderTests(unittest.TestCase):
gp=162, gp=162,
pp=2, pp=2,
weapons=["rapier"], weapons=["rapier"],
magic_items=(), # magic_items=(),
armor="padded armor", armor="padded armor",
shield="shield", shield="shield",
personality_traits="Loves a good lawyer joke.", personality_traits="Loves a good lawyer joke.",
@@ -284,3 +284,14 @@ class FoundryReaderTests(unittest.TestCase):
this_result = list(this_result) this_result = list(this_result)
self.assertEqual(this_result, val, key) self.assertEqual(this_result, val, key)
def test_load_homebrew_weapon(self):
"""Check that the properties of a homebrew magic weapon get read
properly.
"""
charfile = FOUNDRY_JSON_FILE
with warnings.catch_warnings(record=True):
result = read_sheet_file(charfile)
# Check that some magic items were set
self.assertGreater(len(result['magic_items']), 0,
"No magic items imported")