Skip to content

Commit

Permalink
feat!:split startup and closedown from fight (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Jan 12, 2024
1 parent a5bfec6 commit a7e2621
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 45 deletions.
3 changes: 3 additions & 0 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ The main feature of `maa-cli` is to run tasks, including predefined tasks and cu

#### Predefined tasks

- `maa startup [client]`: start the game client and enter the main screen, the `client` is the client type of game, leave it empty to don't start the game;
- `maa closedown`: close the game client;
- `maa fight [stage]`: run a fight task, the `stage` is the stage to fight, like `1-7`, `CE-6`, etc; if not given, it will be queried from user;
- `maa copilot <maa_uri>`: run a copilot task, the `maa_uri` is the URI of a copilot task; it can be `maa://1234` or local file path;
- `maa roguelike [theme]`: run a roguelike task, the `theme` is the theme of roguelike, available themes are `Phantom`, `Mizuki` and `Sami`.

#### Custom tasks

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ OpenSSL 库是 `git2` 在所有平台和 `reqwest` 在 Linux 上的依赖。如

#### 预定义任务

- `maa startup [client]`: 启动游戏并进入主界面,`[client]` 是客户端类型,如果留空则不会启动游戏客户端。
- `maa closedown`: 关闭游戏客户端;
- `maa fight [stage]`: 运行战斗任务,`[stage]` 是关卡名称,例如 `1-7`;留空选择上次或者当前关卡;
- `maa copilot <maa_uri>`: 运行自动战斗任务,其中 `<maa_uri>` 是作业的 URI, 其可以是 `maa://1234` 或者本地文件路径 `./1234.json`
- `maa roguelike [theme]`: 运行 roguelike 模式的战斗任务,`[theme]` 是 roguelike 模式的主题,可选值为 `Phantom`, `Mizuki` 以及 `Sami`
Expand Down
6 changes: 3 additions & 3 deletions maa-cli/src/config/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ mod tests {
deps: Map::from([("start_game_enabled".to_string(), true.into())]),
input: SelectD::<String>::new(
vec![
Official,
YoStarEN,
YoStarJP,
Official.as_ref(),
YoStarEN.as_ref(),
YoStarJP.as_ref(),
],
None,
Some("a client type"),
Expand Down
132 changes: 99 additions & 33 deletions maa-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ mod installer;
mod run;
mod value;

use crate::{config::cli, dirs::Ensure, installer::resource, value::userinput::enable_batch_mode};
use crate::{
config::cli, dirs::Ensure, installer::resource, run::preset,
value::userinput::enable_batch_mode,
};

#[cfg(feature = "cli_installer")]
use crate::installer::maa_cli;
Expand Down Expand Up @@ -140,17 +143,34 @@ enum SubCommand {
#[command(flatten)]
common: run::CommonArgs,
},
/// 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)]
common: run::CommonArgs,
},
/// Close game client
#[command(name = "closedown")]
CloseDown {
#[command(flatten)]
common: run::CommonArgs,
},
/// Run fight task
Fight {
/// Stage to fight
#[arg(default_value = "")]
stage: String,
/// Run startup task before the fight
#[arg(long)]
startup: bool,
/// Close the game after the fight
#[arg(long)]
closedown: bool,
/// medicine to use
#[arg(short, long)]
medicine: Option<i64>,
#[command(flatten)]
common: run::CommonArgs,
},
Expand All @@ -165,9 +185,8 @@ enum SubCommand {
/// Run rouge-like task
Roguelike {
/// Theme of the game
///
/// The theme of the game, can be one of "Phantom", "Mizuki" and "Sami".
args: run::RoguelikeTheme,
#[arg(ignore_case = true)]
theme: preset::RoguelikeTheme,
#[command(flatten)]
common: run::CommonArgs,
},
Expand Down Expand Up @@ -349,17 +368,22 @@ fn main() -> Result<()> {
}
},
SubCommand::Run { task, common } => run::run_custom(task, common)?,
SubCommand::StartUp {
client,
account,
common,
} => run::run(|_| preset::startup(client, account), common)?,
SubCommand::CloseDown { common } => run::run(|_| preset::closedown(), common)?,
SubCommand::Fight {
stage,
startup,
closedown,
medicine,
common,
} => run::run(|_| run::fight(stage, startup, closedown), common)?,
} => run::run(|_| preset::fight(stage, medicine), common)?,
SubCommand::Copilot { uri, common } => run::run(
|config| run::copilot(uri, config.resource.base_dirs()),
|config| preset::copilot(uri, config.resource.base_dirs()),
common,
)?,
SubCommand::Roguelike { args, common } => run::run(|_| run::roguelike(args), common)?,
SubCommand::Roguelike { theme, common } => run::run(|_| preset::roguelike(theme), common)?,
SubCommand::Convert {
input,
output,
Expand Down Expand Up @@ -703,6 +727,45 @@ mod test {
));
}

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

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

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

#[test]
fn closedown() {
assert_matches!(
CLI::parse_from(["maa", "closedown"]).command,
SubCommand::CloseDown {
common: run::CommonArgs { .. },
}
);
}

#[test]
fn fight() {
assert_matches!(
Expand All @@ -714,15 +777,21 @@ mod test {
);

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

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

Expand All @@ -745,19 +814,16 @@ mod test {
);
}

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

#[test]
fn convert() {
Expand Down
11 changes: 2 additions & 9 deletions maa-cli/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ use callback::summary;
#[cfg(target_os = "macos")]
mod playcover;

mod fight;
pub use fight::fight;

mod copilot;
pub use copilot::copilot;

mod roguelike;
pub use roguelike::{roguelike, Theme as RoguelikeTheme};
pub mod preset;

use crate::{
config::{
Expand Down Expand Up @@ -108,7 +101,7 @@ where
let task = with_asst_config(f)?;
let task_config = task.init()?;
if let Some(client_type) = task_config.client_type {
debug!("Detected client type: {}", client_type.as_ref());
debug!("Detected client type: {}", client_type);
if let Some(resource) = client_type.resource() {
with_mut_asst_config(|config| {
config.resource.use_global_resource(resource);
Expand Down
File renamed without changes.
Loading

0 comments on commit a7e2621

Please sign in to comment.