Add spin session functionality and update game mechanics
- Introduced a new `spin_sessions` table to track user spin activities. - Updated the API to include a new endpoint for spinning games, allowing users to place bets and specify spin counts. - Enhanced game information structure and added validation for bets and spin counts. - Implemented the `Forest Fortune` slot game mechanics, including payline evaluation and scatter functionality. - Updated dependencies in `pyproject.toml` for development and added `httpx` for HTTP requests. - Added unit tests for the new slot mechanics and API endpoints to ensure functionality and reliability.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from decimal import Decimal
|
||||
|
||||
@@ -8,41 +9,28 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import create_access_token, decode_access_token
|
||||
from app.models import Deposit, User
|
||||
from app.models import Deposit, SpinSession, User
|
||||
from app.schemas import (
|
||||
DepositRequest,
|
||||
DepositResponse,
|
||||
DevLoginRequest,
|
||||
GameStub,
|
||||
GameInfo,
|
||||
PaylineWinResponse,
|
||||
SessionSpinResponse,
|
||||
SpinOutcomeResponse,
|
||||
SpinRequest,
|
||||
TelegramAuthRequest,
|
||||
TokenResponse,
|
||||
UserResponse,
|
||||
WalletResponse,
|
||||
)
|
||||
from app.slots.registry import get_slot, list_slots
|
||||
|
||||
router = APIRouter()
|
||||
security = HTTPBearer()
|
||||
|
||||
ALLOWED_DEPOSIT_AMOUNTS = {Decimal("100"), Decimal("500"), Decimal("1000")}
|
||||
|
||||
GAMES: list[GameStub] = [
|
||||
GameStub(
|
||||
slug="raven-reels",
|
||||
title="Вороньи Барабаны",
|
||||
description="Слоты у врат Вырия. Скоро откроются.",
|
||||
),
|
||||
GameStub(
|
||||
slug="golden-gate",
|
||||
title="Золотые Врата",
|
||||
description="Портал удачи. Пока запечатан.",
|
||||
),
|
||||
GameStub(
|
||||
slug="forest-fortune",
|
||||
title="Лесная Удача",
|
||||
description="Древние символы ещё не проснулись.",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def user_to_response(user: User) -> UserResponse:
|
||||
return UserResponse(
|
||||
@@ -71,6 +59,11 @@ def get_current_user(
|
||||
return user
|
||||
|
||||
|
||||
def slot_to_info(slot) -> GameInfo:
|
||||
data = slot.info()
|
||||
return GameInfo(**data)
|
||||
|
||||
|
||||
@router.post("/auth/dev-login", response_model=TokenResponse)
|
||||
def dev_login(payload: DevLoginRequest, db: Session = Depends(get_db)) -> TokenResponse:
|
||||
user = db.execute(select(User).where(User.telegram_id == payload.telegram_id)).scalar_one_or_none()
|
||||
@@ -176,14 +169,105 @@ def deposit_stub(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/games", response_model=list[GameStub])
|
||||
def list_games() -> list[GameStub]:
|
||||
return GAMES
|
||||
@router.get("/games", response_model=list[GameInfo])
|
||||
def list_games() -> list[GameInfo]:
|
||||
return [slot_to_info(slot) for slot in list_slots()]
|
||||
|
||||
|
||||
@router.get("/games/{slug}", response_model=GameStub)
|
||||
def get_game(slug: str) -> GameStub:
|
||||
for game in GAMES:
|
||||
if game.slug == slug:
|
||||
return game
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Game not found")
|
||||
@router.get("/games/{slug}", response_model=GameInfo)
|
||||
def get_game(slug: str) -> GameInfo:
|
||||
slot = get_slot(slug)
|
||||
if slot is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Game not found")
|
||||
return slot_to_info(slot)
|
||||
|
||||
|
||||
@router.post("/games/{slug}/spin", response_model=SessionSpinResponse)
|
||||
def spin_game(
|
||||
slug: str,
|
||||
payload: SpinRequest,
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
) -> SessionSpinResponse:
|
||||
if user.agreement_accepted_at is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Accept user agreement first",
|
||||
)
|
||||
|
||||
slot = get_slot(slug)
|
||||
if slot is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Game not found")
|
||||
if slot.status != "available":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Game is not available yet",
|
||||
)
|
||||
|
||||
bet = payload.bet.quantize(Decimal("0.01"))
|
||||
spin_count = payload.spin_count
|
||||
try:
|
||||
slot.validate_bet(bet)
|
||||
slot.validate_spin_count(spin_count)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
|
||||
total_bet = (bet * spin_count).quantize(Decimal("0.01"))
|
||||
balance = user.balance or Decimal("0.00")
|
||||
if balance < total_bet:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Insufficient balance")
|
||||
|
||||
user.balance = balance - total_bet
|
||||
try:
|
||||
session_result = slot.play_session(bet, spin_count)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
|
||||
user.balance = (user.balance or Decimal("0.00")) + session_result.total_win
|
||||
|
||||
record = SpinSession(
|
||||
user_id=user.id,
|
||||
slug=slug,
|
||||
bet=bet,
|
||||
spin_count=spin_count,
|
||||
total_win=session_result.total_win,
|
||||
payload_json=json.dumps(session_result.to_payload(), ensure_ascii=False),
|
||||
)
|
||||
db.add(record)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
return SessionSpinResponse(
|
||||
slug=slug,
|
||||
bet=session_result.bet,
|
||||
spin_count=session_result.spin_count,
|
||||
total_bet=session_result.total_bet,
|
||||
total_win=session_result.total_win,
|
||||
max_level=session_result.max_level,
|
||||
trigger_count=session_result.trigger_count,
|
||||
balance=user.balance,
|
||||
spins=[
|
||||
SpinOutcomeResponse(
|
||||
index=s.index,
|
||||
grid=s.grid,
|
||||
line_wins=[
|
||||
PaylineWinResponse(
|
||||
line_index=w.line_index,
|
||||
symbol=w.symbol,
|
||||
count=w.count,
|
||||
amount=w.amount,
|
||||
path=w.path,
|
||||
)
|
||||
for w in s.line_wins
|
||||
],
|
||||
scatter_count=s.scatter_count,
|
||||
scatter_win=s.scatter_win,
|
||||
total_win=s.total_win,
|
||||
level=s.level,
|
||||
multiplier=s.multiplier,
|
||||
meta_triggered=s.meta_triggered,
|
||||
)
|
||||
for s in session_result.spins
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user