import json
import time
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
from collections import deque
import Settings
from Multiplayer_Server import LoadData, SaveData
MAX_HISTORY_LENGTH = 100 # Store last 100 data points
class WebUIHandler(BaseHTTPRequestHandler):
player_count_history = deque(maxlen=MAX_HISTORY_LENGTH)
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(self.generate_html().encode())
elif self.path == '/data':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(self.get_server_data()).encode())
else:
self.send_error(404)
def generate_html(self):
html = """
Dani's Race - Retro Dashboard
Dani's Race Dashboard
Connect to the game server at: https://dani.madiator.com:6969
Server Status: Checking...
Player Count History
Active Rooms:
Server Log:
"""
return html
def get_server_data(self):
try:
# Try to get health status from the game server
health_response = requests.get("http://localhost:6971/health", timeout=5)
server_status = "Online" if health_response.status_code == 200 else "Offline"
except requests.RequestException:
server_status = "Offline"
users = LoadData("users", {})
objects = LoadData("objects", {})
rooms = {}
for user_id, user_data in users.items():
room = user_data.get("room", "Unknown")
if room not in rooms:
rooms[room] = []
rooms[room].append(user_data.get("username", f"User-{user_id[:5]}"))
# Get the last 20 log entries
log_entries = LoadData("server_log", [])[-20:]
# Update player count history
self.player_count_history.append(len(users))
return {
"server_status": server_status,
"active_users": len(users),
"total_objects": len(objects),
"rooms": rooms,
"log": log_entries,
"player_count_history": list(self.player_count_history),
"server_timestamp": time.time()
}
if __name__ == "__main__":
PORT = 6970
serve = HTTPServer(("", PORT), WebUIHandler)
print(f"Web UI server running on port {PORT}")
serve.serve_forever()