servii-backend/server_mc_manager.py

180 lines
6.7 KiB
Python
Raw Normal View History

2024-06-14 21:33:55 +00:00
import shlex
2024-09-06 15:54:04 +00:00
import subprocess
import time
from typing import Union, Callable
2024-09-06 15:54:04 +00:00
import mcipc.query
import mcipc.query.client
2024-06-14 21:33:55 +00:00
2024-09-06 15:56:56 +00:00
import file_manager
import firebase_manager
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
class MinecraftServerManager:
allowed_properties: list[str] = ["difficulty", "gamemode", "force-gamemode", "hardcore", "generate-structures",
"motd", "pvp", "online-mode", "max-players", "enable-command-block"]
2024-06-14 21:33:55 +00:00
def __init__(self):
self.servers: dict = {}
self.servers_count: int = 0
self.cooldowns = {}
self.offline_ports: list[int] = []
2024-06-14 21:33:55 +00:00
def start_server(self, server_directory: str, port: int, user_id: str, server_name: str,
2024-09-15 21:01:23 +00:00
version: str, jar_file: str = 'server.jar', memory_size: str ='4G', modded: bool = False) -> Union[int, None]:
if port in self.servers:
return None
2024-08-19 09:58:10 +00:00
reg_flags: str = ("-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200"
" -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch"
" -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M"
" -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4"
" -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90"
" -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem"
" -XX:MaxTenuringThreshold=1 -Daikars.new.flags=true"
" -Dusing.aikars.flags=https://mcutils.com")
java: str = f"/usr/lib/jvm/java-{get_sdk_version(version)}-openjdk-amd64/bin/java"
command = f"{java} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui"
2024-09-15 21:01:23 +00:00
if modded:
command = "./start.sh"
2024-06-14 21:33:55 +00:00
process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE)
2024-08-20 18:03:59 +00:00
#TODO: Track process behavior and stderr, while excepting Advanced Terminal features not to be avail.
2024-06-14 21:33:55 +00:00
self.servers_count = len(self.servers) + 1
self.servers[port] = {
2024-06-14 21:33:55 +00:00
'process': process,
'directory': server_directory,
'port': port,
'user_id': user_id,
'name': server_name,
'version': version,
'time': time.time(),
2024-06-14 21:33:55 +00:00
}
return port
def execute_server_command(self, port, command) -> bool:
if port not in self.servers:
return False
process = self.servers[port]['process']
process.stdin.write(command.encode('utf-8') + b'\n')
process.stdin.flush()
return True
def stop_server(self, port: int) -> bool:
if port not in self.servers:
return False
process = self.servers[port]['process']
process.communicate(input=b"stop\n")
del self.servers[port]
if port in self.offline_ports:
self.offline_ports.remove(port)
return True
def stop_server_forcefully(self, port) -> bool:
if port not in self.servers:
return False
process = self.servers[port]['process']
process.terminate()
del self.servers[port]
return True
2024-06-14 21:33:55 +00:00
def get_servers(self):
return self.servers.values()
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
def get_server_id_by_port(self, port):
for server_id, server_info in self.servers.items():
if server_info['port'] == port:
return server_id
return None
2024-08-11 09:02:10 +00:00
def get_online_players(self, port) -> int:
if not self.servers[port]:
return 0
2024-09-06 15:56:56 +00:00
try:
with mcipc.query.Client('127.0.0.1', port) as client:
stats: int = client.stats(full=False).num_players
return stats
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return 0
2024-06-14 21:33:55 +00:00
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
2024-08-11 09:02:10 +00:00
def check_servers_idle(self) -> None:
servers: dict = self.servers.copy()
for port, server_info in servers.items():
online_players = self.get_online_players(port)
if online_players == 0:
if port not in self.offline_ports:
self.offline_ports.append(port)
else:
user_id = server_info.get("user_id", None)
name = server_info.get("name", None)
server_stamp = server_info.get("time", None)
firebase_manager.close_idle_server(
user_id=user_id,
name=name,
server_stamp=server_stamp,
port=port,
)
else:
if port in self.offline_ports:
self.offline_ports.remove(port)
return
2024-06-14 21:33:55 +00:00
def version_range_checker(*ranges: tuple[str, str, str], default_sdk: str = "java") -> Callable[[str], str]:
def compare_mc_versions(version1: str):
def inner(version2: str) -> int:
v1_parts: list[int] = list(map(int, version1.split('.')))
v2_parts: list[int] = list(map(int, version2.split('.')))
for v1, v2 in zip(v1_parts, v2_parts):
if v1 > v2:
return 1
elif v1 < v2:
return -1
if len(v1_parts) > len(v2_parts):
return 1
elif len(v1_parts) < len(v2_parts):
return -1
return 0
return inner
def check_range(lower_bound: str, upper_bound: str, version: str) -> bool:
return compare_mc_versions(lower_bound)(version) <= 0 <= compare_mc_versions(upper_bound)(version)
def check_version(version: str) -> str:
for lower_bound, upper_bound, sdk in ranges:
if check_range(lower_bound, upper_bound, version):
return sdk
return default_sdk
return check_version
2024-09-15 16:46:52 +00:00
get_sdk_version: Callable[[str], str] = version_range_checker(("1.0", "1.13.2", "8"),
("1.14.1", "1.16.5", "11"),
("1.17.1", "1.18.1", "17"),
default_sdk="21")
2024-06-14 21:33:55 +00:00
if __name__ == "__main__":
pass