Add players_list property

This commit is contained in:
trueold89 2024-07-07 21:59:59 +03:00
parent 4d9b2586bb
commit c56bc396fe
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
3 changed files with 28 additions and 4 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):
@ -67,12 +73,14 @@ class AIOMCServer(IMCServer):
_count: int _count: int
_motd: str _motd: str
_data: bytes _data: bytes
_players: Iterable
def __init__(self, host: str, port: int) -> None: def __init__(self, host: str, port: int) -> None:
super().__init__(host, port) super().__init__(host, port)
self._data = self._pack_data( self._data = self._pack_data(
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self. b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01") _pack_port(self.port) + b"\x01")
self._players = ()
@staticmethod @staticmethod
async def _unpack_varint(s): async def _unpack_varint(s):
@ -124,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:
@ -144,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