[+] New property management system !

Now syncs with firebase

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-07-09 18:40:37 +01:00
parent 92e5a13ae6
commit 827706aaf7
3 changed files with 29 additions and 6 deletions

View File

@ -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):

View File

@ -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)}"

View File

@ -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 = {}