From 2e9289b243cf695cc2b61d3482ddfa39f1b06236 Mon Sep 17 00:00:00 2001 From: Lord Blackhawk Date: Thu, 21 Sep 2023 21:19:46 +0200 Subject: [PATCH] calling leave() resulted in hanging bot --- src/bot.rs | 9 +++++---- src/client.rs | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 3f2628f..d5cf080 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -390,6 +390,7 @@ pub struct Bot { pub(crate) process: Option, pub(crate) api: Option, pub(crate) game_step: Rs, + pub(crate) game_left: bool, #[doc(hidden)] pub disable_fog: bool, /// Actual race of your bot. @@ -1746,10 +1747,9 @@ impl Bot { /// /// [`on_end`]: crate::Player::on_end /// [`debug.end_game`]: Debugger::end_game - pub fn leave(&self) -> SC2Result<()> { - let mut req = Request::new(); - req.mut_leave_game(); - self.api().send_request(req) + pub fn leave(&mut self) -> SC2Result<()> { + self.game_left = true; + Ok(()) } pub(crate) fn close_client(&mut self) { @@ -1779,6 +1779,7 @@ impl Default for Bot { fn default() -> Self { Self { game_step: Rs::new(LockU32::new(1)), + game_left: false, disable_fog: false, race: Race::Random, enemy_race: Race::Random, diff --git a/src/client.rs b/src/client.rs index d17f8fb..a52e85d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -653,6 +653,12 @@ where bot.on_event(e)?; } bot.on_step(iteration)?; + if bot.game_left { + let mut req = Request::new(); + req.mut_leave_game(); + bot.api().send_request(req)?; + return Ok(false); + } let bot_actions = bot.get_actions(); if !bot_actions.is_empty() {