Extract Entity base class from Player
This commit is contained in:
parent
d159a88ca4
commit
fea563f158
2 changed files with 24 additions and 4 deletions
16
src/mudlib/entity.py
Normal file
16
src/mudlib/entity.py
Normal 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
|
||||
|
|
@ -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] = {}
|
||||
|
|
|
|||
Loading…
Reference in a new issue