Added function to check if folder exists.

This commit is contained in:
2026-05-30 14:40:52 +02:00
parent 6d1ff496dd
commit 84b84f45b6

View File

@@ -8,17 +8,15 @@ load_dotenv()
SERVER_PATH = os.getenv("SERVERPATH") SERVER_PATH = os.getenv("SERVERPATH")
def create_server(name, port, channel): def create_server(name: str, port: int, channel: int, command: str):
if is_port_in_use(port): if is_port_in_use(port):
return "ERROR_PORT_IN_USE" return "ERROR_PORT_IN_USE"
os.mkdir("games/" + name) os.mkdir("games/" + name)
# print(SERVER_PATH)
# print(server_command_builder(name, port, channel))
game = Dom5game(name, channel, [], 0, {}) game = Dom5game(name, channel, [], 0, {})
game.to_json() game.to_json()
try: try:
p = subprocess.Popen( p = subprocess.Popen(
server_command_builder(name, port), command,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )
@@ -55,9 +53,11 @@ def get_nations():
def server_command_builder( def server_command_builder(
name: str, name: str,
port: int, port: int,
era: int, era: int, # 1 EA, 2 MA, 3 LA
closed_slots: list[int] = [], closed_slots: list[int] = [], # list of nations that will be closed
ai_slots: list[tuple[int, int]] = [], ai_slots: list[
tuple[int, int]
] = [], # list of nations that will be ai tuple format: NationID, AI Level (1-6)
client_start: bool = True, client_start: bool = True,
teams: list[tuple[int, int, int]] = [], teams: list[tuple[int, int, int]] = [],
clustered_start: bool = False, clustered_start: bool = False,
@@ -253,3 +253,7 @@ def is_port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0 return s.connect_ex(("localhost", port)) == 0
def does_game_folder_exist(folder: str) -> bool:
return not (os.path.isdir(SERVER_PATH + "/" + folder))