From 0f80b196149a0fb75d84b61c8bbbeb9a71267129 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 1 Jun 2022 20:19:13 +0200 Subject: set up stub grpc client --- planetwars-client/src/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 planetwars-client/src/main.rs (limited to 'planetwars-client/src') diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs new file mode 100644 index 0000000..9d9bdab --- /dev/null +++ b/planetwars-client/src/main.rs @@ -0,0 +1,21 @@ +pub mod pb { + tonic::include_proto!("grpc.planetwars.bot_api"); +} + +use pb::test_service_client::TestServiceClient; +use pb::{Hello, HelloResponse}; +use tonic::Response; + +#[tokio::main] +async fn main() { + let mut client = TestServiceClient::connect("http://localhost:50051") + .await + .unwrap(); + let response: Response = client + .greet(Hello { + hello_message: "robbe".to_string(), + }) + .await + .unwrap(); + println!("{}", response.get_ref().response); +} -- cgit v1.2.3 From 90ecb13a1772dfdab20a006b421102c0aa584f60 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 5 Jun 2022 21:22:38 +0200 Subject: baby steps towards a working bot api --- planetwars-client/src/main.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'planetwars-client/src') diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 9d9bdab..d995ebc 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -2,20 +2,34 @@ pub mod pb { tonic::include_proto!("grpc.planetwars.bot_api"); } -use pb::test_service_client::TestServiceClient; -use pb::{Hello, HelloResponse}; -use tonic::Response; +use pb::bot_api_service_client::BotApiServiceClient; +use tokio_stream::wrappers::UnboundedReceiverStream; + +use tokio::sync::mpsc; #[tokio::main] async fn main() { - let mut client = TestServiceClient::connect("http://localhost:50051") + let mut client = BotApiServiceClient::connect("http://localhost:50051") .await .unwrap(); - let response: Response = client - .greet(Hello { - hello_message: "robbe".to_string(), - }) + + let (tx, rx) = mpsc::unbounded_channel(); + let mut stream = client + .connect_bot(UnboundedReceiverStream::new(rx)) .await + .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: [] }"#; + tx.send(pb::PlayerRequestResponse { + request_id: message.request_id, + content: response.as_bytes().to_vec(), + }) .unwrap(); - println!("{}", response.get_ref().response); + } + std::mem::drop(tx); + // for clean exit + std::mem::drop(client); } -- cgit v1.2.3 From 2f915af91982073644be94bb2c68e095ffd35596 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 6 Jun 2022 14:25:56 +0200 Subject: send player_id through request metadata --- planetwars-client/src/main.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'planetwars-client/src') diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index d995ebc..0fbcdb2 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -6,13 +6,21 @@ use pb::bot_api_service_client::BotApiServiceClient; use tokio_stream::wrappers::UnboundedReceiverStream; use tokio::sync::mpsc; +use tonic::{metadata::MetadataValue, transport::Channel, Request}; #[tokio::main] async fn main() { - let mut client = BotApiServiceClient::connect("http://localhost:50051") + let channel = Channel::from_static("http://localhost:50051") + .connect() .await .unwrap(); + let mut client = BotApiServiceClient::with_interceptor(channel, |mut req: Request<()>| { + let player_id: MetadataValue<_> = "test_player".parse().unwrap(); + req.metadata_mut().insert("player_id", player_id); + Ok(req) + }); + let (tx, rx) = mpsc::unbounded_channel(); let mut stream = client .connect_bot(UnboundedReceiverStream::new(rx)) -- cgit v1.2.3 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 From e3cf0df4509fe5cb0ad114040f52f61436a6663f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 8 Jun 2022 23:24:32 +0200 Subject: update client to request matches --- planetwars-client/src/main.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'planetwars-client/src') diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 8840a89..9df100d 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -5,10 +5,10 @@ pub mod pb { use pb::bot_api_service_client::BotApiServiceClient; use planetwars_matchrunner::bot_runner::Bot; use serde::Deserialize; -use std::path::PathBuf; +use std::{path::PathBuf, time::Duration}; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; -use tonic::{metadata::MetadataValue, transport::Channel, Request}; +use tonic::{metadata::MetadataValue, transport::Channel, Request, Status}; #[derive(Deserialize)] struct BotConfig { @@ -27,8 +27,24 @@ async fn main() { .await .unwrap(); + let created_match = create_match(channel.clone()).await.unwrap(); + run_player(bot_config, created_match.player_key, channel).await; + tokio::time::sleep(Duration::from_secs(1)).await; +} + +async fn create_match(channel: Channel) -> Result { + let mut client = BotApiServiceClient::new(channel); + let res = client + .create_match(Request::new(pb::MatchRequest { + opponent_name: "simplebot".to_string(), + })) + .await; + res.map(|response| response.into_inner()) +} + +async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel) { let mut client = BotApiServiceClient::with_interceptor(channel, |mut req: Request<()>| { - let player_id: MetadataValue<_> = "test_player".parse().unwrap(); + let player_id: MetadataValue<_> = player_key.parse().unwrap(); req.metadata_mut().insert("player_id", player_id); Ok(req) }); @@ -46,7 +62,6 @@ async fn main() { .unwrap() .into_inner(); while let Some(message) = stream.message().await.unwrap() { - 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, @@ -54,7 +69,4 @@ async fn main() { }) .unwrap(); } - std::mem::drop(tx); - // for clean exit - std::mem::drop(client); } -- cgit v1.2.3 From d1977b95c82f608bc558432cdfba8026aaf0648d Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 9 Jun 2022 20:57:45 +0200 Subject: consistently use player_key and player_id --- planetwars-client/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'planetwars-client/src') diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 9df100d..3ece5b3 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -44,8 +44,8 @@ async fn create_match(channel: Channel) -> Result { async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel) { let mut client = BotApiServiceClient::with_interceptor(channel, |mut req: Request<()>| { - let player_id: MetadataValue<_> = player_key.parse().unwrap(); - req.metadata_mut().insert("player_id", player_id); + let player_key: MetadataValue<_> = player_key.parse().unwrap(); + req.metadata_mut().insert("player_key", player_key); Ok(req) }); -- cgit v1.2.3