完成版

This commit is contained in:
taiyi 2025-12-17 18:41:25 +08:00
parent c6f71c7b16
commit eb11867a98
4 changed files with 4 additions and 202 deletions

View File

@ -1,6 +1,7 @@
"""
游戏服务
"""
from typing import List
from sqlalchemy.orm import Session
from sqlalchemy import and_, update
from ..models.user import User
@ -47,7 +48,7 @@ class GameService:
return db_chest
@staticmethod
def get_active_chests(db: Session, streamer_id: int = None) -> list[Chest]:
def get_active_chests(db: Session, streamer_id: int = None) -> List[Chest]:
"""
获取活跃宝箱
"""
@ -57,7 +58,7 @@ class GameService:
return query.order_by(Chest.created_at.desc()).all()
@staticmethod
def get_chests_by_streamer(db: Session, streamer_id: int) -> list[Chest]:
def get_chests_by_streamer(db: Session, streamer_id: int) -> List[Chest]:
"""
获取指定主播的所有宝箱包括历史宝箱
"""
@ -539,7 +540,7 @@ class GameService:
update_pool_cache(chest.id, 0, 0)
@staticmethod
def get_chest_bets(db: Session, chest_id: int) -> list[Bet]:
def get_chest_bets(db: Session, chest_id: int) -> List[Bet]:
"""
获取宝箱下注记录
"""

View File

@ -1,76 +0,0 @@
#!/usr/bin/env python3
"""
创建一个新的测试宝箱并验证API返回的数据
"""
import requests
import json
def create_and_test_chest():
"""创建测试宝箱并测试API"""
print("=== 创建测试宝箱并验证API ===")
try:
# 先登录获取token
login_data = {
"username": "streamer", # 使用刚创建的主播用户
"password": "streamer123" # 使用默认密码
}
login_response = requests.post("http://localhost:8000/api/auth/login", data=login_data)
print(f"Login Status Code: {login_response.status_code}")
if login_response.status_code == 200:
token_data = login_response.json()
token = token_data["access_token"]
print(f"Token: {token}")
# 设置请求头
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# 设置请求体
data = {
"title": "倒计时测试宝箱",
"option_a": "选项A",
"option_b": "选项B",
"countdown_seconds": 120 # 2分钟倒计时
}
# 发送POST请求创建宝箱
response = requests.post("http://localhost:8000/api/chests", headers=headers, data=json.dumps(data))
print(f"Create Chest Status Code: {response.status_code}")
print(f"Response: {response.text}")
if response.status_code == 200:
chest = response.json()
print(f"Created chest: ID={chest['id']}, Title={chest['title']}, Time Remaining={chest.get('time_remaining', 'N/A')}")
# 测试获取单个宝箱
single_chest_response = requests.get(f"http://localhost:8000/api/chests/{chest['id']}")
if single_chest_response.status_code == 200:
single_chest = single_chest_response.json()
print(f"Single chest: ID={single_chest['id']}, Time Remaining={single_chest.get('time_remaining', 'N/A')}")
else:
print(f"Get single chest failed: {single_chest_response.status_code}")
# 测试获取宝箱列表
list_response = requests.get("http://localhost:8000/api/chests")
if list_response.status_code == 200:
chests = list_response.json()
print(f"Total chests in list: {len(chests)}")
for c in chests:
if c['id'] == chest['id']:
print(f"List chest: ID={c['id']}, Time Remaining={c.get('time_remaining', 'N/A')}")
break
else:
print(f"Get chests list failed: {list_response.status_code}")
else:
print(f"Create chest failed: {response.text}")
else:
print(f"Login failed: {login_response.text}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
create_and_test_chest()

View File

@ -1,58 +0,0 @@
version: '3.8'
services:
mysql:
image: mysql:8.0
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./mysql/init:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=treasure_box_2024
- MYSQL_DATABASE=treasure_box_game
- MYSQL_USER=treasure_box
- MYSQL_PASSWORD=treasure_box_2024
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$$MYSQL_ROOT_PASSWORD"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "8000:8000"
depends_on:
mysql:
condition: service_healthy
redis:
condition: service_healthy
environment:
- DATABASE_URL=mysql+pymysql://root:treasure_box_2024@mysql:3306/treasure_box_game
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
- ./backend:/app
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
restart: unless-stopped
volumes:
mysql_data:
redis_data:

View File

@ -1,65 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试交易流水API接口
"""
import os
import sys
import django
# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo07.settings')
try:
django.setup()
except Exception as e:
print(f"Django setup error: {e}")
import requests
import json
def test_transactions_api():
print("=" * 60)
print("测试交易流水API接口")
print("=" * 60)
# 这里需要替换为实际的API基础URL
base_url = "http://localhost:8000" # 假设后端运行在8000端口
# 测试获取交易流水接口
url = f"{base_url}/api/users/me/transactions"
print(f"测试接口: {url}")
try:
# 这里需要提供有效的认证token
# 在实际测试中你需要先登录获取token
headers = {
"Authorization": "Bearer YOUR_JWT_TOKEN_HERE"
}
response = requests.get(url, headers=headers, timeout=10)
print(f"HTTP状态码: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"返回交易记录数量: {len(data)}")
if data:
print("第一条交易记录:")
print(json.dumps(data[0], indent=2, ensure_ascii=False))
else:
print("没有交易记录")
else:
print(f"错误响应: {response.text}")
except requests.exceptions.ConnectionError:
print("连接错误: 无法连接到后端服务,请确保后端服务正在运行")
except requests.exceptions.Timeout:
print("超时错误: 请求超时")
except Exception as e:
print(f"其他错误: {e}")
if __name__ == "__main__":
test_transactions_api()