Block quit command during combat

This commit is contained in:
Jared Miller 2026-02-13 23:59:04 -05:00
parent a4c9f31056
commit 36fcbecc12
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 88 additions and 0 deletions

View file

@ -12,6 +12,11 @@ async def cmd_quit(player: Player, args: str) -> None:
player: The player executing the command
args: Command arguments (unused)
"""
# Block quitting during combat
if player.mode == "combat":
await player.send("You can't quit during combat!\r\n")
return
# Save player state before disconnecting
save_player(player)

83
tests/test_quit.py Normal file
View file

@ -0,0 +1,83 @@
"""Tests for quit command."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from mudlib.commands.quit import cmd_quit
from mudlib.player import Player, players
from mudlib.zone import Zone
@pytest.fixture(autouse=True)
def clear_state():
"""Clear players before and after each test."""
players.clear()
yield
players.clear()
@pytest.fixture
def test_zone():
"""Create a test zone for players."""
terrain = [["." for _ in range(256)] for _ in range(256)]
zone = Zone(
name="testzone",
width=256,
height=256,
toroidal=True,
terrain=terrain,
impassable=set(),
)
return zone
@pytest.fixture
def mock_writer():
writer = MagicMock()
writer.write = MagicMock()
writer.drain = AsyncMock()
writer.close = MagicMock()
return writer
@pytest.fixture
def mock_reader():
return MagicMock()
@pytest.fixture
def player(mock_reader, mock_writer, test_zone):
p = Player(name="Goku", x=0, y=0, reader=mock_reader, writer=mock_writer)
p.location = test_zone
test_zone._contents.append(p)
players[p.name] = p
return p
@pytest.mark.asyncio
async def test_quit_blocked_during_combat(player):
"""Test quit is blocked when player is in combat mode."""
player.mode_stack.append("combat")
await cmd_quit(player, "")
player.writer.write.assert_called_once_with("You can't quit during combat!\r\n")
player.writer.close.assert_not_called()
assert player.name in players # Still in player registry
@pytest.mark.asyncio
async def test_quit_works_when_not_in_combat(player):
"""Test quit works normally when not in combat."""
# Player starts in "normal" mode by default
with patch("mudlib.commands.quit.save_player"):
await cmd_quit(player, "")
# Should write goodbye message
player.writer.write.assert_called_with("Goodbye!\r\n")
player.writer.drain.assert_called_once()
player.writer.close.assert_called_once()
assert player.name not in players # Removed from registry
assert player.location is None # Removed from zone