aboutsummaryrefslogtreecommitdiff
path: root/planetwars-cli/src/match_runner/mod.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-01-01 12:10:02 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-01-01 12:10:02 +0100
commit4a077c7c65eced447c45389acf05007dd571bf26 (patch)
treef291c61c6164a58fb1d3ab66952d07d0b7e4609c /planetwars-cli/src/match_runner/mod.rs
parente145947d052450618af3ba094e66a27c3c7f86e4 (diff)
downloadplanetwars.dev-4a077c7c65eced447c45389acf05007dd571bf26.tar.xz
planetwars.dev-4a077c7c65eced447c45389acf05007dd571bf26.zip
extract matchrunner crate from planetwars-cli
Diffstat (limited to 'planetwars-cli/src/match_runner/mod.rs')
-rw-r--r--planetwars-cli/src/match_runner/mod.rs91
1 files changed, 0 insertions, 91 deletions
diff --git a/planetwars-cli/src/match_runner/mod.rs b/planetwars-cli/src/match_runner/mod.rs
deleted file mode 100644
index fdd02d5..0000000
--- a/planetwars-cli/src/match_runner/mod.rs
+++ /dev/null
@@ -1,91 +0,0 @@
-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::workspace::bot::WorkspaceBot;
-
-use self::match_context::{EventBus, PlayerHandle};
-
-pub struct MatchConfig {
- pub map_name: String,
- pub map_path: PathBuf,
- pub log_path: PathBuf,
- pub players: Vec<MatchPlayer>,
-}
-
-#[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 MatchPlayer {
- pub name: String,
- pub bot: WorkspaceBot,
-}
-
-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, player)| {
- let player_id = (player_id + 1) as u32;
- let bot = bot_runner::Bot {
- working_dir: player.bot.path.clone(),
- argv: player.bot.config.get_run_argv(),
- };
- 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;
-}