Add changing gamemode

This commit is contained in:
SemvdH
2025-11-22 01:08:47 +01:00
parent 15c280b45e
commit d084696e42
3 changed files with 177 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ 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, get_current_track
from ac_cars import get_all_cars, get_car_image, update_cars, get_current_cars
from ac_gamemodes import get_gamemodes, update_gamemodes
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
@@ -39,6 +40,8 @@ class Handler(BaseHTTPRequestHandler):
return self.handle_get_current_cars_path()
if self.path == "/currenttrack":
return self.handle_get_current_track_path()
if self.path == "/gamemodes":
return self.handle_get_gamemodes_path()
if self.path == "/" or self.path == "/index.html":
return self.handle_get_root_path()
@@ -128,6 +131,22 @@ class Handler(BaseHTTPRequestHandler):
self.wfile.write(json.dumps(data).encode())
return True
def handle_get_gamemodes_path(self):
gamemodes = get_gamemodes()
data = {
"gamemodes": {
"practice_minutes": gamemodes[0],
"qualify_minutes": gamemodes[1],
"race_laps": gamemodes[2]
}
}
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()
@@ -149,6 +168,8 @@ class Handler(BaseHTTPRequestHandler):
return self.handle_post_change_track()
if (self.path.startswith("/changecars")):
return self.handle_post_change_car()
if (self.path.startswith("/changegamemodes")):
return self.handle_post_change_gamemodes()
return False
def handle_post_change_track(self):
@@ -214,6 +235,44 @@ class Handler(BaseHTTPRequestHandler):
print("Invalid JSON received")
return False
def handle_post_change_gamemodes(self):
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length).decode("utf-8")
import json
try:
data = json.loads(body)
gamemodes = data.get("gamemodes", {})
print("Received gamemodes:", gamemodes)
practice = gamemodes.get("practice_minutes", 0)
qualify = gamemodes.get("qualify_minutes", 0)
race = gamemodes.get("race_laps", 0)
success, message = update_gamemodes(practice, qualify, race)
if success:
success, message = restart_ac_server()
print(f"restart succeeded: {success}, {message}")
if success:
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
response = {"status": "success", "message": message}
self.wfile.write(json.dumps(response).encode())
else:
self.send_response(400)
self.send_header("Content-type", "application/json")
self.end_headers()
response = {"status": "error", "message": message}
self.wfile.write(json.dumps(response).encode())
return True
except json.JSONDecodeError:
self.send_response(400)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"error":"invalid json"}')
print("Invalid JSON received")
return False
def send_image(self, img_path: str):
if os.path.exists(img_path):