diff --git a/tale/base.py b/tale/base.py index a6057d60..504cce2a 100644 --- a/tale/base.py +++ b/tale/base.py @@ -997,7 +997,7 @@ def set_stats_from_race(self) -> None: self.size = r.size self.hp = r.hp self.max_hp = r.hp - self.unarmed_attack = Weapon(name=r.unarmed_attack.name) + self.unarmed_attack = Weapon(r.unarmed_attack.name, weapon_type=WeaponType.UNARMED) def get_weapon_skill(self, weapon_type: WeaponType) -> int: return self.weapon_skills.get(weapon_type, 0) diff --git a/tale/parse_utils.py b/tale/parse_utils.py index ba35eb4a..ce56db54 100644 --- a/tale/parse_utils.py +++ b/tale/parse_utils.py @@ -10,7 +10,6 @@ from tale.story import GameMode, MoneyType, TickMethod, StoryConfig from tale.weapon_type import WeaponType from tale.wearable import WearLocation -from tale.zone import Zone import json import re import sys @@ -86,7 +85,7 @@ def location_from_json(json_object: dict): location.built = json_object.get('built', True) return location -def load_items(json_items: [], locations = {}) -> dict: +def load_items(json_items: list, locations = {}) -> dict: """ Loads and returns a dict of items from a supplied json dict Inserts into locations if supplied and has location @@ -131,7 +130,7 @@ def load_items(json_items: [], locations = {}) -> dict: _insert(new_item, locations, item['location']) return items -def load_npcs(json_npcs: [], locations = {}) -> dict: +def load_npcs(json_npcs: list, locations = {}) -> dict: """ Loads npcs and returns a dict from a supplied json dict May be custom classes, but be sure the class is available @@ -636,7 +635,7 @@ def load_stats(json_stats: dict) -> Stats: stats.strength = json_stats['strength'] stats.dexterity = json_stats['dexterity'] if json_stats.get('unarmed_attack'): - stats.unarmed_attack = UnarmedAttack[json_stats['unarmed_attack'].upper()] + stats.unarmed_attack = Weapon(UnarmedAttack[json_stats['unarmed_attack'].upper()], WeaponType.UNARMED) if json_stats['weapon_skills']: json_skills = json_stats['weapon_skills'] stats.weapon_skills = {} diff --git a/tests/test_llm_ext.py b/tests/test_llm_ext.py index 5dc50ca0..031b8f4a 100644 --- a/tests/test_llm_ext.py +++ b/tests/test_llm_ext.py @@ -75,6 +75,12 @@ def test_character_card(self): assert('knife' in json_card['items']) assert(eval(json_card['wielding']) == knife.to_dict()) + npc.wielding = None + card = npc.character_card + json_card = json.loads(card) + assert(eval(json_card['wielding']) == npc.stats.unarmed_attack.to_dict()) + + def test_wearing(self): npc = LivingNpc(name='test', gender='m', age=42, personality='') hat = Item("hat", "hat", descr="A big hat.") diff --git a/tests/test_parse_utils.py b/tests/test_parse_utils.py index b96a73f8..b1b4a77f 100644 --- a/tests/test_parse_utils.py +++ b/tests/test_parse_utils.py @@ -1,11 +1,12 @@ import datetime import json from tale import json_story, mud_context, util -from tale.base import Exit, Location, Weapon, Wearable +from tale.base import Exit, Living, Location, Weapon, Wearable from tale.coord import Coord from tale.driver_if import IFDriver from tale.items.basic import Boxlike, Drink, Food, Health, Money from tale.story import GameMode, MoneyType +from tale.weapon_type import WeaponType from tale.wearable import WearLocation from tale.zone import Zone import tale.parse_utils as parse_utils @@ -77,6 +78,8 @@ def test_load_npcs(self): driver = IFDriver(screen_delay=99, gui=False, web=True, wizard_override=True) driver.game_clock = util.GameDateTime(datetime.datetime(year=2023, month=1, day=1), 1) mud_context.driver = driver + + locations = {} locations['Royal grotto'] = Location('Royal grotto', 'A small grotto, fit for a kobold king') @@ -84,17 +87,27 @@ def test_load_npcs(self): npcs = parse_utils.load_npcs(npcs_json, locations) assert(len(npcs) == 3) + npc = npcs['Kobbo'] assert(npc.title == 'Kobbo the King') assert(npc.location == locations['Royal grotto']) assert(npc.aliases.pop() == 'kobbo') + assert(isinstance(npc.stats.unarmed_attack, Weapon)) npc2 = npcs['generated name'] assert(npc2.name == 'generated name') assert(npc2.title == 'generated name') assert(npc2.aliases.pop() == 'generated') + assert(npc2.location == locations['Royal grotto']) npc3 = npcs['name'] + assert(npc3.location == locations['Royal grotto']) assert(npc3.name == 'name') + + saved_npcs = parse_utils.save_npcs(npcs.values()) + + + + def test_load_npcs_generated(self): driver = IFDriver(screen_delay=99, gui=False, web=True, wizard_override=True) driver.game_clock = util.GameDateTime(datetime.datetime(year=2023, month=1, day=1), 1) @@ -310,3 +323,14 @@ def test_trim_response(self): response = '*' trimmed = parse_utils.trim_response(response) assert(trimmed == '') + + def test_save_and_load_stats(self): + npc = Living('test', gender='m') + unarmed = npc.stats.unarmed_attack + npc.stats.set_weapon_skill(WeaponType.UNARMED, 10) + json_stats = parse_utils.save_stats(npc.stats) + assert(json_stats['unarmed_attack'] == 'FISTS') + + loaded_stats = parse_utils.load_stats(json_stats) + assert(isinstance(loaded_stats.unarmed_attack, Weapon)) + assert(loaded_stats.get_weapon_skill(WeaponType.UNARMED) == 10) \ No newline at end of file