-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (77 loc) · 2.53 KB
/
main.py
File metadata and controls
109 lines (77 loc) · 2.53 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from storage import create_room, join_room, rooms
app = FastAPI(title="Raja-Mantri-Chor-Sipahi Backend")
from uuid import uuid4
app = FastAPI()
# ----------- Request Models -----------
class CreateRoomRequest(BaseModel):
name: str
class JoinRoomRequest(BaseModel):
room_id: str
name: str
# ----------- APIs -----------
@app.get("/")
def health():
return {"status": "Backend running"}
@app.post("/room/create")
def room_create(payload: CreateRoomRequest):
room_id, player_id = create_room(payload.name)
return {
"room_id": room_id,
"player_id": player_id,
"message": "Room created successfully"
}
rooms = {}
class JoinRoomRequest(BaseModel):
room_id: str
name: str
@app.post("/room/join")
def join_room(data: JoinRoomRequest):
if data.room_id not in rooms:
raise HTTPException(status_code=404, detail="Room not found")
if len(rooms[data.room_id]["players"]) >= 4:
raise HTTPException(status_code=400, detail="Room is full")
player_id = str(uuid4())
rooms[data.room_id]["players"][player_id] = data.name
return {
"room_id": data.room_id,
"player_id": player_id,
"name": data.name
}
@app.get("/room/players/{room_id}")
def room_players(room_id: str):
if room_id not in rooms:
raise HTTPException(status_code=404, detail="Room not found")
players = rooms[room_id]["players"]
return {
"room_id": room_id,
"players": [
{
"player_id": p["player_id"],
"name": p["name"]
} for p in players
]
}
from game_logic import assign_roles, get_player_role
@app.post("/room/assign/{room_id}")
def room_assign(room_id: str):
if room_id not in rooms:
raise HTTPException(status_code=404, detail="Room not found")
room = rooms[room_id]
success = assign_roles(room)
if not success:
raise HTTPException(
status_code=400,
detail="Cannot assign roles (need 4 players or already assigned)"
)
return {"message": "Roles assigned successfully"}
@app.get("/role/me/{room_id}/{player_id}")
def role_me(room_id: str, player_id: str):
if room_id not in rooms:
raise HTTPException(status_code=404, detail="Room not found")
room = rooms[room_id]
role = get_player_role(room, player_id)
if role is None:
raise HTTPException(status_code=404, detail="Player not found")
return {"role": role}