speed up loading and saving to redis that the move request are getting a answer when switching workers, strip data that neats to get recomuted every turn
Build and Push Docker Container / build-and-push (push) Successful in 4m49s
Build and Push Docker Container / build-and-push (push) Successful in 4m49s
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from typing import TYPE_CHECKING, cast
|
||||
import json, time, os
|
||||
import asyncio, json, time, os
|
||||
|
||||
from quart import Blueprint, request, jsonify
|
||||
|
||||
@@ -61,9 +61,27 @@ def create_battlesnake_blueprint(server:'Server') -> Blueprint:
|
||||
server.metrics_collector.record_http_request('move')
|
||||
game_state = await request.get_json()
|
||||
move_started = time.perf_counter()
|
||||
game_board = cast(GameBoard, await server.game_runtime.get_game_board(game_state))
|
||||
next_move = game_board.snake_neat_make_a_move()
|
||||
await server.game_runtime.persist_game_board(game_state['game']['id'], game_board)
|
||||
|
||||
game_id = game_state['game']['id']
|
||||
timeout_ms = int(game_state.get('game', {}).get('timeout', 500))
|
||||
budget_sec = max(0.05, (timeout_ms - 50) / 1000.0)
|
||||
|
||||
next_move = None
|
||||
move_completed = False
|
||||
game_board = None
|
||||
|
||||
try:
|
||||
async with asyncio.timeout(budget_sec):
|
||||
game_board = cast(GameBoard, await server.game_runtime.get_game_board(game_state))
|
||||
loop = asyncio.get_running_loop()
|
||||
next_move = await loop.run_in_executor(None, game_board.snake_neat_make_a_move)
|
||||
move_completed = True
|
||||
except TimeoutError:
|
||||
await await_log(server.logger.warning(f'MOVE TIMEOUT: turn={game_state.get("turn")}, game={game_id}, returning fallback {next_move!r}'))
|
||||
|
||||
if move_completed:
|
||||
await server.game_runtime.persist_game_board(game_id, game_board)
|
||||
|
||||
await server.gameplay_tracking.record_gameplay_turn(game_state, next_move, game_board)
|
||||
elapsed_ms = (time.perf_counter() - move_started) * 1000.0
|
||||
await server.metrics_collector.record_move(next_move, elapsed_ms)
|
||||
|
||||
Reference in New Issue
Block a user