planetwars.dev/planetwars-matchrunner/src/lib.rs

133 lines
3.2 KiB
Rust
Raw Normal View History

2022-01-18 19:17:13 +00:00
pub mod bot_runner;
2022-01-22 13:32:43 +00:00
pub mod docker_runner;
2022-01-18 19:17:13 +00:00
pub mod match_context;
2022-02-23 20:08:56 +00:00
pub mod match_log;
2022-01-18 19:17:13 +00:00
pub mod pw_match;
2021-12-25 13:45:05 +00:00
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
2022-01-22 13:32:43 +00:00
use async_trait::async_trait;
2022-02-23 20:08:56 +00:00
use futures::{stream::FuturesOrdered, StreamExt};
2021-12-25 13:45:05 +00:00
use match_context::MatchCtx;
2022-02-23 20:08:56 +00:00
use match_log::{create_log_sink, MatchLogger};
2021-12-25 13:45:05 +00:00
use planetwars_rules::PwConfig;
2021-12-25 20:49:16 +00:00
use serde::{Deserialize, Serialize};
2021-12-25 13:45:05 +00:00
use self::match_context::{EventBus, PlayerHandle};
pub struct MatchConfig {
2021-12-25 20:49:16 +00:00
pub map_name: String,
2021-12-25 13:45:05 +00:00
pub map_path: PathBuf,
pub log_path: PathBuf,
2021-12-28 13:57:41 +00:00
pub players: Vec<MatchPlayer>,
2021-12-25 13:45:05 +00:00
}
2021-12-25 20:49:16 +00:00
#[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,
}
2021-12-28 13:57:41 +00:00
pub struct MatchPlayer {
2022-01-22 13:32:43 +00:00
pub bot_spec: Box<dyn BotSpec>,
}
#[async_trait]
2022-02-03 19:46:40 +00:00
pub trait BotSpec: Send + Sync {
2022-01-22 13:32:43 +00:00
async fn run_bot(
&self,
player_id: u32,
event_bus: Arc<Mutex<EventBus>>,
2022-02-23 20:08:56 +00:00
match_logger: MatchLogger,
2022-01-22 13:32:43 +00:00
) -> Box<dyn PlayerHandle>;
2021-12-25 13:45:05 +00:00
}
2022-04-27 18:08:48 +00:00
pub struct MatchOutcome {
pub winner: Option<usize>,
}
pub async fn run_match(config: MatchConfig) -> MatchOutcome {
2021-12-25 13:45:05 +00:00
let pw_config = PwConfig {
map_file: config.map_path,
max_turns: 100,
};
let event_bus = Arc::new(Mutex::new(EventBus::new()));
2022-02-23 20:08:56 +00:00
let match_logger = create_log_sink(&config.log_path).await;
2021-12-25 13:45:05 +00:00
// start bots
2022-01-22 13:32:43 +00:00
// TODO: what happens when a bot fails?
2021-12-25 13:45:05 +00:00
let players = config
.players
.iter()
.enumerate()
2021-12-28 13:57:41 +00:00
.map(|(player_id, player)| {
2021-12-25 13:45:05 +00:00
let player_id = (player_id + 1) as u32;
2022-02-23 20:08:56 +00:00
start_bot(
player_id,
event_bus.clone(),
2022-03-13 14:20:03 +00:00
player.bot_spec.as_ref(),
2022-02-23 20:08:56 +00:00
match_logger.clone(),
)
2021-12-25 13:45:05 +00:00
})
2022-01-22 13:32:43 +00:00
.collect::<FuturesOrdered<_>>()
// await all results
.collect()
.await;
2021-12-25 20:49:16 +00:00
2022-02-23 20:08:56 +00:00
let match_ctx = MatchCtx::new(event_bus, players, match_logger);
2021-12-25 20:49:16 +00:00
2022-02-23 20:08:56 +00:00
// TODO: is this still needed?
// 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();
2021-12-25 13:45:05 +00:00
2022-04-27 18:08:48 +00:00
let mut match_state = pw_match::PwMatch::create(match_ctx, pw_config);
2021-12-25 13:45:05 +00:00
match_state.run().await;
2022-04-27 18:08:48 +00:00
let final_state = match_state.match_state.state();
let survivors = final_state.living_players();
let winner = if survivors.len() == 1 {
Some(survivors[0])
} else {
None
};
MatchOutcome { winner }
2021-12-25 13:45:05 +00:00
}
2022-01-23 12:23:23 +00:00
// writing this as a closure causes lifetime inference errors
async fn start_bot(
player_id: u32,
event_bus: Arc<Mutex<EventBus>>,
2022-03-13 14:20:03 +00:00
bot_spec: &dyn BotSpec,
2022-02-23 20:08:56 +00:00
match_logger: MatchLogger,
2022-01-23 12:23:23 +00:00
) -> (u32, Box<dyn PlayerHandle>) {
2022-02-23 20:08:56 +00:00
let player_handle = bot_spec.run_bot(player_id, event_bus, match_logger).await;
2022-01-23 12:23:23 +00:00
(player_id, player_handle)
}