reduce target latency

This commit is contained in:
Max Känner 2024-10-06 21:04:45 +02:00
parent d1b0ef3ccf
commit ac4c2d6f72
2 changed files with 20 additions and 6 deletions

View File

@ -87,8 +87,20 @@ pub fn end(game: &Game, turn: i32, _board: &Board, you: &Battlesnake) {
// move is called on every turn and returns your next move // move is called on every turn and returns your next move
// Valid moves are "up", "down", "left", or "right" // Valid moves are "up", "down", "left", or "right"
// See https://docs.battlesnake.com/api/example-move for available data // See https://docs.battlesnake.com/api/example-move for available data
pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Option<Action> { pub fn get_move(
let start = Instant::now(); game: &Game,
turn: i32,
board: &Board,
you: &Battlesnake,
start: &Instant,
) -> Option<Action> {
let calc_start = Instant::now();
if calc_start - *start > Duration::from_millis(10) {
error!(
"The calculation was started long after the request ({}ms)",
(calc_start - *start).as_millis()
);
}
let game_info = GAME_INFOS let game_info = GAME_INFOS
.lock() .lock()
.ok() .ok()
@ -115,7 +127,7 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt
); );
let possible_actions = board.possible_actions().get(&game_info.my_token).cloned()?; let possible_actions = board.possible_actions().get(&game_info.my_token).cloned()?;
if possible_actions.len() == 1 { if possible_actions.len() == 1 {
info!("Only one movement option exists. Skipping Tree evaluation"); info!("Only one movement option exists in turn {turn}. Skipping Tree evaluation");
return possible_actions.first().map(|direction| Action { return possible_actions.first().map(|direction| Action {
r#move: *direction, r#move: *direction,
shout: None, shout: None,
@ -123,11 +135,11 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt
} }
// do some latency compensation // do some latency compensation
let deadline = start let deadline = *start
+ game_info.calculation_time.lock().map_or_else( + game_info.calculation_time.lock().map_or_else(
|_| Duration::from_millis(u64::from(game.timeout) / 2), |_| Duration::from_millis(u64::from(game.timeout) / 2),
|mut guard| { |mut guard| {
let target_latency = game.timeout * 3 / 4; let target_latency = game.timeout / 2;
let latency = you.latency.parse().unwrap_or_else(|e| { let latency = you.latency.parse().unwrap_or_else(|e| {
error!("Unable to parse latency: {e}"); error!("Unable to parse latency: {e}");
target_latency target_latency

View File

@ -6,7 +6,7 @@ use rocket::{
fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task, fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task,
}; };
use serde_json::Value; use serde_json::Value;
use std::env; use std::{env, time::Instant};
#[get("/")] #[get("/")]
fn handle_index() -> Json<Value> { fn handle_index() -> Json<Value> {
@ -27,12 +27,14 @@ fn handle_start(start_req: Json<GameState>) -> Status {
#[post("/move", format = "json", data = "<move_req>")] #[post("/move", format = "json", data = "<move_req>")]
async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> { async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> {
let start = Instant::now();
let response = task::spawn_blocking(move || { let response = task::spawn_blocking(move || {
logic::get_move( logic::get_move(
&move_req.game, &move_req.game,
move_req.turn, move_req.turn,
&move_req.board, &move_req.board,
&move_req.you, &move_req.you,
&start,
) )
}) })
.await .await