Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (147 loc) · 5.42 KB
/
Copy pathmain.py
File metadata and controls
183 lines (147 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends
from fastapi.middleware.cors import CORSMiddleware
from src.game.game_manager import game_manager
from src.auth.auth import get_current_user
from supabase import create_client
from pydantic import BaseModel
import os
import json
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# ----------------------
# Supabase Setup
# ----------------------
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
raise RuntimeError("❌ Supabase environment variables not set! Add them to .env")
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
SESSIONS_TABLE = "sessions" # Make sure this exists in Supabase
# ----------------------
# FastAPI Setup
# ----------------------
app = FastAPI()
# CORS (keep your existing version if you already had one)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # tighten this later
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ----------------------
# WebSocket Connection Manager
# ----------------------
class ConnectionManager:
def __init__(self):
self.active_connections: dict[str, WebSocket] = {}
async def connect(self, session_id: str, websocket: WebSocket):
await websocket.accept()
self.active_connections[session_id] = websocket
def disconnect(self, session_id: str):
if session_id in self.active_connections:
del self.active_connections[session_id]
async def send_message(self, session_id: str, message: dict):
if session_id in self.active_connections:
await self.active_connections[session_id].send_json(message)
manager = ConnectionManager()
# ----------------------
# API ROUTES
# ----------------------
@app.post("/api/game/start")
async def start_game(user = Depends(get_current_user)):
"""Start a new game session"""
session = game_manager.create_session(user.id)
return {
"session_id": session.session_id,
"max_duration": session.max_duration,
"initial_target": {
"x": session.current_target.center_x,
"y": session.current_target.center_y,
"type": session.current_target.punch_type,
"radius": session.current_target.radius
}
}
@app.websocket("/ws/game/{session_id}")
async def game_websocket(websocket: WebSocket, session_id: str):
await manager.connect(session_id, websocket)
try:
while True:
data = await websocket.receive_json()
# Handle punch
if data["type"] == "punch_detected":
result = await game_manager.validate_punch(
session_id,
data["landmark_data"],
data["detected_punch"]
)
await manager.send_message(session_id, {
"type": "punch_result",
"data": result
})
# Handle live stats request
elif data["type"] == "get_stats":
stats = game_manager.get_session_stats(session_id)
await manager.send_message(session_id, {
"type": "stats_update",
"data": stats
})
except WebSocketDisconnect:
manager.disconnect(session_id)
game_manager.end_session(session_id)
@app.get("/api/game/{session_id}/stats")
async def get_game_stats(session_id: str, user = Depends(get_current_user)):
"""Return current game statistics (from memory)"""
stats = game_manager.get_session_stats(session_id)
if not stats:
return {"error": "Session not found"}
return stats
@app.post("/api/game/{session_id}/end")
async def end_game(session_id: str, user = Depends(get_current_user)):
"""
End game session and return final stats.
ALSO saves session data to Supabase.
"""
stats = game_manager.end_session(session_id)
if not stats:
return {"error": "Session not found"}
# ----------------------
# Persist to Supabase
# ----------------------
try:
supabase.table(SESSIONS_TABLE).insert({
"user_id": user.id,
"session_id": session_id,
"session_data": stats, # JSON stored directly
}).execute()
print(f"💾 Saved session {session_id} for user {user.id}")
except Exception as e:
print("❌ Failed to save session to Supabase:", e)
return stats
@app.get("/session/latest")
async def get_latest_game(user = Depends(get_current_user)):
"""
Retrieve the most recent completed session for the current user.
This is the endpoint your React Dashboard should fetch.
"""
try:
response = (
supabase.table(SESSIONS_TABLE)
.select("session_data, session_id, created_at")
.eq("user_id", user.id)
.order("created_at", desc=True)
.limit(1)
.execute()
)
if not response.data:
return {"error": "No sessions found"}
latest = response.data[0]
return {
"session_id": latest["session_id"],
"data": latest["session_data"],
"timestamp": latest["created_at"],
}
except Exception as e:
print("❌ Error fetching latest session:", e)
return {"error": "Could not fetch latest session"}