aboutsummaryrefslogtreecommitdiff
path: root/planetwars-client/src/main.rs
blob: 8840a89c26b00e170b93ed53d3f35ddf0204f5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
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
        .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 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 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: moves.as_bytes().to_vec(),
        })
        .unwrap();
    }
    std::mem::drop(tx);
    // for clean exit
    std::mem::drop(client);
}