Compare commits

..

No commits in common. "3d58e5331b2ebacbbeb3a837ff7c69f82c21945d" and "12276bc3544e5b5d45959ab4a8c536d1f20329ce" have entirely different histories.

2 changed files with 14 additions and 28 deletions

View File

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

View File

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