ColoradoMadLibs / app.py
awacke1's picture
Update app.py
6c4d06c verified
raw
history blame
3.96 kB
import streamlit as st
import asyncio
import websockets
import uuid
from datetime import datetime
import os
import random
import hashlib
import base64
import edge_tts
import nest_asyncio
import re
import threading
import time
import json
import streamlit.components.v1 as components
from gradio_client import Client
from streamlit_marquee import streamlit_marquee
import folium
from streamlit_folium import folium_static
import glob
import pytz
from collections import defaultdict
import pandas as pd
# ๐Ÿ› ๏ธ (1) Allow nested asyncio loops for compatibility
nest_asyncio.apply()
# ๐ŸŽจ (2) Page Configuration for Streamlit UI
st.set_page_config(
layout="wide",
page_title="Rocky Mountain Quest 3D ๐Ÿ”๏ธ๐ŸŽฎ",
page_icon="๐ŸฆŒ",
initial_sidebar_state="auto",
menu_items={
'Get Help': 'https://huggingface.co/awacke1',
'Report a bug': 'https://huggingface.co/spaces/awacke1',
'About': "Rocky Mountain Quest 3D ๐Ÿ”๏ธ๐ŸŽฎ"
}
)
# ๐ŸŒŸ (3) Prominent Player Join Notifications
async def broadcast_player_join(username):
message = json.dumps({"type": "player_joined", "username": username})
await broadcast_message(message, "quest")
# ๐Ÿ”„ (4) Real-Time Game State Synchronization
async def broadcast_game_state():
while True:
game_state = load_game_state(time.time())
await broadcast_message(json.dumps({"type": "sync", "state": game_state}), "quest")
await asyncio.sleep(1)
# ๐ŸŒŒ (5) Fractal and Emergent Data Generation for 3D Visualization
FRAC_SEEDS = [random.uniform(-40, 40) for _ in range(100)]
# ๐ŸŽฒ (6) Three.js Shape Primitives and Fractal Data Structures
shape_primitives = ['sphere', 'cube', 'cylinder', 'cone', 'torus', 'dodecahedron', 'octahedron', 'tetrahedron', 'icosahedron']
fractals = [{
"type": random.choice(shape_primitives),
"position": [random.uniform(-40,40), random.uniform(0,20), random.uniform(-40,40)],
"color": random.randint(0, 0xFFFFFF)
} for _ in range(100)]
# ๐Ÿ–ฅ๏ธ (7) Initialize WebSocket Server for Multiplayer Connectivity
if not st.session_state.get('server_running', False):
threading.Thread(target=start_websocket_server, daemon=True).start()
st.session_state['server_running'] = True
# ๐Ÿ’ฐ (8) Increment Scores and Automatically Save at Milestones
async def increment_and_save_scores():
while True:
game_state = load_game_state(time.time())
for player in game_state["players"].values():
player["score"] += 1
if player["score"] % 100 == 0:
filename = f"cache_{player['username']}_score_{player['score']}.json"
with open(filename, 'w') as f:
json.dump(player, f)
update_game_state(game_state)
await asyncio.sleep(1)
# ๐Ÿš€ (9) Start Background Async Tasks for Game Operations
asyncio.run(increment_and_save_scores())
asyncio.run(broadcast_game_state())
# ๐ŸŒ (10) Main Streamlit UI Function
def main():
# ๐Ÿ”๏ธ Game Title Display
st.title("Rocky Mountain Quest 3D ๐Ÿ”๏ธ๐ŸŽฎ")
# ๐Ÿ“ UI Layout with Columns
col1, col2 = st.columns([3, 1])
# ๐ŸŽฎ Left Column - Three.js Game Canvas
with col1:
components.html(rocky_map_html, width=800, height=600)
# ๐Ÿ“‹ Right Column - Player Information and Maps
with col2:
st.subheader("Active Players ๐Ÿง™โ€โ™‚๏ธ")
current_state = load_game_state(time.time())
for player in current_state["players"].values():
st.write(f"๐Ÿ‘ค {player['username']} - ๐ŸŒŸ {player['score']} points")
st.subheader("๐ŸŒพ Prairie Map")
prairie_map = folium.Map(location=[44.0, -103.0], zoom_start=8)
folium_static(prairie_map)
# ๐ŸŽฏ Sidebar Game Controls
st.sidebar.markdown("### ๐ŸŽฏ Game Controls")
if st.sidebar.button("Reset World ๐ŸŒ"):
reset_game_state()
st.rerun()
# โœ… Run the application
if __name__ == "__main__":
main()