baoxiang/backend/app/schemas/game.py

82 lines
1.9 KiB
Python
Raw Normal View History

2025-12-16 18:06:50 +08:00
import json
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime
class ChestBase(BaseModel):
title: str = Field(..., min_length=1, max_length=200)
option_a: str = Field(..., min_length=1, max_length=100)
option_b: str = Field(..., min_length=1, max_length=100)
countdown_seconds: int = Field(default=300, ge=10, le=3600) # 10秒到1小时
class ChestCreate(ChestBase):
pass
class ChestResponse(ChestBase):
id: int
streamer_id: int
status: int
pool_a: int
pool_b: int
total_bets: int
locked_at: Optional[datetime] = None
settled_at: Optional[datetime] = None
result: Optional[str] = None
created_at: datetime
time_remaining: int = 0 # 改为非可选字段默认值为0
# 计算属性
@property
def total_pool(self) -> int:
return self.pool_a + self.pool_b
@property
def odds_a(self) -> float:
"""计算A边赔率 (1 : X)"""
if self.pool_a == 0:
return 0.0
distributable = self.total_pool * 0.9
return round(distributable / self.pool_a, 2)
@property
def odds_b(self) -> float:
"""计算B边赔率 (1 : X)"""
if self.pool_b == 0:
return 0.0
distributable = self.total_pool * 0.9
return round(distributable / self.pool_b, 2)
class Config:
from_attributes = True
class BetCreate(BaseModel):
chest_id: int
option: str = Field(..., pattern="^[AB]$")
amount: int = Field(..., gt=0)
class BetResponse(BaseModel):
id: int
user_id: int
chest_id: int
option: str
amount: int
payout: int
status: str
created_at: datetime
class Config:
from_attributes = True
class ChestSettle(BaseModel):
"""宝箱结算"""
result: str = Field(..., pattern="^(A|B|REFUND)$")
class ChestLock(BaseModel):
"""宝箱封盘"""
pass