From 827706aaf7d2b37c79ccbb218f7073e1959aaf07 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Tue, 9 Jul 2024 18:40:37 +0100 Subject: [PATCH] [+] New property management system ! Now syncs with firebase Signed-off-by: Charles Le Maux --- firebase_manager.py | 30 ++++++++++++++++++++++++++---- generic_executor.py | 3 ++- server_mc_manager.py | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/firebase_manager.py b/firebase_manager.py index 3305c96..4ca31f3 100644 --- a/firebase_manager.py +++ b/firebase_manager.py @@ -5,6 +5,8 @@ import jwt from firebase_admin import auth, credentials, firestore from google.api_core.exceptions import Aborted, DataLoss, NotFound, OutOfRange, PermissionDenied, ResourceExhausted +import file_manager + cred = credentials.Certificate('secrets/servii.json') app = firebase_admin.initialize_app(cred) firestore_database = firestore.client() @@ -119,7 +121,21 @@ def create_server(user_id: str, server_name: str, version: str, port: str, frame servers_ref = firestore_database.collection('users').document(user_id).collection('servers') server_doc_ref = servers_ref.document(server_name) server_doc_ref.set( - {'name': server_name, 'port': port, 'running': False, 'version': version, 'framework': framework}) + {'name': server_name, + 'port': port, + 'running': "false", + 'version': version, + 'framework': framework, + "difficulty": "easy", + "gamemode": "survival", + "forceGamemode": "false", + "hardcore": "false", + "generateStructures": "true", + "motd": "A Minecraft Server", + "pvp": "true", + "onlineMode": "true", + "maxPlayers": 20, + "enableCommandBlock": "false"}) def delete_server(user_id: str, server_name: str): @@ -135,9 +151,15 @@ def delete_user(user_id: str): def update_server_running_state(user_id: str, server_name: str, state: bool): - user_ref = firestore_database.collection('users').document(user_id).collection('servers').document(server_name) - if user_ref.get().get('running') != state: - user_ref.update({'running': state}) + server_ref = firestore_database.collection('users').document(user_id).collection('servers').document(server_name) + if server_ref.get().get('running') != state: + server_ref.update({'running': state}) + + +def update_server_property(user_id: str, server_name: str, prop: str, value: str): + server_ref = firestore_database.collection('users').document(user_id).collection('servers').document(server_name) + prop = file_manager.kebab_to_camel_case(prop) + server_ref.update({prop: value}) def log_exception_to_firestore(exception: Exception = None, user_id: str = None, data: dict = None): diff --git a/generic_executor.py b/generic_executor.py index f291a7c..9bc1de2 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -174,12 +174,13 @@ def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPSta property_file_path: str = f"users/{uid}/{name}/server.properties" try: file_manager.update_server_property(property_file_path, prop, value) + firebase_manager.update_server_property(uid, name, prop, value) return HTTPStatus.OK, f"Successfully set '{prop}' to '{value}'." except ValueError as e: file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.BAD_REQUEST, f"Property '{prop}' not found." except FileNotFoundError: - return HTTPStatus.NOT_FOUND, f"Requested server '{name}' not found." + return HTTPStatus.NOT_FOUND, f"File server.properties for server '{name}' not found." except Exception as e: return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}" diff --git a/server_mc_manager.py b/server_mc_manager.py index 6070861..e96984e 100644 --- a/server_mc_manager.py +++ b/server_mc_manager.py @@ -4,7 +4,7 @@ import shlex class MinecraftServerManager: allowed_properties: list[str] = ["difficulty", "gamemode", "force-gamemode", "hardcore", "generate-structures", - "motd", "pvp", "online-mode", "max-players"] + "motd", "pvp", "online-mode", "max-players", "enable-command-block"] def __init__(self): self.servers: dict = {}