Compare commits
2 Commits
be81204cd9
...
c56bc396fe
Author | SHA1 | Date | |
---|---|---|---|
trueold89 | c56bc396fe |
|
|
trueold89 | 4d9b2586bb |
|
|
@ -68,6 +68,7 @@ print(name)
|
|||
| motd | Server motd |
|
||||
| players_count | Current number of players on the server |
|
||||
| maxplayers | Max number of players on the server |
|
||||
| players_list | List of current players on server |
|
||||
|
||||
|
||||
### As cli:
|
||||
|
@ -83,3 +84,4 @@ MC_HOST=localhost MC_PORT=25565 mcaio name
|
|||
| motd | Server motd |
|
||||
| pcount | Current number of players on the server |
|
||||
| pmax | Max number of players on the server |
|
||||
| players | List of current players on server |
|
||||
|
|
14
mcaio/cli.py
14
mcaio/cli.py
|
@ -33,19 +33,25 @@ async def get_max(server: AIOMCServer) -> int:
|
|||
return await server.players_count
|
||||
|
||||
|
||||
async def get_players(server: AIOMCServer) -> tuple:
|
||||
return tuple(await server.players_list)
|
||||
|
||||
|
||||
async def action() -> None:
|
||||
try:
|
||||
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
|
||||
server = AIOMCServer(HOST, PORT)
|
||||
match arg():
|
||||
case 'name':
|
||||
case "name":
|
||||
out = get_name(server)
|
||||
case 'pmax':
|
||||
case "pmax":
|
||||
out = get_max(server)
|
||||
case 'pcount':
|
||||
case "pcount":
|
||||
out = get_count(server)
|
||||
case 'motd':
|
||||
case "motd":
|
||||
out = get_motd(server)
|
||||
case "players":
|
||||
out = get_players(server)
|
||||
case _:
|
||||
raise RuntimeError
|
||||
print(await out)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from asyncio import open_connection as aiocon, StreamReader, StreamWriter
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Iterable
|
||||
from struct import pack as struct_pack
|
||||
from json import loads as jl
|
||||
|
||||
|
@ -59,6 +60,11 @@ class IMCServer(ABC):
|
|||
async def motd(self) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
async def players_list(self) -> Iterable:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class AIOMCServer(IMCServer):
|
||||
|
||||
|
@ -66,6 +72,15 @@ class AIOMCServer(IMCServer):
|
|||
_max: int
|
||||
_count: int
|
||||
_motd: str
|
||||
_data: bytes
|
||||
_players: Iterable
|
||||
|
||||
def __init__(self, host: str, port: int) -> None:
|
||||
super().__init__(host, port)
|
||||
self._data = self._pack_data(
|
||||
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
|
||||
_pack_port(self.port) + b"\x01")
|
||||
self._players = ()
|
||||
|
||||
@staticmethod
|
||||
async def _unpack_varint(s):
|
||||
|
@ -100,9 +115,7 @@ class AIOMCServer(IMCServer):
|
|||
|
||||
async def _get_data(self) -> dict:
|
||||
async with AIOConnection(self.host, self.port) as socket:
|
||||
await socket.send(self._pack_data(
|
||||
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
|
||||
_pack_port(self.port) + b"\x01"))
|
||||
await socket.send(self._data)
|
||||
await socket.send(self._pack_data("\x00"))
|
||||
await self._unpack_varint(socket)
|
||||
await self._unpack_varint(socket)
|
||||
|
@ -119,6 +132,9 @@ class AIOMCServer(IMCServer):
|
|||
players = data["players"]
|
||||
self._count = int(players["online"])
|
||||
self._max = int(players["max"])
|
||||
if "sample" in tuple(players.keys()):
|
||||
self._players = tuple(map(lambda player: player["name"],
|
||||
players["sample"]))
|
||||
|
||||
@property
|
||||
async def players_count(self) -> int:
|
||||
|
@ -139,3 +155,8 @@ class AIOMCServer(IMCServer):
|
|||
async def motd(self) -> str:
|
||||
await self.update()
|
||||
return self._motd
|
||||
|
||||
@property
|
||||
async def players_list(self) -> Iterable:
|
||||
await self.update()
|
||||
return self._players
|
||||
|
|
Loading…
Reference in New Issue