Compare commits

...

2 Commits

Author SHA1 Message Date
3d58e5331b give the snake more thinging time 2024-10-03 05:37:19 +02:00
a1bdd4cb1b remove container after regression test 2024-10-03 05:37:06 +02:00
2 changed files with 28 additions and 14 deletions

View File

@ -12,7 +12,6 @@
use core::f64; use core::f64;
use std::{ use std::{
cell::Cell,
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
sync::{Arc, LazyLock, Mutex}, sync::{Arc, LazyLock, Mutex},
time::{Duration, Instant}, time::{Duration, Instant},
@ -42,9 +41,9 @@ pub fn info() -> Value {
}) })
} }
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, Clone)]
struct GameInfo { struct GameInfo {
calculation_time: Cell<Duration>, calculation_time: Arc<Mutex<Duration>>,
token_mapping: Arc<BTreeMap<String, SnakeToken>>, token_mapping: Arc<BTreeMap<String, SnakeToken>>,
my_token: SnakeToken, my_token: SnakeToken,
} }
@ -64,7 +63,9 @@ pub fn start(game: &Game, _turn: i32, board: &Board, you: &Battlesnake) {
game_infos.insert( game_infos.insert(
(game.id.clone(), you.id.clone()), (game.id.clone(), you.id.clone()),
GameInfo { GameInfo {
calculation_time: Cell::new(Duration::from_millis(50)), calculation_time: Arc::new(Mutex::new(Duration::from_millis(
u64::from(game.timeout) / 2,
))),
token_mapping, token_mapping,
my_token, my_token,
}, },
@ -94,7 +95,9 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt
let token_mapping = Arc::new(SnakeToken::from_board(board)); let token_mapping = Arc::new(SnakeToken::from_board(board));
let my_token = token_mapping[&you.id]; let my_token = token_mapping[&you.id];
GameInfo { GameInfo {
calculation_time: Cell::new(Duration::from_millis(50)), calculation_time: Arc::new(Mutex::new(Duration::from_millis(
u64::from(game.timeout) / 2,
))),
token_mapping, token_mapping,
my_token, my_token,
} }
@ -117,15 +120,25 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt
} }
// do some latency compensation // do some latency compensation
game_info.calculation_time.set( let deadline = start
game_info.calculation_time.get() + game_info.calculation_time.lock().map_or_else(
|_| Duration::from_millis(u64::from(game.timeout) / 2),
|mut guard| {
let new_duration = *guard
+ Duration::from_millis( + Duration::from_millis(
u64::from( u64::from(game.timeout) * 7 / 8
game.timeout * 3 / 4 - you.latency.parse().unwrap_or(game.timeout * 3 / 4), - you
) / 3, .latency
), .parse()
.unwrap_or(u64::from(game.timeout) * 7 / 8),
);
*guard = new_duration.clamp(
Duration::from_millis(1),
Duration::from_millis(u64::from(game.timeout) * 7 / 8),
);
*guard
},
); );
let deadline = start + game_info.calculation_time.get();
let mut tree = Node::default(); let mut tree = Node::default();

View File

@ -252,6 +252,7 @@ fn run_production(port: u16) -> Result<Child, DynError> {
format!("ROCKET_PORT={}", port).as_str(), format!("ROCKET_PORT={}", port).as_str(),
"--network=host", "--network=host",
"-i", "-i",
"--rm",
"docker.mkaenner.de/snake:latest", "docker.mkaenner.de/snake:latest",
]) ])
.spawn()?; .spawn()?;