diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-26 21:06:52 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-26 21:07:19 +0100 |
commit | c04d4a449bd147c632c0b6ceae04f0514803b66f (patch) | |
tree | 6c75eea8bbd8cf1be06330ddf4d592b019c6b339 /planetwars-cli/src/match_runner/mod.rs | |
parent | b1e9490f55e4f360c249a107dcc5809a663dec42 (diff) | |
download | planetwars.dev-c04d4a449bd147c632c0b6ceae04f0514803b66f.tar.xz planetwars.dev-c04d4a449bd147c632c0b6ceae04f0514803b66f.zip |
rename to planetwars-cli
Diffstat (limited to 'planetwars-cli/src/match_runner/mod.rs')
-rw-r--r-- | planetwars-cli/src/match_runner/mod.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/planetwars-cli/src/match_runner/mod.rs b/planetwars-cli/src/match_runner/mod.rs new file mode 100644 index 0000000..50b7a3b --- /dev/null +++ b/planetwars-cli/src/match_runner/mod.rs @@ -0,0 +1,91 @@ +mod bot_runner; +mod match_context; +mod pw_match; + +use std::{ + io::Write, + path::PathBuf, + sync::{Arc, Mutex}, +}; + +use match_context::MatchCtx; +use planetwars_rules::PwConfig; +use serde::{Deserialize, Serialize}; + +use crate::BotConfig; + +use self::match_context::{EventBus, PlayerHandle}; + +pub struct MatchConfig { + pub map_name: String, + pub map_path: PathBuf, + pub log_path: PathBuf, + pub players: Vec<MatchBot>, +} + +#[derive(Serialize, Deserialize)] +pub struct MatchMeta { + pub map_name: String, + pub timestamp: chrono::DateTime<chrono::Local>, + pub players: Vec<PlayerInfo>, +} + +#[derive(Serialize, Deserialize)] +pub struct PlayerInfo { + pub name: String, +} + +pub struct MatchBot { + pub name: String, + pub bot_config: BotConfig, +} + +pub async fn run_match(config: MatchConfig) { + let pw_config = PwConfig { + map_file: config.map_path, + max_turns: 100, + }; + + let event_bus = Arc::new(Mutex::new(EventBus::new())); + + // start bots + let players = config + .players + .iter() + .enumerate() + .map(|(player_id, bot)| { + let player_id = (player_id + 1) as u32; + let bot = bot_runner::Bot { + working_dir: bot.bot_config.path.clone(), + argv: bot.bot_config.argv.clone(), + }; + let handle = bot_runner::run_local_bot(player_id, event_bus.clone(), bot); + (player_id, Box::new(handle) as Box<dyn PlayerHandle>) + }) + .collect(); + let mut log_file = std::fs::File::create(config.log_path).expect("could not create log file"); + + // assemble the math meta struct + let match_meta = MatchMeta { + map_name: config.map_name.clone(), + timestamp: chrono::Local::now(), + players: config + .players + .iter() + .map(|bot| PlayerInfo { + name: bot.name.clone(), + }) + .collect(), + }; + write!( + log_file, + "{}\n", + serde_json::to_string(&match_meta).unwrap() + ) + .unwrap(); + + let match_ctx = MatchCtx::new(event_bus, players, log_file); + + let match_state = pw_match::PwMatch::create(match_ctx, pw_config); + match_state.run().await; +} |