Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neph1 committed Dec 2, 2023
1 parent 794b408 commit aa20fef
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
6 changes: 4 additions & 2 deletions tale/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,9 @@ def _process_player_command(self, cmd: str, conn: player.PlayerConnection) -> No
def go_through_exit(self, player: player.Player, direction: str, evoke: bool=True) -> None:
xt = player.location.exits[direction]
xt.allow_passage(player)
dynamic_story = typing.cast(DynamicStory, self.story)

if not xt.target.built:
dynamic_story = typing.cast(DynamicStory, self.story)
zone = dynamic_story.find_zone(location=player.location.name)
# are we close to the edge of a zone? if so we need to build the next zone.
new_zone = self.llm_util.get_neighbor_or_generate_zone(current_zone=zone,
Expand All @@ -637,9 +638,10 @@ def go_through_exit(self, player: player.Player, direction: str, evoke: bool=Tru
zone = dynamic_story.find_zone(location=xt.target.name)
self.llm_util.generate_random_spawn(xt.target, zone.get_info())
else:
dynamic_story = typing.cast(DynamicStory, self.story)
zone = dynamic_story.find_zone(location=player.location.name)
new_zone = dynamic_story.find_zone(location=xt.target.name)
if zone.name != new_zone.name:
if zone and zone.name != new_zone.name:
player.tell(f"You're entering {new_zone.name}:{new_zone.description}")


Expand Down
2 changes: 1 addition & 1 deletion tale/llm/LivingNpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def notify_action(self, parsed: ParseResult, actor: Living) -> None:
else:
result = ItemHandlingResult(item=parsed_split[0].strip(), to=self.title)
if self.handle_item_result(result, actor) and self.quest:
self.quest.check_completion(result)
self.quest.check_completion({"item":result.item, "npc":result.to})
self.do_say(parsed.unparsed, actor)
if self.quest and self.quest.is_completed():
# do last to give npc chance to react
Expand Down
8 changes: 4 additions & 4 deletions tale/llm/world_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ def _add_items(self, location: Location, json_result: dict, world_items: dict =
location.insert(item, None)
return location

def _add_npcs(self, location: Location, json_result: dict, world_creatures: dict = {}):
def _add_npcs(self, location: Location, json_result: dict, world_creatures: dict = {}) -> dict:
generated_npcs = json_result.get("npcs", [])
if not generated_npcs:
return location
return {}
if world_creatures:
generated_npcs = parse_utils.replace_creature_with_world_creature(generated_npcs, world_creatures)
try:
Expand Down Expand Up @@ -191,7 +191,7 @@ def validate_zone(self, json_result: dict, center: Coord) -> Zone:
zone.items = json_result.get('items', [])
return zone

def generate_start_location(self, location: Location, zone_info: dict, story_type: str, story_context: str, world_info: str):
def generate_start_location(self, location: Location, zone_info: dict, story_type: str, story_context: str, world_info: str) -> (list, list, list):
""" Generate a location based on the current story context
One gotcha is that the location is not returned, its contents are just updated"""

Expand All @@ -214,7 +214,7 @@ def generate_start_location(self, location: Location, zone_info: dict, story_typ
return self._validate_location(json_result, location, '')
except Exception as exc:
print(exc)
return None, None
return None, None, None

def generate_start_zone(self, location_desc: str, story_type: str, story_context: str, world_info: dict) -> Zone:
""" Generate a zone based on the current story context"""
Expand Down
2 changes: 1 addition & 1 deletion tale/story_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def apply_to_story(self, story: DynamicStory, llm_util: LlmUtil):
self.connection.output("Generating starting location...")
start_location = Location(name="", descr=self.story_info.start_location)
for i in range(3):
new_locations, exits = llm_util.generate_start_location(location=start_location,
new_locations, exits, npcs = llm_util.generate_start_location(location=start_location,
story_type=self.story_info.type,
story_context=story.config.context,
world_info=self.story_info.world_info,
Expand Down
22 changes: 11 additions & 11 deletions tests/test_llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class TestWorldBuilding():
def test_validate_location(self):
location = Location(name='Outside')
location.built = False
locations, exits = self.llm_util._world_building._validate_location(json.loads(self.generated_location), location_to_build=location, exit_location_name='Entrance')
locations, exits, npcs = self.llm_util._world_building._validate_location(json.loads(self.generated_location), location_to_build=location, exit_location_name='Entrance')
location.add_exits(exits)
assert(location.description.startswith('A barren wasteland'))
assert(len(location.exits) == 2)
Expand All @@ -196,7 +196,7 @@ def test_validate_location_with_world_objects(self):
loc = json.loads(self.generated_location)
loc["npcs"] = ["wolf"]
world_creatures = [{"name": "wolf", "body": "Creature", "unarmed_attack": "BITE", "hp":10, "level":10}]
locations, exits = self.llm_util._world_building._validate_location(loc, location_to_build=location, exit_location_name='Entrance', world_creatures=world_creatures)
locations, exits, npcs = self.llm_util._world_building._validate_location(loc, location_to_build=location, exit_location_name='Entrance', world_creatures=world_creatures)
location.add_exits(exits)
assert(location.description.startswith('A barren wasteland'))
assert(len(location.exits) == 2)
Expand All @@ -211,7 +211,7 @@ def test_validate_location_with_world_objects(self):
def test_generate_start_location(self):
self.llm_util._world_building.io_util.response='{"name": "Greenhaven", "exits": [{"direction": "north", "name": "Misty Meadows", "description": "A lush and misty area filled with rolling hills and sparkling streams. The air is crisp and refreshing, and the gentle chirping of birds can be heard throughout."}, {"direction": "south", "name": "Riverdale", "description": "A bustling town nestled near a winding river. The smell of freshly baked bread and roasting meats fills the air, and the sound of laughter and chatter can be heard from the local tavern."}, {"direction": "east", "name": "Forest of Shadows", "description": "A dark and eerie forest filled with twisted trees and mysterious creatures. The air is thick with an ominous energy, and the rustling of leaves can be heard in the distance."}], "items": [], "npcs": []}'
location = Location(name='', descr='on a small road outside a village')
new_locations, exits = self.llm_util._world_building.generate_start_location(location,
new_locations, exits, npcs = self.llm_util._world_building.generate_start_location(location,
story_type='',
story_context='',
zone_info={},
Expand Down Expand Up @@ -315,15 +315,15 @@ def test_build_location(self):
self.llm_util._world_building.io_util.response = self.generated_location
self.llm_util.set_story(self.story)

new_locations, exits = self.llm_util.build_location(location, exit_location_name, zone_info={})
new_locations, exits, npcs = self.llm_util.build_location(location, exit_location_name, zone_info={})
assert(len(new_locations) == 2)

def test_build_location_extra_json(self):
location = Location(name='Outside')
exit_location_name = 'Cave entrance'
self.llm_util._world_building.io_util.response = self.generated_location_extra
self.llm_util.set_story(self.story)
new_locations, exits = self.llm_util.build_location(location, exit_location_name, zone_info={})
new_locations, exits, npcs = self.llm_util.build_location(location, exit_location_name, zone_info={})
assert(len(new_locations) == 2)

def test_validate_zone(self):
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_issue_1_build_location(self):
exit_location_name = 'Harvest Grove'
self.llm_util._world_building.io_util.response = '{"description": "Whispering Meadows is a serene and idyllic area nestled within Eldervale. It is a sprawling expanse of lush green meadows, dotted with colorful wildflowers swaying gently in the breeze. The sweet fragrance of blooming lavender fills the air, creating an enchanting atmosphere. The meadows are home to a variety of friendly creatures, and the soothing whispers of the wind carry tales of peace and harmony. With its tranquil beauty, Whispering Meadows provides the perfect backdrop for a cosy social and farming experience.", "exits": [ { "direction": "North", "name": "Harvest Grove", "short_descr": "A hidden pathway leads to the Harvest Grove, where trees bear fruits of extraordinary flavors." }, { "direction": "East", "name": "Glimmering Glade", "short_descr": "A shimmering path leads to the Glimmering Glade, where fireflies illuminate secrets of the woods." }, { "direction": "West", "name": "Twilight Meadow", "short_descr": "A mysterious trail leads to the Twilight Meadow, where moonlight reveals hidden wonders to explorers." } ], "items": [ "Enchanted Seeds (plantable)", "Harvesting Scythe", "Rainbow Fruit Basket", "Magic Beehive", "Fairy Lantern", "Mystical Herb Pouch", "Whispering Wind Chime", "Dreamcatcher Necklace" ], "npcs": [ { "name": "Amelia", "sentiment": "friendly", "race": "Pixie", "gender": "f", "level": 3, "description": "A mischievous Pixie with wings shimmering in various hues. She loves to play pranks but has a heart of gold." }, { "name": "Basil", "sentiment": "friendly", "race": "Centaur", "gender": "m", "level": 5, "description": "A gentle Centaur with a serene disposition. He imparts wisdom with every gallop and nurtures plants with care." }, { "name": "Celeste", "sentiment": "friendly", "race": "Fairie", "gender": "n", "level": 4, "description": "A gracious Fairie with shimmering wings that radiate ethereal light. She ensures the beauty of Whispering Meadows remains eternal." } ] }'
self.llm_util.set_story(self.story)
new_locations, exits = self.llm_util.build_location(location, exit_location_name, zone_info=z.get_info())
new_locations, exits, npcs = self.llm_util.build_location(location, exit_location_name, zone_info=z.get_info())
assert(len(new_locations) == 2)

def test_chatgpt_generated_story(self):
Expand Down Expand Up @@ -389,7 +389,7 @@ def test_chatgpt_generated_story(self):
story_context='',
world_info=world_info)
assert(zone)
new_locations, exits = self.llm_util.build_location(location, exit_location_name, zone_info=zone.get_info(), world_creatures=world_creatures, world_items=world_items)
new_locations, exits, npcs = self.llm_util.build_location(location, exit_location_name, zone_info=zone.get_info(), world_creatures=world_creatures, world_items=world_items)
assert(len(new_locations) > 0)
assert(len(exits) > 0)
assert(len(location.items) == 3)
Expand All @@ -399,7 +399,7 @@ def test_chatgpt_generated_story(self):
exit_location_name2 = 'Meadow\'s Edge'


new_locations, exits = self.llm_util.build_location(location2, exit_location_name2, zone_info=zone.get_info(), world_creatures=world_creatures, world_items=world_items)
new_locations, exits, npcs = self.llm_util.build_location(location2, exit_location_name2, zone_info=zone.get_info(), world_creatures=world_creatures, world_items=world_items)
assert(len(new_locations) > 0)
assert(len(exits) > 0)

Expand Down Expand Up @@ -447,7 +447,7 @@ def test_issue_overwriting_exits(self):
self.llm_util._world_building.io_util.response=['{"name": "Forest Path", "exits": [{"direction": "north", "name": "Mystic Woods", "short_descr": "A dense, misty forest teeming with ancient magic."}, {"direction": "south", "name": "Blooming Meadow", "short_descr": "A lush, vibrant meadow filled with wildflowers and gentle creatures."}, {"direction": "west", "name": "Rocky Cliffs", "short_descr": "A rugged, rocky terrain with breathtaking views of the surrounding landscape."}], "items": [{"name": "enchanted forest amulet", "type": "Wearable", "description": "A shimmering amulet infused with the magic of the forest, granting the wearer a moderate boost to their defense and resistance to harm."}], "npcs": [{"name": "Florabug", "sentiment": "neutral", "race": "florabug", "gender": "m", "level": 5, "description": "A friendly, curious creature who loves to make new friends."}]}',
'{"description": "A picturesque beach with soft, golden sand and crystal clear waters. The sun shines bright overhead, casting a warm glow over the area. The air is filled with the sound of gentle waves and the cries of seagulls. A few scattered palm trees provide shade and a sense of tranquility.", "exits": [{"direction": "north", "name": "Coastal Caves", "short_descr": "A network of dark, damp caves hidden behind the sandy shores."}, {"direction": "south", "name": "Rocky Cliffs", "short_descr": "A rugged, rocky coastline with steep drop-offs and hidden sea creatures."}, {"direction": "east", "name": "Mermaid\'s Grotto", "short_descr": "A hidden underwater cave system, rumored to be home to magical sea creatures."}], "items": [], "npcs": []}']
location = Location(name='', descr='on a small road outside a forest')
new_locations, exits = self.llm_util._world_building.generate_start_location(location,
new_locations, exits, npcs = self.llm_util._world_building.generate_start_location(location,
story_type='',
story_context='',
zone_info={},
Expand All @@ -462,7 +462,7 @@ def test_issue_overwriting_exits(self):
rocky_cliffs = new_locations[2] # type: Location
assert(rocky_cliffs.name == 'Rocky Cliffs')

new_locations, exits = self.llm_util._world_building.build_location(rocky_cliffs,
new_locations, exits, npcs2 = self.llm_util._world_building.build_location(rocky_cliffs,
'Rocky Cliffs',
story_type='',
story_context='',
Expand All @@ -484,7 +484,7 @@ class TestQuestBuilding():

def test_generate_note_quest(self):
self.llm_util._quest_building.io_util.response = '{"name": "Test Quest", "reason": "A test quest", "target":"Arto", "type":"talk"}'
quest = self.llm_util._quest_building.generate_note_quest(story_context='', story_type='', world_info='')
quest = self.llm_util._quest_building.generate_note_quest(story_context='', story_type='', world_info='', zone_info='')
assert(quest.name == 'Test Quest')
assert(quest.reason == 'A test quest')
assert(quest.target == 'Arto')
Expand Down
2 changes: 2 additions & 0 deletions tests/test_mudobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from tale.base import Location, Exit, Item, MudObject, Living, _limbo, Container, Weapon, Door, Key, ParseResult, MudObjRegistry
from tale.demo.story import Story as DemoStory
from tale.errors import ActionRefused, LocationIntegrityError, UnknownVerbException, TaleError
from tale.llm.llm_ext import DynamicStory
from tale.player import Player
from tale.story import MoneyType
from tale.shop import Shopkeeper
Expand Down Expand Up @@ -413,6 +414,7 @@ def test_exits(self):

def test_go_through_exit(self):
driver = FakeDriver()
driver.story = DynamicStory()
hall = Location("hall")
attic = Location("attic")
exit1 = Exit("ladder1", attic, "exit 1 to attic", enter_msg="entering the attic via exit 1")
Expand Down

0 comments on commit aa20fef

Please sign in to comment.