47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
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
|