Compare commits

...

2 commits

Author SHA1 Message Date
e7b2c21d2b
Remove player from zone contents on disconnect
Player objects were removed from the players dict on quit/disconnect
but never removed from zone._contents, leaving ghost * markers on
other players' maps.
2026-02-12 16:54:51 -05:00
bea3c98124
Show entities at player position beneath the map
Adds a "Here: goblin, Ally" line after the map grid listing mobs and
other players sharing the tile. Dead mobs are excluded.
2026-02-12 16:41:55 -05:00
4 changed files with 118 additions and 1 deletions

View file

@ -102,6 +102,18 @@ async def cmd_look(player: Player, args: str) -> None:
# Send to player
player.writer.write("\r\n".join(output_lines) + "\r\n")
# Show entities (mobs, other players) at the player's position
entities_here = [
obj
for obj in zone.contents_at(player.x, player.y)
if isinstance(obj, Entity)
and obj is not player
and (not hasattr(obj, "alive") or obj.alive)
]
if entities_here:
names = ", ".join(e.name for e in entities_here)
player.writer.write(f"Here: {names}\r\n")
# Show items on the ground at player's position
from mudlib.portal import Portal

View file

@ -19,7 +19,8 @@ async def cmd_quit(player: Player, args: str) -> None:
await player.writer.drain()
player.writer.close()
# Remove from player registry
# Remove from zone and player registry
player.move_to(None)
if player.name in players:
del players[player.name]

View file

@ -450,6 +450,7 @@ async def shell(
# Save player state on disconnect (if not already saved by quit command)
if player_name in players:
save_player(player)
player.move_to(None)
del players[player_name]
log.info("%s disconnected", player_name)

View file

@ -432,3 +432,106 @@ 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()
@pytest.mark.asyncio
async def test_look_shows_entities_here(player, test_zone):
"""look lists mobs and other players at the player's position."""
from mudlib.entity import Mob
from mudlib.player import players
players.clear()
players[player.name] = player
Mob(name="goblin", location=test_zone, x=5, y=5, description="a goblin")
other = Player(
name="Ally",
location=test_zone,
x=5,
y=5,
reader=MagicMock(),
writer=MagicMock(),
)
players["Ally"] = other
await look.cmd_look(player, "")
output = "".join(c[0][0] for c in player.writer.write.call_args_list)
assert "Here: " in output
assert "goblin" in output
assert "Ally" in output
@pytest.mark.asyncio
async def test_look_hides_dead_mobs_from_here(player, test_zone):
"""look omits dead mobs from the here line."""
from mudlib.entity import Mob
from mudlib.player import players
players.clear()
players[player.name] = player
Mob(
name="corpse",
location=test_zone,
x=5,
y=5,
description="dead",
alive=False,
)
await look.cmd_look(player, "")
output = "".join(c[0][0] for c in player.writer.write.call_args_list)
assert "corpse" not in output
@pytest.mark.asyncio
async def test_look_no_here_line_when_alone(player, test_zone):
"""look omits the here line when no other entities are present."""
from mudlib.player import players
players.clear()
players[player.name] = player
await look.cmd_look(player, "")
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