Spaces:
Sleeping
Sleeping
Update main.py
Browse files18 unmodified lines192021222324252627282929 unmodified lines596061626364656667686970717273747576454 unmodified lines53153253353453518 unmodified linesawait pool.stop()app = FastAPI(title="Deepcastle Engine API", lifespan=lifespan)# βββ Multiplaying / Challenge Manager ββββββββββββββββββββββββββββββββββββββββββclass ConnectionManager:def __init__(self):# match_id -> list of websocketsself.active_connections: Dict[str, List[WebSocket]] = {}async def connect(self, websocket: WebSocket, match_id: str):29 unmodified linestry:while True:data = await websocket.receive_text()# Relay the message (move, etc.) to others in the same roomawait manager.broadcast(data, match_id, exclude=websocket)except WebSocketDisconnect:manager.disconnect(websocket, match_id)# Notify remaining players that opponent disconnected β they winawait manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)except Exception:manager.disconnect(websocket, match_id)await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)# Allow ALL for easy testing (we can restrict this later if needed)app.add_middleware(CORSMiddleware,allow_origins=["*"],454 unmodified linesif __name__ == "__main__":import uvicorn# Hugging Face Spaces port is 7860uvicorn.run(app, host="0.0.0.0", port=7860)18 unmodified lines192021222324252629 unmodified lines5657585960616263646566676869454 unmodified lines52452552652718 unmodified linesawait pool.stop()app = FastAPI(title="Deepcastle Engine API", lifespan=lifespan)class ConnectionManager:def __init__(self):self.active_connections: Dict[str, List[WebSocket]] = {}async def connect(self, websocket: WebSocket, match_id: str):29 unmodified linestry:while True:data = await websocket.receive_text()await manager.broadcast(data, match_id, exclude=websocket)except WebSocketDisconnect:manager.disconnect(websocket, match_id)await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)except Exception:manager.disconnect(websocket, match_id)await manager.broadcast(json.dumps({"type": "opponent_disconnected"}), match_id)app.add_middleware(CORSMiddleware,allow_origins=["*"],454 unmodified linesif __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)
main.py-8