diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-26 21:54:26 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-26 21:54:26 +0100 |
commit | 0d03a0fbc214dd3fb9a8af562f9d88cccaf5f2c0 (patch) | |
tree | aa93706592a050fc01a220546b73889faeab80fe /planetwars-cli/src/commands/run_match.rs | |
parent | c04d4a449bd147c632c0b6ceae04f0514803b66f (diff) | |
download | planetwars.dev-0d03a0fbc214dd3fb9a8af562f9d88cccaf5f2c0.tar.xz planetwars.dev-0d03a0fbc214dd3fb9a8af562f9d88cccaf5f2c0.zip |
refactor commands
Diffstat (limited to 'planetwars-cli/src/commands/run_match.rs')
-rw-r--r-- | planetwars-cli/src/commands/run_match.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/planetwars-cli/src/commands/run_match.rs b/planetwars-cli/src/commands/run_match.rs new file mode 100644 index 0000000..a5512f4 --- /dev/null +++ b/planetwars-cli/src/commands/run_match.rs @@ -0,0 +1,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::ProjectConfig; + +#[derive(Parser)] +pub struct RunMatchCommand { + /// map name + map: String, + /// bot names + bots: Vec<String>, +} + +impl RunMatchCommand { + pub async fn run(self) -> io::Result<()> { + let project_dir = env::current_dir().unwrap(); + + let config_path = project_dir.join("pw_project.toml"); + + let map_path = project_dir.join(format!("maps/{}.json", self.map)); + + let timestamp = chrono::Local::now().format("%Y-%m-%d-%H-%M-%S"); + let log_path = project_dir.join(format!("matches/{}.log", timestamp)); + + let config_str = std::fs::read_to_string(config_path).unwrap(); + let project_config: ProjectConfig = toml::from_str(&config_str).unwrap(); + + let players = self + .bots + .into_iter() + .map(|bot_name| { + let bot_config = project_config.bots.get(&bot_name).unwrap().clone(); + let resolved_config = resolve_bot_config(&project_dir, 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(()) + } +} |