Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -95,7 +95,7 @@ async def handle_action(request: Request):
|
|
| 95 |
return {"success": True, "challenge": game.get("challenge", None)}
|
| 96 |
|
| 97 |
if action == 'steal':
|
| 98 |
-
#
|
| 99 |
if game["permissions"].get(player, {}).get("steal", True):
|
| 100 |
game["challenge"] = {
|
| 101 |
"action": 'steal',
|
|
@@ -108,21 +108,15 @@ async def handle_action(request: Request):
|
|
| 108 |
return {"success": True, "message": f"Steal initiated by {player} targeting {target}. Awaiting response from {target}."}
|
| 109 |
else:
|
| 110 |
return {"success": False, "message": "You can only steal once per turn."}
|
| 111 |
-
|
| 112 |
if action == 'coup':
|
| 113 |
-
#
|
| 114 |
player_data = next((p for p in game["players"] if p["name"] == player), None)
|
| 115 |
-
|
| 116 |
if not player_data:
|
| 117 |
return {"success": False, "message": "Player not found."}
|
| 118 |
-
|
| 119 |
if player_data["coins"] < 7:
|
| 120 |
return {"success": False, "message": "You don't have enough coins to coup another player."}
|
| 121 |
-
|
| 122 |
-
# Deduct 7 coins for the coup
|
| 123 |
player_data["coins"] -= 7
|
| 124 |
-
|
| 125 |
-
# Initialize the coup challenge
|
| 126 |
game["challenge"] = {
|
| 127 |
"action": 'coup',
|
| 128 |
"challenger": player,
|
|
@@ -130,16 +124,14 @@ async def handle_action(request: Request):
|
|
| 130 |
"challengeType": 'coup',
|
| 131 |
"status": 'choose'
|
| 132 |
}
|
| 133 |
-
|
| 134 |
return {"success": True, "message": f"Coup initiated by {player} targeting {target}."}
|
| 135 |
|
| 136 |
if action == 'challengeResponse':
|
|
|
|
| 137 |
if not game.get("challenge"):
|
| 138 |
return {"success": False, "message": "No challenge pending."}
|
| 139 |
-
|
| 140 |
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 141 |
target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
|
| 142 |
-
|
| 143 |
if response == 'accept':
|
| 144 |
coins_to_steal = min(target_player["coins"], 2)
|
| 145 |
target_player["coins"] -= coins_to_steal
|
|
@@ -181,6 +173,52 @@ async def handle_action(request: Request):
|
|
| 181 |
if not player_found:
|
| 182 |
raise HTTPException(status_code=404, detail="Player not found in the game.")
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
return {"success": True, "message": f"Action '{action}' processed for player {player}."}
|
| 185 |
|
| 186 |
@app.post("/api/joinGame")
|
|
|
|
| 95 |
return {"success": True, "challenge": game.get("challenge", None)}
|
| 96 |
|
| 97 |
if action == 'steal':
|
| 98 |
+
# Existing steal code…
|
| 99 |
if game["permissions"].get(player, {}).get("steal", True):
|
| 100 |
game["challenge"] = {
|
| 101 |
"action": 'steal',
|
|
|
|
| 108 |
return {"success": True, "message": f"Steal initiated by {player} targeting {target}. Awaiting response from {target}."}
|
| 109 |
else:
|
| 110 |
return {"success": False, "message": "You can only steal once per turn."}
|
| 111 |
+
|
| 112 |
if action == 'coup':
|
| 113 |
+
# Existing coup code…
|
| 114 |
player_data = next((p for p in game["players"] if p["name"] == player), None)
|
|
|
|
| 115 |
if not player_data:
|
| 116 |
return {"success": False, "message": "Player not found."}
|
|
|
|
| 117 |
if player_data["coins"] < 7:
|
| 118 |
return {"success": False, "message": "You don't have enough coins to coup another player."}
|
|
|
|
|
|
|
| 119 |
player_data["coins"] -= 7
|
|
|
|
|
|
|
| 120 |
game["challenge"] = {
|
| 121 |
"action": 'coup',
|
| 122 |
"challenger": player,
|
|
|
|
| 124 |
"challengeType": 'coup',
|
| 125 |
"status": 'choose'
|
| 126 |
}
|
|
|
|
| 127 |
return {"success": True, "message": f"Coup initiated by {player} targeting {target}."}
|
| 128 |
|
| 129 |
if action == 'challengeResponse':
|
| 130 |
+
# Existing challengeResponse code…
|
| 131 |
if not game.get("challenge"):
|
| 132 |
return {"success": False, "message": "No challenge pending."}
|
|
|
|
| 133 |
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 134 |
target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
|
|
|
|
| 135 |
if response == 'accept':
|
| 136 |
coins_to_steal = min(target_player["coins"], 2)
|
| 137 |
target_player["coins"] -= coins_to_steal
|
|
|
|
| 173 |
if not player_found:
|
| 174 |
raise HTTPException(status_code=404, detail="Player not found in the game.")
|
| 175 |
|
| 176 |
+
# --- NEW: Foreign Aid ---
|
| 177 |
+
if action == 'foreignAid':
|
| 178 |
+
# Ensure it is the player's turn and they haven't already performed a gain action.
|
| 179 |
+
if game["turn"] != player:
|
| 180 |
+
return {"success": False, "message": "Not your turn."}
|
| 181 |
+
if not game["permissions"].get(player, {}).get("gain", True):
|
| 182 |
+
return {"success": False, "message": "You can only perform a gain action once per turn."}
|
| 183 |
+
# Create a pending foreign aid challenge.
|
| 184 |
+
game["challenge"] = {
|
| 185 |
+
"action": "foreignAid",
|
| 186 |
+
"challenger": player,
|
| 187 |
+
"challengeType": "foreignAid",
|
| 188 |
+
"responses": {},
|
| 189 |
+
"status": "pending"
|
| 190 |
+
}
|
| 191 |
+
game["permissions"][player]["gain"] = False
|
| 192 |
+
return {"success": True, "message": f"Foreign Aid initiated by {player}. Waiting for opponents to respond."}
|
| 193 |
+
|
| 194 |
+
if action == 'foreignAidResponse':
|
| 195 |
+
# This branch handles responses from opponents for the foreign aid action.
|
| 196 |
+
if not game.get("challenge") or game["challenge"].get("challengeType") != "foreignAid":
|
| 197 |
+
return {"success": False, "message": "No foreign aid action pending."}
|
| 198 |
+
# Only opponents may respond.
|
| 199 |
+
if player == game["challenge"]["challenger"]:
|
| 200 |
+
return {"success": False, "message": "Challenger cannot respond to their own action."}
|
| 201 |
+
# Record the response.
|
| 202 |
+
game["challenge"]["responses"][player] = response
|
| 203 |
+
# If any opponent chooses to block, immediately cancel the foreign aid.
|
| 204 |
+
if response == "block":
|
| 205 |
+
game["challenge"]["status"] = "blocked"
|
| 206 |
+
challenger = game["challenge"]["challenger"]
|
| 207 |
+
game["challenge"] = None
|
| 208 |
+
return {"success": True, "message": f"Foreign Aid blocked by {player}."}
|
| 209 |
+
else:
|
| 210 |
+
# Check if all opponents have responded (all players except the challenger).
|
| 211 |
+
total_opponents = len(game["players"]) - 1
|
| 212 |
+
if len(game["challenge"]["responses"]) == total_opponents:
|
| 213 |
+
# No opponent blocked; grant the acting player +2 coins.
|
| 214 |
+
challenger_player = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 215 |
+
challenger_player["coins"] += 2
|
| 216 |
+
game["challenge"] = None
|
| 217 |
+
return {"success": True, "message": f"Foreign Aid accepted. {challenger_player['name']} gains 2 coins."}
|
| 218 |
+
else:
|
| 219 |
+
return {"success": True, "message": f"{player} allowed Foreign Aid. Awaiting other responses."}
|
| 220 |
+
|
| 221 |
+
# Default response.
|
| 222 |
return {"success": True, "message": f"Action '{action}' processed for player {player}."}
|
| 223 |
|
| 224 |
@app.post("/api/joinGame")
|