This commit is contained in:
2026-04-06 13:30:19 +02:00
commit ca012cf823
3 changed files with 201 additions and 0 deletions

46
servermanager.py Normal file
View File

@@ -0,0 +1,46 @@
import subprocess
from dotenv import load_dotenv
import os
from dom5game import Dom5game
load_dotenv()
SERVER_PATH = os.getenv("SERVERPATH")
def create_server(name, port, channel):
if is_port_in_use(port):
return "ERROR_PORT_IN_USE"
# os.mkdir("games/" + name)
# print(SERVER_PATH)
# print(server_command_builder(name, port, channel))
try:
subprocess.Popen(
server_command_builder(name, port, channel),
stdin=None,
stdout=None,
stderr=None,
)
except Exception as e:
return "EXCEPTION OCCURED: " + str(e)
def server_command_builder(name, port, channel):
command = [
SERVER_PATH,
"-TS",
name,
"--port",
str(port),
"--statuspage",
os.getcwd() + "/games/" + name + "/turnstats.html",
]
game = Dom5game(name, channel, [], 0, {})
game.to_json()
return command
def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0