aboutsummaryrefslogtreecommitdiff
path: root/planetwars-cli/src/commands/run_match.rs
blob: 6341b3131abf0568ea5771514cb5e56aecf06965 (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
use std::io;

use clap::Parser;

use crate::match_runner::MatchConfig;
use crate::match_runner::{self, MatchPlayer};
use crate::workspace::Workspace;
#[derive(Parser)]
pub struct RunMatchCommand {
    /// map name
    map: String,
    /// bot names
    bots: Vec<String>,
}

impl RunMatchCommand {
    pub async fn run(self) -> io::Result<()> {
        let workspace = Workspace::open_current_dir()?;

        let map_path = workspace.map_path(&self.map);
        let timestamp = chrono::Local::now().format("%Y-%m-%d-%H-%M-%S");
        let log_path = workspace.match_path(&format!("{}-{}", &self.map, &timestamp));

        let mut players = Vec::new();
        for bot_name in &self.bots {
            let bot = workspace.get_bot(&bot_name)?;
            players.push(MatchPlayer {
                name: bot_name.clone(),
                bot,
            });
        }

        let match_config = MatchConfig {
            map_name: self.map,
            map_path,
            log_path: log_path.clone(),
            players,
        };

        match_runner::run_match(match_config).await;
        println!("match completed successfully");
        // TODO: maybe print the match result as well?
        println!("wrote match log to {}", log_path.to_str().unwrap());
        Ok(())
    }
}