diff --git a/src/mudlib/entity.py b/src/mudlib/entity.py new file mode 100644 index 0000000..bc443c3 --- /dev/null +++ b/src/mudlib/entity.py @@ -0,0 +1,16 @@ +"""Base entity class for characters in the world.""" + +from dataclasses import dataclass + + +@dataclass +class Entity: + """Base class for anything with position and identity in the world.""" + + name: str + x: int + y: int + + async def send(self, message: str) -> None: + """Send a message to this entity. Base implementation is a no-op.""" + pass diff --git a/src/mudlib/player.py b/src/mudlib/player.py index 26d6455..f75463d 100644 --- a/src/mudlib/player.py +++ b/src/mudlib/player.py @@ -3,14 +3,13 @@ from dataclasses import dataclass, field from typing import Any +from mudlib.entity import Entity + @dataclass -class Player: +class Player(Entity): """Represents a connected player.""" - name: str - x: int # position in world - y: int writer: Any # telnetlib3 TelnetWriter for sending output reader: Any # telnetlib3 TelnetReader for reading input flying: bool = False @@ -21,6 +20,11 @@ class Player: """Current mode is the top of the stack.""" return self.mode_stack[-1] + async def send(self, message: str) -> None: + """Send a message to the player via their telnet writer.""" + self.writer.write(message) + await self.writer.drain() + # Global registry of connected players players: dict[str, Player] = {}