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 5, 2024
1 parent 8c1a129 commit 5f452f3
Show file tree
Hide file tree
Showing 16 changed files with 1,738 additions and 841 deletions.
36 changes: 16 additions & 20 deletions .github/actions/install-core/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,23 @@ runs:
shell: bash
working-directory: ${{ runner.temp }}
run: |
./maa install stable -t0 \
--api-url https://github.com/MaaAssistantArknights/MaaRelease/raw/main/MaaAssistantArknights/api/version/
MAA_CORE_DIR="$(./maa dir lib)"
MAA_RESOURCE_DIR="$(./maa dir resource)"
ls -l "$MAA_CORE_DIR"
ls -l "$MAA_RESOURCE_DIR"
echo "MAA_CORE_DIR=$MAA_CORE_DIR" >> $GITHUB_ENV
echo "MAA_RESOURCE_DIR=$MAA_RESOURCE_DIR" >> $GITHUB_ENV
package_name=$(basename "$(ls "$(./maa dir cache)")")
echo "Downloaded MaaCore package: $package_name"
core_version=${package_name#MAA-v}
core_version=${core_version%%-*}
if [[ $core_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Downloaded MaaCore version: $core_version"
echo "MAA_CORE_VERSION=v$core_version" >> "$GITHUB_ENV"
fi
echo "MAA_CORE_INSTALLED=true" >> "$GITHUB_ENV"
- name: Remove Prebuilt CLI
MAA=MaaAssistantArknights
./maa install beta -t0 \
--api-url "https://github.com/$MAA/MaaRelease/raw/main/$MAA/api/version/"
core_dir=$(./maa dir lib)
resource_dir=$(./maa dir resource)
version=$(./maa version core)
version=${version#MaaCore v}
ls -l "$core_dir"
ls -l "$resource_dir"
{
echo "MAA_CORE_DIR=$core_dir"
echo "MAA_RESOURCE_DIR=$resource_dir"
echo "MAA_CORE_VERSION=v$version"
} >> "$GITHUB_ENV"
- name: Cleanup
shell: bash
working-directory: ${{ runner.temp }}
run: |
./maa cleanup --batch
rm -vrf maa_cli* stable.json maa*
29 changes: 17 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,24 @@ jobs:
env:
MAA_CONFIG_DIR: ${{ github.workspace }}/maa-cli/config_examples
run: |
cargo run -- install stable
ls -l "$(cargo run -- dir library)"
ls -l "$(cargo run -- dir resource)"
ls -l "$(cargo run -- dir cache)"
package_name=$(basename "$(ls "$(cargo run -- dir cache)")")
echo "Downloaded MaaCore package: $package_name"
cargo run -- install beta
core_dir=$(cargo run -- dir library)
resource_dir=$(cargo run -- dir resource)
cache_dir=$(cargo run -- dir cache)
package_name=$(basename "$cache_dir"/MAA-v*)
version=${package_name#MAA-v}
version=${version%%-*}
if [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Downloaded MaaCore version: $version"
echo "MAA_CORE_VERSION=v$version" >> "$GITHUB_ENV"
fi
echo "MAA_CORE_INSTALLED=true" >> "$GITHUB_ENV"
version=${version%%-linux*}
version=${version%%-macos*}
version=${version%%-win*}
ls -l "$core_dir"
ls -l "$resource_dir"
ls -l "$cache_dir"
echo "Downloaded MaaCore version: $version"
{
echo "MAA_CORE_DIR=$core_dir"
echo "MAA_RESOURCE_DIR=$resource_dir"
echo "MAA_CORE_VERSION=v$version"
} >> "$GITHUB_ENV"
- name: Test
run: |
cargo test -- --include-ignored
Expand Down
157 changes: 24 additions & 133 deletions maa-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,53 +112,51 @@ 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
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 +564,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 5f452f3

Please sign in to comment.