diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-20 11:37:45 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-20 11:37:45 +0100 |
commit | b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a (patch) | |
tree | d1ff7ccde70b5101949c64ed5d9786a7e07950cc /planetwars-client/src/main.rs | |
parent | d0948b54f409d248765c39a44048afb07170edaf (diff) | |
download | planetwars.dev-b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a.tar.xz planetwars.dev-b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a.zip |
don't crash client when bot crashes
Diffstat (limited to 'planetwars-client/src/main.rs')
-rw-r--r-- | planetwars-client/src/main.rs | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 1821fd3..6ae2a31 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -79,7 +79,12 @@ async fn main() { ) .await .unwrap(); - run_player(bot_config, created_match.player_key, channel).await; + match run_player(bot_config, created_match.player_key, channel).await { + Ok(()) => (), + Err(RunPlayerError::RunBotError(err)) => { + println!("Error running bot: {}", err) + } + } println!( "Match completed. Watch the replay at {}", created_match.match_url @@ -102,7 +107,17 @@ async fn create_match( res.map(|response| response.into_inner()) } -async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel) { +#[derive(thiserror::Error, Debug)] +enum RunPlayerError { + #[error("error running bot")] + RunBotError(std::io::Error), +} + +async fn run_player( + bot_config: BotConfig, + player_key: String, + channel: Channel, +) -> Result<(), RunPlayerError> { let mut client = ClientApiServiceClient::with_interceptor(channel, |mut req: Request<()>| { let player_key: MetadataValue<_> = player_key.parse().unwrap(); req.metadata_mut().insert("player_key", player_key); @@ -128,7 +143,10 @@ async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel) while let Some(message) = stream.message().await.unwrap() { match message.server_message { Some(pb::PlayerApiServerMessageType::ActionRequest(req)) => { - let moves = bot_process.communicate(&req.content).await.unwrap(); + let moves = bot_process + .communicate(&req.content) + .await + .map_err(RunPlayerError::RunBotError)?; let action = pb::PlayerAction { action_request_id: req.action_request_id, content: moves.as_bytes().to_vec(), @@ -141,4 +159,6 @@ async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel) _ => {} // pass } } + + Ok(()) } |