shortdeck1.0
This commit is contained in:
50
shortdeck_server/main.py
Normal file
50
shortdeck_server/main.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from .persistence import append_game_history
|
||||
from .arena_adapter import ArenaGame
|
||||
|
||||
app = FastAPI(title="shortdeck-server")
|
||||
GAME = ArenaGame()
|
||||
|
||||
class JoinRequest(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class JoinResponse(BaseModel):
|
||||
player_id: int
|
||||
name: str
|
||||
|
||||
|
||||
class ActionRequest(BaseModel):
|
||||
player_id: int
|
||||
action: str
|
||||
amount: int | None = None
|
||||
|
||||
|
||||
@app.post("/join", response_model=JoinResponse)
|
||||
def join(req: JoinRequest):
|
||||
try:
|
||||
player_id = GAME.join_game(req.name)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
return JoinResponse(player_id=player_id, name=req.name)
|
||||
|
||||
|
||||
@app.get("/get_game_state")
|
||||
def get_game_state(player_id):
|
||||
try:
|
||||
state = GAME.info(player_id)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
return state
|
||||
|
||||
|
||||
@app.post("/apply_action")
|
||||
def apply_action(req: ActionRequest):
|
||||
try:
|
||||
GAME.apply_action(req.player_id, req.action, req.amount)
|
||||
append_game_history(GAME.game_id, {"history": GAME.history})
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user