Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,69 @@ async def get_game_status(pin: str):
|
|
| 74 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 75 |
return {"success": True, "game": game}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@app.post("/api/joinGame")
|
| 78 |
async def join_game(request: Request):
|
| 79 |
data = await request.json()
|
|
|
|
| 74 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 75 |
return {"success": True, "game": game}
|
| 76 |
|
| 77 |
+
@app.post("/api/handleAction")
|
| 78 |
+
async def handle_action(request: Request):
|
| 79 |
+
data = await request.json()
|
| 80 |
+
pin = data.get("pin")
|
| 81 |
+
player = data.get("player")
|
| 82 |
+
action = data.get("action")
|
| 83 |
+
target = data.get("target")
|
| 84 |
+
response = data.get("response")
|
| 85 |
+
challenge = data.get("challenge")
|
| 86 |
+
|
| 87 |
+
if pin not in games:
|
| 88 |
+
raise HTTPException(status_code=404, detail="Game not found.")
|
| 89 |
+
|
| 90 |
+
game = games[pin]
|
| 91 |
+
|
| 92 |
+
if action == 'getStatus':
|
| 93 |
+
return {"success": True, "challenge": game.get("challenge", None)}
|
| 94 |
+
|
| 95 |
+
if action == 'steal':
|
| 96 |
+
game["challenge"] = {
|
| 97 |
+
"action": 'steal',
|
| 98 |
+
"challenger": player,
|
| 99 |
+
"target": target,
|
| 100 |
+
"challengeType": 'steal',
|
| 101 |
+
"status": 'pending'
|
| 102 |
+
}
|
| 103 |
+
return {"success": True, "message": f"Steal initiated by {player} targeting {target}. Awaiting response from {target}."}
|
| 104 |
+
|
| 105 |
+
if action == 'challengeResponse':
|
| 106 |
+
if not game.get("challenge"):
|
| 107 |
+
return {"success": False, "message": "No challenge pending."}
|
| 108 |
+
|
| 109 |
+
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 110 |
+
target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
|
| 111 |
+
|
| 112 |
+
if response == 'accept':
|
| 113 |
+
coins_to_steal = min(target_player["coins"], 2)
|
| 114 |
+
target_player["coins"] -= coins_to_steal
|
| 115 |
+
challenger["coins"] += coins_to_steal
|
| 116 |
+
game["challenge"] = None
|
| 117 |
+
return {"success": True, "message": f"Steal accepted. {coins_to_steal} coins transferred from {target_player['name']} to {challenger['name']}."}
|
| 118 |
+
elif response == 'challenge':
|
| 119 |
+
if "Captain" in challenger["cards"]:
|
| 120 |
+
coins_to_steal = min(target_player["coins"], 2)
|
| 121 |
+
target_player["coins"] -= coins_to_steal
|
| 122 |
+
challenger["coins"] += coins_to_steal
|
| 123 |
+
game["challenge"] = None
|
| 124 |
+
return {"success": True, "message": f"Challenge failed. {target_player['name']} loses {coins_to_steal} coins to {challenger['name']}."}
|
| 125 |
+
else:
|
| 126 |
+
game["challenge"]["status"] = 'choose'
|
| 127 |
+
return {"success": True, "message": f"Challenge successful. {challenger['name']} must choose a card to lose.", "challenge": game["challenge"]}
|
| 128 |
+
|
| 129 |
+
if action == 'choose':
|
| 130 |
+
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 131 |
+
if target in challenger["cards"]:
|
| 132 |
+
challenger["cards"].remove(target)
|
| 133 |
+
game["challenge"] = None
|
| 134 |
+
return {"success": True, "message": f"{challenger['name']} loses the {target} card."}
|
| 135 |
+
else:
|
| 136 |
+
return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
|
| 137 |
+
|
| 138 |
+
return {"success": True, "message": f"Action '{action}' processed for player {player}."}
|
| 139 |
+
|
| 140 |
@app.post("/api/joinGame")
|
| 141 |
async def join_game(request: Request):
|
| 142 |
data = await request.json()
|