Skip to content

Commit

Permalink
Merge pull request #88 from LordBlackhawk/bugfix/fixed-leave-game
Browse files Browse the repository at this point in the history
calling leave() resulted in hanging bot
  • Loading branch information
UltraMachine authored Nov 28, 2023
2 parents f2d0c9e + 2e9289b commit 40ee6d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ pub struct Bot {
pub(crate) process: Option<Child>,
pub(crate) api: Option<API>,
pub(crate) game_step: Rs<LockU32>,
pub(crate) game_left: bool,
#[doc(hidden)]
pub disable_fog: bool,
/// Actual race of your bot.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,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() {
Expand Down

0 comments on commit 40ee6d5

Please sign in to comment.