From 49a5e04a1c6a4310062cf30a3e51c94be01c134c Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sun, 14 Jul 2024 22:04:29 +0200 Subject: [PATCH 1/4] [~] Nothing important --- unit_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/unit_test.py b/unit_test.py index 148dcd8..0211a83 100644 --- a/unit_test.py +++ b/unit_test.py @@ -1,4 +1,3 @@ -import file_manager import firebase_manager From 37dc160a12ae5d29ac0bf0a768f269888c480dc1 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sun, 14 Jul 2024 22:05:32 +0200 Subject: [PATCH 2/4] [+] get_online_players function. [+] mcipc implementation --- server_mc_manager.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server_mc_manager.py b/server_mc_manager.py index c217436..90179c8 100644 --- a/server_mc_manager.py +++ b/server_mc_manager.py @@ -1,6 +1,8 @@ import subprocess import shlex import time +import mcipc.query +import mcipc.query.client class MinecraftServerManager: @@ -61,6 +63,12 @@ class MinecraftServerManager: if server_info['port'] == port: return server_id return None + + def get_online_players(self, port)->int: + with mcipc.query.Client('127.0.0.1', port) as client: + stats: mcipc.query.proto.FullStats = client.stats(full=True) + stats: int = stats.num_players + return stats def set_cooldown(self, user_id): expiry_timestamp = time.time() + 30 @@ -76,7 +84,7 @@ class MinecraftServerManager: else: del self.cooldowns[user_id] return False - + if __name__ == "__main__": pass From 0747e02fcecbe20b82b8df2f70368b3b15f44d84 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sun, 14 Jul 2024 22:06:02 +0200 Subject: [PATCH 3/4] [+] /FetchServers now also returns the amount of connected players --- generic_executor.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/generic_executor.py b/generic_executor.py index 284564c..ce4e002 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -31,7 +31,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or try: record: SRVRecord = cloudflare_manager.add_cloudflare_dns(subdomain, port) record_id: str = record.id - if record_id is None: + if record_id == "": return HTTPStatus.BAD_REQUEST, "Failed to add record to cloudflare" firebase_manager.update_firestore(user_id, {'cloudflare_entry': str(record_id)}) except Exception as e: @@ -47,7 +47,16 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]: server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers') try: servers = list(server_ref.stream()) - servers_data = [doc.to_dict() for doc in servers] + servers_data: list[dict[str, any]] = [] + for doc in servers: + server_info = doc.to_dict() + if server_info.get('running', False): + try: + connected_players = mc_manager.get_online_players(server_info.get('port', 25565)) + except Exception as e: + connected_players = 0 + server_info['onlinePlayers'] = connected_players + servers_data.append(server_info) if not servers_data: account_create(user) return HTTPStatus.OK, servers_data From cb0487659224362d81dcca145fed3c6287f8f6e2 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sun, 14 Jul 2024 22:19:33 +0200 Subject: [PATCH 4/4] [+] Enable query on the right port on server creation --- generic_executor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generic_executor.py b/generic_executor.py index ce4e002..e4db239 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -107,7 +107,10 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p file_manager.create_folder(server_path) file_manager.copy_folder_contents(server_template_path, server_path) file_manager.copy_folder_contents("servers/shared", server_path) - file_manager.update_server_property(server_path + "/server.properties", "server-port", port) + prop_path: str = server_path + "/server.properties" + file_manager.update_server_property(prop_path, "server-port", port) + file_manager.update_server_property(prop_path, "query.port", port) + file_manager.update_server_property(prop_path, "enable-query", "true") return HTTPStatus.CREATED, f"Successfully created server '{name}'." except Exception as e: file_manager.log_error(type(e).__name__, str(e))