2024-05-16 04:12:06 +00:00
|
|
|
from logging import getLogger
|
2024-06-16 21:36:24 +00:00
|
|
|
from typing import List
|
2024-05-16 04:12:06 +00:00
|
|
|
|
2024-05-27 13:10:24 +00:00
|
|
|
from taleweave.context import get_game_systems
|
|
|
|
from taleweave.game_system import FormatPerspective
|
|
|
|
from taleweave.models.entity import Character, WorldEntity
|
2024-05-16 04:12:06 +00:00
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-05-27 01:32:03 +00:00
|
|
|
def describe_character(
|
|
|
|
character: Character,
|
|
|
|
perspective: FormatPerspective = FormatPerspective.SECOND_PERSON,
|
2024-05-16 04:12:06 +00:00
|
|
|
) -> str:
|
2024-05-27 01:32:03 +00:00
|
|
|
attribute_descriptions = format_attributes(character, perspective=perspective)
|
2024-06-05 02:15:36 +00:00
|
|
|
logger.info(
|
|
|
|
"describing character: %s, attributes: '%s'",
|
|
|
|
character.name,
|
|
|
|
attribute_descriptions,
|
|
|
|
)
|
2024-05-16 04:12:06 +00:00
|
|
|
|
|
|
|
if perspective == FormatPerspective.SECOND_PERSON:
|
2024-05-27 01:32:03 +00:00
|
|
|
character_description = character.backstory
|
2024-05-16 04:12:06 +00:00
|
|
|
elif perspective == FormatPerspective.THIRD_PERSON:
|
2024-05-27 01:32:03 +00:00
|
|
|
character_description = character.description
|
2024-05-16 04:12:06 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Perspective {perspective} is not implemented")
|
|
|
|
|
2024-06-16 21:36:24 +00:00
|
|
|
# TODO: use template
|
|
|
|
return f"{character_description} {'. '.join(attribute_descriptions)}"
|
2024-05-16 04:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def describe_static(entity: WorldEntity) -> str:
|
2024-06-05 02:40:17 +00:00
|
|
|
attribute_descriptions = format_attributes(entity)
|
|
|
|
logger.info(
|
|
|
|
"describing entity: %s, attributes: '%s'",
|
|
|
|
entity.name,
|
|
|
|
attribute_descriptions,
|
|
|
|
)
|
2024-06-16 21:36:24 +00:00
|
|
|
|
|
|
|
# TODO: use template
|
|
|
|
return f"{entity.description} {'. '.join(attribute_descriptions)}"
|
2024-05-16 04:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def describe_entity(
|
|
|
|
entity: WorldEntity,
|
2024-05-31 03:44:19 +00:00
|
|
|
perspective: FormatPerspective = FormatPerspective.THIRD_PERSON,
|
2024-05-16 04:12:06 +00:00
|
|
|
) -> str:
|
2024-05-27 01:32:03 +00:00
|
|
|
if isinstance(entity, Character):
|
|
|
|
return describe_character(entity, perspective)
|
2024-05-16 04:12:06 +00:00
|
|
|
|
|
|
|
return describe_static(entity)
|
|
|
|
|
|
|
|
|
|
|
|
def format_attributes(
|
|
|
|
entity: WorldEntity,
|
|
|
|
perspective: FormatPerspective = FormatPerspective.SECOND_PERSON,
|
2024-06-16 21:36:24 +00:00
|
|
|
) -> List[str]:
|
2024-05-16 04:12:06 +00:00
|
|
|
systems = get_game_systems()
|
2024-06-16 21:36:24 +00:00
|
|
|
attribute_descriptions = []
|
|
|
|
for system in systems:
|
|
|
|
if system.format:
|
|
|
|
attribute_descriptions.extend(
|
|
|
|
system.format(entity, perspective=perspective)
|
|
|
|
)
|
|
|
|
|
2024-06-13 04:02:25 +00:00
|
|
|
attribute_descriptions = [
|
|
|
|
description
|
|
|
|
for description in attribute_descriptions
|
|
|
|
if len(description.strip()) > 0
|
|
|
|
]
|
2024-05-16 04:12:06 +00:00
|
|
|
|
2024-06-16 21:36:24 +00:00
|
|
|
return attribute_descriptions
|
2024-05-31 23:58:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def name_entity(
|
|
|
|
entity: str | WorldEntity,
|
|
|
|
) -> str:
|
|
|
|
if isinstance(entity, str):
|
|
|
|
return entity
|
|
|
|
|
|
|
|
return entity.name
|