Files
drinking-game/test_multiplayer.py
gongch 6d1dd5d56c Initial commit: 酒桌派对 / 大话骰 multiplayer drinking-game hub
- server.py: zero-dependency stdlib backend (HTTP + WebSocket /ws), 大话骰 rule engine, server-driven bots
- live.html: real MVP frontend wired to the backend over WebSocket
- index.html/app.js/style.css: older static prototype
- deploy/: systemd unit + Nginx reverse proxy + Let's Encrypt setup.sh
- test_*.py: standalone HTTP / in-process test scripts

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 10:05:10 +08:00

31 lines
1.7 KiB
Python

import json, urllib.request, time
base='http://127.0.0.1:8765'
def post(path, data):
req=urllib.request.Request(base+path, data=json.dumps(data).encode(), headers={'Content-Type':'application/json'})
try: return json.loads(urllib.request.urlopen(req, timeout=5).read())
except Exception as e:
body=e.read().decode() if hasattr(e,'read') else str(e)
try: return json.loads(body)
except Exception: return {'ok':False,'error':body}
def get(path): return json.loads(urllib.request.urlopen(base+path, timeout=5).read())
time.sleep(1)
r=post('/api/create', {'game':'dice','name':'房主','playerId':'p-host'})
code=r['room']['code']
print('create', r['ok'], code, r['player']['host'])
j=post('/api/join', {'code':code,'name':'客人','playerId':'p-guest'})
print('join', j['ok'], len(j['room']['players']))
re=post('/api/join', {'code':code,'name':'客人','playerId':'p-guest'})
print('rejoin', re['ok'], re['reused'], len(re['room']['players']))
bad=post('/api/start', {'code':code,'playerId':'p-guest'})
print('guest start', bad['ok'], bad.get('error'))
s=post('/api/start', {'code':code,'playerId':'p-host'})
print('host start', s['ok'], s['room']['status'])
room_host=get('/api/room?code='+code+'&playerId=p-host')['room']
room_guest=get('/api/room?code='+code+'&playerId=p-guest')['room']
print('privacy host dice lens', [len(p['dice']) for p in room_host['players']])
print('privacy guest dice lens', [len(p['dice']) for p in room_guest['players']])
c=post('/api/call', {'code':code,'playerId':'p-host','count':1,'point':1})
print('bad min', c['ok'], c.get('error'))
c=post('/api/call', {'code':code,'playerId':'p-host','count':len(s['room']['players']),'point':1})
print('call min', c['ok'], c['room']['currentCall'], len(c['room']['history']))