Add loading existing cars on page load. First part of #2

This commit is contained in:
SemvdH
2025-11-21 23:49:06 +01:00
parent 880680871d
commit 808db12e25
3 changed files with 77 additions and 4 deletions

View File

@@ -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}")