Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI, Request, HTTPException
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import uvicorn
|
| 4 |
import random
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -396,8 +397,6 @@ async def handle_action(request: Request):
|
|
| 396 |
return {"success": True, "message": f"Action '{action}' processed for player {player}."}
|
| 397 |
|
| 398 |
|
| 399 |
-
import logging
|
| 400 |
-
|
| 401 |
@app.post("/api/joinGame")
|
| 402 |
async def join_game(request: Request):
|
| 403 |
data = await request.json()
|
|
@@ -408,6 +407,10 @@ async def join_game(request: Request):
|
|
| 408 |
if not game:
|
| 409 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 410 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
formatted_name = capitalize_name(player_name)
|
| 412 |
|
| 413 |
existing_names = [name.lower() for name in game["permissions"].keys()]
|
|
@@ -415,7 +418,6 @@ async def join_game(request: Request):
|
|
| 415 |
raise HTTPException(status_code=400, detail="Player name already taken.")
|
| 416 |
|
| 417 |
game["players"].append(formatted_name)
|
| 418 |
-
|
| 419 |
game["permissions"][formatted_name] = {"steal": True, "gain": True}
|
| 420 |
|
| 421 |
return {"success": True, "message": "Player joined successfully!"}
|
|
@@ -444,6 +446,26 @@ async def start_game(request: Request):
|
|
| 444 |
|
| 445 |
return {"success": True, "message": "Game started successfully!"}
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
def generate_cards(numofcards):
|
| 448 |
cards = ['Duke', 'Assassin', 'Captain', 'Ambassador', 'Contessa']
|
| 449 |
shuffled = sorted(cards, key=lambda x: 0.5 - random.random())
|
|
@@ -486,12 +508,12 @@ def capitalize_name(name):
|
|
| 486 |
capitalize_next = True # Capitalize first letter
|
| 487 |
|
| 488 |
for char in name:
|
| 489 |
-
if capitalize_next and char.isalpha():
|
| 490 |
result += char.upper()
|
| 491 |
-
capitalize_next = False
|
| 492 |
else:
|
| 493 |
result += char
|
| 494 |
-
if char == " ":
|
| 495 |
-
capitalize_next = True
|
| 496 |
|
| 497 |
return result
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import uvicorn
|
| 4 |
import random
|
| 5 |
+
import logging
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 397 |
return {"success": True, "message": f"Action '{action}' processed for player {player}."}
|
| 398 |
|
| 399 |
|
|
|
|
|
|
|
| 400 |
@app.post("/api/joinGame")
|
| 401 |
async def join_game(request: Request):
|
| 402 |
data = await request.json()
|
|
|
|
| 407 |
if not game:
|
| 408 |
raise HTTPException(status_code=404, detail="Game not found.")
|
| 409 |
|
| 410 |
+
# Prevent joining if game has already started.
|
| 411 |
+
if game.get("gameStarted"):
|
| 412 |
+
raise HTTPException(status_code=400, detail="Cannot join a game that has already started.")
|
| 413 |
+
|
| 414 |
formatted_name = capitalize_name(player_name)
|
| 415 |
|
| 416 |
existing_names = [name.lower() for name in game["permissions"].keys()]
|
|
|
|
| 418 |
raise HTTPException(status_code=400, detail="Player name already taken.")
|
| 419 |
|
| 420 |
game["players"].append(formatted_name)
|
|
|
|
| 421 |
game["permissions"][formatted_name] = {"steal": True, "gain": True}
|
| 422 |
|
| 423 |
return {"success": True, "message": "Player joined successfully!"}
|
|
|
|
| 446 |
|
| 447 |
return {"success": True, "message": "Game started successfully!"}
|
| 448 |
|
| 449 |
+
# New endpoint for deleting a game
|
| 450 |
+
@app.post("/api/deleteGame")
|
| 451 |
+
async def delete_game(request: Request):
|
| 452 |
+
data = await request.json()
|
| 453 |
+
pin = data.get("pin")
|
| 454 |
+
|
| 455 |
+
if not pin:
|
| 456 |
+
raise HTTPException(status_code=400, detail="PIN is required.")
|
| 457 |
+
|
| 458 |
+
game = games.get(pin)
|
| 459 |
+
if not game:
|
| 460 |
+
raise HTTPException(status_code=404, detail="Game not found.")
|
| 461 |
+
|
| 462 |
+
# Only allow deletion if there is exactly one player in the game.
|
| 463 |
+
if len(game["players"]) != 1:
|
| 464 |
+
raise HTTPException(status_code=400, detail="Game cannot be deleted unless there is only one player left.")
|
| 465 |
+
|
| 466 |
+
del games[pin]
|
| 467 |
+
return {"success": True, "message": "Game deleted successfully!"}
|
| 468 |
+
|
| 469 |
def generate_cards(numofcards):
|
| 470 |
cards = ['Duke', 'Assassin', 'Captain', 'Ambassador', 'Contessa']
|
| 471 |
shuffled = sorted(cards, key=lambda x: 0.5 - random.random())
|
|
|
|
| 508 |
capitalize_next = True # Capitalize first letter
|
| 509 |
|
| 510 |
for char in name:
|
| 511 |
+
if capitalize_next and char.isalpha():
|
| 512 |
result += char.upper()
|
| 513 |
+
capitalize_next = False
|
| 514 |
else:
|
| 515 |
result += char
|
| 516 |
+
if char == " ":
|
| 517 |
+
capitalize_next = True
|
| 518 |
|
| 519 |
return result
|