-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
208 lines (164 loc) · 6.31 KB
/
server.py
File metadata and controls
208 lines (164 loc) · 6.31 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import asyncio
import base64
import json
import os
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from game_manager import GameManager, GameSession, list_scenarios
IS_PRODUCTION = os.environ.get("IS_PRODUCTION", "").lower() == "true"
PORT = 3000 if IS_PRODUCTION else int(os.environ.get("PORT", "3000"))
DIST_DIR = os.path.join(os.path.dirname(__file__), "dist")
TICK_RATE = 1 / 35 # 35 fps (original Doom tic rate)
# Binary message tags (first byte)
TAG_VIDEO = b"\x01"
TAG_AUDIO = b"\x02"
app = FastAPI()
manager = GameManager()
# --- Pydantic models ---
class StartRequest(BaseModel):
scenario: str = "basic.cfg"
class ActionRequest(BaseModel):
actions: list[float]
class ResetRequest(BaseModel):
scenario: str | None = None
# --- WebSocket for browser play ---
@app.websocket("/ws")
async def websocket_play(ws: WebSocket):
await ws.accept()
# Create a dedicated game session for this WebSocket connection
session_id = await asyncio.to_thread(manager.create_session)
session = manager.get_session(session_id)
if not session:
await ws.close()
return
# Send initial scenario list
scenarios = await asyncio.to_thread(list_scenarios)
await ws.send_text(json.dumps({
"type": "scenarios",
"scenarios": [s["name"] for s in scenarios],
}))
# Background task: game loop streaming frames
stop_event = asyncio.Event()
paused = True # Start paused until player picks a level
async def game_loop():
nonlocal paused
loop = asyncio.get_event_loop()
next_tick = loop.time()
while not stop_event.is_set():
if paused:
await asyncio.sleep(TICK_RATE)
next_tick = loop.time()
continue
# Separate game errors (skip frame) from WS errors (stop loop)
try:
jpeg, state, audio = await asyncio.to_thread(session.tick)
except Exception:
await asyncio.sleep(TICK_RATE)
next_tick = loop.time()
continue
try:
await ws.send_bytes(TAG_VIDEO + jpeg)
await ws.send_text(json.dumps({"type": "state", **state}))
if audio:
await ws.send_bytes(TAG_AUDIO + audio)
except Exception:
break
next_tick += TICK_RATE
sleep_for = next_tick - loop.time()
if sleep_for > 0:
await asyncio.sleep(sleep_for)
else:
next_tick = loop.time()
loop_task = asyncio.create_task(game_loop())
try:
while True:
data = await ws.receive_text()
msg = json.loads(data)
msg_type = msg.get("type")
if msg_type == "keydown":
session.key_down(msg.get("key", ""))
elif msg_type == "keyup":
session.key_up(msg.get("key", ""))
elif msg_type == "mouse":
session.mouse_move(msg.get("dx", 0), msg.get("dy", 0))
elif msg_type == "mousedown":
session.mouse_button_down()
elif msg_type == "mouseup":
session.mouse_button_up()
elif msg_type == "reset":
await asyncio.to_thread(session.reset)
elif msg_type == "scenario":
scenario_name = msg.get("name", "basic.cfg")
start_map = msg.get("map")
skill = msg.get("skill")
await asyncio.to_thread(session.reset, scenario_name, start_map, skill)
elif msg_type == "next_level":
await asyncio.to_thread(session.next_level)
elif msg_type == "pause":
paused = True
elif msg_type == "unpause":
paused = False
except WebSocketDisconnect:
pass
except Exception:
pass
finally:
stop_event.set()
loop_task.cancel()
try:
await loop_task
except asyncio.CancelledError:
pass
manager.destroy_session(session_id)
# --- REST API for AI agents ---
@app.get("/api/game/scenarios")
async def get_scenarios():
scenarios = await asyncio.to_thread(list_scenarios)
return {"scenarios": [s["name"] for s in scenarios]}
@app.post("/api/game/start")
async def start_game(req: StartRequest):
session_id = await asyncio.to_thread(manager.create_session, req.scenario)
return {"session_id": session_id}
@app.post("/api/game/{session_id}/action")
async def game_action(session_id: str, req: ActionRequest):
session = manager.get_session(session_id)
if not session:
return JSONResponse(status_code=404, content={"error": "Session not found"})
jpeg, state, reward = await asyncio.to_thread(session.step, req.actions)
frame_b64 = base64.b64encode(jpeg).decode("ascii")
return {
"frame": frame_b64,
"state": state,
"reward": reward,
}
@app.get("/api/game/{session_id}/state")
async def game_state(session_id: str):
session = manager.get_session(session_id)
if not session:
return JSONResponse(status_code=404, content={"error": "Session not found"})
state = await asyncio.to_thread(session._get_state_dict)
return {"state": state}
@app.post("/api/game/{session_id}/reset")
async def game_reset(session_id: str, req: ResetRequest):
session = manager.get_session(session_id)
if not session:
return JSONResponse(status_code=404, content={"error": "Session not found"})
await asyncio.to_thread(session.reset, req.scenario)
return {"status": "ok"}
@app.delete("/api/game/{session_id}")
async def game_destroy(session_id: str):
destroyed = manager.destroy_session(session_id)
if not destroyed:
return JSONResponse(status_code=404, content={"error": "Session not found"})
return {"status": "ok"}
# --- Static file serving (production) ---
if IS_PRODUCTION:
if not os.path.isdir(DIST_DIR):
raise RuntimeError(f"Production mode but dist/ not found: {DIST_DIR}")
app.mount("/", StaticFiles(directory=DIST_DIR, html=True), name="static")
# --- Entry point ---
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=PORT)