Send telegraph messages to player when mobs attack

This commit is contained in:
Jared Miller 2026-02-12 18:05:29 -05:00
parent a4a95694f8
commit 61ab785b59
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
3 changed files with 36 additions and 43 deletions

View file

@ -40,10 +40,10 @@ async def process_mobs(combat_moves: dict[str, CombatMove]) -> None:
# Attack AI: act when encounter is IDLE
if encounter.state == CombatState.IDLE:
_try_attack(mob, encounter, combat_moves, now)
await _try_attack(mob, encounter, combat_moves, now)
def _try_attack(mob, encounter, combat_moves, now):
async def _try_attack(mob, encounter, combat_moves, now):
"""Attempt to pick and execute an attack move."""
# Filter to affordable attack moves from mob's move list
affordable = []
@ -71,8 +71,10 @@ def _try_attack(mob, encounter, combat_moves, now):
encounter.attack(move)
mob.next_action_at = now + MOB_ACTION_COOLDOWN
# Send telegraph to the player (the other participant)
# This is fire-and-forget since mob.send is a no-op
# Send telegraph to the player (the defender after role swap)
if move.telegraph:
telegraph_msg = move.telegraph.format(attacker=mob.name)
await encounter.defender.send(f"{telegraph_msg}\r\n")
def _try_defend(mob, encounter, combat_moves, now):

View file

@ -432,7 +432,6 @@ async def test_look_ignores_items_at_other_positions(player, test_zone):
output = "".join([call[0][0] for call in player.writer.write.call_args_list])
assert "far_rock" not in output.lower()
<<<<<<< HEAD
@pytest.mark.asyncio
@ -536,41 +535,3 @@ async def test_look_no_here_line_when_alone(player, test_zone):
output = "".join(c[0][0] for c in player.writer.write.call_args_list)
assert "Here:" not in output
@pytest.mark.asyncio
async def test_quit_removes_player_from_zone(monkeypatch):
"""Quitting removes player from zone contents."""
from mudlib.commands import quit as quit_mod
from mudlib.player import players
monkeypatch.setattr(quit_mod, "save_player", lambda p: None)
terrain = [["." for _ in range(10)] for _ in range(10)]
zone = Zone(
name="qz",
width=10,
height=10,
toroidal=True,
terrain=terrain,
impassable=set(),
)
writer = MagicMock()
writer.write = MagicMock()
writer.drain = AsyncMock()
writer.close = MagicMock()
p = Player(
name="quitter",
location=zone,
x=5,
y=5,
reader=MagicMock(),
writer=writer,
)
players.clear()
players["quitter"] = p
assert p in zone._contents
await quit_mod.cmd_quit(p, "")
assert p not in zone._contents
assert "quitter" not in players

View file

@ -234,6 +234,36 @@ class TestMobAttackAI:
# next_action_at should be ~1 second in the future
assert mob.next_action_at >= before + 0.9
@pytest.mark.asyncio
async def test_mob_sends_telegraph_to_player(
self, player, goblin_toml, moves, test_zone
):
"""When mob attacks, player receives the telegraph message."""
template = load_mob_template(goblin_toml)
mob = spawn_mob(template, 0, 0, test_zone)
mob.next_action_at = 0.0
start_encounter(player, mob)
player.mode_stack.append("combat")
await process_mobs(moves)
# Player's writer should have received the telegraph
# The telegraph should contain mob name and actual telegraph content
player.writer.write.assert_called()
calls = [str(call) for call in player.writer.write.call_args_list]
# Check for actual telegraph patterns from the TOML files
# Goblin moves: punch left/right ("winds up"), sweep ("drops low")
telegraph_patterns = ["winds up", "drops low"]
telegraph_sent = any(
"goblin" in call.lower()
and any(pattern in call.lower() for pattern in telegraph_patterns)
for call in calls
)
assert telegraph_sent, (
f"No telegraph with mob name and content found in: {calls}"
)
class TestMobDefenseAI:
@pytest.fixture