aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-11-20 11:37:45 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-11-20 11:37:45 +0100
commitb75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a (patch)
treed1ff7ccde70b5101949c64ed5d9786a7e07950cc
parentd0948b54f409d248765c39a44048afb07170edaf (diff)
downloadplanetwars.dev-b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a.tar.xz
planetwars.dev-b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a.zip
don't crash client when bot crashes
-rw-r--r--Cargo.lock1
-rw-r--r--planetwars-client/Cargo.toml1
-rw-r--r--planetwars-client/src/main.rs26
3 files changed, 25 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 947c300..58149cb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1568,6 +1568,7 @@ dependencies = [
"prost",
"serde",
"shlex",
+ "thiserror",
"tokio",
"tokio-stream",
"toml",
diff --git a/planetwars-client/Cargo.toml b/planetwars-client/Cargo.toml
index c83950b..2ba646b 100644
--- a/planetwars-client/Cargo.toml
+++ b/planetwars-client/Cargo.toml
@@ -15,6 +15,7 @@ toml = "0.5"
planetwars-matchrunner = { path = "../planetwars-matchrunner" }
clap = { version = "3.2", features = ["derive", "env"]}
shlex = "1.1"
+thiserror = "1.0"
[build-dependencies]
tonic-build = "0.7.2"
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(())
}