add GameplayDatabase database with dashboard

This commit is contained in:
2026-04-05 16:48:12 +02:00
parent 2601c2dcff
commit 4151810f1b
6 changed files with 1471 additions and 4 deletions
+115 -4
View File
@@ -7,13 +7,14 @@ from server.GameBoard import GameBoard
from snakes import SnakeBuilder
from server.storage import StorageLoader
from server.database import GameplayDatabase
from server.metrics import (
MetricsStoreBuilder,
MetricsCollector,
)
from quart import Quart, request, jsonify
from quart import Quart, request, jsonify, render_template
import logging, json, os, re, time
from typing import cast
@@ -27,7 +28,7 @@ class Server:
'version': '1.0.0',
}
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, game_state_backend:str='memory', game_state_redis_url:str='redis://localhost:6379/0', game_state_ttl_sec:int=900, game_state_local_cache:bool=True, metrics_backend:str='memory', metrics_redis_url:str='redis://localhost:6379/0', metrics_ttl_sec:int|None=None):
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, game_state_backend:str='memory', game_state_redis_url:str='redis://localhost:6379/0', game_state_ttl_sec:int=900, game_state_local_cache:bool=True, metrics_backend:str='memory', metrics_redis_url:str='redis://localhost:6379/0', metrics_ttl_sec:int|None=None, gameplay_db_enabled:bool=True, gameplay_db_path:str|None=None, gameplay_db_busy_timeout_ms:int=5000):
self.debug = debug
self.snake_type = snake_type
self.storage_type = storage_type
@@ -71,8 +72,15 @@ class Server:
self.logger = build_logger('Battlesnake', debug_env_var='DEBUG_SERVER')
self.snake_version = self._get_snake_version()
self.gameplay_database = None
if gameplay_db_enabled:
db_path = gameplay_db_path or os.path.join(data_path, 'data', 'database', 'gameplay.sqlite3')
self.gameplay_database = GameplayDatabase(
db_path=db_path,
busy_timeout_ms=gameplay_db_busy_timeout_ms,
)
self.app = Quart('Battlesnake')
self.app = Quart('Battlesnake', template_folder=os.path.join(data_path, 'server', 'templates'))
# info is called when you create your Battlesnake on play.battlesnake.com
# and controls your Battlesnake's appearance
@@ -92,6 +100,7 @@ class Server:
await self._prune_stale_games()
game_state = await request.get_json()
await self._create_game_board(game_state)
await self._record_gameplay_start(game_state)
await await_log(self.logger.info(f'GAME START: {game_state['game']}'))
return 'ok'
@@ -104,6 +113,7 @@ class Server:
game_board = cast(GameBoard, await self._get_game_board(game_state))
next_move = game_board.snake_neat_make_a_move()
await self._persist_game_board(game_state['game']['id'], game_board)
await self._record_gameplay_turn(game_state, next_move, game_board)
elapsed_ms = (time.perf_counter() - move_started) * 1000.0
await self.metrics_collector.record_move(next_move, elapsed_ms)
@@ -158,6 +168,8 @@ class Server:
async def shutdown_state_storage():
await self.game_state_store.close()
await self.metrics_collector.close()
if self.gameplay_database is not None:
await self.gameplay_database.close()
@self.app.get('/cleanup')
async def cleanup():
@@ -178,6 +190,36 @@ class Server:
{'Content-Type': 'text/plain; version=0.0.4; charset=utf-8'},
)
@self.app.get('/dashboard/summary')
async def dashboard_summary():
summary = await self._get_dashboard_summary()
return jsonify(summary)
@self.app.get('/dashboard')
async def dashboard_view():
initial_game_id = request.args.get('game_id', '')
return await render_template(
'dashboard.html',
initial_game_id=initial_game_id,
)
@self.app.get('/dashboard/games')
async def dashboard_games():
raw_limit = request.args.get('limit', '50')
try:
limit = max(1, min(200, int(raw_limit)))
except ValueError:
limit = 50
games = await self._get_dashboard_games(limit)
return jsonify(games)
@self.app.get('/dashboard/game/<string:game_id>')
async def dashboard_game_replay(game_id:str):
replay = await self._get_dashboard_game_replay(game_id)
if replay is None:
return jsonify({'error':'game_not_found', 'game_id':game_id}), 404
return jsonify(replay)
async def run(self, host:str='0.0.0.0', port:int=8000, debug:bool=False):
logging.getLogger('werkzeug').setLevel(logging.ERROR)
@@ -278,7 +320,7 @@ class Server:
async def _get_game_board(self, game_state:dict, end:bool=False) -> GameBoard:
game_id = game_state['game']['id']
game_board: GameBoard
game_board:GameBoard
if self.game_state_local_cache and game_id in self.running_games:
game_board = self.running_games[game_id]
else:
@@ -325,3 +367,72 @@ class Server:
self.game_move_counts.pop(game_id, None)
self.game_last_seen_unix.pop(game_id, None)
await self.metrics_collector.record_stuck_removed()
async def _record_gameplay_start(self, game_state:dict) -> None:
if self.gameplay_database is None:
return
try:
await self.gameplay_database.record_game_start(game_state)
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB start record failed:{error}'))
def _extract_latest_snake_thinking(self, game_board:GameBoard) -> dict | None:
try:
history = game_board.snake_class.get_history()
except Exception:
return None
if not isinstance(history, list) or len(history) == 0:
return None
latest = history[-1]
return latest if isinstance(latest, dict) else None
async def _record_gameplay_turn(self, game_state:dict, my_move:str, game_board:GameBoard) -> None:
if self.gameplay_database is None:
return
try:
thinking = self._extract_latest_snake_thinking(game_board)
await self.gameplay_database.record_turn(game_state, my_move, thinking)
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB turn record failed:{error}'))
async def _record_gameplay_end(self, game_state:dict) -> None:
if self.gameplay_database is None:
return
try:
await self.gameplay_database.record_game_end(game_state)
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB end record failed:{error}'))
async def _get_dashboard_summary(self) -> dict:
if self.gameplay_database is None:
return {'enabled':False}
try:
summary = await self.gameplay_database.get_summary()
summary['enabled'] = True
return summary
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB summary failed:{error}'))
return {'enabled':True, 'error':'summary_unavailable'}
async def _get_dashboard_games(self, limit:int=50) -> dict:
if self.gameplay_database is None:
return {'enabled':False, 'games':[]}
try:
games = await self.gameplay_database.list_games(limit=limit)
return {'enabled':True, 'games':games}
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB game list failed:{error}'))
return {'enabled':True, 'error':'games_unavailable', 'games':[]}
async def _get_dashboard_game_replay(self, game_id:str) -> dict | None:
if self.gameplay_database is None:
return {'enabled':False, 'error':'database_disabled', 'game_id':game_id}
try:
replay = await self.gameplay_database.get_game_replay(game_id)
if replay is None:
return None
replay['enabled'] = True
return replay
except Exception as error:
await await_log(self.logger.warning(f'Gameplay DB replay failed:{error}'))
return {'enabled':True, 'error':'replay_unavailable', 'game_id':game_id}