diff --git a/dungeonsheets/readers.py b/dungeonsheets/readers.py index 0d402e9..b266801 100644 --- a/dungeonsheets/readers.py +++ b/dungeonsheets/readers.py @@ -9,6 +9,8 @@ from typing import Union from pathlib import Path from dungeonsheets import exceptions +from dungeonsheets.magic_items import MagicItem +from dungeonsheets.content_registry import find_content log = logging.getLogger(__file__) @@ -431,6 +433,29 @@ class FoundryCharacterReader(JSONCharacterReader): item_name += f"({quantity})" 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): for item in self.json_data()["items"]: if item["type"] == "class": @@ -544,6 +569,8 @@ class FoundryCharacterReader(JSONCharacterReader): char_props["equipment"] = ", ".join(self.equipment()) char_props["armor"] = self.armor() char_props["shield"] = self.shield() + # Magic items + char_props["magic_items"] = self.magic_items() # Personality, etc char_props["personality_traits"] = details["trait"].strip() char_props["flaws"] = details["flaw"].strip() @@ -555,12 +582,10 @@ class FoundryCharacterReader(JSONCharacterReader): # Some unused values warn_msg = ( "Importing the following traits from JSON is not yet supported: " - "magic_items, attacks_and_spellcasting, " - "infusions, wild_shapes." + "attacks_and_spellcasting, infusions, wild_shapes." ) warnings.warn(warn_msg) log.warning(warn_msg) - char_props["magic_items"] = () char_props["attacks_and_spellcasting"] = "" char_props["infusions"] = [] char_props["wild_shapes"] = [] diff --git a/tests/test_readers.py b/tests/test_readers.py index c0d2801..21d694d 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -226,7 +226,7 @@ class FoundryReaderTests(unittest.TestCase): gp=162, pp=2, weapons=["rapier"], - magic_items=(), + # magic_items=(), armor="padded armor", shield="shield", personality_traits="Loves a good lawyer joke.", @@ -284,3 +284,14 @@ class FoundryReaderTests(unittest.TestCase): this_result = list(this_result) 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")