diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index 94a607b..2a62672 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -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 // Valid moves are "up", "down", "left", or "right" // See https://docs.battlesnake.com/api/example-move for available data -pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Option { - let start = Instant::now(); +pub fn get_move( + game: &Game, + turn: i32, + board: &Board, + you: &Battlesnake, + start: &Instant, +) -> Option { + 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 .lock() .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()?; 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 { r#move: *direction, shout: None, @@ -123,11 +135,11 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt } // do some latency compensation - let deadline = start + let deadline = *start + game_info.calculation_time.lock().map_or_else( |_| Duration::from_millis(u64::from(game.timeout) / 2), |mut guard| { - let target_latency = game.timeout * 3 / 4; + let target_latency = game.timeout / 2; let latency = you.latency.parse().unwrap_or_else(|e| { error!("Unable to parse latency: {e}"); target_latency diff --git a/battlesnake/src/main.rs b/battlesnake/src/main.rs index 8d26b99..702f16b 100644 --- a/battlesnake/src/main.rs +++ b/battlesnake/src/main.rs @@ -6,7 +6,7 @@ use rocket::{ fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task, }; use serde_json::Value; -use std::env; +use std::{env, time::Instant}; #[get("/")] fn handle_index() -> Json { @@ -27,12 +27,14 @@ fn handle_start(start_req: Json) -> Status { #[post("/move", format = "json", data = "")] async fn handle_move(move_req: Json) -> Option> { + let start = Instant::now(); let response = task::spawn_blocking(move || { logic::get_move( &move_req.game, move_req.turn, &move_req.board, &move_req.you, + &start, ) }) .await