From 028d4a99e4033f9289239600c0dd6ec499a99c04 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 7 Jun 2022 20:16:42 +0200 Subject: [PATCH] run bot process in client --- planetwars-client/Cargo.toml | 3 +++ planetwars-client/simplebot.toml | 2 ++ planetwars-client/src/main.rs | 29 +++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 planetwars-client/simplebot.toml diff --git a/planetwars-client/Cargo.toml b/planetwars-client/Cargo.toml index 52c3c64..9c68391 100644 --- a/planetwars-client/Cargo.toml +++ b/planetwars-client/Cargo.toml @@ -10,6 +10,9 @@ tokio = { version = "1.15", features = ["full"] } tokio-stream = "0.1.9" prost = "0.10" tonic = "0.7.2" +serde = { version = "1.0", features = ["derive"] } +toml = "0.5" +planetwars-matchrunner = { path = "../planetwars-matchrunner" } [build-dependencies] tonic-build = "0.7.2" diff --git a/planetwars-client/simplebot.toml b/planetwars-client/simplebot.toml new file mode 100644 index 0000000..dfee25c --- /dev/null +++ b/planetwars-client/simplebot.toml @@ -0,0 +1,2 @@ +name = "simplebot" +command = ["python", "../simplebot/simplebot.py"] diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 0fbcdb2..8840a89 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -3,13 +3,25 @@ pub mod pb { } use pb::bot_api_service_client::BotApiServiceClient; -use tokio_stream::wrappers::UnboundedReceiverStream; - +use planetwars_matchrunner::bot_runner::Bot; +use serde::Deserialize; +use std::path::PathBuf; use tokio::sync::mpsc; +use tokio_stream::wrappers::UnboundedReceiverStream; use tonic::{metadata::MetadataValue, transport::Channel, Request}; +#[derive(Deserialize)] +struct BotConfig { + #[allow(dead_code)] + name: String, + command: Vec, +} + #[tokio::main] async fn main() { + let content = std::fs::read_to_string("simplebot.toml").unwrap(); + let bot_config: BotConfig = toml::from_str(&content).unwrap(); + let channel = Channel::from_static("http://localhost:50051") .connect() .await @@ -21,6 +33,12 @@ async fn main() { Ok(req) }); + let mut bot_process = Bot { + working_dir: PathBuf::from("."), + argv: bot_config.command, + } + .spawn_process(); + let (tx, rx) = mpsc::unbounded_channel(); let mut stream = client .connect_bot(UnboundedReceiverStream::new(rx)) @@ -28,12 +46,11 @@ async fn main() { .unwrap() .into_inner(); while let Some(message) = stream.message().await.unwrap() { - let state = String::from_utf8(message.content).unwrap(); - println!("{}", state); - let response = r#"{ moves: [] }"#; + let state = std::str::from_utf8(&message.content).unwrap(); + let moves = bot_process.communicate(&message.content).await.unwrap(); tx.send(pb::PlayerRequestResponse { request_id: message.request_id, - content: response.as_bytes().to_vec(), + content: moves.as_bytes().to_vec(), }) .unwrap(); }