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"
|
tokio-stream = "0.1.9"
|
||||||
prost = "0.10"
|
prost = "0.10"
|
||||||
tonic = "0.7.2"
|
tonic = "0.7.2"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
toml = "0.5"
|
||||||
|
planetwars-matchrunner = { path = "../planetwars-matchrunner" }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = "0.7.2"
|
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 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::sync::mpsc;
|
||||||
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
use tonic::{metadata::MetadataValue, transport::Channel, Request};
|
use tonic::{metadata::MetadataValue, transport::Channel, Request};
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct BotConfig {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
name: String,
|
||||||
|
command: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn 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")
|
let channel = Channel::from_static("http://localhost:50051")
|
||||||
.connect()
|
.connect()
|
||||||
.await
|
.await
|
||||||
|
@ -21,6 +33,12 @@ async fn main() {
|
||||||
Ok(req)
|
Ok(req)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let mut bot_process = Bot {
|
||||||
|
working_dir: PathBuf::from("."),
|
||||||
|
argv: bot_config.command,
|
||||||
|
}
|
||||||
|
.spawn_process();
|
||||||
|
|
||||||
let (tx, rx) = mpsc::unbounded_channel();
|
let (tx, rx) = mpsc::unbounded_channel();
|
||||||
let mut stream = client
|
let mut stream = client
|
||||||
.connect_bot(UnboundedReceiverStream::new(rx))
|
.connect_bot(UnboundedReceiverStream::new(rx))
|
||||||
|
@ -28,12 +46,11 @@ async fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_inner();
|
.into_inner();
|
||||||
while let Some(message) = stream.message().await.unwrap() {
|
while let Some(message) = stream.message().await.unwrap() {
|
||||||
let state = String::from_utf8(message.content).unwrap();
|
let state = std::str::from_utf8(&message.content).unwrap();
|
||||||
println!("{}", state);
|
let moves = bot_process.communicate(&message.content).await.unwrap();
|
||||||
let response = r#"{ moves: [] }"#;
|
|
||||||
tx.send(pb::PlayerRequestResponse {
|
tx.send(pb::PlayerRequestResponse {
|
||||||
request_id: message.request_id,
|
request_id: message.request_id,
|
||||||
content: response.as_bytes().to_vec(),
|
content: moves.as_bytes().to_vec(),
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue