-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (41 loc) · 1.52 KB
/
main.py
File metadata and controls
54 lines (41 loc) · 1.52 KB
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
import logging
import os
import uvicorn
from fastapi import FastAPI
from move_validator import MoveValidator, NoSafeMovesException
from schemas import GameState, GameStats, Move, SelectedMove
app = FastAPI()
@app.get("/")
def on_info() -> GameStats:
"""Customize your snake see https://play.battlesnake.com/customizations for more details """
return GameStats(
apiversion="1",
author="Qstars_worker_x", # TODO: Your Battlesnake Username
color="#888888", # TODO: Choose color
head="default", # TODO: Choose head
tail="default", # TODO: Choose tail
)
@app.post("/start")
def on_start(game_state: GameState) -> str:
"""Called in beginning of the game"""
print("GAME START")
return "ok"
@app.post("/move")
def on_move(game_state: GameState) -> SelectedMove:
"""Called on each turn of the game, here you can decide where to move your snake"""
move_selector = MoveValidator(game_state)
move_selector.avoid_moving_backwards()
try:
return SelectedMove(move=move_selector.choose_random_safe_move())
except NoSafeMovesException:
return SelectedMove(move=Move.LEFT)
@app.post("/end")
def on_end(game_state: GameState) -> str:
"""Called when a game ends"""
print("GAME OVER\n")
return "ok"
if __name__ == "__main__":
logging.getLogger("uvicorn").setLevel(logging.ERROR)
port = int(os.environ.get("PORT", "8080"))
print(f"\nRunning Battlesnake at http://0.0.0.0:{port}/docs")
uvicorn.run(app, host="0.0.0.0", port=port)