Skip to content

Commit

Permalink
feat: better input (#163)
Browse files Browse the repository at this point in the history
- Optional input, where certain parameters are used only when other parameters satisfy specific conditions. 
- Description for alternatives of select.
- Default value of select.
- Allow user input a custom value instead of index for select.
- Slightly update for fight and roguelike subcommand.
  • Loading branch information
wangl-cc authored Jan 3, 2024
1 parent 47dc1f0 commit 3020f68
Show file tree
Hide file tree
Showing 22 changed files with 2,752 additions and 1,753 deletions.
20 changes: 18 additions & 2 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,15 @@ condition = { type = "DateTime", start = "2023-08-01T16:00:00", end = "2023-08-2

# Set the stage to a `Select` type with alternatives and description
[tasks.variants.params.stage]
alternatives = ["SL-6", "SL-7", "SL-8"] # the alternatives of stage, at least one alternative should be given
# the alternatives of stage, at least one alternative should be given
# the element of alternatives can be a single value or a table with `value` and `desc` fields·
alternatives = [
"SL-7", # will be displayed as "1. SL-7"
{ value = "SL-8", desc = "Manganese Ore" } # will be displayed as "2. SL-8 (Manganese Ore)"
]
default_index = 1 # the index of default value, start from 1, if not given, empty value will be re-prompt
description = "a stage to fight in summer event" # description of the input, optional
allow_custom = true # whether allow input custom value, default to false, if allow, non-integer value will be treated as custom value

# Task without input
[[tasks.variants]]
Expand All @@ -329,9 +336,18 @@ params = { stage = "CE-6" }
[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
[tasks.variants.params.medicine]
# dependency of parameters, the key is the name of parameter, the value is the expected value of dependency parameter
# when set, the parameter will be required to input only if all dependency parameters are satisfied
deps = { stage = "1-7" }
default = 1000
description = "medicine to use"
```

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. To promote and input can be disabled by `--batch` option, which is useful for running tasks in Schedule.
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 input a index or custom value (if `allow_custom` is `true`). If the default index is given, it will be used if user input empty value, otherwise it will re-prompt.

`--batch` option can be used to run tasks in batch mode, which will use default value for all inputs and panic if no default value is given.

### `MaaCore` related configurations

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

#### 预定义任务

- `maa fight [stage]`: 运行战斗任务,`[stage]` 是关卡名称,例如 `1-7`如果 `stage` 为空,那么 `maa` 将询问你要刷的关卡,然后运行战斗任务
- `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`,如果留空,那么你将需要在 `maa` 询问后手动输入主题,
- `maa roguelike [theme]`: 运行 roguelike 模式的战斗任务,`[theme]` 是 roguelike 模式的主题,可选值为 `Phantom`, `Mizuki` 以及 `Sami`

#### 自定义任务

Expand Down Expand Up @@ -326,8 +326,15 @@ 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"] # 可选的关卡,必须提供至少一个可选值
# 可选的关卡,必须提供至少一个可选值
# 可选值可以是一个值,也可以是同时包含值和描述的一个表
alternatives = [
"SL-7", # 将被显示为 "1. SL-7"
{ value = "SL-8", desc = "轻锰矿" } # 将被显示为 "2. SL-8 (轻锰矿)"
]
default_index = 1 # 默认值的索引,从 1 开始,如果没有设置,输入空值将会重新提示输入
description = "a stage to fight in summer event" # 描述,可选
allow_custom = true # 是否允许输入自定义的值,默认为 false,如果允许,那么非整数的值将会被视为自定义的值

# 无需任何输入
[[tasks.variants]]
Expand All @@ -339,11 +346,18 @@ params = { stage = "CE-6" }
[tasks.variants.params.stage]
default = "1-7" # 默认的关卡,可选(如果没有默认值,输入空值将会重新提示输入)
description = "a stage to fight" # 描述,可选
[tasks.variants.params.medicine]
# 依赖的参数,键为参数名,值为依赖的参数的预期值
# 当设置时,只有所有的依赖参数都满足预期值时,这个参数才会被要求输入
deps = { stage = "1-7" }
default = 1000
description = "medicine to use"
```

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

`--batch` 选项可以用于在运行任务时跳过所有的输入,这将会使用默认值;如果有任何输入没有默认值,那么将会导致错误。

### MaaCore 相关配置

Expand Down
12 changes: 10 additions & 2 deletions maa-cli/config_examples/tasks/daily.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
{
"type": "StartUp",
"params": {
"client_type": "Official",
"client_type": {
"alternatives": ["Official", "YoStarEN", "YoStarJP"],
"description": "a client type",
"deps": {
"start_game_enabled": true
}
},
"start_game_enabled": {
"default": true,
"description": "start the game"
Expand Down Expand Up @@ -51,7 +57,9 @@
"params": {
"stage": {
"alternatives": ["SL-6", "SL-7", "SL-8"],
"description": "a stage to fight in summer event"
"default_index": 2,
"description": "a stage to fight in summer event",
"allow_custom": true
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion maa-cli/config_examples/tasks/daily.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
type = "StartUp"

[tasks.params]
client_type = "Official"
start_game_enabled = { default = true, description = "start the game" }
[tasks.params.client_type]
alternatives = ["Official", "YoStarEN", "YoStarJP"]
description = "a client type"
deps = { start_game_enabled = true }

[[tasks]]
name = "Fight Daily"
Expand Down Expand Up @@ -34,7 +37,9 @@ params = { stage = "CE-6" }
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"]
default_index = 2
description = "a stage to fight in summer event"
allow_custom = true

# Mall after 16:00
[[tasks]]
Expand Down
8 changes: 7 additions & 1 deletion maa-cli/config_examples/tasks/daily.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
tasks:
- type: StartUp
params:
client_type: Official
start_game_enabled:
default: true
description: start the game
client_type:
alternatives: [Official, YoStarEN, YoStarJP]
description: a client type
deps:
start_game_enabled: true
- type: Fight
name: Fight Daily
strategy: merge
Expand All @@ -31,7 +35,9 @@ tasks:
params:
stage:
alternatives: [SL-6, SL-7, SL-8]
default_index: 2
description: a stage to fight in summer event
allow_custom: true
- type: Mall
params:
shopping: true
Expand Down
37 changes: 26 additions & 11 deletions maa-cli/schemas/task.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@
{ "$ref": "#/definitions/maaBool" },
{ "$ref": "#/definitions/maaNumber" },
{ "$ref": "#/definitions/maaString" },
{ "$ref": "#/definitions/maaObject" },
{ "type": "null" }
{ "$ref": "#/definitions/maaObject" }
]
},
"maaObject": {
Expand All @@ -227,8 +226,10 @@
"type": "object",
"properties": {
"default": { "type": "boolean" },
"deps": { "type": "object" },
"description": { "type": "string" }
}
},
"additionalProperties": false
}
]
},
Expand All @@ -239,8 +240,10 @@
"type": "object",
"properties": {
"default": { "type": "number" },
"deps": { "type": "object" },
"description": { "type": "string" }
}
},
"additionalProperties": false
},
{
"type": "object",
Expand All @@ -255,14 +258,19 @@
"required": ["value"],
"properties": {
"value": { "type": "number" },
"display": { "type": "string" }
}
"desc": { "type": "string" }
},
"additionalProperties": false
}
]
}
},
"deps": { "type": "object" },
"default_index": { "type": "number" },
"allow_custom": { "type": "boolean" },
"description": { "type": "string" }
}
},
"additionalProperties": false
}
]
},
Expand All @@ -273,8 +281,10 @@
"type": "object",
"properties": {
"default": { "type": "string" },
"deps": { "type": "object" },
"description": { "type": "string" }
}
},
"additionalProperties": false
},
{
"type": "object",
Expand All @@ -290,14 +300,19 @@
"required": ["value"],
"properties": {
"value": { "type": "string" },
"display": { "type": "string" }
}
"desc": { "type": "string" }
},
"additionalProperties": false
}
]
}
},
"deps": { "type": "object" },
"default_index": { "type": "number" },
"allow_custom": { "type": "boolean" },
"description": { "type": "string" }
}
},
"additionalProperties": false
}
]
},
Expand Down
Loading

0 comments on commit 3020f68

Please sign in to comment.