This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (118 loc) · 3.78 KB
/
main.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
import json
import argparse
import os
import random
import bottle
from api import ping_response, start_response, move_response, end_response
#import signal
from xbox360controller import Xbox360Controller
last_axis={"x":0,"y":0}
last_direction='up'
first_move=True
snakeColor="#000"
def on_axis_moved(axis):
# print('Axis {0} moved to {1} {2}'.format(axis.name, axis.x, axis.y))
global last_axis
last_axis={"x":axis.x,"y":axis.y}
@bottle.route('/')
def index():
return '''
Battlesnake documentation can be found at
<a href="https://docs.battlesnake.io">https://docs.battlesnake.io</a>.
'''
@bottle.route('/static/<path:path>')
def static(path):
"""
Given a path, return the static file located relative
to the static folder.
This can be used to return the snake head URL in an API response.
"""
return bottle.static_file(path, root='static/')
@bottle.post('/ping')
def ping():
"""
A keep-alive endpoint used to prevent cloud application platforms,
such as Heroku, from sleeping the application instance.
"""
return ping_response()
@bottle.post('/start')
def start():
data = bottle.request.json
global first_move, snakeColor
first_move = True
"""
TODO: If you intend to have a stateful snake AI,
initialize your snake state here using the
request's data if necessary.
"""
print(json.dumps(data))
color = snakeColor
return start_response(color)
@bottle.post('/move')
def move():
data = bottle.request.json
"""
TODO: Using the data from the endpoint request object, your
snake AI must choose a direction to move in.
"""
#print(json.dumps(data))
global last_direction
direction = last_direction
global last_axis
print("{}".format(last_axis))
if(last_axis.get("x",0)<-0.5):
direction='left'
elif(last_axis.get("x",0)>0.5):
direction='right'
if(last_axis.get("y",0)<-0.5):
direction='up'
elif(last_axis.get("y",0)>0.5):
direction='down'
# Safety: we die if move backward
global first_move
if(not first_move):
if(last_direction == 'up' and direction == 'down' ):
direction = 'up'
elif(last_direction == 'down' and direction == 'up' ):
direction='down'
elif(last_direction == 'left' and direction == 'right' ):
direction='left'
elif(last_direction == 'right' and direction == 'left' ):
direction='right'
first_move=False
last_direction=direction
print(direction)
return move_response(direction)
@bottle.post('/end')
def end():
data = bottle.request.json
"""
TODO: If your snake AI was stateful,
clean up any stateful objects here.
"""
print(json.dumps(data))
return end_response()
# Expose WSGI app (so gunicorn can find it)
application = bottle.default_app()
parser = argparse.ArgumentParser(
description='Start the human Snake')
parser.add_argument('--port',type=int, default=8080, metavar="S", help='port for the API')
parser.add_argument('--color',type=string, default="#000", metavar="S", help='color of the Snake')
parser.add_argument('--controllerId',type=int, default=0, metavar="S", help='Controller ID')
args = parser.parse_args()
port = args.port
controllerId = args.controllerId
snakeColor = args.color
if __name__ == '__main__':
try:
with Xbox360Controller(controllerId, axis_threshold=0.2) as controller:
controller.axis_l.when_moved = on_axis_moved
controller.axis_r.when_moved = on_axis_moved
bottle.run(
application,
host=os.getenv('IP', '0.0.0.0'),
port=os.getenv('PORT', port),
debug=os.getenv('DEBUG', True)
)
except KeyboardInterrupt:
pass