Fable will fix it! Pt 2
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, Request, Form, Depends, HTTPException, status, APIRouter
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi import FastAPI, Form, Depends, HTTPException, APIRouter
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlmodel import Session, select
|
||||
from sqlmodel import Session
|
||||
import uvicorn
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from . import crud
|
||||
from .database import create_db_and_tables, get_session
|
||||
from .models import GameEvent
|
||||
|
||||
EVENT_PAGE_SIZE = 50
|
||||
|
||||
BASE_DIR = Path(__file__).parent
|
||||
|
||||
app = FastAPI(title="Rats with Gats Remote Play API")
|
||||
api = APIRouter()
|
||||
|
||||
# Register startup event to create tables
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
create_db_and_tables()
|
||||
yield
|
||||
|
||||
app = FastAPI(title="Rats with Gats Remote Play API", lifespan=lifespan)
|
||||
api = APIRouter()
|
||||
|
||||
# --- API Core Route Handlers ---
|
||||
|
||||
@@ -29,7 +30,8 @@ def create_game_route(
|
||||
db: Session = Depends(get_session)
|
||||
):
|
||||
game = crud.create_game(db, crew_name=crew_name.strip())
|
||||
return {"id": game.id}
|
||||
# admin_key is only revealed here, to the creator; the state endpoint hides it
|
||||
return {"id": game.id, "admin_key": game.admin_key}
|
||||
|
||||
@api.post("/game/{game_id}/join")
|
||||
def join_game_submit(
|
||||
@@ -53,16 +55,36 @@ def get_game_state(game_id: str, player_id: str, db: Session = Depends(get_sessi
|
||||
if not game or not player:
|
||||
raise HTTPException(status_code=404, detail="Game or Player not found")
|
||||
|
||||
events = db.exec(select(GameEvent).where(GameEvent.game_id == game_id).order_by(GameEvent.timestamp.asc())).all()
|
||||
|
||||
events, events_have_more = crud.get_events_page(db, game_id, limit=EVENT_PAGE_SIZE)
|
||||
|
||||
return {
|
||||
"game": game.model_dump(),
|
||||
"game": game.model_dump(exclude={"admin_key"}),
|
||||
"player": player.model_dump(),
|
||||
"players": [p.model_dump() for p in game.players],
|
||||
"obstacles": [o.model_dump() for o in game.obstacles],
|
||||
"challenges": [c.model_dump() for c in game.challenges],
|
||||
"votes": [v.model_dump() for v in game.votes],
|
||||
"events": [e.model_dump() for e in events],
|
||||
"events_have_more": events_have_more,
|
||||
}
|
||||
|
||||
@api.get("/game/{game_id}/events")
|
||||
def get_game_events(
|
||||
game_id: str,
|
||||
before: Optional[float] = None,
|
||||
limit: int = EVENT_PAGE_SIZE,
|
||||
db: Session = Depends(get_session),
|
||||
):
|
||||
"""Pages back through the event log: returns the newest `limit` events older than `before`."""
|
||||
game = crud.get_game(db, game_id)
|
||||
if not game:
|
||||
raise HTTPException(status_code=404, detail="Game not found")
|
||||
|
||||
limit = max(1, min(limit, 200))
|
||||
events, have_more = crud.get_events_page(db, game_id, before=before, limit=limit)
|
||||
return {
|
||||
"events": [e.model_dump() for e in events],
|
||||
"have_more": have_more,
|
||||
}
|
||||
|
||||
# --- Register Modular Phase Routers ---
|
||||
|
||||
Reference in New Issue
Block a user