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} time.sleep(1) r=post('/api/create', {'game':'dice','name':'房主','playerId':'p-host'}) code=r['room']['code'] print('create', r['ok'], code, r['player']['ready']) j=post('/api/join', {'code':code,'name':'客人','playerId':'p-guest'}) print('join guest ready', j['room']['players'][1]['ready']) bad=post('/api/start', {'code':code,'playerId':'p-host'}) print('start before ready', bad['ok'], bad.get('error')) ready=post('/api/ready', {'code':code,'playerId':'p-guest'}) print('guest ready', [p['ready'] for p in ready['room']['players']]) setg=post('/api/settings', {'code':code,'playerId':'p-host','cupsPerLoss':2}) print('settings', setg['room']['settings']) bot=post('/api/add-bot', {'code':code,'playerId':'p-host'}) print('add bot', len(bot['room']['players'])) s=post('/api/start', {'code':code,'playerId':'p-host'}) print('start', s['ok'], s['room']['status'], s['room']['diceRule']) # 3人局最低3个1 badcall=post('/api/call', {'code':code,'playerId':'p-host','count':2,'point':6}) print('bad call', badcall['ok'], badcall.get('error')) c=post('/api/call', {'code':code,'playerId':'p-host','count':3,'point':1}) print('call', c['ok'], c['room']['currentCall'])