Skip to content

Commit

Permalink
feat: subcommand roguelike (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Dec 13, 2023
1 parent 253b607 commit 4b88288
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ OpenSSL 库是 `git2` 在所有平台和 `reqwest` 在 Linux 上的依赖。如

#### 预定义任务

- `maa fight [stage]`: 运行战斗任务, `<stage>` 是关卡名称,例如 `1-7`;如果 `stage` 为空,那么 `maa` 将询问你要刷的关卡,然后运行战斗任务;
- `maa fight [stage]`: 运行战斗任务,`[stage]` 是关卡名称,例如 `1-7`;如果 `stage` 为空,那么 `maa` 将询问你要刷的关卡,然后运行战斗任务;
- `maa copilot <maa_uri>`: 运行自动战斗任务,其中 `<maa_uri>` 是作业的 URI, 其可以是 `maa://1234` 或者本地文件路径 `./1234.json`
- `maa roguelike [theme]`: 运行 roguelike 模式的战斗任务,`[theme]` 是 roguelike 模式的主题,可选值为 `phantom`, `mizuki` 以及 `sami`,如果留空,那么你将需要在 `maa` 询问后手动输入主题,

#### 自定义任务

Expand Down
6 changes: 6 additions & 0 deletions maa-cli/src/config/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ mod tests {
mod task_config {
use super::*;

impl TaskConfig {
pub fn tasks(&self) -> &[Task] {
&self.tasks
}
}

mod serde {
use super::*;

Expand Down
55 changes: 49 additions & 6 deletions maa-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ enum SubCommand {
#[command(flatten)]
common: run::CommonArgs,
},
/// Run rouge-like task
Roguelike {
/// Theme of the game
///
/// The theme of the game, can be one of "Phantom", "Mizuki" and "Sami".
/// If not specified, it will be asked in the game.
theme: Option<run::RoguelikeTheme>,
#[command(flatten)]
common: run::CommonArgs,
},
/// List all available tasks
List,
/// Generate completion script for given shell
Expand Down Expand Up @@ -286,6 +296,7 @@ fn main() -> Result<()> {
|config| run::copilot(uri, config.resource.base_dirs()),
common,
)?,
SubCommand::Roguelike { theme, common } => run::run(|_| run::roguelike(theme), common)?,
SubCommand::List => {
let task_dir = dirs::config().join("tasks");
if !task_dir.exists() {
Expand Down Expand Up @@ -600,20 +611,52 @@ mod test {
));
}

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

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

#[test]
fn rougelike() {
assert_matches!(
CLI::parse_from(["maa", "roguelike"]).command,
SubCommand::Roguelike { theme: None, .. }
);

assert_matches!(
CLI::parse_from(["maa", "roguelike", "phantom"]).command,
SubCommand::Roguelike {
theme: Some(theme),
..
} if matches!(theme, run::RoguelikeTheme::Phantom)
);
}

#[test]
fn list() {
assert!(matches!(
CLI::parse_from(["maa", "list"]).command,
SubCommand::List
));
assert_matches!(CLI::parse_from(["maa", "list"]).command, SubCommand::List);
}

#[test]
fn complete() {
assert!(matches!(
assert_matches!(
CLI::parse_from(["maa", "complete", "bash"]).command,
SubCommand::Complete { shell: Shell::Bash }
));
);
}
}
}
3 changes: 3 additions & 0 deletions maa-cli/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use fight::fight;
mod copilot;
pub use copilot::copilot;

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

use crate::{
config::{
asst::{with_asst_config, with_mut_asst_config, AsstConfig},
Expand Down
97 changes: 97 additions & 0 deletions maa-cli/src/run/roguelike.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::{
config::task::{
task_type::MAATask,
value::input::{BoolInput, Input},
Task, TaskConfig, Value,
},
object,
};

use anyhow::Result;
use clap::ValueEnum;

#[derive(ValueEnum, Clone, Copy)]
pub enum Theme {
Phantom,
Mizuki,
Sami,
}

impl Theme {
pub fn to_str(self) -> &'static str {
match self {
Theme::Phantom => "Phantom",
Theme::Mizuki => "Mizuki",
Theme::Sami => "Sami",
}
}
}

pub fn roguelike(theme: Option<Theme>) -> Result<TaskConfig> {
let mut task_config = TaskConfig::new();

let theme = if let Some(theme) = theme {
Value::String(theme.to_str().into())
} else {
Value::InputString(Input::<String>::new(Some("Sami"), Some("theme")).into())
};

// TODO: better prompt and options
task_config.push(Task::new_with_default(
MAATask::Roguelike,
object!(
"theme" => theme,
"mode" => Input::<i64>::new(Some(0), Some("mode")),
"squad" => Input::<String>::new::<String, &str>(None, Some("a squad name")),
"core_char" => Input::<String>::new::<String, &str>(None, Some("a operator name")),
"use_support" => BoolInput::new(Some(true), Some("use support")),
),
));

Ok(task_config)
}

#[cfg(test)]
mod tests {
use super::*;

use crate::config::task::task_type::TaskOrUnknown;

#[test]
fn theme_to_str() {
assert_eq!(Theme::Phantom.to_str(), "Phantom");
assert_eq!(Theme::Mizuki.to_str(), "Mizuki");
assert_eq!(Theme::Sami.to_str(), "Sami");
}

#[test]
fn test_roguelike() {
assert_eq!(
roguelike(None).unwrap().tasks()[0],
Task::new_with_default(
TaskOrUnknown::MAATask(MAATask::Roguelike),
object!(
"theme" => Value::InputString(Input::<String>::new(Some("Sami"), Some("theme")).into()),
"mode" => Input::<i64>::new(Some(0), Some("mode")),
"squad" => Input::<String>::new::<String, &str>(None, Some("a squad name")),
"core_char" => Input::<String>::new::<String, &str>(None, Some("a operator name")),
"use_support" => BoolInput::new(Some(true), Some("use support")),
),
)
);

assert_eq!(
roguelike(Some(Theme::Phantom)).unwrap().tasks()[0],
Task::new_with_default(
TaskOrUnknown::MAATask(MAATask::Roguelike),
object!(
"theme" => Value::String("Phantom".into()),
"mode" => Input::<i64>::new(Some(0), Some("mode")),
"squad" => Input::<String>::new::<String, &str>(None, Some("a squad name")),
"core_char" => Input::<String>::new::<String, &str>(None, Some("a operator name")),
"use_support" => BoolInput::new(Some(true), Some("use support")),
),
)
);
}
}

0 comments on commit 4b88288

Please sign in to comment.