Add a cooldown between rounds (ROUND_COOLDOWN_SECONDS)

open_new_round_if_needed now withholds opening the next round until
ROUND_COOLDOWN_SECONDS (default 30) have passed since the previous
round's closed_at, returning None in that window instead of a Round.
Without this, the next round opened within one scheduler tick (~5s) of
the previous payout confirming — not enough time for a player to
notice the round they were in actually resolved.

Callers updated: the scheduler treats None as "nothing to do this
tick", and place_bet raises a "try again shortly" BetError instead of
crashing on a None round.

Not in the original flowchart — a deliberate UX addition on top of it,
documented as such in CLAUDE.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 11:29:59 +02:00
co-authored by Claude Sonnet 5
parent abb3418669
commit f21ecbd4ee
8 changed files with 51 additions and 7 deletions
+2
View File
@@ -21,6 +21,8 @@ class BetError(Exception):
async def place_bet(session: AsyncSession, client: ElectrumClient, user: User) -> RoundParticipant:
round_ = await open_new_round_if_needed(session)
if round_ is None:
raise BetError("no round open right now, please try again shortly")
if round_.status != "open":
raise BetError("the current round is closing, please try again shortly")
+1
View File
@@ -18,6 +18,7 @@ class Settings(BaseSettings):
admin_token: str = ""
round_duration_seconds: int = 600
round_cooldown_seconds: int = 30
bet_amount_sats: int = 10 * 100_000_000
min_amount_sats: int = 1 * 100_000_000
+2
View File
@@ -50,6 +50,8 @@ class RoundScheduler:
async with self._session_factory() as session:
round_ = await open_new_round_if_needed(session)
await session.commit()
if round_ is None:
return # still in the cooldown window after the last round closed
round_id, status, opened_at = round_.id, round_.status, round_.opened_at
if status != "open":
+17 -5
View File
@@ -1,6 +1,9 @@
from datetime import datetime, timedelta, timezone
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.config import settings
from app.db.models import Round
_ACTIVE_STATUSES = ("open", "closing", "drawing", "paying_out")
@@ -13,14 +16,23 @@ async def get_active_round(session: AsyncSession) -> Round | None:
return await session.scalar(select(Round).where(Round.status.in_(_ACTIVE_STATUSES)).order_by(Round.id.desc()))
async def open_new_round_if_needed(session: AsyncSession) -> Round:
"""Returns the active round if one exists (whatever its status), otherwise
opens a fresh one. Callers that need to attach a bet must additionally check
the returned round's status == "open" — a round in closing/drawing/paying_out
isn't accepting new bets, but a new round can't open until it's done."""
async def open_new_round_if_needed(session: AsyncSession) -> Round | None:
"""Returns the active round if one exists (whatever its status). Otherwise
opens a fresh one, unless the last closed round's cooldown (ROUND_COOLDOWN_SECONDS)
hasn't elapsed yet — in which case returns None. Callers that need to attach a
bet must additionally check the returned round's status == "open" — a round in
closing/drawing/paying_out isn't accepting new bets, but a new round can't open
until it's done."""
active = await get_active_round(session)
if active is not None:
return active
last_closed = await session.scalar(select(Round).where(Round.status == "closed").order_by(Round.id.desc()))
if last_closed is not None and last_closed.closed_at is not None:
closed_at = last_closed.closed_at.replace(tzinfo=timezone.utc)
if datetime.now(timezone.utc) < closed_at + timedelta(seconds=settings.round_cooldown_seconds):
return None
round_ = Round(status="open")
session.add(round_)
await session.flush()