[+] FetchPlayersStatus API call.

Users are now able to fetch their user data : Whitelist, Operators, BannedIps, BannedPlayers.
This commit is contained in:
Charles Le Maux 2024-08-24 13:23:19 +02:00
parent 79d680db3a
commit 2632f70b65
2 changed files with 32 additions and 1 deletions

1
app.py
View File

@ -82,6 +82,7 @@ route_handlers = {
'FetchServers': generic_executor.fetch_servers, 'FetchServers': generic_executor.fetch_servers,
'FetchLogs': generic_executor.fetch_logs, 'FetchLogs': generic_executor.fetch_logs,
'FetchHistory': generic_executor.fetch_history, 'FetchHistory': generic_executor.fetch_history,
'FetchPlayersStatus': generic_executor.fetch_players_status,
'AccountCreate': generic_executor.account_create, 'AccountCreate': generic_executor.account_create,
'ServerCreate': generic_executor.server_create, 'ServerCreate': generic_executor.server_create,
'ServerDelete': generic_executor.server_delete, 'ServerDelete': generic_executor.server_delete,

View File

@ -1,5 +1,7 @@
import json
import os
from http import HTTPStatus from http import HTTPStatus
from typing import Union from typing import Union, Tuple, Dict
from cloudflare.types.dns import SRVRecord from cloudflare.types.dns import SRVRecord
from firebase_admin.auth import UserRecord from firebase_admin.auth import UserRecord
@ -95,6 +97,34 @@ def fetch_history(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, N
return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error." return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error."
def fetch_players_status(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[dict[str, str], str]]:
user_id: str = user.uid
server_path = f"users/{user_id}/{name}"
players_status_files = {
"Whitelist": "whitelist.json",
"BannedPlayers": "banned-players.json",
"BannedIps": "banned-ips.json",
"Operators": "ops.json"
}
try:
file_contents = {}
for file_name, file_path in players_status_files.items():
full_file_path = f"{server_path}/{file_path}"
if os.path.exists(full_file_path):
with open(full_file_path, 'r') as file:
file_contents[file_name] = file.read()
else:
return HTTPStatus.NOT_FOUND, "Launch the server at least once."
file_contents_json = json.dumps(file_contents, indent=4)
return HTTPStatus.OK, file_contents
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error."
def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
if firebase_manager.user_field_exists(user.uid): if firebase_manager.user_field_exists(user.uid):
return HTTPStatus.FORBIDDEN, "User already exists." return HTTPStatus.FORBIDDEN, "User already exists."