diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-06-07 20:16:42 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-06-07 20:16:42 +0200 |
commit | 028d4a99e4033f9289239600c0dd6ec499a99c04 (patch) | |
tree | 0b1dd536af58d981b7053f963347a4ea76890603 /planetwars-client | |
parent | ff061f2a7a0e3a62792ffcef8f2cd3ec6ddc5710 (diff) | |
download | planetwars.dev-028d4a99e4033f9289239600c0dd6ec499a99c04.tar.xz planetwars.dev-028d4a99e4033f9289239600c0dd6ec499a99c04.zip |
run bot process in client
Diffstat (limited to 'planetwars-client')
-rw-r--r-- | planetwars-client/Cargo.toml | 3 | ||||
-rw-r--r-- | planetwars-client/simplebot.toml | 2 | ||||
-rw-r--r-- | planetwars-client/src/main.rs | 29 |
3 files changed, 28 insertions, 6 deletions
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<String>, +} + #[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(); } |