From 028d4a99e4033f9289239600c0dd6ec499a99c04 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 7 Jun 2022 20:16:42 +0200 Subject: run bot process in client --- planetwars-client/src/main.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'planetwars-client/src') 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(); } -- cgit v1.2.3