Skip to content

Commit

Permalink
feat!: add reclamation and improve fight, roguelike, copilot commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Sep 8, 2024
1 parent dcea6df commit 369b522
Show file tree
Hide file tree
Showing 16 changed files with 1,851 additions and 808 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions maa-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ tar = { version = "0.4.40", optional = true }

# Logging support
log = "0.4.20"
color-print = "0.3.6"

[dependencies.env_logger]
version = "0.11"
Expand Down
158 changes: 25 additions & 133 deletions maa-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,53 +112,52 @@ pub(crate) enum Command {
/// Startup Game and Enter Main Screen
#[command(name = "startup")]
StartUp {
/// Client type of the game client
///
/// The client type of the game client, used to launch the game client.
/// If not specified, the client will not be launched.
client: Option<config::task::ClientType>,
/// Account name to switch to
#[arg(long)]
account: Option<String>,
#[command(flatten)]
params: run::preset::StartUpParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Close game client
#[command(name = "closedown")]
CloseDown {
/// Client type of the game client
///
/// The client type of the game client, used to close the game client.
/// If not specified, default to the Official client.
#[arg(default_value = "Official")]
client: config::task::ClientType,
#[command(flatten)]
params: run::preset::CloseDownParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Run fight task
Fight {
/// Stage to fight
#[arg(default_value = "")]
stage: String,
/// medicine to use
#[arg(short, long)]
medicine: Option<i32>,
#[command(flatten)]
params: run::preset::FightParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Run copilot task
Copilot {
/// A code copied from <https://prts.plus> or a json file,
/// such as "maa://12345" or "/your/json/path.json".
uri: String,
#[command(flatten)]
params: run::preset::CopilotParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Run SSSCopilot task
#[command(name = "ssscopilot")]
SSSCopilot {
#[command(flatten)]
params: run::preset::SSSCopilotParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Run rouge-like task
Roguelike {
/// Theme of the game
#[arg(ignore_case = true)]
theme: run::preset::RoguelikeTheme,
#[command(flatten)]
params: run::preset::RoguelikeParams,
#[command(flatten)]
common: run::CommonArgs,
},
/// Run Reclamation Algorithm task
Reclamation {
#[command(flatten)]
params: run::preset::ReclamationParams,
#[command(flatten)]
common: run::CommonArgs,
},
Expand Down Expand Up @@ -566,113 +565,6 @@ mod test {
));
}

#[test]
fn startup() {
assert_matches!(
parse_from(["maa", "startup"]).command,
Command::StartUp {
client: None,
account: None,
common: run::CommonArgs { .. },
}
);

assert_matches!(
parse_from(["maa", "startup", "YoStarEN"]).command,
Command::StartUp {
client: Some(client),
..
} if client == config::task::ClientType::YoStarEN
);

assert_matches!(
parse_from(["maa", "startup", "YoStarEN", "--account", "account"]).command,
Command::StartUp {
client: Some(client),
account: Some(account),
..
} if client == config::task::ClientType::YoStarEN && account == "account"
);
}

#[test]
fn closedown() {
assert_matches!(
parse_from(["maa", "closedown"]).command,
Command::CloseDown {
client: config::task::ClientType::Official,
common: run::CommonArgs { .. }
}
);

assert_matches!(
parse_from(["maa", "closedown", "YoStarEN"]).command,
Command::CloseDown {
client: config::task::ClientType::YoStarEN,
common: run::CommonArgs { .. }
}
);
}

#[test]
fn fight() {
assert_matches!(
parse_from(["maa", "fight", "1-7"]).command,
Command::Fight {
stage,
..
} if stage == "1-7"
);

assert_matches!(
parse_from(["maa", "fight", "1-7", "-m", "1"]).command,
Command::Fight {
stage,
medicine: Some(medicine),
..
} if stage == "1-7" && medicine == 1
);

assert_matches!(
parse_from(["maa", "fight", "1-7", "--medicine", "1"]).command,
Command::Fight {
stage,
medicine: Some(medicine),
..
} if stage == "1-7" && medicine == 1
);
}

#[test]
fn copilot() {
assert_matches!(
parse_from(["maa", "copilot", "maa://12345"]).command,
Command::Copilot {
uri,
..
} if uri == "maa://12345"
);

assert_matches!(
parse_from(["maa", "copilot", "/your/json/path.json"]).command,
Command::Copilot {
uri,
common: run::CommonArgs { .. }
} if uri == "/your/json/path.json"
);
}

#[test]
fn rougelike() {
assert_matches!(
parse_from(["maa", "roguelike", "phantom"]).command,
Command::Roguelike {
theme,
..
} if matches!(theme, run::preset::RoguelikeTheme::Phantom)
);
}

#[test]
fn convert() {
assert_matches!(
Expand Down
4 changes: 1 addition & 3 deletions maa-cli/src/config/asst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ impl<'de> Deserialize<'de> for AsstConfig {
}
}

impl super::FromFile for AsstConfig {}

#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Deserialize, Clone, Default)]
pub struct ConnectionConfig {
Expand Down Expand Up @@ -232,7 +230,7 @@ pub struct ResourceConfig {
user_resource: bool,
/// Resource base directories, a list of directories containing resource directories
/// Not deserialized from config file
resource_base_dirs: Vec<PathBuf>,
pub(crate) resource_base_dirs: Vec<PathBuf>,
}

impl<'de> Deserialize<'de> for ResourceConfig {
Expand Down
2 changes: 0 additions & 2 deletions maa-cli/src/config/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl CLIConfig {
}
}

impl super::FromFile for CLIConfig {}

pub fn cli_config() -> &'static CLIConfig {
static INSTALLER_CONFIG: OnceLock<CLIConfig> = OnceLock::new();
INSTALLER_CONFIG.get_or_init(|| {
Expand Down
9 changes: 3 additions & 6 deletions maa-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::dirs::{self, Ensure};
use std::fs::{self, File};
use std::path::Path;

use clap::ValueEnum;
use serde_json::Value as JsonValue;

#[derive(Debug)]
Expand Down Expand Up @@ -79,7 +78,7 @@ fn file_not_found(path: impl AsRef<Path>) -> Error {

const SUPPORTED_EXTENSION: [&str; 4] = ["json", "yaml", "yml", "toml"];

#[derive(Clone, Copy, ValueEnum)]
#[derive(Clone, Copy, clap::ValueEnum)]
pub enum Filetype {
#[clap(alias = "j")]
Json,
Expand Down Expand Up @@ -158,6 +157,8 @@ pub trait FromFile: Sized + serde::de::DeserializeOwned {
}
}

impl<T> FromFile for T where T: serde::de::DeserializeOwned {}

pub trait FindFile: FromFile {
/// Find file with supported extension and deserialize it.
///
Expand Down Expand Up @@ -193,8 +194,6 @@ impl<T> FindFile for T where T: FromFile {}

impl<T> FindFileOrDefault for T where T: FromFile + Default {}

impl FromFile for JsonValue {}

pub fn convert(file: &Path, out: Option<&Path>, ft: Option<Filetype>) -> Result<()> {
let ft = ft.or_else(|| {
out.and_then(|path| path.extension())
Expand Down Expand Up @@ -410,8 +409,6 @@ mod tests {
b: String,
}

impl FromFile for TestConfig {}

let test_root = temp_dir().join("find_file");
std::fs::create_dir_all(&test_root).unwrap();

Expand Down
21 changes: 21 additions & 0 deletions maa-cli/src/config/task/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ impl ClientType {
YoStarJP | YoStarKR => 5,
}
}

/// The server type of sample used in the report.
pub const fn server_report(self) -> Option<&'static str> {
match self {
Official | Bilibili => Some("CN"),
YoStarEN => Some("US"),
YoStarJP => Some("JP"),
YoStarKR => Some("KR"),
_ => None,
}
}
}

impl<'de> Deserialize<'de> for ClientType {
Expand Down Expand Up @@ -220,6 +231,16 @@ mod tests {
assert_eq!(YoStarKR.server_time_zone(), 5);
}

#[test]
fn to_server_report() {
assert_eq!(Official.server_report(), Some("CN"));
assert_eq!(Bilibili.server_report(), Some("CN"));
assert_eq!(Txwy.server_report(), None);
assert_eq!(YoStarEN.server_report(), Some("US"));
assert_eq!(YoStarJP.server_report(), Some("JP"));
assert_eq!(YoStarKR.server_report(), Some("KR"));
}

#[test]
fn to_str() {
assert_eq!(Official.to_str(), "Official");
Expand Down
Loading

0 comments on commit 369b522

Please sign in to comment.