import argparse import logging import os import typing from flask import Flask from flask import request from snakes.snake import Snake def run_server(snake: Snake, host: str, port: int): path = snake.name app = Flask("Battlesnake") @app.get(f"/{path}/") def on_info(): return snake.info() @app.post(f"/{path}/start") def on_start(): game_state = request.get_json() snake.start(game_state) return "ok" @app.post(f"/{path}/move") def on_move(): game_state = request.get_json() return snake.move(game_state) @app.post(f"/{path}/end") def on_end(): game_state = request.get_json() snake.end(game_state) return "ok" @app.after_request def identify_server(response): response.headers.set( "server", "shillerben/gitea/starter-snake-python" ) return response logging.getLogger("werkzeug").setLevel(logging.ERROR) print(f"\nRunning Battlesnake at http://{host}:{port}/{path}") app.run(host=host, port=port) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-b", "--bind", default="0.0.0.0", help="host ip to listen on") parser.add_argument("-p", "--port", type=int, default=8000, help="host port to listen on") parser.add_argument("snake", help="snake to run") args = parser.parse_args() host = args.bind port = args.port snake_name = args.snake snake = Snake.SNAKES[snake_name]() run_server(snake, host, port)