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']))