fix latency calculation

standard Duration can't go negative
This commit is contained in:
Max Känner 2024-10-05 18:56:19 +02:00
parent 5cb9c3d202
commit c5b0319ac4

View File

@ -125,18 +125,16 @@ pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Opt
+ 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),
);
let target_latency = game.timeout * 3 / 4;
let latency = you.latency.parse().unwrap_or_else(|e| {
error!("Unable to parse latency: {e}");
target_latency
});
let last_computation_time = u32::try_from(guard.as_millis()).unwrap_or(0);
let computation_time =
(last_computation_time + target_latency).saturating_sub(latency);
*guard =
Duration::from_millis(u64::from(computation_time.clamp(1, target_latency)));
*guard
},
);