Add a public current-round status endpoint
GET /rounds/current returns the active round's id/status, opened_at/ closes_at (derived from ROUND_DURATION_SECONDS), participant count and jackpot (participant_count * bet_amount_sats). No active round still returns bet_amount_sats so clients can render a fixed-entry hint either way. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
from datetime import timedelta, timezone
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy import func, select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.config import settings
|
||||||
|
from app.db.models import RoundParticipant
|
||||||
|
from app.db.session import get_session
|
||||||
|
from app.rounds.config import get_round_config
|
||||||
|
from app.rounds.service import get_active_round
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/rounds", tags=["rounds"])
|
||||||
|
|
||||||
|
|
||||||
|
class CurrentRoundResponse(BaseModel):
|
||||||
|
round_id: int | None = None
|
||||||
|
status: str | None = None
|
||||||
|
opened_at: str | None = None
|
||||||
|
closes_at: str | None = None
|
||||||
|
participant_count: int = 0
|
||||||
|
bet_amount_sats: int
|
||||||
|
jackpot_sats: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/current", response_model=CurrentRoundResponse)
|
||||||
|
async def current_round(session: AsyncSession = Depends(get_session)) -> CurrentRoundResponse:
|
||||||
|
config = await get_round_config(session)
|
||||||
|
round_ = await get_active_round(session)
|
||||||
|
if round_ is None:
|
||||||
|
await session.commit()
|
||||||
|
return CurrentRoundResponse(bet_amount_sats=config.bet_amount_sats)
|
||||||
|
|
||||||
|
participant_count = await session.scalar(
|
||||||
|
select(func.count()).select_from(RoundParticipant).where(RoundParticipant.round_id == round_.id)
|
||||||
|
) or 0
|
||||||
|
opened_at = round_.opened_at.replace(tzinfo=timezone.utc)
|
||||||
|
closes_at = opened_at + timedelta(seconds=settings.round_duration_seconds)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
return CurrentRoundResponse(
|
||||||
|
round_id=round_.id,
|
||||||
|
status=round_.status,
|
||||||
|
opened_at=opened_at.isoformat(),
|
||||||
|
closes_at=closes_at.isoformat(),
|
||||||
|
participant_count=participant_count,
|
||||||
|
bet_amount_sats=config.bet_amount_sats,
|
||||||
|
jackpot_sats=participant_count * config.bet_amount_sats,
|
||||||
|
)
|
||||||
@@ -16,6 +16,7 @@ import app.withdrawals.confirmation # noqa: F401 (registers the "withdrawal" c
|
|||||||
from app.api.routes.admin import router as admin_router
|
from app.api.routes.admin import router as admin_router
|
||||||
from app.api.routes.bets import router as bets_router
|
from app.api.routes.bets import router as bets_router
|
||||||
from app.api.routes.qr import router as qr_router
|
from app.api.routes.qr import router as qr_router
|
||||||
|
from app.api.routes.rounds import router as rounds_router
|
||||||
from app.api.routes.users import router as users_router
|
from app.api.routes.users import router as users_router
|
||||||
from app.api.routes.withdrawals import router as withdrawals_router
|
from app.api.routes.withdrawals import router as withdrawals_router
|
||||||
from app.auth.routes import router as auth_router
|
from app.auth.routes import router as auth_router
|
||||||
@@ -67,6 +68,7 @@ app.include_router(bets_router)
|
|||||||
app.include_router(withdrawals_router)
|
app.include_router(withdrawals_router)
|
||||||
app.include_router(admin_router)
|
app.include_router(admin_router)
|
||||||
app.include_router(qr_router)
|
app.include_router(qr_router)
|
||||||
|
app.include_router(rounds_router)
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(Exception)
|
@app.exception_handler(Exception)
|
||||||
|
|||||||
Reference in New Issue
Block a user