1
0
Fork 0
taleweave-ai/adventure/context.py

165 lines
3.4 KiB
Python
Raw Normal View History

from typing import Callable, Dict, List, Sequence, Tuple
2024-05-02 11:56:57 +00:00
from packit.agent import Agent
from adventure.game_system import GameSystem
2024-05-09 02:11:16 +00:00
from adventure.models.entity import Actor, Room, World
from adventure.models.event import GameEvent
2024-05-02 11:56:57 +00:00
current_broadcast: Callable[[str | GameEvent], None] | None = None
2024-05-04 20:35:42 +00:00
current_world: World | None = None
current_room: Room | None = None
current_actor: Actor | None = None
2024-05-02 11:56:57 +00:00
current_step = 0
2024-05-05 22:46:24 +00:00
dungeon_master: Agent | None = None
game_systems: List[GameSystem] = []
2024-05-02 11:56:57 +00:00
# TODO: where should this one go?
actor_agents: Dict[str, Tuple[Actor, Agent]] = {}
def broadcast(message: str | GameEvent):
2024-05-09 02:11:16 +00:00
if current_broadcast:
current_broadcast(message)
def has_dungeon_master():
return dungeon_master is not None
# region context getters
2024-05-05 22:46:24 +00:00
def get_current_context() -> Tuple[World, Room, Actor]:
2024-05-02 11:56:57 +00:00
if not current_world:
raise ValueError(
"The current world must be set before calling action functions"
)
if not current_room:
raise ValueError("The current room must be set before calling action functions")
if not current_actor:
raise ValueError(
"The current actor must be set before calling action functions"
)
return (current_world, current_room, current_actor)
2024-05-05 22:46:24 +00:00
def get_current_world() -> World | None:
2024-05-02 11:56:57 +00:00
return current_world
2024-05-05 22:46:24 +00:00
def get_current_room() -> Room | None:
2024-05-02 11:56:57 +00:00
return current_room
2024-05-05 22:46:24 +00:00
def get_current_actor() -> Actor | None:
2024-05-02 11:56:57 +00:00
return current_actor
2024-05-04 20:35:42 +00:00
def get_current_broadcast():
return current_broadcast
2024-05-09 02:11:16 +00:00
def get_current_step() -> int:
return current_step
2024-05-04 20:35:42 +00:00
2024-05-09 02:11:16 +00:00
def get_dungeon_master() -> Agent:
if not dungeon_master:
raise ValueError(
"The dungeon master must be set before calling action functions"
)
return dungeon_master
def get_game_systems() -> List[GameSystem]:
return game_systems
2024-05-09 02:11:16 +00:00
# endregion
# region context setters
2024-05-04 20:35:42 +00:00
def set_current_broadcast(broadcast):
global current_broadcast
current_broadcast = broadcast
2024-05-05 22:46:24 +00:00
def set_current_world(world: World | None):
2024-05-02 11:56:57 +00:00
global current_world
current_world = world
2024-05-05 22:46:24 +00:00
def set_current_room(room: Room | None):
2024-05-02 11:56:57 +00:00
global current_room
current_room = room
2024-05-05 22:46:24 +00:00
def set_current_actor(actor: Actor | None):
2024-05-02 11:56:57 +00:00
global current_actor
current_actor = actor
2024-05-05 22:46:24 +00:00
def set_current_step(step: int):
2024-05-02 11:56:57 +00:00
global current_step
current_step = step
2024-05-09 02:11:16 +00:00
def set_actor_agent(name, actor, agent):
actor_agents[name] = (actor, agent)
def set_dungeon_master(agent):
global dungeon_master
dungeon_master = agent
def set_game_systems(systems: Sequence[GameSystem]):
global game_systems
game_systems = list(systems)
2024-05-09 02:11:16 +00:00
# endregion
# region search functions
2024-05-02 11:56:57 +00:00
def get_actor_for_agent(agent):
return next(
(
inner_actor
for inner_actor, inner_agent in actor_agents.values()
if inner_agent == agent
),
None,
)
def get_agent_for_actor(actor):
return next(
(
inner_agent
for inner_actor, inner_agent in actor_agents.values()
if inner_actor == actor
),
None,
)
def get_actor_agent_for_name(name):
return next(
(
(actor, agent)
for actor, agent in actor_agents.values()
if actor.name.lower() == name.lower()
2024-05-02 11:56:57 +00:00
),
(None, None),
)
def get_all_actor_agents():
return list(actor_agents.values())
2024-05-05 22:46:24 +00:00
2024-05-09 02:11:16 +00:00
# endregion