baoxiang/backend/app/schemas/game.py

118 lines
3.4 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
2025-12-17 13:19:55 +08:00
# 修改赔率计算方式,只对获胜方抽水
return round((self.pool_a + self.pool_b * 0.9) / self.pool_a, 2)
2025-12-16 18:06:50 +08:00
@property
def odds_b(self) -> float:
"""计算B边赔率 (1 : X)"""
if self.pool_b == 0:
return 0.0
2025-12-17 13:19:55 +08:00
# 修改赔率计算方式,只对获胜方抽水
return round((self.pool_b + self.pool_a * 0.9) / self.pool_b, 2)
2025-12-16 18:06:50 +08:00
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):
"""宝箱封盘"""
2025-12-18 15:03:25 +08:00
pass
# ==================== 宝箱模板相关 schemas ====================
class ChestTemplateBase(BaseModel):
"""宝箱模板基础模型"""
name: str = Field(..., min_length=1, max_length=100, description="模板名称")
title: str = Field(..., min_length=1, max_length=200, description="宝箱标题")
option_a: str = Field(..., min_length=1, max_length=100, description="选项A")
option_b: str = Field(..., min_length=1, max_length=100, description="选项B")
countdown_seconds: int = Field(default=300, ge=10, le=3600, description="倒计时(秒)")
class ChestTemplateCreate(ChestTemplateBase):
"""创建宝箱模板"""
pass
class ChestTemplateUpdate(BaseModel):
"""更新宝箱模板"""
name: Optional[str] = Field(None, min_length=1, max_length=100, description="模板名称")
title: Optional[str] = Field(None, min_length=1, max_length=200, description="宝箱标题")
option_a: Optional[str] = Field(None, min_length=1, max_length=100, description="选项A")
option_b: Optional[str] = Field(None, min_length=1, max_length=100, description="选项B")
countdown_seconds: Optional[int] = Field(None, ge=10, le=3600, description="倒计时(秒)")
class ChestTemplateResponse(ChestTemplateBase):
"""宝箱模板响应模型"""
id: int
streamer_id: int
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True