fix
This commit is contained in:
@ -35,7 +35,13 @@ class Settings(BaseSettings):
|
||||
max_unique_cards: int = 1000 # Maximum number of unique cards
|
||||
|
||||
# CORS
|
||||
cors_origins: list[str] = ["http://localhost:3000", "https://card.syui.ai"]
|
||||
cors_origins: list[str] = [
|
||||
"http://localhost:3000",
|
||||
"http://localhost:5173",
|
||||
"http://localhost:4173",
|
||||
"https://card.syui.ai",
|
||||
"https://xxxcard.syui.ai"
|
||||
]
|
||||
|
||||
# Security
|
||||
secret_key: str = "your-secret-key-change-this-in-production"
|
||||
|
@ -92,6 +92,51 @@ class CardRepository(BaseRepository[UserCard]):
|
||||
obtained_at=card.obtained_at
|
||||
)
|
||||
self.session.add(registry)
|
||||
|
||||
async def get_total_card_count(self) -> int:
|
||||
"""Get total number of cards obtained"""
|
||||
result = await self.session.execute(
|
||||
select(func.count(UserCard.id))
|
||||
)
|
||||
return result.scalar() or 0
|
||||
|
||||
async def get_cards_by_rarity(self) -> dict:
|
||||
"""Get card count by rarity"""
|
||||
result = await self.session.execute(
|
||||
select(UserCard.status, func.count(UserCard.id))
|
||||
.group_by(UserCard.status)
|
||||
)
|
||||
|
||||
cards_by_rarity = {}
|
||||
for status, count in result.all():
|
||||
cards_by_rarity[status.value if hasattr(status, 'value') else str(status)] = count
|
||||
|
||||
return cards_by_rarity
|
||||
|
||||
async def get_recent_cards(self, limit: int = 10) -> List[dict]:
|
||||
"""Get recent card activities"""
|
||||
result = await self.session.execute(
|
||||
select(
|
||||
UserCard.card_id,
|
||||
UserCard.status,
|
||||
UserCard.obtained_at,
|
||||
User.did.label('owner_did')
|
||||
)
|
||||
.join(User, UserCard.user_id == User.id)
|
||||
.order_by(UserCard.obtained_at.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
activities = []
|
||||
for row in result.all():
|
||||
activities.append({
|
||||
'card_id': row.card_id,
|
||||
'status': row.status.value if hasattr(row.status, 'value') else str(row.status),
|
||||
'obtained_at': row.obtained_at,
|
||||
'owner_did': row.owner_did
|
||||
})
|
||||
|
||||
return activities
|
||||
|
||||
|
||||
class UniqueCardRepository(BaseRepository[UniqueCardRegistry]):
|
||||
|
@ -115,4 +115,46 @@ async def get_unique_cards(db: AsyncSession = Depends(get_session)):
|
||||
"unique_id": str(uc.unique_id)
|
||||
}
|
||||
for uc in unique_cards
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@router.get("/stats")
|
||||
async def get_gacha_stats(db: AsyncSession = Depends(get_session)):
|
||||
"""
|
||||
ガチャ統計情報を取得
|
||||
"""
|
||||
try:
|
||||
card_repo = CardRepository(db)
|
||||
|
||||
# 総ガチャ実行数
|
||||
total_draws = await card_repo.get_total_card_count()
|
||||
|
||||
# レアリティ別カード数
|
||||
cards_by_rarity = await card_repo.get_cards_by_rarity()
|
||||
|
||||
# 成功率計算(簡易版)
|
||||
success_rates = {}
|
||||
if total_draws > 0:
|
||||
for rarity, count in cards_by_rarity.items():
|
||||
success_rates[rarity] = count / total_draws
|
||||
|
||||
# 最近の活動(最新10件)
|
||||
recent_cards = await card_repo.get_recent_cards(limit=10)
|
||||
recent_activity = []
|
||||
for card_data in recent_cards:
|
||||
recent_activity.append({
|
||||
"timestamp": card_data.get("obtained_at", "").isoformat() if card_data.get("obtained_at") else "",
|
||||
"user_did": card_data.get("owner_did", "unknown"),
|
||||
"card_name": f"Card #{card_data.get('card_id', 0)}",
|
||||
"rarity": card_data.get("status", "common")
|
||||
})
|
||||
|
||||
return {
|
||||
"total_draws": total_draws,
|
||||
"cards_by_rarity": cards_by_rarity,
|
||||
"success_rates": success_rates,
|
||||
"recent_activity": recent_activity
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Statistics error: {str(e)}")
|
Reference in New Issue
Block a user