Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,7 @@ async def create_game(request: Request):
|
|
| 28 |
if pin in games:
|
| 29 |
raise HTTPException(status_code=400, detail="Game already exists.")
|
| 30 |
|
|
|
|
| 31 |
games[pin] = {
|
| 32 |
"pin": pin,
|
| 33 |
"players": [player_name],
|
|
@@ -94,6 +95,7 @@ async def handle_action(request: Request):
|
|
| 94 |
return {"success": True, "challenge": game.get("challenge", None)}
|
| 95 |
|
| 96 |
if action == 'steal':
|
|
|
|
| 97 |
if game["permissions"].get(player, {}).get("steal", True):
|
| 98 |
game["challenge"] = {
|
| 99 |
"action": 'steal',
|
|
@@ -109,6 +111,7 @@ async def handle_action(request: Request):
|
|
| 109 |
if not game.get("challenge"):
|
| 110 |
return {"success": False, "message": "No challenge pending."}
|
| 111 |
|
|
|
|
| 112 |
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 113 |
target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
|
| 114 |
|
|
@@ -141,12 +144,13 @@ async def handle_action(request: Request):
|
|
| 141 |
return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
|
| 142 |
|
| 143 |
if action == 'income':
|
| 144 |
-
#
|
| 145 |
player_found = False
|
| 146 |
for p in game["players"]:
|
| 147 |
if p["name"] == player:
|
| 148 |
player_found = True
|
| 149 |
-
|
|
|
|
| 150 |
raise HTTPException(status_code=403, detail="You already took coins, and cannot take income.")
|
| 151 |
p["coins"] += 1
|
| 152 |
game["permissions"][player]["gain"] = False
|
|
@@ -166,8 +170,14 @@ async def join_game(request: Request):
|
|
| 166 |
if not game:
|
| 167 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
game["players"].append(player_name)
|
|
|
|
| 170 |
game["permissions"][player_name] = {"steal": True, "gain": True}
|
|
|
|
| 171 |
return {"success": True, "message": "Player joined successfully!"}
|
| 172 |
|
| 173 |
@app.put("/api/startGame")
|
|
@@ -185,9 +195,10 @@ async def start_game(request: Request):
|
|
| 185 |
if len(game["players"]) == 0:
|
| 186 |
raise HTTPException(status_code=400, detail="No players in the game.")
|
| 187 |
|
| 188 |
-
# Initialize players with 2 coins and cards
|
| 189 |
game["players"] = [{"name": name, "coins": 2, "cards": generate_cards()} for name in game["players"]]
|
| 190 |
-
|
|
|
|
| 191 |
game["gameStarted"] = True
|
| 192 |
game["turn"] = game["players"][0]["name"]
|
| 193 |
|
|
|
|
| 28 |
if pin in games:
|
| 29 |
raise HTTPException(status_code=400, detail="Game already exists.")
|
| 30 |
|
| 31 |
+
# Initialize permissions as a dictionary with proper keys.
|
| 32 |
games[pin] = {
|
| 33 |
"pin": pin,
|
| 34 |
"players": [player_name],
|
|
|
|
| 95 |
return {"success": True, "challenge": game.get("challenge", None)}
|
| 96 |
|
| 97 |
if action == 'steal':
|
| 98 |
+
# Check if the player has permission to steal
|
| 99 |
if game["permissions"].get(player, {}).get("steal", True):
|
| 100 |
game["challenge"] = {
|
| 101 |
"action": 'steal',
|
|
|
|
| 111 |
if not game.get("challenge"):
|
| 112 |
return {"success": False, "message": "No challenge pending."}
|
| 113 |
|
| 114 |
+
# Assuming game["players"] now contains player dictionaries with a "name" key.
|
| 115 |
challenger = next(p for p in game["players"] if p["name"] == game["challenge"]["challenger"])
|
| 116 |
target_player = next(p for p in game["players"] if p["name"] == game["challenge"]["target"])
|
| 117 |
|
|
|
|
| 144 |
return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
|
| 145 |
|
| 146 |
if action == 'income':
|
| 147 |
+
# Find the player in the players list
|
| 148 |
player_found = False
|
| 149 |
for p in game["players"]:
|
| 150 |
if p["name"] == player:
|
| 151 |
player_found = True
|
| 152 |
+
# If gain permission is not available, then income has already been taken.
|
| 153 |
+
if not game["permissions"].get(player, {}).get("gain", True):
|
| 154 |
raise HTTPException(status_code=403, detail="You already took coins, and cannot take income.")
|
| 155 |
p["coins"] += 1
|
| 156 |
game["permissions"][player]["gain"] = False
|
|
|
|
| 170 |
if not game:
|
| 171 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 172 |
|
| 173 |
+
# Prevent duplicate player names.
|
| 174 |
+
if player_name in game["permissions"]:
|
| 175 |
+
raise HTTPException(status_code=400, detail="Player name already taken.")
|
| 176 |
+
|
| 177 |
game["players"].append(player_name)
|
| 178 |
+
# Properly initialize the new player's permissions.
|
| 179 |
game["permissions"][player_name] = {"steal": True, "gain": True}
|
| 180 |
+
|
| 181 |
return {"success": True, "message": "Player joined successfully!"}
|
| 182 |
|
| 183 |
@app.put("/api/startGame")
|
|
|
|
| 195 |
if len(game["players"]) == 0:
|
| 196 |
raise HTTPException(status_code=400, detail="No players in the game.")
|
| 197 |
|
| 198 |
+
# Initialize players with 2 coins and cards.
|
| 199 |
game["players"] = [{"name": name, "coins": 2, "cards": generate_cards()} for name in game["players"]]
|
| 200 |
+
# Reset permissions to a dictionary with proper keys for each player.
|
| 201 |
+
game["permissions"] = {player["name"]: {"steal": True, "gain": True} for player in game["players"]}
|
| 202 |
game["gameStarted"] = True
|
| 203 |
game["turn"] = game["players"][0]["name"]
|
| 204 |
|