-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
77 lines (56 loc) · 2.02 KB
/
server.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
import logging
import os
from flask import Flask
from flask import request
import server_logic
app = Flask(__name__)
@app.get("/")
def handle_info():
"""
This function is called when you register your Battlesnake on play.battlesnake.com
See https://docs.battlesnake.com/guides/getting-started#step-4-register-your-battlesnake
It controls your Battlesnake appearance and author permissions.
For customization options, see https://docs.battlesnake.com/references/personalization
TIP: If you open your Battlesnake URL in browser you should see this data.
"""
print("INFO")
return {
"apiversion": "1",
"author": "nikkikapadia", # TODO: Your Battlesnake Username
"color": "#708090", # TODO: Personalize
"head": "fang", # TODO: Personalize
"tail": "pixel", # TODO: Personalize
}
@app.post("/start")
def handle_start():
"""
This function is called everytime your snake is entered into a game.
request.json contains information about the game that's about to be played.
"""
data = request.get_json()
print(f"{data['game']['id']} START")
return "ok"
@app.post("/move")
def handle_move():
"""
This function is called on every turn of a game. It's how your snake decides where to move.
Valid moves are "up", "down", "left", or "right".
"""
data = request.get_json()
# TODO - look at the server_logic.py file to see how we decide what move to return!
move = server_logic.choose_move(data)
return {"move": move}
@app.post("/end")
def end():
"""
This function is called when a game your snake was in ends.
It's purely for informational purposes, you don't have to make any decisions here.
"""
data = request.get_json()
print(f"{data['game']['id']} END")
return "ok"
if __name__ == "__main__":
logging.getLogger("werkzeug").setLevel(logging.ERROR)
print("Starting Battlesnake Server...")
port = int(os.environ.get("PORT", "8080"))
app.run(host="0.0.0.0", port=port, debug=True)