import { useState, useEffect } from 'react'; import { Play, Pause, RotateCcw, FastForward, Activity, MapPin } from 'lucide-react'; import './App.css'; const API_BASE = window.location.origin === 'http://localhost:5173' || window.location.origin === 'http://127.0.0.1:5173' ? 'http://127.0.0.1:8000/api' : '/api'; function App() { const [state, setState] = useState({ queues: [0, 0, 0, 0, 0, 0, 0, 0], // N_SR, N_L, E_SR, E_L, S_SR, S_L, W_SR, W_L phase: 0, reward: 0, vehicles_passed: 0, step: 0, total_reward: 0, is_done: false }); const [isPlaying, setIsPlaying] = useState(false); const [speedMs, setSpeedMs] = useState(250); const fetchState = async (endpoint) => { try { const res = await fetch(`${API_BASE}/${endpoint}`, { method: 'POST' }); const data = await res.json(); setState(data); if (data.is_done) { setIsPlaying(false); } } catch (err) { console.error(err); setIsPlaying(false); } }; useEffect(() => { fetchState('reset'); }, []); useEffect(() => { let interval; if (isPlaying) { interval = setInterval(() => { fetchState('step'); }, speedMs); } return () => clearInterval(interval); }, [isPlaying, speedMs]); // Phase logic: 0=N, 1=E, 2=S, 3=W const isNGreen = state.phase === 0; const isEGreen = state.phase === 1; const isSGreen = state.phase === 2; const isWGreen = state.phase === 3; const renderCars = (count, direction, isLeftTurn) => { const displayCount = Math.min(count, 5); // Cap visuals for ultra-premium look return Array.from({ length: displayCount }).map((_, i) => (
Deep Q-Network Traffic Simulation