Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -19,11 +19,8 @@ async def lifespan(app: FastAPI):
|
|
| 19 |
await pool.stop()
|
| 20 |
|
| 21 |
app = FastAPI(title="Deepcastle Engine API", lifespan=lifespan)
|
| 22 |
-
|
| 23 |
-
# βββ Multiplaying / Challenge Manager ββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
class ConnectionManager:
|
| 25 |
def __init__(self):
|
| 26 |
-
# match_id -> list of websockets
|
| 27 |
self.active_connections: Dict[str, List[WebSocket]] = {}
|
| 28 |
|
| 29 |
async def connect(self, websocket: WebSocket, match_id: str):
|
|
@@ -59,18 +56,14 @@ async def websocket_endpoint(websocket: WebSocket, match_id: str):
|
|
| 59 |
try:
|
| 60 |
while True:
|
| 61 |
data = await websocket.receive_text()
|
| 62 |
-
# Relay the message (move, etc.) to others in the same room
|
| 63 |
await manager.broadcast(data, match_id, exclude=websocket)
|
| 64 |
except WebSocketDisconnect:
|
| 65 |
manager.disconnect(websocket, match_id)
|
| 66 |
-
# Notify remaining players that opponent disconnected β they win
|
| 67 |
await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)
|
| 68 |
except Exception:
|
| 69 |
manager.disconnect(websocket, match_id)
|
| 70 |
await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# Allow ALL for easy testing (we can restrict this later if needed)
|
| 74 |
app.add_middleware(
|
| 75 |
CORSMiddleware,
|
| 76 |
allow_origins=["*"],
|
|
@@ -531,5 +524,4 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 531 |
|
| 532 |
if __name__ == "__main__":
|
| 533 |
import uvicorn
|
| 534 |
-
# Hugging Face Spaces port is 7860
|
| 535 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 19 |
await pool.stop()
|
| 20 |
|
| 21 |
app = FastAPI(title="Deepcastle Engine API", lifespan=lifespan)
|
|
|
|
|
|
|
| 22 |
class ConnectionManager:
|
| 23 |
def __init__(self):
|
|
|
|
| 24 |
self.active_connections: Dict[str, List[WebSocket]] = {}
|
| 25 |
|
| 26 |
async def connect(self, websocket: WebSocket, match_id: str):
|
|
|
|
| 56 |
try:
|
| 57 |
while True:
|
| 58 |
data = await websocket.receive_text()
|
|
|
|
| 59 |
await manager.broadcast(data, match_id, exclude=websocket)
|
| 60 |
except WebSocketDisconnect:
|
| 61 |
manager.disconnect(websocket, match_id)
|
|
|
|
| 62 |
await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)
|
| 63 |
except Exception:
|
| 64 |
manager.disconnect(websocket, match_id)
|
| 65 |
await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)
|
| 66 |
|
|
|
|
|
|
|
| 67 |
app.add_middleware(
|
| 68 |
CORSMiddleware,
|
| 69 |
allow_origins=["*"],
|
|
|
|
| 524 |
|
| 525 |
if __name__ == "__main__":
|
| 526 |
import uvicorn
|
|
|
|
| 527 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|