Write cars to file

This commit is contained in:
SemvdH
2025-11-17 23:55:02 +01:00
parent 8d57f3fe33
commit 12f1adccf2
2 changed files with 63 additions and 3 deletions

View File

@@ -4,7 +4,7 @@ import json
import dbus
from urllib.parse import parse_qs
from ac_maps import change_track, get_all_tracks, get_configs, get_preview_image, get_outline_image
from ac_cars import get_all_cars, get_car_image
from ac_cars import get_all_cars, get_car_image, update_cars
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
@@ -166,6 +166,7 @@ class Handler(BaseHTTPRequestHandler):
import json
try:
data = json.loads(body)
success, message = update_cars(data.get("cars", []))
except json.JSONDecodeError:
self.send_response(400)
self.send_header("Content-Type", "application/json")

View File

@@ -1,6 +1,7 @@
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"
@@ -8,11 +9,23 @@ CONFIG_PATH = "/home/sem/assetto-corsa/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 = {
@@ -25,7 +38,6 @@ def get_all_cars():
"skins": skins_data
}
all_cars.append(car_data)
print(f"added data " + str(car_data))
return all_cars
@@ -45,4 +57,51 @@ def get_car_image(car: str, skin: str) -> str:
if "preview" in lower and lower.endswith(".png") or lower.endswith(".jpg"):
return os.path.join(img_path, name)
return ""
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};"
car_numer += 1
final_cars.append(Car(car_entry, car.get("skin"), car_number))
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)