Compare commits

..

2 Commits

Author SHA1 Message Date
trueold89 c56bc396fe
Add players_list property 2024-07-07 21:59:59 +03:00
trueold89 4d9b2586bb
Add _data param 2024-07-07 21:21:17 +03:00
3 changed files with 36 additions and 7 deletions

View File

@ -68,6 +68,7 @@ print(name)
| motd | Server motd | | motd | Server motd |
| players_count | Current number of players on the server | | players_count | Current number of players on the server |
| maxplayers | Max number of players on the server | | maxplayers | Max number of players on the server |
| players_list | List of current players on server |
### As cli: ### As cli:
@ -83,3 +84,4 @@ MC_HOST=localhost MC_PORT=25565 mcaio name
| motd | Server motd | | motd | Server motd |
| pcount | Current number of players on the server | | pcount | Current number of players on the server |
| pmax | Max number of players on the server | | pmax | Max number of players on the server |
| players | List of current players on server |

View File

@ -33,19 +33,25 @@ async def get_max(server: AIOMCServer) -> int:
return await server.players_count return await server.players_count
async def get_players(server: AIOMCServer) -> tuple:
return tuple(await server.players_list)
async def action() -> None: async def action() -> None:
try: try:
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT")) HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
server = AIOMCServer(HOST, PORT) server = AIOMCServer(HOST, PORT)
match arg(): match arg():
case 'name': case "name":
out = get_name(server) out = get_name(server)
case 'pmax': case "pmax":
out = get_max(server) out = get_max(server)
case 'pcount': case "pcount":
out = get_count(server) out = get_count(server)
case 'motd': case "motd":
out = get_motd(server) out = get_motd(server)
case "players":
out = get_players(server)
case _: case _:
raise RuntimeError raise RuntimeError
print(await out) print(await out)

View File

@ -1,5 +1,6 @@
from asyncio import open_connection as aiocon, StreamReader, StreamWriter from asyncio import open_connection as aiocon, StreamReader, StreamWriter
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections.abc import Iterable
from struct import pack as struct_pack from struct import pack as struct_pack
from json import loads as jl from json import loads as jl
@ -59,6 +60,11 @@ class IMCServer(ABC):
async def motd(self) -> str: async def motd(self) -> str:
raise NotImplementedError raise NotImplementedError
@property
@abstractmethod
async def players_list(self) -> Iterable:
raise NotImplementedError
class AIOMCServer(IMCServer): class AIOMCServer(IMCServer):
@ -66,6 +72,15 @@ class AIOMCServer(IMCServer):
_max: int _max: int
_count: int _count: int
_motd: str _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 @staticmethod
async def _unpack_varint(s): async def _unpack_varint(s):
@ -100,9 +115,7 @@ class AIOMCServer(IMCServer):
async def _get_data(self) -> dict: async def _get_data(self) -> dict:
async with AIOConnection(self.host, self.port) as socket: async with AIOConnection(self.host, self.port) as socket:
await socket.send(self._pack_data( await socket.send(self._data)
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01"))
await socket.send(self._pack_data("\x00")) await socket.send(self._pack_data("\x00"))
await self._unpack_varint(socket) await self._unpack_varint(socket)
await self._unpack_varint(socket) await self._unpack_varint(socket)
@ -119,6 +132,9 @@ class AIOMCServer(IMCServer):
players = data["players"] players = data["players"]
self._count = int(players["online"]) self._count = int(players["online"])
self._max = int(players["max"]) self._max = int(players["max"])
if "sample" in tuple(players.keys()):
self._players = tuple(map(lambda player: player["name"],
players["sample"]))
@property @property
async def players_count(self) -> int: async def players_count(self) -> int:
@ -139,3 +155,8 @@ class AIOMCServer(IMCServer):
async def motd(self) -> str: async def motd(self) -> str:
await self.update() await self.update()
return self._motd return self._motd
@property
async def players_list(self) -> Iterable:
await self.update()
return self._players