[+] /FetchServers now return the amount of connected players at this time

This commit is contained in:
charleslemaux 2024-07-17 14:09:44 +02:00 committed by GitHub
commit 87f92934ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View File

@ -32,7 +32,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[s
try: try:
record: SRVRecord = cloudflare_manager.add_cloudflare_dns(subdomain, port) record: SRVRecord = cloudflare_manager.add_cloudflare_dns(subdomain, port)
record_id: str = record.id record_id: str = record.id
if record_id is None: if record_id == "":
return HTTPStatus.BAD_REQUEST, "Failed to add record to cloudflare" return HTTPStatus.BAD_REQUEST, "Failed to add record to cloudflare"
firebase_manager.update_firestore(user_id, {'cloudflare_entry': str(record_id)}) firebase_manager.update_firestore(user_id, {'cloudflare_entry': str(record_id)})
except Exception as e: 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') server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers')
try: try:
servers = list(server_ref.stream()) 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: if not servers_data:
account_create(user) account_create(user)
return HTTPStatus.OK, servers_data 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.create_folder(server_path)
file_manager.copy_folder_contents(server_template_path, server_path) file_manager.copy_folder_contents(server_template_path, server_path)
file_manager.copy_folder_contents("servers/shared", 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}'." return HTTPStatus.CREATED, f"Successfully created server '{name}'."
except Exception as e: except Exception as e:
file_manager.log_error(type(e).__name__, str(e)) file_manager.log_error(type(e).__name__, str(e))

View File

@ -1,6 +1,8 @@
import subprocess import subprocess
import shlex import shlex
import time import time
import mcipc.query
import mcipc.query.client
from typing import Union from typing import Union
@ -62,6 +64,12 @@ class MinecraftServerManager:
if server_info['port'] == port: if server_info['port'] == port:
return server_id return server_id
return None 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): def set_cooldown(self, user_id):
expiry_timestamp = time.time() + 30 expiry_timestamp = time.time() + 30
@ -77,7 +85,7 @@ class MinecraftServerManager:
else: else:
del self.cooldowns[user_id] del self.cooldowns[user_id]
return False return False
if __name__ == "__main__": if __name__ == "__main__":
pass pass

View File

@ -1,4 +1,3 @@
import file_manager
import firebase_manager import firebase_manager