Extract Entity base class from Player

This commit is contained in:
Jared Miller 2026-02-07 20:30:59 -05:00
parent d159a88ca4
commit fea563f158
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 24 additions and 4 deletions

16
src/mudlib/entity.py Normal file
View file

@ -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

View file

@ -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] = {}