From 808db12e2511941ec030078fe347ca697cb4f32c Mon Sep 17 00:00:00 2001 From: SemvdH Date: Fri, 21 Nov 2025 23:49:06 +0100 Subject: [PATCH] Add loading existing cars on page load. First part of #2 --- ac-server-scripts-api.py | 16 +++++++++++++++- ac_cars.py | 41 ++++++++++++++++++++++++++++++++++++++++ index.html | 24 ++++++++++++++++++++--- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/ac-server-scripts-api.py b/ac-server-scripts-api.py index 7945fcf..7eda822 100644 --- a/ac-server-scripts-api.py +++ b/ac-server-scripts-api.py @@ -4,7 +4,7 @@ import json import dbus from urllib.parse import parse_qs from ac_tracks import change_track, get_all_tracks, get_configs, get_preview_image, get_outline_image -from ac_cars import get_all_cars, get_car_image, update_cars +from ac_cars import get_all_cars, get_car_image, update_cars, get_current_cars class Handler(BaseHTTPRequestHandler): def do_GET(self): @@ -35,6 +35,8 @@ class Handler(BaseHTTPRequestHandler): if self.path == "/cars": return self.handle_get_cars_path() + if self.path == "/currentcars": + return self.handle_get_current_cars_path() if self.path == "/" or self.path == "/index.html": return self.handle_get_root_path() @@ -99,6 +101,18 @@ class Handler(BaseHTTPRequestHandler): self.wfile.write(json.dumps(data).encode()) return True + def handle_get_current_cars_path(self): + cars = get_current_cars() + data = { + "cars": cars + } + + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + self.wfile.write(json.dumps(data).encode()) + return True + def handle_get_root_path(self): with open("index.html", "r") as f: html = f.read() diff --git a/ac_cars.py b/ac_cars.py index 5bd4575..1cb2f26 100644 --- a/ac_cars.py +++ b/ac_cars.py @@ -58,6 +58,47 @@ 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 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}") diff --git a/index.html b/index.html index 2413c17..8e542b8 100644 --- a/index.html +++ b/index.html @@ -159,7 +159,6 @@ function update() { updateTrack(); - } function updateTrack() { @@ -272,7 +271,7 @@ }); } - async function addCar(preselectedCar = null, preselectedSkin = null) { + async function addCar(preselectedCar = null, preselectedSkin = null, amount = 0) { const wrapper = document.createElement("div"); wrapper.style.marginTop = "12px"; wrapper.className = "car-block"; @@ -303,6 +302,9 @@ amountSelect.style.marginLeft = "10px"; amountSelect.type = "number"; amountSelect.min = "1"; + if (amount > 0) { + amountSelect.value = amount; + } amountSelect.placeholder = "Amount"; amountSelect.min = "10"; @@ -346,7 +348,7 @@ skinSelect.appendChild(o); }); - // show first stkin + // show first skin try { updateImage(car.skins[0].image); } catch (e) { @@ -391,7 +393,23 @@ } } + function getCurrentCars() { + const url = '/currentcars'; + fetch(url) + .then(res => res.json()) + .then(data => { + console.log("Current cars:", data); + data.cars.forEach(c => { + addCar(c.car, c.skin, c.amount); + }); + }); + } + addCarBtn.addEventListener("click", addCar); + + window.onload = () => { + getCurrentCars(); + };