incognitolm commited on
Commit
3d389e7
·
verified ·
1 Parent(s): 509331b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -37,7 +37,7 @@ async def create_game(request: Request):
37
  "currentChallenge": None,
38
  "pendingCardLoss": None,
39
  "votes": [],
40
- "permissions": {player_name: True} # using a dictionary for permissions
41
  }
42
 
43
  return {"success": True, "message": "Game created successfully!"}
@@ -137,17 +137,18 @@ async def handle_action(request: Request):
137
  return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
138
 
139
  if action == 'income':
140
- # Find the player's dictionary and update coins if game has started
141
  player_found = False
142
  for p in game["players"]:
143
  if p["name"] == player:
144
- p["coins"] += 1
145
  player_found = True
146
- permissions[player] = False
147
- break
 
 
 
148
  if not player_found:
149
  raise HTTPException(status_code=404, detail="Player not found in the game.")
150
- return {"success": True, "message": f"Player {player} now has {p['coins']} coins."}
151
 
152
  return {"success": True, "message": f"Action '{action}' processed for player {player}."}
153
 
@@ -162,7 +163,7 @@ async def join_game(request: Request):
162
  raise HTTPException(status_code=404, detail="Game not found.")
163
 
164
  game["players"].append(player_name)
165
- # Add the new player's permission as 1 in the dictionary
166
  game["permissions"][player_name] = True
167
  return {"success": True, "message": "Player joined successfully!"}
168
 
@@ -181,7 +182,9 @@ async def start_game(request: Request):
181
  if len(game["players"]) == 0:
182
  raise HTTPException(status_code=400, detail="No players in the game.")
183
 
 
184
  game["players"] = [{"name": name, "coins": 2, "cards": generate_cards()} for name in game["players"]]
 
185
  game["gameStarted"] = True
186
  game["turn"] = game["players"][0]["name"]
187
 
 
37
  "currentChallenge": None,
38
  "pendingCardLoss": None,
39
  "votes": [],
40
+ "permissions": {player_name: True} # default permission True for the creator
41
  }
42
 
43
  return {"success": True, "message": "Game created successfully!"}
 
137
  return {"success": False, "message": f"Card {target} not found in {challenger['name']}'s hand."}
138
 
139
  if action == 'income':
140
+ # Check if the game has started and update coins only if player's permission is True
141
  player_found = False
142
  for p in game["players"]:
143
  if p["name"] == player:
 
144
  player_found = True
145
+ if not game["permissions"].get(player, False):
146
+ raise HTTPException(status_code=403, detail="You already took coins, and cannot take income.")
147
+ p["coins"] += 1
148
+ game["permissions"][player] = False # set permission to false after taking income
149
+ return {"success": True, "message": f"Player {player} now has {p['coins']} coins."}
150
  if not player_found:
151
  raise HTTPException(status_code=404, detail="Player not found in the game.")
 
152
 
153
  return {"success": True, "message": f"Action '{action}' processed for player {player}."}
154
 
 
163
  raise HTTPException(status_code=404, detail="Game not found.")
164
 
165
  game["players"].append(player_name)
166
+ # Add the new player's permission as True in the dictionary
167
  game["permissions"][player_name] = True
168
  return {"success": True, "message": "Player joined successfully!"}
169
 
 
182
  if len(game["players"]) == 0:
183
  raise HTTPException(status_code=400, detail="No players in the game.")
184
 
185
+ # Initialize players with 2 coins and cards, and reset permissions to True for all
186
  game["players"] = [{"name": name, "coins": 2, "cards": generate_cards()} for name in game["players"]]
187
+ game["permissions"] = {player["name"]: True for player in game["players"]}
188
  game["gameStarted"] = True
189
  game["turn"] = game["players"][0]["name"]
190