Interactive server console

This commit is contained in:
Emily 2023-03-15 07:27:33 +01:00
parent f1152cfb25
commit 8e59db2ad6
2 changed files with 22 additions and 2 deletions

View File

@ -5,9 +5,9 @@ import textwrap
def main(args: argparse.Namespace) -> int:
from sampy.network.protocol import Protocol
from sampy.server import Server
from sampy.server import InteractiveServer
server = Server(Protocol)
server = InteractiveServer(Protocol)
server.start()
asyncio.get_event_loop().run_forever()

View File

@ -34,6 +34,9 @@ class UDPProtocol:
def connection_made(self, transport: asyncio.transports.DatagramTransport):
self.transport = transport
def connection_lost(self, exc: Exception | None):
pass
def datagram_received(self, data: bytes, addr: Tuple[str, int]):
raise NotImplementedError
@ -61,3 +64,20 @@ class Server(UDPProtocol):
@property
def players(self) -> list[Player]: # TODO
return []
class InteractiveServer(Server):
def __init__(self, protocol: Type[Protocol], config: Config = Config()):
super().__init__(protocol=protocol, config=config)
loop = asyncio.get_event_loop()
loop.create_task(self.run_input_loop())
async def run_input_loop(self):
loop = asyncio.get_event_loop()
while True:
command = await loop.run_in_executor(None, input)
if command in ("quit", "exit", "stop"):
self.stop()
loop.stop()
return