change from sync to async and from flask to quart
Build and Push Docker Container / build-and-push (push) Successful in 1m35s

This commit is contained in:
2026-01-06 13:36:43 +01:00
parent 962d8b1043
commit 6e74b5fb57
6 changed files with 85 additions and 90 deletions
+57 -70
View File
@@ -4,8 +4,7 @@ from server.SnakeBuilder import SnakeBuilder
from server.storage.StorageLoader import StorageLoader
from flask import Flask, jsonify
from flask import request
from quart import Quart, request, jsonify
import logging, json, os, re
class Server:
@@ -25,33 +24,67 @@ class Server:
self.running_games:dict[str, GameBoard] = {}
self.app = Flask("Battlesnake")
self.app = Quart("Battlesnake")
# info is called when you create your Battlesnake on play.battlesnake.com
# and controls your Battlesnake's appearance
# TIP: If you open your Battlesnake URL in a browser you should see this data
@self.app.get("/")
async def on_info():
return self._info()
snake_config = await self._read_json_config_or_create()
print("INFO Snake:", snake_config)
return snake_config
# start is called when your Battlesnake begins a game
@self.app.post("/start")
async def on_start():
game_state = request.get_json()
self._start(game_state)
game_state = await request.get_json()
await self._create_game_board(game_state)
print("GAME START:", game_state["game"])
return "ok"
# move is called when your Battlesnake game is running game
@self.app.post("/move")
async def on_move():
game_state = request.get_json()
return self._move(game_state)
game_state = await request.get_json()
game_board = await self._get_game_board(game_state)
next_move = game_board.snake_neat_make_a_move()
if self.debug:
print("TURN:", f'{game_state["turn"]:3},', "MOVE:", f"{next_move:5}")
return {"move": next_move}
# end is called when your Battlesnake finishes a game
@self.app.post("/end")
async def on_end():
game_state = request.get_json()
self._end(game_state)
game_state = await request.get_json()
if self.store_game_state:
game_board = await self._get_game_board(game_state, end=True)
#if not game_board.get_winner() == "me" and not game_board.get_turn() <= self.store_game_when_win_and_moves_are_bigger_as:
if self.check_tls_security:
await game_board.save(
StorageLoader.build(self.storage_type),
file_path=os.path.join(self.data_path, 'data'),
database=os.getenv("EDGEDB_DATABASE", None),
tls_security=None
)
else:
await game_board.save(
StorageLoader.build(self.storage_type),
file_path=os.path.join(self.data_path, 'data'),
database=os.getenv("EDGEDB_DATABASE", None),
)
print("GAME ENDED: Winner is", [ x["name"] for x in game_state["board"]['snakes']])
self._delete_game_board(game_state)
return "ok"
@self.app.after_request
async def identify_server(response):
response.headers.set(
"server", "battlesnake/github/starter-snake-python"
"server", "battlesnake/gitea/snake-python"
)
return response
@@ -66,20 +99,20 @@ class Server:
print(f"\nRunning Battlesnake at http://{host}:{port} with the {' '.join(re.findall('[A-Z][^A-Z]*', self.snake_type))}")
self.app.run(host=host, port=port, debug=debug)
def _read_json_config_or_create(self):
snake_config = read_file(self.config_file, json.load)
async def _read_json_config_or_create(self):
snake_config = await read_file(self.config_file, json.load)
if not snake_config:
snake_config = self._override_snake_config_with_environment_variables(self.default_snake_config)
return await self._override_snake_config_with_environment_variables(self.default_snake_config)
return await self._override_snake_config_with_environment_variables(snake_config)
return self._override_snake_config_with_environment_variables(snake_config)
def _override_snake_config_with_environment_variables(self, config:dict[str]):
for key in ["author", "color", "head", "tail"]:
if os.environ.get(f"SNAKE_{key.upper()}", None):
config[key.lower()] = os.environ.get(f"SNAKE_{key.upper()}")
async def _override_snake_config_with_environment_variables(self, config: dict[str, str]) -> dict[str, str]:
for key in ("author", "color", "head", "tail"):
value = os.environ.get(f"SNAKE_{key.upper()}")
if value is not None:
config[key] = value
return config
def _create_game_board(self, game_state:dict):
async def _create_game_board(self, game_state:dict):
new_game_board = GameBoard(
game_id=game_state["game"]["id"],
width=game_state['board']['width'],
@@ -89,7 +122,7 @@ class Server:
map=game_state['game']['map'],
snake_class=SnakeBuilder.build(self.snake_type)
)
new_game_board.start_game(game_state)
await new_game_board.start_game(game_state)
self.running_games[game_state["game"]["id"]] = new_game_board
return new_game_board
@@ -97,11 +130,11 @@ class Server:
def _delete_game_board(self, game_state):
del self.running_games[game_state["game"]["id"]]
def _get_game_board(self, game_state:str, end:bool=False):
async def _get_game_board(self, game_state:str, end:bool=False):
try:
game_board = self.running_games[game_state["game"]["id"]]
except KeyError:
game_board = self._create_game_board(game_state)
game_board = await self._create_game_board(game_state)
game_board.read_game_data(game_state)
if end:
@@ -112,52 +145,6 @@ class Server:
def enable_store_game_state(self):
self.store_game_state = True
# info is called when you create your Battlesnake on play.battlesnake.com
# and controls your Battlesnake's appearance
# TIP: If you open your Battlesnake URL in a browser you should see this data
def _info(self) -> dict:
snake_config = self._read_json_config_or_create()
print("INFO Snake:", snake_config)
return snake_config
# start is called when your Battlesnake begins a game
def _start(self, game_state:dict):
self._create_game_board(game_state)
print("GAME START:", game_state["game"])
# move is called when your Battlesnake game is running game
def _move(self, game_state:dict) -> dict:
game_board = self._get_game_board(game_state)
next_move = game_board.snake_neat_make_a_move()
if self.debug:
print("TURN:", f'{game_state["turn"]:3},', "MOVE:", f"{next_move:5}")
return {"move": next_move}
# end is called when your Battlesnake finishes a game
def _end(self, game_state:dict):
if self.store_game_state:
game_board = self._get_game_board(game_state, end=True)
#if not game_board.get_winner() == "me" and not game_board.get_turn() <= self.store_game_when_win_and_moves_are_bigger_as:
if self.check_tls_security:
game_board.save(
StorageLoader.build(self.storage_type),
file_path=os.path.join(self.data_path, 'data'),
database=os.getenv("EDGEDB_DATABASE", None),
tls_security=None
)
else:
game_board.save(
StorageLoader.build(self.storage_type),
file_path=os.path.join(self.data_path, 'data'),
database=os.getenv("EDGEDB_DATABASE", None),
)
print("GAME ENDED: Winner is", [ x["name"] for x in game_state["board"]['snakes']])
self._delete_game_board(game_state)
def _cleanup_database(self):
storage = StorageLoader.build(self.storage_type)()
return storage.cleanup()