Add loading existing cars on page load. First part of #2
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
|||||||
import dbus
|
import dbus
|
||||||
from urllib.parse import parse_qs
|
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_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):
|
class Handler(BaseHTTPRequestHandler):
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
@@ -35,6 +35,8 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
if self.path == "/cars":
|
if self.path == "/cars":
|
||||||
return self.handle_get_cars_path()
|
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":
|
if self.path == "/" or self.path == "/index.html":
|
||||||
return self.handle_get_root_path()
|
return self.handle_get_root_path()
|
||||||
@@ -99,6 +101,18 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
self.wfile.write(json.dumps(data).encode())
|
self.wfile.write(json.dumps(data).encode())
|
||||||
return True
|
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):
|
def handle_get_root_path(self):
|
||||||
with open("index.html", "r") as f:
|
with open("index.html", "r") as f:
|
||||||
html = f.read()
|
html = f.read()
|
||||||
|
|||||||
41
ac_cars.py
41
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"):
|
if "preview" in lower and lower.endswith(".png") or lower.endswith(".jpg"):
|
||||||
return os.path.join(img_path, name)
|
return os.path.join(img_path, name)
|
||||||
return ""
|
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]:
|
def update_cars(cars: list[dict]) -> tuple[bool, str]:
|
||||||
print(f"handling car change for {cars}")
|
print(f"handling car change for {cars}")
|
||||||
|
|||||||
24
index.html
24
index.html
@@ -159,7 +159,6 @@
|
|||||||
|
|
||||||
function update() {
|
function update() {
|
||||||
updateTrack();
|
updateTrack();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function 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");
|
const wrapper = document.createElement("div");
|
||||||
wrapper.style.marginTop = "12px";
|
wrapper.style.marginTop = "12px";
|
||||||
wrapper.className = "car-block";
|
wrapper.className = "car-block";
|
||||||
@@ -303,6 +302,9 @@
|
|||||||
amountSelect.style.marginLeft = "10px";
|
amountSelect.style.marginLeft = "10px";
|
||||||
amountSelect.type = "number";
|
amountSelect.type = "number";
|
||||||
amountSelect.min = "1";
|
amountSelect.min = "1";
|
||||||
|
if (amount > 0) {
|
||||||
|
amountSelect.value = amount;
|
||||||
|
}
|
||||||
amountSelect.placeholder = "Amount";
|
amountSelect.placeholder = "Amount";
|
||||||
amountSelect.min = "10";
|
amountSelect.min = "10";
|
||||||
|
|
||||||
@@ -346,7 +348,7 @@
|
|||||||
skinSelect.appendChild(o);
|
skinSelect.appendChild(o);
|
||||||
});
|
});
|
||||||
|
|
||||||
// show first stkin
|
// show first skin
|
||||||
try {
|
try {
|
||||||
updateImage(car.skins[0].image);
|
updateImage(car.skins[0].image);
|
||||||
} catch (e) {
|
} 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);
|
addCarBtn.addEventListener("click", addCar);
|
||||||
|
|
||||||
|
window.onload = () => {
|
||||||
|
getCurrentCars();
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user