change to new code of battlesnake
This commit is contained in:
@@ -1,77 +1,45 @@
|
||||
import logging
|
||||
import os
|
||||
import typing
|
||||
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
|
||||
import server_logic
|
||||
def run_server(handlers: typing.Dict):
|
||||
app = Flask("Battlesnake")
|
||||
|
||||
@app.get("/")
|
||||
def on_info():
|
||||
return handlers["info"]()
|
||||
|
||||
app = Flask(__name__)
|
||||
@app.post("/start")
|
||||
def on_start():
|
||||
game_state = request.get_json()
|
||||
handlers["start"](game_state)
|
||||
return "ok"
|
||||
|
||||
@app.post("/move")
|
||||
def on_move():
|
||||
game_state = request.get_json()
|
||||
return handlers["move"](game_state)
|
||||
|
||||
@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
|
||||
@app.post("/end")
|
||||
def on_end():
|
||||
game_state = request.get_json()
|
||||
handlers["end"](game_state)
|
||||
return "ok"
|
||||
|
||||
It controls your Battlesnake appearance and author permissions.
|
||||
For customization options, see https://docs.battlesnake.com/references/personalization
|
||||
@app.after_request
|
||||
def identify_server(response):
|
||||
response.headers.set(
|
||||
"server", "battlesnake/github/starter-snake-python"
|
||||
)
|
||||
return response
|
||||
|
||||
TIP: If you open your Battlesnake URL in browser you should see this data.
|
||||
"""
|
||||
print("INFO")
|
||||
return {
|
||||
"apiversion": "1",
|
||||
"author": "daniel156161", # TODO: Your Battlesnake Username
|
||||
"color": "#00ff00", # TODO: Personalize
|
||||
"head": "default", # TODO: Personalize
|
||||
"tail": "default", # TODO: Personalize
|
||||
}
|
||||
host = "0.0.0.0"
|
||||
port = int(os.environ.get("PORT", "8000"))
|
||||
|
||||
|
||||
@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)
|
||||
print(f"\nRunning Battlesnake at http://{host}:{port}")
|
||||
app.run(host=host, port=port)
|
||||
|
||||
Reference in New Issue
Block a user