diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-25 14:45:05 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-25 14:45:05 +0100 |
commit | d0a0fcfdeda2c315a13ddd96b4fca958da0d9858 (patch) | |
tree | 6fb53e3b6e2999ac0c9e35f390d6be26a438a634 /planetwars-localdev/src/match_runner/mod.rs | |
parent | d0af8d3bbff506d4f4caf03750c3b85c69c4f168 (diff) | |
download | planetwars.dev-d0a0fcfdeda2c315a13ddd96b4fca958da0d9858.tar.xz planetwars.dev-d0a0fcfdeda2c315a13ddd96b4fca958da0d9858.zip |
cli for running matches
Diffstat (limited to 'planetwars-localdev/src/match_runner/mod.rs')
-rw-r--r-- | planetwars-localdev/src/match_runner/mod.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/planetwars-localdev/src/match_runner/mod.rs b/planetwars-localdev/src/match_runner/mod.rs new file mode 100644 index 0000000..2715b96 --- /dev/null +++ b/planetwars-localdev/src/match_runner/mod.rs @@ -0,0 +1,56 @@ +mod bot_runner; +mod match_context; +mod pw_match; + +use std::{ + path::PathBuf, + sync::{Arc, Mutex}, +}; + +use match_context::MatchCtx; +use planetwars_rules::PwConfig; + +use crate::BotConfig; + +use self::match_context::{EventBus, PlayerHandle}; + +pub struct MatchConfig { + pub map_path: PathBuf, + pub log_path: PathBuf, + pub players: Vec<MatchBot>, +} + +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 log_file = std::fs::File::create(config.log_path).expect("could not create log file"); + 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; +} |