diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-22 14:32:43 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-22 14:32:43 +0100 |
commit | f62196d983c04a94b892086a4ea6926bd7b6e4fb (patch) | |
tree | 7e0399883be05dcc78a931df3d7939bb81840e85 /planetwars-matchrunner/src/lib.rs | |
parent | 3dd940321cd7f6e4356afa4e505bac8016c83707 (diff) | |
download | planetwars.dev-f62196d983c04a94b892086a4ea6926bd7b6e4fb.tar.xz planetwars.dev-f62196d983c04a94b892086a4ea6926bd7b6e4fb.zip |
implement docker runner
Diffstat (limited to 'planetwars-matchrunner/src/lib.rs')
-rw-r--r-- | planetwars-matchrunner/src/lib.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/planetwars-matchrunner/src/lib.rs b/planetwars-matchrunner/src/lib.rs index 1f23551..2e4200c 100644 --- a/planetwars-matchrunner/src/lib.rs +++ b/planetwars-matchrunner/src/lib.rs @@ -1,4 +1,5 @@ pub mod bot_runner; +pub mod docker_runner; pub mod match_context; pub mod pw_match; @@ -8,6 +9,8 @@ use std::{ sync::{Arc, Mutex}, }; +use async_trait::async_trait; +use futures::{stream::FuturesOrdered, FutureExt, StreamExt}; use match_context::MatchCtx; use planetwars_rules::PwConfig; use serde::{Deserialize, Serialize}; @@ -35,8 +38,16 @@ pub struct PlayerInfo { pub struct MatchPlayer { pub name: String, - pub path: PathBuf, - pub argv: Vec<String>, + pub bot_spec: Box<dyn BotSpec>, +} + +#[async_trait] +pub trait BotSpec { + async fn run_bot( + &self, + player_id: u32, + event_bus: Arc<Mutex<EventBus>>, + ) -> Box<dyn PlayerHandle>; } pub async fn run_match(config: MatchConfig) { @@ -48,20 +59,22 @@ pub async fn run_match(config: MatchConfig) { let event_bus = Arc::new(Mutex::new(EventBus::new())); // start bots + // TODO: what happens when a bot fails? let players = config .players .iter() .enumerate() .map(|(player_id, player)| { let player_id = (player_id + 1) as u32; - let bot = bot_runner::Bot { - working_dir: player.path.clone(), - argv: player.argv.clone(), - }; - let handle = bot_runner::run_local_bot(player_id, event_bus.clone(), bot); - (player_id, Box::new(handle) as Box<dyn PlayerHandle>) + player + .bot_spec + .run_bot(player_id, event_bus.clone()) + .map(move |handle| (player_id, handle)) }) - .collect(); + .collect::<FuturesOrdered<_>>() + // await all results + .collect() + .await; let mut log_file = std::fs::File::create(config.log_path).expect("could not create log file"); // assemble the math meta struct |