Wire up spectator broadcasting in server and play command

- server.py: broadcast IF output to spectators after each input (skip :: escape commands)
- server.py: broadcast leave message when player exits IF mode
- play.py: broadcast game intro text when player starts a game

Spectators at the same x,y coordinates now see formatted output with
[PlayerName's terminal] header and game text.
This commit is contained in:
Jared Miller 2026-02-09 16:25:39 -05:00
parent 3dd095b9ea
commit 057a746687
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 16 additions and 1 deletions

View file

@ -3,7 +3,7 @@
import pathlib import pathlib
from mudlib.commands import CommandDefinition, register from mudlib.commands import CommandDefinition, register
from mudlib.if_session import IFSession from mudlib.if_session import IFSession, broadcast_to_spectators
from mudlib.player import Player from mudlib.player import Player
# Story files directory # Story files directory
@ -79,6 +79,9 @@ async def cmd_play(player: Player, args: str) -> None:
await player.send("(type ::help for escape commands)\r\n") await player.send("(type ::help for escape commands)\r\n")
if intro: if intro:
await player.send(intro + "\r\n") await player.send(intro + "\r\n")
# Broadcast intro to spectators
spectator_msg = f"[{player.name}'s terminal]\r\n{intro}\r\n"
await broadcast_to_spectators(player, spectator_msg)
register( register(

View file

@ -27,6 +27,7 @@ from mudlib.combat.commands import register_combat_commands
from mudlib.combat.engine import process_combat from mudlib.combat.engine import process_combat
from mudlib.content import load_commands from mudlib.content import load_commands
from mudlib.effects import clear_expired from mudlib.effects import clear_expired
from mudlib.if_session import broadcast_to_spectators
from mudlib.mob_ai import process_mobs from mudlib.mob_ai import process_mobs
from mudlib.mobs import load_mob_templates, mob_templates from mudlib.mobs import load_mob_templates, mob_templates
from mudlib.player import Player, players from mudlib.player import Player, players
@ -340,11 +341,22 @@ async def shell(
response = await player.if_session.handle_input(command) response = await player.if_session.handle_input(command)
if response.output: if response.output:
await player.send(response.output) await player.send(response.output)
# Broadcast to spectators unless it's an escape command
if not command.startswith("::"):
spectator_msg = (
f"[{player.name}'s terminal]\r\n"
f"> {command}\r\n"
f"{response.output}"
)
await broadcast_to_spectators(player, spectator_msg)
if response.done: if response.done:
await player.if_session.stop() await player.if_session.stop()
player.if_session = None player.if_session = None
player.mode_stack.pop() player.mode_stack.pop()
await player.send("you leave the terminal.\r\n") await player.send("you leave the terminal.\r\n")
# Notify spectators
leave_msg = f"{player.name} steps away from the terminal.\r\n"
await broadcast_to_spectators(player, leave_msg)
else: else:
# Dispatch normal command # Dispatch normal command
await mudlib.commands.dispatch(player, command) await mudlib.commands.dispatch(player, command)