Skip to content

Commit

Permalink
feat: allow user input in task definition (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Oct 4, 2023
1 parent c405ec5 commit 9bdf39b
Show file tree
Hide file tree
Showing 16 changed files with 2,215 additions and 662 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

39 changes: 39 additions & 0 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ platform specific resource.

A task should be defined with a TOML or JSON file, the located in `$MAA_CONFIG_DIR/tasks`.

#### Basic structure

A task is consists of multiple subtasks,
available subtasks and params are defined by `type` and `params` fields,
it will passed to MaaCore, see [here](https://maa.plus/docs/en-us/3.1-INTEGRATION.html#asstappendtask) for more details:
Expand All @@ -121,6 +123,8 @@ type = "StartUp" # the type of maa task
params = { client_type = "Official", start_game_enabled = true } # the params of given task
```

#### Task variants and conditions

If you want to run a task with different params based on some conditions,
you can define multiple variants of a task:
```toml
Expand Down Expand Up @@ -189,6 +193,41 @@ blacklist = ["碳", "家具", "加急许可"]
condition = { type = "Time", start = "18:00:00" }
```

#### User input

In some case, you may want to input some value at runtime, instead of hard code it in the task file.
Such as the stage to fight, the item to buy, etc.
You can specify the value `Input` or `Select` type:

```toml
[[tasks]]
type = "Fight"

# Select a stage to fight
[[tasks.variants]]
condition = { type = "DateTime", start = "2023-08-01T16:00:00", end = "2023-08-21T03:59:59" }
[tasks.variants.params.stage]
alternatives = ["SL-6", "SL-7", "SL-8"] # the alternatives of stage, at least one alternative should be given
description = "a stage to fight in summer event" # description of the input, optional

# Task without input
[[tasks.variants]]
condition = { type = "Weekday", weekdays = ["Tue", "Thu", "Sat"] }
params = { stage = "CE-6" }

# Input a stage to fight
[[tasks.variants]]
[tasks.variants.params.stage]
default = "1-7" # default value of stage, optional (if not given, user can input empty value to re-prompt)
description = "a stage to fight" # description of the input, optional
```

For `Input` type, a prompt will be shown to ask user to input a value.
If the default value is given, it will be used if user input empty value, otherwise it will re-prompt.
For `Select` type, a prompt will be shown to ask user to select a value from alternatives (by index).
If user input is not a valid index, it will re-prompt.


Example of config file can be found at [`config_examples` directory](./config_examples).
Anothor example can be found at my [dotfiles](https://github.com/wangl-cc/dotfiles/tree/master/.config/maa).

Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,18 @@ resources = ["platform_diff/macOS"]
每一个任务都是一个单独的文件,它们储存在`$MAA_CONFIG_DIR/tasks`中。
任务文件的格式是`<name>.toml`或者`<name>.json`,其中`<name>`是任务的名字。


#### 基本结构

一个任务文件包含多个子任务,每一个子任务是一个[MAA任务链](https://maa.plus/docs/3.1-集成文档.html#asstappendtask)
```toml
[[tasks]]
type = "StartUp" # maa任务的类型
params = { client_type = "Official", start_game_enabled = true } # maa任务的参数
```

#### 任务条件

如果你想要根据一些条件运行不同参数的任务,你可以定义多个任务的变体:
```toml
[[tasks]]
Expand Down Expand Up @@ -181,6 +187,39 @@ blacklist = ["碳", "家具", "加急许可"]
condition = { type = "Time", start = "18:00:00" }
```

#### 用户输入

对于一些任务,你可能想要在运行时输入一些参数,例如关卡名称。
你可以将对应需要输入的参数设置为 `Input` 或者 `Select` 类型:

```toml
[[tasks]]
type = "Fight"

# 选择一个关卡
[[tasks.variants]]
condition = { type = "DateTime", start = "2023-08-01T16:00:00", end = "2023-08-21T03:59:59" }
[tasks.variants.params.stage]
alternatives = ["SL-6", "SL-7", "SL-8"] # 可选的关卡,必须提供至少一个可选值
description = "a stage to fight in summer event" # 描述,可选

# 无需任何输入
[[tasks.variants]]
condition = { type = "Weekday", weekdays = ["Tue", "Thu", "Sat"] }
params = { stage = "CE-6" }

# 输入一个关卡
[[tasks.variants]]
[tasks.variants.params.stage]
default = "1-7" # 默认的关卡,可选(如果没有默认值,输入空值将会重新提示输入)
description = "a stage to fight" # 描述,可选
```

对于 `Input` 类型,当运行任务时,你将会被提示输入一个值。如果你输入了一个空值,那么默认值将会被使用。
对于 `Select` 类型,当运行任务时,你将会被提示选择一个值 (通过输入可选值的序号)。
注意,当你的输入不是可选值时,你将会被提示重新输入。


配置文件的例子可以在[`config_examples`目录](./config_examples)中找到。
另一个例子是我自己的配置文件,你可以在[这里](https://github.com/wangl-cc/dotfiles/tree/master/.config/maa)找到。

Expand Down
14 changes: 12 additions & 2 deletions config_examples/tasks/daily.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
"end": "2023-08-21T03:59:59"
},
"params": {
"stage": ""
"stage": {
"alternatives": [
"SL-6",
"SL-7",
"SL-8"
],
"description": "a stage to fight in summer event"
}
}
},
{
Expand All @@ -35,7 +42,10 @@
},
{
"params": {
"stage": "1-7"
"stage": {
"default": "1-7",
"description": "a stage to fight"
}
}
}
]
Expand Down
8 changes: 6 additions & 2 deletions config_examples/tasks/daily.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ type = "Fight"

# fight the last stage if on event period
[[tasks.variants]]
params = { stage = "" }
condition = { type = "DateTime", start = "2023-08-01T16:00:00", end = "2023-08-21T03:59:59" }
[tasks.variants.params.stage]
alternatives = ["SL-6", "SL-7", "SL-8"]
description = "a stage to fight in summer event"

# fight CE-6 on Tue, Thu, Sat
[[tasks.variants]]
Expand All @@ -18,7 +20,9 @@ params = { stage = "CE-6" }

# fight 1-7 otherwise
[[tasks.variants]]
params = { stage = "1-7" }
[tasks.variants.params.stage]
default = "1-7"
description = "a stage to fight"

# Mall after 16:00
[[tasks]]
Expand Down
11 changes: 9 additions & 2 deletions config_examples/tasks/daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ tasks:
start: 2023-08-01T16:00:00
end: 2023-08-21T03:59:59
params:
stage: ""
stage:
alternatives:
- SL-6
- SL-7
- SL-8
description: a stage to fight in summer event
- condition:
type: Weekday
weekdays:
Expand All @@ -20,7 +25,9 @@ tasks:
params:
stage: CE-6
- params:
stage: 1-7
stage:
default: 1-7
description: a stage to fight
- type: Mall
variants:
- condition:
Expand Down
3 changes: 3 additions & 0 deletions maa-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ features = ["std", "clock", "serde"]
version = "0.11"
default-features = false
features = ["rustls-tls", "blocking", "stream", "json"]

[dev-dependencies]
serde_test = "1.0.176"
1 change: 1 addition & 0 deletions maa-cli/share/fish/vendor_completions.d/maa.fish
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ complete -c maa -n "__fish_seen_subcommand_from run" -f -a "$(maa list)"
complete -c maa -n "__fish_seen_subcommand_from run" -s a -l addr -d 'ADB serial number of device or MaaTools address set in PlayCover' -r
complete -c maa -n "__fish_seen_subcommand_from run" -s v -l verbose -d 'Output more information, repeat to increase verbosity'
complete -c maa -n "__fish_seen_subcommand_from run" -s q -l quiet -d 'Output less information, repeat to increase quietness'
complete -c maa -n "__fish_seen_subcommand_from run" -s b -l batch -d 'Run task in batch mode without interaction'
complete -c maa -n "__fish_seen_subcommand_from run" -l user-resource -d 'Load resources from the user config directory'
complete -c maa -n "__fish_seen_subcommand_from run" -s h -l help -d 'Print help (see more with \'--help\')'
## Help
Expand Down
Loading

0 comments on commit 9bdf39b

Please sign in to comment.