From 4a077c7c65eced447c45389acf05007dd571bf26 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 1 Jan 2022 12:10:02 +0100 Subject: [PATCH] extract matchrunner crate from planetwars-cli --- Cargo.toml | 1 + planetwars-cli/Cargo.toml | 2 +- planetwars-cli/src/commands/run_match.rs | 8 ++++---- planetwars-cli/src/lib.rs | 1 - planetwars-cli/src/web/mod.rs | 3 ++- planetwars-matchrunner/Cargo.toml | 16 ++++++++++++++++ .../src}/bot_runner.rs | 0 .../mod.rs => planetwars-matchrunner/src/lib.rs | 9 ++++----- .../src}/match_context.rs | 0 .../src}/pw_match.rs | 0 10 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 planetwars-matchrunner/Cargo.toml rename {planetwars-cli/src/match_runner => planetwars-matchrunner/src}/bot_runner.rs (100%) rename planetwars-cli/src/match_runner/mod.rs => planetwars-matchrunner/src/lib.rs (92%) rename {planetwars-cli/src/match_runner => planetwars-matchrunner/src}/match_context.rs (100%) rename {planetwars-cli/src/match_runner => planetwars-matchrunner/src}/pw_match.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 35061a8..cebf247 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "planetwars-rules", + "planetwars-matchrunner", "planetwars-cli", "planetwars-server", ] diff --git a/planetwars-cli/Cargo.toml b/planetwars-cli/Cargo.toml index e1f0a8e..972a02b 100644 --- a/planetwars-cli/Cargo.toml +++ b/planetwars-cli/Cargo.toml @@ -15,10 +15,10 @@ rand = "0.6" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" toml = "0.5" -planetwars-rules = { path = "../planetwars-rules" } clap = { version = "3.0.0-rc.8", features = ["derive"] } chrono = { version = "0.4", features = ["serde"] } shlex = "1.1" +planetwars-matchrunner = { path = "../planetwars-matchrunner" } rust-embed = "6.3.0" axum = { version = "0.4", features = ["ws"] } diff --git a/planetwars-cli/src/commands/run_match.rs b/planetwars-cli/src/commands/run_match.rs index 868e87c..03868ae 100644 --- a/planetwars-cli/src/commands/run_match.rs +++ b/planetwars-cli/src/commands/run_match.rs @@ -1,9 +1,8 @@ use std::io; use clap::Parser; +use planetwars_matchrunner::{run_match, MatchConfig, MatchPlayer}; -use crate::match_runner::MatchConfig; -use crate::match_runner::{self, MatchPlayer}; use crate::workspace::Workspace; #[derive(Parser)] pub struct RunMatchCommand { @@ -26,7 +25,8 @@ impl RunMatchCommand { let bot = workspace.get_bot(&bot_name)?; players.push(MatchPlayer { name: bot_name.clone(), - bot, + path: bot.path.clone(), + argv: bot.config.get_run_argv(), }); } @@ -37,7 +37,7 @@ impl RunMatchCommand { players, }; - match_runner::run_match(match_config).await; + run_match(match_config).await; println!("match completed successfully"); // TODO: maybe print the match result as well? diff --git a/planetwars-cli/src/lib.rs b/planetwars-cli/src/lib.rs index e5566b0..f67b67f 100644 --- a/planetwars-cli/src/lib.rs +++ b/planetwars-cli/src/lib.rs @@ -1,5 +1,4 @@ mod commands; -mod match_runner; mod web; mod workspace; diff --git a/planetwars-cli/src/web/mod.rs b/planetwars-cli/src/web/mod.rs index a0e452e..f66b0c6 100644 --- a/planetwars-cli/src/web/mod.rs +++ b/planetwars-cli/src/web/mod.rs @@ -8,6 +8,7 @@ use axum::{ AddExtensionLayer, Json, }; use mime_guess; +use planetwars_matchrunner::MatchMeta; use rust_embed::RustEmbed; use serde::{Deserialize, Serialize}; use std::{ @@ -18,7 +19,7 @@ use std::{ sync::Arc, }; -use crate::{match_runner::MatchMeta, workspace::Workspace}; +use crate::workspace::Workspace; struct State { workspace: Workspace, diff --git a/planetwars-matchrunner/Cargo.toml b/planetwars-matchrunner/Cargo.toml new file mode 100644 index 0000000..da05f13 --- /dev/null +++ b/planetwars-matchrunner/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "planetwars-matchrunner" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +futures-core = "0.3" +futures = "0.3" +tokio = { version = "1", features = ["full"] } +rand = "0.6" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +planetwars-rules = { path = "../planetwars-rules" } +chrono = { version = "0.4", features = ["serde"] } \ No newline at end of file diff --git a/planetwars-cli/src/match_runner/bot_runner.rs b/planetwars-matchrunner/src/bot_runner.rs similarity index 100% rename from planetwars-cli/src/match_runner/bot_runner.rs rename to planetwars-matchrunner/src/bot_runner.rs diff --git a/planetwars-cli/src/match_runner/mod.rs b/planetwars-matchrunner/src/lib.rs similarity index 92% rename from planetwars-cli/src/match_runner/mod.rs rename to planetwars-matchrunner/src/lib.rs index fdd02d5..50c6518 100644 --- a/planetwars-cli/src/match_runner/mod.rs +++ b/planetwars-matchrunner/src/lib.rs @@ -12,8 +12,6 @@ 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 { @@ -37,7 +35,8 @@ pub struct PlayerInfo { pub struct MatchPlayer { pub name: String, - pub bot: WorkspaceBot, + pub path: PathBuf, + pub argv: Vec, } pub async fn run_match(config: MatchConfig) { @@ -56,8 +55,8 @@ pub async fn run_match(config: MatchConfig) { .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(), + 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) diff --git a/planetwars-cli/src/match_runner/match_context.rs b/planetwars-matchrunner/src/match_context.rs similarity index 100% rename from planetwars-cli/src/match_runner/match_context.rs rename to planetwars-matchrunner/src/match_context.rs diff --git a/planetwars-cli/src/match_runner/pw_match.rs b/planetwars-matchrunner/src/pw_match.rs similarity index 100% rename from planetwars-cli/src/match_runner/pw_match.rs rename to planetwars-matchrunner/src/pw_match.rs