planetwars-client: configure bot config path and opponent name

This commit is contained in:
Ilion Beyst 2022-07-22 22:27:12 +02:00
parent 1011015b29
commit b0725c21df
2 changed files with 19 additions and 6 deletions

View file

@ -13,6 +13,7 @@ tonic = "0.7.2"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.5" toml = "0.5"
planetwars-matchrunner = { path = "../planetwars-matchrunner" } planetwars-matchrunner = { path = "../planetwars-matchrunner" }
clap = { version = "3.2", features = ["derive"]}
[build-dependencies] [build-dependencies]
tonic-build = "0.7.2" tonic-build = "0.7.2"

View file

@ -2,6 +2,7 @@ pub mod pb {
tonic::include_proto!("grpc.planetwars.bot_api"); tonic::include_proto!("grpc.planetwars.bot_api");
} }
use clap::Parser;
use pb::bot_api_service_client::BotApiServiceClient; use pb::bot_api_service_client::BotApiServiceClient;
use planetwars_matchrunner::bot_runner::Bot; use planetwars_matchrunner::bot_runner::Bot;
use serde::Deserialize; use serde::Deserialize;
@ -10,6 +11,15 @@ use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
use tonic::{metadata::MetadataValue, transport::Channel, Request, Status}; use tonic::{metadata::MetadataValue, transport::Channel, Request, Status};
#[derive(clap::Parser)]
struct PlayMatch {
#[clap(value_parser)]
bot_config_path: String,
#[clap(value_parser)]
opponent_name: String,
}
#[derive(Deserialize)] #[derive(Deserialize)]
struct BotConfig { struct BotConfig {
#[allow(dead_code)] #[allow(dead_code)]
@ -19,7 +29,9 @@ struct BotConfig {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let content = std::fs::read_to_string("simplebot.toml").unwrap(); let play_match = PlayMatch::parse();
let content = std::fs::read_to_string(play_match.bot_config_path).unwrap();
let bot_config: BotConfig = toml::from_str(&content).unwrap(); let bot_config: BotConfig = toml::from_str(&content).unwrap();
let channel = Channel::from_static("http://localhost:50051") let channel = Channel::from_static("http://localhost:50051")
@ -27,7 +39,9 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
let created_match = create_match(channel.clone()).await.unwrap(); let created_match = create_match(channel.clone(), play_match.opponent_name)
.await
.unwrap();
run_player(bot_config, created_match.player_key, channel).await; run_player(bot_config, created_match.player_key, channel).await;
println!( println!(
"Match completed. Watch the replay at {}", "Match completed. Watch the replay at {}",
@ -36,12 +50,10 @@ async fn main() {
tokio::time::sleep(Duration::from_secs(1)).await; tokio::time::sleep(Duration::from_secs(1)).await;
} }
async fn create_match(channel: Channel) -> Result<pb::CreatedMatch, Status> { async fn create_match(channel: Channel, opponent_name: String) -> Result<pb::CreatedMatch, Status> {
let mut client = BotApiServiceClient::new(channel); let mut client = BotApiServiceClient::new(channel);
let res = client let res = client
.create_match(Request::new(pb::MatchRequest { .create_match(Request::new(pb::MatchRequest { opponent_name }))
opponent_name: "simplebot".to_string(),
}))
.await; .await;
res.map(|response| response.into_inner()) res.map(|response| response.into_inner())
} }