149 lines
5.1 KiB
Python
149 lines
5.1 KiB
Python
import os
|
|
|
|
CONFIG_FILE = "server_cfg.ini"
|
|
ENTRY_LIST_FILE = "entry_list.ini"
|
|
CONTENT_FOLDER = "/home/sem/assetto-corsa/content"
|
|
CONFIG_PATH = "/home/sem/assetto-corsa/cfg"
|
|
# CONTENT_FOLDER = "./content"
|
|
# CONFIG_PATH = "./cfg"
|
|
|
|
CARS_FOLDER = os.path.join(CONTENT_FOLDER, "cars")
|
|
|
|
class Car:
|
|
def __init__(self, name, skin, number):
|
|
self.name = name
|
|
self.skin = skin
|
|
self.number = number
|
|
|
|
def __str__(self):
|
|
return f"[CAR_{self.number}]\nMODEL={self.name}\nSKIN={self.skin}\nSPECTATOR_MODE=0\nDRIVERNAME=\nTEAM=\nGUID=\nBALLAST=0\nAI=auto\n"
|
|
|
|
|
|
def get_all_cars():
|
|
cars_folders = sorted([x.name for x in os.scandir(CARS_FOLDER) if x.is_dir()])
|
|
all_cars = []
|
|
for i in range(len(cars_folders)):
|
|
skins = get_car_skins(cars_folders[i])
|
|
if len(skins) == 0:
|
|
continue # skip cars without skins
|
|
|
|
skins_data = []
|
|
for j in range(len(skins)):
|
|
skin_data = {
|
|
"name": skins[j],
|
|
"image": f"/img/car/{cars_folders[i]}/{skins[j]}"
|
|
}
|
|
skins_data.append(skin_data)
|
|
car_data = {
|
|
"name": cars_folders[i],
|
|
"skins": skins_data
|
|
}
|
|
all_cars.append(car_data)
|
|
return all_cars
|
|
|
|
|
|
def get_car_skins(car: str) -> list[str]:
|
|
car_path = os.path.join(CARS_FOLDER, car);
|
|
skin_path = os.path.join(car_path, "skins")
|
|
if os.path.exists(skin_path) == False:
|
|
return []
|
|
|
|
skindirs = [x.path.replace(skin_path + "/","") for x in os.scandir(skin_path) if x.is_dir()]
|
|
return skindirs
|
|
|
|
def get_car_image(car: str, skin: str) -> str:
|
|
img_path = os.path.join(CARS_FOLDER, car,"skins", skin)
|
|
for name in os.listdir(img_path):
|
|
lower = name.lower()
|
|
if "preview" in lower and lower.endswith(".png") or lower.endswith(".jpg"):
|
|
return os.path.join(img_path, name)
|
|
return ""
|
|
|
|
def get_current_cars() -> list[dict]:
|
|
cars = []
|
|
entry_list_path = os.path.join(CONFIG_PATH, ENTRY_LIST_FILE)
|
|
if os.path.exists(entry_list_path) == False:
|
|
return cars
|
|
with open(entry_list_path, "r") as f:
|
|
lines = f.readlines()
|
|
current_car_entry = None
|
|
curent_model = ""
|
|
current_skin = ""
|
|
for line in lines:
|
|
line = line.strip()
|
|
if line.startswith("[CAR_"):
|
|
print(f"found new car entry line: {line}")
|
|
if current_car_entry is not None:
|
|
# update previous car entry
|
|
update_found_cars(cars, current_car_entry)
|
|
current_car_entry = {"car": "", "skin": "", "amount": 1}
|
|
elif line.startswith("MODEL="):
|
|
current_car_entry["car"] = line.replace("MODEL=", "").strip()
|
|
elif line.startswith("SKIN="):
|
|
current_car_entry["skin"] = line.replace("SKIN=", "").strip()
|
|
|
|
if current_car_entry is not None:
|
|
# update last car entry because no new [CAR_] line at the end of the file
|
|
update_found_cars(cars, current_car_entry)
|
|
print(f"loaded current cars: {cars}")
|
|
return cars
|
|
|
|
def update_found_cars(cars: list[dict], current_car_entry: dict) -> tuple[bool, str]:
|
|
found = False
|
|
for c in cars:
|
|
if c["car"] == current_car_entry["car"] and c["skin"] == current_car_entry["skin"]:
|
|
c["amount"] += 1
|
|
print(f"updated car: {c} with new amount {c['amount']}")
|
|
found = True
|
|
break
|
|
if not found:
|
|
cars.append(current_car_entry)
|
|
print(f"added car: {current_car_entry}")
|
|
|
|
def update_cars(cars: list[dict]) -> tuple[bool, str]:
|
|
print(f"handling car change for {cars}")
|
|
cars_line = ""
|
|
final_cars = []
|
|
car_number = 0
|
|
for car in cars:
|
|
car_entry = car.get("car", "")
|
|
if (car_entry == ""):
|
|
continue
|
|
if (car_entry not in cars_line):
|
|
cars_line += f"{car_entry};"
|
|
final_cars.append(Car(car_entry, car.get("skin"), car_number))
|
|
car_number += 1
|
|
cars_line = cars_line.rstrip(";")
|
|
|
|
write_car_entries_line(cars_line)
|
|
write_max_clients(len(final_cars))
|
|
write_entry_list_file(final_cars)
|
|
|
|
return True, "Cars updated successfully."
|
|
|
|
def write_entry_list_file(cars: list[Car]):
|
|
with open(os.path.join(CONFIG_PATH, ENTRY_LIST_FILE),"w") as f:
|
|
for car in cars:
|
|
f.write(str(car))
|
|
print(f"cars wrote to {ENTRY_LIST_FILE}")
|
|
|
|
def write_max_clients(max_clients: int):
|
|
change_config_file("MAX_CLIENTS=", str(max_clients))
|
|
|
|
def write_car_entries_line(cars_line: str):
|
|
change_config_file("CARS=", cars_line)
|
|
|
|
def change_config_file(line_start: str, new_value: str):
|
|
with open(os.path.join(CONFIG_PATH, CONFIG_FILE),'r') as file:
|
|
new_content = ""
|
|
for line in file:
|
|
line = line.strip()
|
|
if line.startswith(line_start):
|
|
new_content += line_start + new_value
|
|
print("changing line " + line + " to " + line_start + new_value)
|
|
else:
|
|
new_content += line
|
|
new_content += "\n"
|
|
|
|
with open(os.path.join(CONFIG_PATH, CONFIG_FILE),'w') as newfile:
|
|
newfile.write(new_content) |