diff --git a/generic_executor.py b/generic_executor.py index e2dbb11..14e40b0 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -32,7 +32,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[s 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: @@ -48,7 +48,16 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, Union[str, 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 @@ -99,7 +108,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)) diff --git a/server_mc_manager.py b/server_mc_manager.py index 72ee28a..a815c9b 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 from typing import Union @@ -62,6 +64,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 @@ -77,7 +85,7 @@ class MinecraftServerManager: else: del self.cooldowns[user_id] return False - + if __name__ == "__main__": pass 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