run bot process in client
This commit is contained in:
parent
ff061f2a7a
commit
028d4a99e4
3 changed files with 28 additions and 6 deletions
|
@ -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"
|
||||
|
|
2
planetwars-client/simplebot.toml
Normal file
2
planetwars-client/simplebot.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
name = "simplebot"
|
||||
command = ["python", "../simplebot/simplebot.py"]
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue