-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
91 lines (80 loc) · 2.63 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import asyncio
import json
from random import (
randint,
choice
)
import sys
import websockets
async def send(websocket, action, data):
message = json.dumps(
{
'action': action,
'data': data,
}
)
print(message)
await websocket.send(message)
async def start(auth_token):
uri = "wss://4yyity02md.execute-api.us-east-1.amazonaws.com/ws?token={}".format(auth_token)
while True:
try:
print('connection to {}'.format(uri))
async with websockets.connect(uri) as websocket:
await play(websocket)
except KeyboardInterrupt:
print('Exiting...')
break
except Exception:
print('connection error!')
time.sleep(3)
async def play(websocket):
while True:
try:
request = await websocket.recv()
print(f"< {request}")
request_data = json.loads(request)
if request_data['event'] == 'update_user_list':
pass
if request_data['event'] == 'gameover':
pass
if request_data['event'] == 'challenge':
# if request_data['data']['opponent'] == 'favoriteopponent':
await send(
websocket,
'accept_challenge',
{
'challenge_id': request_data['data']['challenge_id'],
},
)
if request_data['event'] == 'your_turn':
await process_your_turn(websocket, request_data)
except KeyboardInterrupt:
print('Exiting...')
break
except Exception as e:
print('error {}'.format(str(e)))
break # force login again
async def process_your_turn(websocket, request_data):
if randint(0, 4) >= 1:
await process_action(websocket, "MOVE", request_data)
else:
await process_action(websocket, "SHOOT", request_data)
async def process_action(websocket, action, request_data):
await send(
websocket,
action,
{
'game_id': request_data['data']['game_id'],
'turn_token': request_data['data']['turn_token'],
'from_row': randint(0,17),
'from_col': randint(0,17),
'direction':choice(["NOTH","SOUTH","EAST","WEST"])
},
)
if __name__ == '__main__':
if len(sys.argv) >= 2:
auth_token = sys.argv[1]
asyncio.get_event_loop().run_until_complete(start(auth_token))
else:
print('please provide your auth_token')