Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,7 @@ with col2:
|
|
35 |
# Session State Initialization
|
36 |
def init_session_state():
|
37 |
defaults = {
|
38 |
-
'server_running': False, '
|
39 |
'chat_history': [], 'last_chat_update': 0, 'username': None,
|
40 |
'tts_voice': "en-US-AriaNeural", 'audio_cache': {}
|
41 |
}
|
@@ -122,16 +122,12 @@ async def broadcast_message(message, room_id):
|
|
122 |
if client_id in st.session_state.active_connections[room_id]:
|
123 |
del st.session_state.active_connections[room_id][client_id]
|
124 |
|
125 |
-
async def
|
126 |
if not st.session_state.get('server_running', False):
|
127 |
server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
|
128 |
st.session_state['server_running'] = True
|
129 |
-
|
130 |
-
|
131 |
-
def start_websocket_server():
|
132 |
-
loop = asyncio.new_event_loop()
|
133 |
-
asyncio.set_event_loop(loop)
|
134 |
-
loop.run_until_complete(run_websocket_server())
|
135 |
|
136 |
# Chat Functions
|
137 |
async def save_chat_entry(username, message):
|
@@ -216,7 +212,7 @@ html_code = f"""
|
|
216 |
document.getElementById('gameContainer').appendChild(renderer.domElement);
|
217 |
|
218 |
// Lighting
|
219 |
-
const ambientLight = new THREE.AmbientLight(0xffffff, 0.1);
|
220 |
scene.add(ambientLight);
|
221 |
|
222 |
// Ground
|
@@ -335,8 +331,8 @@ html_code = f"""
|
|
335 |
// Add city lights at intersections
|
336 |
for (let x = -citySize; x <= citySize; x++) {{
|
337 |
for (let z = -citySize; z <= citySize; z++) {{
|
338 |
-
const light = new THREE.PointLight(0xffff99, 1, 20);
|
339 |
-
light.position.set(x * spacing, 5, z * spacing);
|
340 |
light.castShadow = true;
|
341 |
scene.add(light);
|
342 |
lights.push(light);
|
@@ -345,7 +341,7 @@ html_code = f"""
|
|
345 |
}}
|
346 |
|
347 |
function evolveCity() {{
|
348 |
-
if (Math.random() < 0.1) {{
|
349 |
const citySize = 5, spacing = 15;
|
350 |
const x = (Math.random() * 2 - 1) * citySize * spacing;
|
351 |
const z = (Math.random() * 2 - 1) * citySize * spacing;
|
@@ -559,7 +555,7 @@ html_code = f"""
|
|
559 |
cycleTime += delta;
|
560 |
const cycleDuration = 120;
|
561 |
const angle = (cycleTime / cycleDuration) * Math.PI * 2;
|
562 |
-
const intensity = Math.max(0, Math.sin(angle)) * 0.5 + 0.5;
|
563 |
lights.forEach(light => light.intensity = intensity);
|
564 |
const dayColor = new THREE.Color(0x87CEEB);
|
565 |
const nightColor = new THREE.Color(0x001133);
|
@@ -634,10 +630,15 @@ if audio_bytes:
|
|
634 |
message = "Voice message received"
|
635 |
asyncio.run(save_chat_entry(st.session_state.username, message))
|
636 |
|
637 |
-
# Start WebSocket Server
|
638 |
-
|
639 |
-
st.session_state.
|
640 |
-
|
|
|
|
|
|
|
|
|
|
|
641 |
|
642 |
st.sidebar.write("""
|
643 |
### How to Play
|
|
|
35 |
# Session State Initialization
|
36 |
def init_session_state():
|
37 |
defaults = {
|
38 |
+
'server_running': False, 'active_connections': {},
|
39 |
'chat_history': [], 'last_chat_update': 0, 'username': None,
|
40 |
'tts_voice': "en-US-AriaNeural", 'audio_cache': {}
|
41 |
}
|
|
|
122 |
if client_id in st.session_state.active_connections[room_id]:
|
123 |
del st.session_state.active_connections[room_id][client_id]
|
124 |
|
125 |
+
async def start_websocket_server():
|
126 |
if not st.session_state.get('server_running', False):
|
127 |
server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
|
128 |
st.session_state['server_running'] = True
|
129 |
+
st.session_state['server'] = server # Store server reference
|
130 |
+
await asyncio.Future() # Run forever until cancelled
|
|
|
|
|
|
|
|
|
131 |
|
132 |
# Chat Functions
|
133 |
async def save_chat_entry(username, message):
|
|
|
212 |
document.getElementById('gameContainer').appendChild(renderer.domElement);
|
213 |
|
214 |
// Lighting
|
215 |
+
const ambientLight = new THREE.AmbientLight(0xffffff, 0.1);
|
216 |
scene.add(ambientLight);
|
217 |
|
218 |
// Ground
|
|
|
331 |
// Add city lights at intersections
|
332 |
for (let x = -citySize; x <= citySize; x++) {{
|
333 |
for (let z = -citySize; z <= citySize; z++) {{
|
334 |
+
const light = new THREE.PointLight(0xffff99, 1, 20);
|
335 |
+
light.position.set(x * spacing, 5, z * spacing);
|
336 |
light.castShadow = true;
|
337 |
scene.add(light);
|
338 |
lights.push(light);
|
|
|
341 |
}}
|
342 |
|
343 |
function evolveCity() {{
|
344 |
+
if (Math.random() < 0.1) {{
|
345 |
const citySize = 5, spacing = 15;
|
346 |
const x = (Math.random() * 2 - 1) * citySize * spacing;
|
347 |
const z = (Math.random() * 2 - 1) * citySize * spacing;
|
|
|
555 |
cycleTime += delta;
|
556 |
const cycleDuration = 120;
|
557 |
const angle = (cycleTime / cycleDuration) * Math.PI * 2;
|
558 |
+
const intensity = Math.max(0, Math.sin(angle)) * 0.5 + 0.5;
|
559 |
lights.forEach(light => light.intensity = intensity);
|
560 |
const dayColor = new THREE.Color(0x87CEEB);
|
561 |
const nightColor = new THREE.Color(0x001133);
|
|
|
630 |
message = "Voice message received"
|
631 |
asyncio.run(save_chat_entry(st.session_state.username, message))
|
632 |
|
633 |
+
# Start WebSocket Server in Main Event Loop
|
634 |
+
async def main_async():
|
635 |
+
if not st.session_state.get('server_running', False):
|
636 |
+
await start_websocket_server()
|
637 |
+
|
638 |
+
# Run the WebSocket server in the main event loop
|
639 |
+
loop = asyncio.get_event_loop()
|
640 |
+
if not st.session_state.get('server_running', False):
|
641 |
+
loop.run_until_complete(main_async())
|
642 |
|
643 |
st.sidebar.write("""
|
644 |
### How to Play
|