incognitolm commited on
Commit
26d2e1c
·
verified ·
1 Parent(s): 8c2a796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py CHANGED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ import uvicorn
3
+
4
+ app = FastAPI()
5
+
6
+ games = {}
7
+
8
+ @app.post("/api/createGame")
9
+ async def create_game(request: Request):
10
+ data = await request.json()
11
+ pin = data.get("pin")
12
+ player_name = data.get("playerName")
13
+
14
+ if not pin or not player_name:
15
+ raise HTTPException(status_code=400, detail="PIN and player name are required.")
16
+
17
+ if pin in games:
18
+ raise HTTPException(status_code=400, detail="Game already exists.")
19
+
20
+ games[pin] = {
21
+ "pin": pin,
22
+ "players": [player_name],
23
+ "gameStarted": False,
24
+ "turn": player_name,
25
+ "currentAction": None,
26
+ "currentChallenge": None,
27
+ "pendingCardLoss": None,
28
+ "votes": []
29
+ }
30
+
31
+ return {"success": True, "message": "Game created successfully!"}
32
+
33
+ @app.get("/api/gameData")
34
+ async def get_game_data(pin: str):
35
+ game = games.get(pin)
36
+ if not game:
37
+ raise HTTPException(status_code=404, detail="Game not found.")
38
+ return {"success": True, "game": game}
39
+
40
+ @app.put("/api/gameData")
41
+ async def update_game_data(request: Request):
42
+ data = await request.json()
43
+ pin = data.get("pin")
44
+
45
+ if not pin or pin not in games:
46
+ raise HTTPException(status_code=404, detail="Game not found.")
47
+
48
+ games[pin].update(data)
49
+ return {"success": True, "message": "Game updated successfully!"}
50
+
51
+ @app.post("/api/getGameStatus")
52
+ async def get_game_status(pin: str):
53
+ game = games.get(pin)
54
+ if not game:
55
+ raise HTTPException(status_code=404, detail="Game not found.")
56
+ return {"success": True, "game": game}
57
+
58
+ @app.post("/api/handleAction")
59
+ async def handle_action(request: Request):
60
+ data = await request.json()
61
+ pin = data.get("pin")
62
+ player = data.get("player")
63
+ action = data.get("action")
64
+ target = data.get("target")
65
+ response = data.get("response")
66
+ challenge = data.get("challenge")
67
+
68
+ if pin not in games:
69
+ raise HTTPException(status_code=404, detail="Game not found.")
70
+
71
+ game = games[pin]
72
+
73
+ if action == 'getStatus':
74
+ return {"success": True, "challenge": game.get("challenge", None)}
75
+
76
+ if action == 'steal':
77
+ game["challenge"] = {
78
+ "action": 'steal',
79
+ "challenger": player,
80
+ "target": target,
81
+ "challengeType": 'steal',
82
+ "status": 'pending'
83
+ }
84
+ return {"success": True, "message": f"Steal initiated by {player} targeting {target}. Awaiting response from {target}."}
85
+
86
+ if action == 'challengeResponse':
87
+ if not game.get("challenge"):
88
+ return {"success": False, "message": "No challenge pending."}
89
+
90
+ challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
91
+ target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
92
+
93
+ if response == 'accept':
94
+ coins_to_steal = min(target_player["coins"], 2)
95
+ target_player["coins"] -= coins_to_steal
96
+ challenger["coins"] += coins_to_steal
97
+ game["challenge"] = None
98
+ return {"success": True, "message": f"Steal accepted. {coins_to_steal} coins transferred from {target_player['name']} to {challenger['name']}."}
99
+ elif response == 'challenge':
100
+ if "Captain" in challenger["cards"]:
101
+ coins_to_steal = min(target_player["coins"], 2)
102
+ target_player["coins"] -= coins_to_steal
103
+ challenger["coins"] += coins_to_steal
104
+ game["challenge"] = None
105
+ return {"success": True, "message": f"Challenge failed. {target_player['name']} loses {coins_to_steal} coins to {challenger['name']}."}
106
+ else:
107
+ game["challenge"]["status"] = 'choose'
108
+ return {"success": True, "message": f"Challenge successful. {challenger['name']} must choose a card to lose.", "challenge": game["challenge"]}
109
+
110
+ if action == 'choose':
111
+ challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
112
+ if target in challenger["cards"]:
113
+ challenger["cards"].remove(target)
114
+ game["challenge"] = None
115
+ return {"success": True, "message": f"{challenger['name']} loses the {target} card."}
116
+ else:
117
+ return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
118
+
119
+ return {"success": True, "message": f"Action '{action}' processed for player {player}."}
120
+
121
+ @app.post("/api/joinGame")
122
+ async def join_game(request: Request):
123
+ data = await request.json()
124
+ pin = data.get("pin")
125
+ player_name = data.get("playerName")
126
+
127
+ game = games.get(pin)
128
+ if not game:
129
+ raise HTTPException(status_code=404, detail="Game not found.")
130
+
131
+ game["players"].append(player_name)
132
+ return {"success": True, "message": "Player joined successfully!"}
133
+
134
+ @app.put("/api/startGame")
135
+ async def start_game(request: Request):
136
+ data = await request.json()
137
+ pin = data.get("pin")
138
+
139
+ game = games.get(pin)
140
+ if not game:
141
+ raise HTTPException(status_code=404, detail="Game not found.")
142
+
143
+ if not game["players"]:
144
+ raise HTTPException(status_code=400, detail="No players in the game.")
145
+
146
+ game["players"] = [{"name": name, "coins": 2, "cards": generate_cards()} for name in game["players"]]
147
+ game["gameStarted"] = True
148
+ game["turn"] = game["players"][0]["name"]
149
+
150
+ return {"success": True, "message": "Game started successfully!"}
151
+
152
+ def generate_cards():
153
+ cards = ['Duke', 'Assassin', 'Captain', 'Ambassador', 'Contessa']
154
+ shuffled = sorted(cards, key=lambda x: 0.5 - random.random())
155
+ return [shuffled[0], shuffled[1]]
156
+
157
+ if __name__ == "__main__":
158
+ uvicorn.run(app, host="0.0.0.0", port=8000)