-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.py
244 lines (216 loc) · 7.48 KB
/
api.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import requests, time, pickle, sys, os
from io import BytesIO
from PIL import Image
import numpy as np
'''
You can view using browser also!
Contact any team member, if you want a demo
'''
BASE_URL = 'http://10.72.22.92:5000/'
add_usr_url = BASE_URL+'add'
restart_mission_url = BASE_URL+'restart'
cmd_url = BASE_URL+'move'
score_url = BASE_URL+'score'
botPose_url = BASE_URL+'botPose'
obstacle_url = BASE_URL+'obstaclesPose'
greenZone_url = BASE_URL+'greenZone'
original_greenZone_url = BASE_URL+'originalGreenZone'
redZone_url = BASE_URL+'redZone'
level_url = BASE_URL+'level'
numbots_url = BASE_URL+'numbots'
mission_url = BASE_URL+'missionComplete'
map_url = BASE_URL+'map'
def authenticate():
if os.path.isfile('credentials.p'):
f = open('credentials.p', 'rb')
else:
if sys.version_info[0] == 2:
roll = raw_input("Enter your roll: ")
else:
roll = input("Enter your roll: ")
pswd = roll + str(int(time.time()))
args = {
'roll': roll,
'pswd': pswd
}
r=requests.get(add_usr_url, params=args)
if r.json()['success']:
with open('credentials.p', 'wb') as f:
pickle.dump({'pswd': pswd}, f, protocol=pickle.HIGHEST_PROTOCOL)
else:
print("Please contact someone in the team!")
exit(0)
f = open('credentials.p', 'rb')
credentials = pickle.load(f)
f.close()
return credentials
def restart_mission(level):
'''
Inputs:
level int the level of the game you're attempting
Returns:
success bool True, if the move being tried happened, False otherwise
message bool (optional)
Work:
You won't be needing this command, unless you decide to tweak something in
the APIs. This API does exactly what it says, it resets and restarts a mission
based on the "level" you mention.
'''
args = authenticate()
args['level'] = level
r=requests.get(restart_mission_url, params=args)
time.sleep(0.05)
return r.json()['success']
def send_command(botId, moveType):
'''
Inputs:
botId int The ID of the bot
moveType int movement type, as descriped in the README
Returns:
success bool True, if the move being tried happened, False otherwise
mission_complete bool True, if all the goals have been completed
Work:
Use this command to instruct the zooid to move. If the zooid cannot move,
because of obstacle, or another zooid already occupying the grid or all
green regions have already been collected, then it will return False.
Otherwise, it will update the position of the zooid with ID as botId to
the required location and return True. Now, if all the green regions
have been collected, the mission_complete value will be set to True
'''
args = authenticate()
args['botId'], args['moveType'] = botId, moveType
r=requests.get(cmd_url, params=args)
time.sleep(0.05)
return r.json()['success'], r.json()['mission_complete']
def get_level():
'''
Inputs:
Returns:
level int The level which you're attempting
'''
args = authenticate()
r = requests.get(level_url, params=args)
time.sleep(0.05)
return r.json()['level']
def get_numbots():
'''
Inputs:
Returns:
numbots int Total number of zooids on the grid
'''
args = authenticate()
r = requests.get(numbots_url, params=args)
time.sleep(0.05)
return r.json()['numbots']
def is_mission_complete():
'''
Inputs:
Returns:
mission_complete bool True, if mission is complete, else False
Work:
Checks if the mission is complete, and if yes, then returns True else False
'''
args = authenticate()
r = requests.get(mission_url, params=args)
time.sleep(0.05)
return r.json()['mission_complete']
def get_score():
'''
Inputs:
Returns:
score int Total number of steps, the less the better
'''
args = authenticate()
r = requests.get(score_url, params=args)
time.sleep(0.05)
return r.json()['score']
def get_obstacles_list():
'''
Inputs:
Returns:
obs_list list Each element of the returned list is a list containing
4 vertices of the rectangular obstacle, in a
clockwise manner,starting from the left top corner
Work:
This represents the list of rectangular obstacles. You cannot move a zooid
through an obstacle.
'''
args = authenticate()
r = requests.get(obstacle_url, params=args)
time.sleep(0.05)
return r.json()
def get_redZone_list():
'''
Inputs:
Returns:
red_list list Each element of the returned list is a list containing
4 vertices of the rectangular obstacle, in a
clockwise manner,starting from the left top corner
Work:
This represents the list of rectangular red regions. You can move a zooid
through a red region, but you have to pay twice the number of steps. So try
and avoid this region, unless the other viable path is really long.
'''
args = authenticate()
r = requests.get(redZone_url, params=args)
time.sleep(0.05)
return r.json()
def get_greenZone_list():
'''
Inputs:
Returns:
green_list list Each element of the returned list is a list containing
4 vertices of the rectangular obstacle, in a
clockwise manner,starting from the left top corner
Work:
This represents the list of rectangular green regions. Each green region must
be visited by at least one zooid, and a green region is said to be visited,
if any zooid passes through at least one of its grid.
Please keep in mind, that this is a dynamic list. That is, once a green region
is visited, it is moved out of this list. So the mission is to get this list empty.
'''
args = authenticate()
r = requests.get(greenZone_url, params=args)
time.sleep(0.05)
return r.json()
def get_original_greenZone_list():
'''
Inputs:
Returns:
green_list list Each element of the returned list is a list containing
4 vertices of the rectangular obstacle, in a
clockwise manner,starting from the left top corner
Work:
Same as the list of green regions, except that, this list is not changed. So,
it would contain all the green regions that were there in the beginning
'''
args = authenticate()
r = requests.get(original_greenZone_url, params=args)
time.sleep(0.05)
return r.json()
def get_botPose_list():
'''
Inputs:
Returns:
botPose list Each element of the returned list is a list containing
2 elements (x, y) denoting the position of the zooids
on the grid.
Work:
Contains the updated list of position of the zooids on the grid. This is the
most important function, in terms of feedback.
'''
args = authenticate()
r = requests.get(botPose_url, params=args)
time.sleep(0.05)
return r.json()
def get_Map():
'''
Inputs:
Returns:
numpy array of map image
Work:
'''
args = authenticate()
r = requests.get(map_url, params=args)
img = np.array(Image.open(BytesIO(r.content)))
return img