Add inventory command with alias "i"
Lists Thing objects in player.contents with 2-space indented format. Shows "You aren't carrying anything." when inventory is empty.
This commit is contained in:
parent
7c12bf3318
commit
e96fd50de5
2 changed files with 122 additions and 1 deletions
|
|
@ -49,7 +49,7 @@ async def cmd_get(player: Player, args: str) -> None:
|
|||
return
|
||||
|
||||
if not player.can_accept(thing):
|
||||
await player.send(f"You can't pick that up.\r\n")
|
||||
await player.send("You can't pick that up.\r\n")
|
||||
return
|
||||
|
||||
thing.move_to(player)
|
||||
|
|
@ -76,5 +76,21 @@ async def cmd_drop(player: Player, args: str) -> None:
|
|||
await player.send(f"You drop {thing.name}.\r\n")
|
||||
|
||||
|
||||
async def cmd_inventory(player: Player, args: str) -> None:
|
||||
"""List items in the player's inventory."""
|
||||
things = [obj for obj in player.contents if isinstance(obj, Thing)]
|
||||
|
||||
if not things:
|
||||
await player.send("You aren't carrying anything.\r\n")
|
||||
return
|
||||
|
||||
lines = ["You are carrying:\r\n"]
|
||||
for thing in things:
|
||||
lines.append(f" {thing.name}\r\n")
|
||||
|
||||
await player.send("".join(lines))
|
||||
|
||||
|
||||
register(CommandDefinition("get", cmd_get, aliases=["take", "pick"]))
|
||||
register(CommandDefinition("drop", cmd_drop))
|
||||
register(CommandDefinition("inventory", cmd_inventory, aliases=["i"]))
|
||||
|
|
|
|||
105
tests/test_inventory.py
Normal file
105
tests/test_inventory.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
"""Tests for inventory command."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from mudlib import commands
|
||||
from mudlib.commands import things # noqa: F401
|
||||
from mudlib.player import Player
|
||||
from mudlib.thing import Thing
|
||||
from mudlib.zone import Zone
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_zone():
|
||||
"""Create a test zone."""
|
||||
return Zone(
|
||||
name="testzone",
|
||||
width=10,
|
||||
height=10,
|
||||
toroidal=True,
|
||||
terrain=[["." for _ in range(10)] for _ in range(10)],
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_writer():
|
||||
"""Create a mock writer."""
|
||||
return MagicMock(write=MagicMock(), drain=AsyncMock())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_reader():
|
||||
"""Create a mock reader."""
|
||||
return MagicMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def player(mock_reader, mock_writer, test_zone):
|
||||
"""Create a test player."""
|
||||
return Player(
|
||||
name="TestPlayer",
|
||||
x=5,
|
||||
y=5,
|
||||
reader=mock_reader,
|
||||
writer=mock_writer,
|
||||
location=test_zone,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_inventory_empty(player, mock_writer):
|
||||
"""Test inventory with no items."""
|
||||
from mudlib.commands.things import cmd_inventory
|
||||
|
||||
await cmd_inventory(player, "")
|
||||
|
||||
output = mock_writer.write.call_args_list[-1][0][0]
|
||||
assert "You aren't carrying anything." in output
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_inventory_one_item(player, mock_writer):
|
||||
"""Test inventory with one item."""
|
||||
from mudlib.commands.things import cmd_inventory
|
||||
|
||||
Thing(name="rock", location=player)
|
||||
|
||||
await cmd_inventory(player, "")
|
||||
|
||||
output = mock_writer.write.call_args_list[-1][0][0]
|
||||
assert "You are carrying:" in output
|
||||
assert "rock" in output
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_inventory_multiple_items(player, mock_writer):
|
||||
"""Test inventory with multiple items."""
|
||||
from mudlib.commands.things import cmd_inventory
|
||||
|
||||
Thing(name="rock", location=player)
|
||||
Thing(name="sword", location=player)
|
||||
Thing(name="shield", location=player)
|
||||
|
||||
await cmd_inventory(player, "")
|
||||
|
||||
output = mock_writer.write.call_args_list[-1][0][0]
|
||||
assert "You are carrying:" in output
|
||||
assert "rock" in output
|
||||
assert "sword" in output
|
||||
assert "shield" in output
|
||||
|
||||
|
||||
def test_inventory_command_registered():
|
||||
"""Test that inventory command is registered."""
|
||||
assert "inventory" in commands._registry
|
||||
cmd = commands._registry["inventory"]
|
||||
assert cmd.name == "inventory"
|
||||
|
||||
|
||||
def test_inventory_alias_registered():
|
||||
"""Test that 'i' alias is registered."""
|
||||
assert "i" in commands._registry
|
||||
cmd = commands._registry["i"]
|
||||
assert cmd.name == "inventory"
|
||||
Loading…
Reference in a new issue