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

use clap::Parser;

use crate::match_runner;
use crate::match_runner::MatchBot;
use crate::match_runner::MatchConfig;
use crate::resolve_bot_config;
use crate::WorkspaceConfig;

#[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_root = env::current_dir().unwrap();

        let config_path = workspace_root.join("pw_workspace.toml");

        let map_path = workspace_root.join(format!("maps/{}.json", self.map));

        let timestamp = chrono::Local::now().format("%Y-%m-%d-%H-%M-%S");
        let log_path = workspace_root.join(format!("matches/{}.log", timestamp));

        let config_str = std::fs::read_to_string(config_path).unwrap();
        let workspace_config: WorkspaceConfig = toml::from_str(&config_str).unwrap();

        let players = self
            .bots
            .into_iter()
            .map(|bot_name| {
                let bot_config = workspace_config.bots.get(&bot_name).unwrap().clone();
                let resolved_config = resolve_bot_config(&workspace_root, bot_config);
                MatchBot {
                    name: bot_name,
                    bot_config: resolved_config,
                }
            })
            .collect();

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

        match_runner::run_match(match_config).await;
        println!("match completed successfully");
        // TODO: don't hardcode match path.
        // maybe print the match result as well?
        println!("wrote match log to matches/{}.log", timestamp);
        Ok(())
    }
}