[+] Cooldown on server run, has to wait 30s to stop.

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-07-11 16:02:21 +01:00
parent f3b8a483ec
commit 87a242f131
2 changed files with 34 additions and 12 deletions

View File

@ -111,7 +111,7 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]
try:
firebase_manager.delete_server(user_id, name)
file_manager.delete_non_empty_folder(server_path)
return HTTPStatus.OK, f"Successfully deleted server {name}."
return HTTPStatus.OK, f"Successfully deleted server '{name}'."
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, None
@ -132,7 +132,7 @@ def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
return HTTPStatus.EXPECTATION_FAILED, f"Database deletion failed | {e}"
try:
file_manager.delete_non_empty_folder("users/" + user_id)
return HTTPStatus.OK, f"Successfully deleted user {user_id}."
return HTTPStatus.OK, f"Successfully deleted user '{user_id}'."
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server."
@ -140,6 +140,7 @@ def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
user_id = user.uid
mc_manager.set_cooldown(user_id=user_id)
try:
port: int = firebase_manager.get_server_port(user_id)
server_id = mc_manager.start_server(f"users/{user_id}/{name}", port)
@ -151,7 +152,7 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
except Exception as e:
return HTTPStatus.INTERNAL_SERVER_ERROR, (f"Error updating server {name}'s running state in database. "
f"{type(e).__name__}{str(e)}.")
return HTTPStatus.ACCEPTED, f"Successfully started server {name}."
return HTTPStatus.ACCEPTED, f"Successfully started server '{name}'."
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}"
@ -159,21 +160,25 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
port = None
user_id: str = user.uid
if mc_manager.has_cooldown(user_id=user_id):
return HTTPStatus.FORBIDDEN, f"Wait 30 seconds before stopping '{name}'."
try:
port = firebase_manager.get_server_port(user.uid)
port = firebase_manager.get_server_port(user_id)
if port is None:
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
return HTTPStatus.NOT_FOUND, f"Server '{name}' not found in firestore."
exists: bool = mc_manager.stop_server(port)
if exists:
firebase_manager.update_server_running_state(user.uid, name, False)
return HTTPStatus.OK, f"Successfully stopped server {name}."
return HTTPStatus.OK, f"Server {name} already stopped."
firebase_manager.update_server_running_state(user_id, name, False)
return HTTPStatus.OK, f"Successfully stopped server '{name}'."
return HTTPStatus.OK, f"Server '{name}' already stopped."
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
if port:
mc_manager.stop_server_forcefully(port)
return HTTPStatus.OK, f"Successfully stopped server {name}."
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server {name}."
firebase_manager.update_server_running_state(user_id, name, False)
return HTTPStatus.OK, f"Successfully stopped server '{name}'."
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'."
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]:
@ -202,14 +207,14 @@ def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]])
errors.append(message)
if len(errors) > 0:
return HTTPStatus.IM_A_TEAPOT, errors
return HTTPStatus.OK, f"Successfully updated server {name}."
return HTTPStatus.OK, f"Successfully updated server '{name}'."
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]:
try:
port = firebase_manager.get_server_port(user.uid)
if port is None:
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
return HTTPStatus.NOT_FOUND, f"Server '{name}' not found in firestore."
mc_manager.execute_server_command(port, command)
return HTTPStatus.OK, f"Command '{command}' executed successfully."
except Exception as e:

View File

@ -1,5 +1,6 @@
import subprocess
import shlex
import time
class MinecraftServerManager:
@ -9,6 +10,7 @@ class MinecraftServerManager:
def __init__(self):
self.servers: dict = {}
self.servers_count: int = 0
self.cooldowns = {}
def start_server(self, server_directory: str, port: int,
java_executable='java', jar_file='server.jar', memory_size='2048M') -> int or None:
@ -60,6 +62,21 @@ class MinecraftServerManager:
return server_id
return None
def set_cooldown(self, user_id):
expiry_timestamp = time.time() + 30
self.cooldowns[user_id] = expiry_timestamp
def has_cooldown(self, user_id):
expiry_timestamp = self.cooldowns.get(user_id)
if expiry_timestamp is None:
return False
current_time = time.time()
if current_time < expiry_timestamp:
return True
else:
del self.cooldowns[user_id]
return False
if __name__ == "__main__":
pass