Got the starter snake going

This commit is contained in:
Ben Shiller 2023-09-09 11:34:46 -05:00
parent 6de4111b05
commit 3654aebb5e
No known key found for this signature in database
GPG Key ID: DC46F01400846797
6 changed files with 24 additions and 7 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
deploy
publish.sh
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@ -13,4 +13,4 @@ RUN pip install --upgrade pip && pip install -r requirements.txt
# Run Battlesnake
ENTRYPOINT [ "python", "server.py" ]
CMD [ "StarterSnake" ]
CMD [ "starter_snake" ]

10
requirements.txt Normal file
View File

@ -0,0 +1,10 @@
blinker==1.6.2
click==8.1.7
Flask==2.3.3
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
mypy==1.5.1
mypy-extensions==1.0.0
typing_extensions==4.7.1
Werkzeug==2.3.7

View File

@ -10,24 +10,25 @@ from snakes.snake import Snake
def run_server(snake: Snake, host: str, port: int):
path = snake.name
app = Flask("Battlesnake")
@app.get("/")
@app.get(f"/{path}/")
def on_info():
return snake.info()
@app.post("/start")
@app.post(f"/{path}/start")
def on_start():
game_state = request.get_json()
snake.start(game_state)
return "ok"
@app.post("/move")
@app.post(f"/{path}/move")
def on_move():
game_state = request.get_json()
return snake.move(game_state)
@app.post("/end")
@app.post(f"/{path}/end")
def on_end():
game_state = request.get_json()
snake.end(game_state)
@ -42,7 +43,7 @@ def run_server(snake: Snake, host: str, port: int):
logging.getLogger("werkzeug").setLevel(logging.ERROR)
print(f"\nRunning Battlesnake at http://{host}:{port}")
print(f"\nRunning Battlesnake at http://{host}:{port}/{path}")
app.run(host=host, port=port)

View File

@ -4,9 +4,10 @@ import typing
class Snake:
SNAKES: typing.Dict[str, typing.Type["Snake"]] = {}
name = "snake"
def __init_subclass__(cls) -> None:
cls.SNAKES[cls.__name__] = cls
cls.SNAKES[cls.name] = cls
def info(self) -> typing.Dict:
'''info is called when you create your Battlesnake on play.battlesnake.com

View File

@ -8,6 +8,8 @@ from .snake import Snake
class StarterSnake(Snake):
'''Snake from Battlesnake's starter snake repo'''
name = "starter_snake"
def info(self) -> typing.Dict:
print("INFO")