Skip to content

Commit

Permalink
Merge pull request #70 from neph1/update-v0.24.4
Browse files Browse the repository at this point in the history
fix issue witha saved  unarmed weapon not being loaded properly
  • Loading branch information
neph1 authored Feb 18, 2024
2 parents d157128 + b786e8c commit c6f2380
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tale/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions tale/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
6 changes: 6 additions & 0 deletions tests/test_llm_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
26 changes: 25 additions & 1 deletion tests/test_parse_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -77,24 +78,36 @@ 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')
npcs_json = parse_utils.load_json("tests/files/test_npcs.json")
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)
Expand Down Expand Up @@ -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)

0 comments on commit c6f2380

Please sign in to comment.