Add loading current track and config. Second part and fixes #2
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
||||
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_tracks import change_track, get_all_tracks, get_configs, get_preview_image, get_outline_image, get_current_track
|
||||
from ac_cars import get_all_cars, get_car_image, update_cars, get_current_cars
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
@@ -37,6 +37,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return self.handle_get_cars_path()
|
||||
if self.path == "/currentcars":
|
||||
return self.handle_get_current_cars_path()
|
||||
if self.path == "/currenttrack":
|
||||
return self.handle_get_current_track_path()
|
||||
|
||||
if self.path == "/" or self.path == "/index.html":
|
||||
return self.handle_get_root_path()
|
||||
@@ -88,6 +90,19 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.wfile.write(json.dumps(data).encode())
|
||||
return True
|
||||
|
||||
def handle_get_current_track_path(self):
|
||||
track, config = get_current_track()
|
||||
data = {
|
||||
"track": track,
|
||||
"config": config
|
||||
}
|
||||
|
||||
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_cars_path(self):
|
||||
cars = get_all_cars()
|
||||
|
||||
|
||||
14
ac_tracks.py
14
ac_tracks.py
@@ -42,8 +42,20 @@ def get_configs(track) -> list[str]:
|
||||
configdirs.remove(s)
|
||||
if (len(configdirs) == 0):
|
||||
print("===== The map you entered does not have a config! =====")
|
||||
config = ""
|
||||
return configdirs
|
||||
|
||||
def get_current_track() -> tuple[str, str]:
|
||||
track_name = ""
|
||||
config_name = ""
|
||||
config_file = os.path.join(CONFIG_PATH, CONFIG_FILE)
|
||||
with open(config_file,'r') as file:
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
if line.startswith("TRACK="):
|
||||
track_name = line.replace("TRACK=","").strip()
|
||||
elif line.startswith("CONFIG_TRACK="):
|
||||
config_name = line.replace("CONFIG_TRACK=","").strip()
|
||||
return track_name, config_name
|
||||
|
||||
def change_track(newname, config=""):
|
||||
|
||||
|
||||
77
index.html
77
index.html
@@ -157,38 +157,45 @@
|
||||
const previewImg = document.getElementById("preview");
|
||||
const outlineImg = document.getElementById("outline");
|
||||
|
||||
function update() {
|
||||
updateTrack();
|
||||
}
|
||||
function updateCurrentTrack() {
|
||||
fetch('/currenttrack')
|
||||
.then(r => r.json())
|
||||
.then(async data => {
|
||||
trackSelect.value = data.track;
|
||||
|
||||
function updateTrack() {
|
||||
await updateTrack(true);
|
||||
|
||||
const URL = '/track/' + trackSelect.value;
|
||||
fetch(URL, { method: "GET" })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
console.log(data)
|
||||
|
||||
previewImg.src = data.image
|
||||
outlineImg.src = data.outline
|
||||
|
||||
// Configs
|
||||
configSelect.innerHTML = "";
|
||||
data.configs.forEach(c => {
|
||||
let opt = document.createElement("option");
|
||||
opt.value = c;
|
||||
opt.textContent = c;
|
||||
configSelect.appendChild(opt);
|
||||
});
|
||||
|
||||
// Update preview if a config exists
|
||||
if (data.configs.length > 0) {
|
||||
updateConfig();
|
||||
}
|
||||
configSelect.value = data.config;
|
||||
updateTrackImages();
|
||||
});
|
||||
}
|
||||
|
||||
function updateConfig() {
|
||||
function updateTrack(shouldUpdateConfig = true, preselectConfig = null) {
|
||||
return fetch(`/track/${trackSelect.value}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
previewImg.src = data.image;
|
||||
outlineImg.src = data.outline;
|
||||
|
||||
if (shouldUpdateConfig) {
|
||||
configSelect.innerHTML = "";
|
||||
data.configs.forEach(c => {
|
||||
let opt = document.createElement("option");
|
||||
opt.value = c;
|
||||
opt.textContent = c;
|
||||
configSelect.appendChild(opt);
|
||||
});
|
||||
}
|
||||
|
||||
if (preselectConfig) {
|
||||
configSelect.value = preselectConfig;
|
||||
}
|
||||
|
||||
updateTrackImages();
|
||||
});
|
||||
}
|
||||
|
||||
function updateTrackImages() {
|
||||
previewImg.src = "/img/preview/" + trackSelect.value + "/" + configSelect.value;
|
||||
outlineImg.src = "/img/outline/" + trackSelect.value + "/" + configSelect.value;
|
||||
}
|
||||
@@ -208,9 +215,12 @@
|
||||
}
|
||||
|
||||
trackSelect.addEventListener("change", updateTrack);
|
||||
configSelect.addEventListener("change", updateConfig);
|
||||
configSelect.addEventListener("change", updateTrackImages);
|
||||
|
||||
window.onload = updateTrack;
|
||||
if (window.addEventListener) // W3C standard
|
||||
{
|
||||
window.addEventListener('load', updateCurrentTrack, false); // NB **not** 'onload'
|
||||
}
|
||||
</script>
|
||||
<script>
|
||||
const addCarBtn = document.getElementById("addCarBtn");
|
||||
@@ -233,7 +243,7 @@
|
||||
const skinName = skinSelect?.value || "";
|
||||
let amount = 1;
|
||||
if (parseInt(amountInput.value) >= 1) {
|
||||
|
||||
|
||||
console.log("setting amount to value:", amountInput.value);
|
||||
amount = parseInt(amountInput.value);
|
||||
}
|
||||
@@ -407,9 +417,10 @@
|
||||
|
||||
addCarBtn.addEventListener("click", addCar);
|
||||
|
||||
window.onload = () => {
|
||||
getCurrentCars();
|
||||
};
|
||||
if (window.addEventListener) // W3C standard
|
||||
{
|
||||
window.addEventListener('load', getCurrentCars, false); // NB **not** 'onload'
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user