Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
import uvicorn
|
| 4 |
+
import json
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# WebSocket connections manager
|
| 10 |
+
class ConnectionManager:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.active_connections = {}
|
| 13 |
+
|
| 14 |
+
async def connect(self, websocket: WebSocket, player_id: str):
|
| 15 |
+
await websocket.accept()
|
| 16 |
+
self.active_connections[player_id] = websocket
|
| 17 |
+
|
| 18 |
+
def disconnect(self, player_id: str):
|
| 19 |
+
if player_id in self.active_connections:
|
| 20 |
+
del self.active_connections[player_id]
|
| 21 |
+
|
| 22 |
+
async def broadcast(self, message: dict):
|
| 23 |
+
for connection in self.active_connections.values():
|
| 24 |
+
try:
|
| 25 |
+
await connection.send_json(message)
|
| 26 |
+
except:
|
| 27 |
+
self.disconnect(connection)
|
| 28 |
+
|
| 29 |
+
manager = ConnectionManager()
|
| 30 |
+
|
| 31 |
+
# Game state
|
| 32 |
+
game_state = {
|
| 33 |
+
"players": {},
|
| 34 |
+
"fruits": [],
|
| 35 |
+
"obstacles": []
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Serve HTML
|
| 39 |
+
@app.get("/")
|
| 40 |
+
async def get():
|
| 41 |
+
with open("index.html", "r") as f:
|
| 42 |
+
return HTMLResponse(f.read())
|
| 43 |
+
|
| 44 |
+
# WebSocket endpoint
|
| 45 |
+
@app.websocket("/ws/{player_id}")
|
| 46 |
+
async def websocket_endpoint(websocket: WebSocket, player_id: str):
|
| 47 |
+
await manager.connect(websocket, player_id)
|
| 48 |
+
|
| 49 |
+
# Initialize player
|
| 50 |
+
game_state["players"][player_id] = {
|
| 51 |
+
"x": 0,
|
| 52 |
+
"y": 0,
|
| 53 |
+
"direction": "right",
|
| 54 |
+
"length": 3,
|
| 55 |
+
"score": 0
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
while True:
|
| 60 |
+
data = await websocket.receive_text()
|
| 61 |
+
message = json.loads(data)
|
| 62 |
+
|
| 63 |
+
# Update player direction
|
| 64 |
+
if message["type"] == "direction":
|
| 65 |
+
game_state["players"][player_id]["direction"] = message["direction"]
|
| 66 |
+
|
| 67 |
+
# Broadcast game state to all players
|
| 68 |
+
await manager.broadcast({
|
| 69 |
+
"type": "game_state",
|
| 70 |
+
"state": game_state
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
except WebSocketDisconnect:
|
| 74 |
+
manager.disconnect(player_id)
|
| 75 |
+
del game_state["players"][player_id]
|
| 76 |
+
await manager.broadcast({
|
| 77 |
+
"type": "player_disconnected",
|
| 78 |
+
"player_id": player_id
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|