aboutsummaryrefslogtreecommitdiff
path: root/planetwars-client
diff options
context:
space:
mode:
Diffstat (limited to 'planetwars-client')
-rw-r--r--planetwars-client/Cargo.toml18
-rw-r--r--planetwars-client/build.rs9
-rw-r--r--planetwars-client/simplebot.toml2
-rw-r--r--planetwars-client/src/main.rs72
4 files changed, 101 insertions, 0 deletions
diff --git a/planetwars-client/Cargo.toml b/planetwars-client/Cargo.toml
new file mode 100644
index 0000000..9c68391
--- /dev/null
+++ b/planetwars-client/Cargo.toml
@@ -0,0 +1,18 @@
+[package]
+name = "planetwars-client"
+version = "0.0.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+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/build.rs b/planetwars-client/build.rs
new file mode 100644
index 0000000..acabd08
--- /dev/null
+++ b/planetwars-client/build.rs
@@ -0,0 +1,9 @@
+extern crate tonic_build;
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ tonic_build::configure()
+ .build_server(false)
+ .build_client(true)
+ .compile(&["../proto/bot_api.proto"], &["../proto"])?;
+ Ok(())
+}
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
new file mode 100644
index 0000000..3ece5b3
--- /dev/null
+++ b/planetwars-client/src/main.rs
@@ -0,0 +1,72 @@
+pub mod pb {
+ tonic::include_proto!("grpc.planetwars.bot_api");
+}
+
+use pb::bot_api_service_client::BotApiServiceClient;
+use planetwars_matchrunner::bot_runner::Bot;
+use serde::Deserialize;
+use std::{path::PathBuf, time::Duration};
+use tokio::sync::mpsc;
+use tokio_stream::wrappers::UnboundedReceiverStream;
+use tonic::{metadata::MetadataValue, transport::Channel, Request, Status};
+
+#[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
+ .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<pb::CreatedMatch, Status> {
+ 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_key: MetadataValue<_> = player_key.parse().unwrap();
+ req.metadata_mut().insert("player_key", player_key);
+ 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))
+ .await
+ .unwrap()
+ .into_inner();
+ while let Some(message) = stream.message().await.unwrap() {
+ let moves = bot_process.communicate(&message.content).await.unwrap();
+ tx.send(pb::PlayerRequestResponse {
+ request_id: message.request_id,
+ content: moves.as_bytes().to_vec(),
+ })
+ .unwrap();
+ }
+}