This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
144 lines (110 loc) · 3.4 KB
/
app.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
# Required to make bottle work with gevent
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import json
import os
import random
def _respond(response_json):
return json.dumps(response_json)
def map_ai(ai_mode):
if ai_mode not in ['sq', 'n', 's', 'e', 'w']:
return 'r'
return ai_mode
@bottle.post('/<ai_mode>/register')
def register(ai_mode):
ai_mode = map_ai(ai_mode)
request = bottle.request.json
if not request:
return "No request data sent"
print "--- REGISTER ---", str(ai_mode)
print "Game ID:", request.get('game_id')
print "Client ID:", request.get('client_id')
print "Board:"
print " Width:", request.get('board').get('width')
print " Height:", request.get('board').get('height')
print "----------------"
ai_names = {
'sq': 'Square Snake',
'n': 'North Snake',
's': 'South Snake',
'e': 'East Snake',
'w': 'West Snake',
'r': 'RaNd0m Sn4K3'
}
return _respond({
'name': ai_names[ai_mode],
'head_img_url': "http://fc02.deviantart.net/fs70/f/2010/148/3/d/20x20_PNG_Icons_sword_by_JMcIvor.png"
})
@bottle.post('/<ai_mode>/start')
def start(ai_mode):
ai_mode = map_ai(ai_mode)
request = bottle.request.json
if not request:
return "No request data sent"
print "--- START ---", str(ai_mode)
print "Game ID:", request.get('game_id')
print "Num Players:", request.get('num_players')
print "-------------"
return _respond({})
@bottle.post('/<ai_mode>/tick/<client_id>')
def tick(ai_mode, client_id):
ai_mode = map_ai(ai_mode)
request = bottle.request.json
if not request:
return "No request data sent"
print "--- TICK", request.get('turn_num'), '---', str(ai_mode)
print "Game ID:", request.get('id')
print "Turn Num:", request.get('turn_num')
print "Snakes:", len(request.get('snakes'))
# print request.get('board')
print "----------------"
print client_id
print request.get('snakes')
# Find the last move we made
for snake in request.get('snakes'):
if snake['id'] == client_id:
my_snake = snake
continue
random_map = {
'n': [random.choice(['n', 'e', 'w'])],
's': [random.choice(['s', 'e', 'w'])],
'w': [random.choice(['n', 's', 'w'])],
'e': [random.choice(['n', 's', 'e'])],
'': [random.choice(['n', 's', 'e', 'w'])]
}
r_choices = random_map[my_snake['last_move']]
modes = {
'sq': ['n', 'n', 'w', 'w', 's', 's', 'e', 'e'],
'n': ['n'],
's': ['n'],
'e': ['e'],
'w': ['w'],
'r': r_choices
}
# Map
moves = modes[ai_mode]
my_move = moves[request.get('turn_num') % len(moves)]
return _respond({
'move': my_move,
'message': 'Turn %d!' % (request.get('turn_num'))
})
@bottle.post('<ai_mode>/end')
def end(ai_mode):
ai_mode = map_ai(ai_mode)
request = bottle.request.json
if not request:
return "No request data sent"
print "--- END ---", str(ai_mode)
print "Game ID:", request.get('game_id')
print "-------------"
return _respond({})
## Runserver ##
prod_port = os.environ.get('PORT', None)
if prod_port:
# Assume Heroku
bottle.run(host='0.0.0.0', port=int(prod_port), server='gevent')
else:
# Localhost
bottle.debug(True)
bottle.run(host='localhost', port=8080)