Files
nanocode/net-games/test_quickstart.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

70 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""测试快速开始流程"""
import socketio
import asyncio
async def test():
sio = socketio.AsyncClient()
room_created = False
@sio.event
async def connect():
print("✅ 已连接服务器")
# 模拟快速开始:创建房间+AI
await sio.emit('create_room', {
'game_type': 'doudizhu',
'player_name': '蔡叔叔',
'config': {'max_players': 3},
'ai_players': [
{'name': 'AI-1(rule)', 'ai_type': 'rule'},
{'name': 'AI-2(deepseek)', 'ai_type': 'deepseek'}
]
})
@sio.on('room_created')
async def on_room_created(data):
nonlocal room_created
print(f"✅ 房间创建成功: {data['room']['code']}")
print(f" 玩家数: {len(data['room']['players'])}")
for p in data['room']['players']:
print(f" - {p['name']} ({p['type']})")
# 自动准备
print("🤖 自动准备...")
await sio.emit('set_ready', {'ready': True})
room_created = True
@sio.on('room_updated')
async def on_room_updated(data):
nonlocal room_created
if room_created and data.get('room', {}).get('status') == 'waiting':
# 检查是否所有人都准备好了
all_ready = all(p.get('is_ready', False) for p in data['room']['players'])
if all_ready:
print("🎮 所有人已准备,开始游戏...")
await sio.emit('start_game')
room_created = False
@sio.on('game_started')
async def on_game_started(data):
print(f"✅ 游戏开始! 房间: {data['room']['code']}")
print(f" 状态: {data['room']['status']}")
await sio.disconnect()
@sio.on('error')
async def on_error(data):
print(f"❌ 错误: {data}")
await sio.disconnect()
try:
await sio.connect('http://localhost:8900')
await asyncio.wait_for(sio.wait(), timeout=15)
except asyncio.TimeoutError:
print("⏱️ 测试超时")
except Exception as e:
print(f"❌ 错误: {e}")
if __name__ == '__main__':
asyncio.run(test())