feat.增加一个缓慢滑动的 ProcessTask action

This commit is contained in:
MistEO
2022-05-03 23:05:26 +08:00
parent d049efb496
commit fa05fb937a
5 changed files with 241 additions and 185 deletions

View File

@@ -120,7 +120,7 @@
},
"SwipeLeftToStage1-7": {
"algorithm": "OcrDetect",
"action": "swipeToTheLeft",
"action": "SlowlySwipeToTheLeft",
"text": [
"1-8",
"TR-10",
@@ -143,7 +143,7 @@
},
"SwipeRightToStage1-7": {
"algorithm": "OcrDetect",
"action": "swipeToTheRight",
"action": "SlowlySwipeToTheRight",
"text": [
"1-1",
"1-2",
@@ -2705,6 +2705,29 @@
300
]
},
"ProcessTaskSlowlySwipeRightRect": {
"algorithm": "justReturn",
"action": "clickRect",
"specificRect": [
1080,
200,
100,
300
],
"preDelay": 1000,
"rearDelay": 300,
"Doc": "这里的preDelay作为滑动duration使用rearDelay作为滑动额外延时使用"
},
"ProcessTaskSlowlySwipeLeftRect": {
"algorithm": "justReturn",
"action": "clickRect",
"specificRect": [
700,
200,
100,
300
]
},
"InfrastOperListSwipeToTheLeftBegin": {
"algorithm": "justReturn",
"action": "clickRect",

View File

@@ -200,8 +200,10 @@ namespace asst
DoNothing = 0x200, // 什么都不做
Stop = 0x400, // 停止当前Task
BasicSwipe = 0x1000,
SwipeToTheLeft = BasicSwipe | 1, // 往左划一下
SwipeToTheRight = BasicSwipe | 2, // 往右划一下
SwipeToTheLeft = BasicSwipe | 1, // 往左划一下
SwipeToTheRight = BasicSwipe | 2, // 往右划一下
SlowlySwipeToTheLeft = SwipeToTheLeft | 4, // 慢慢的往左划一下
SlowlySwipeToTheRight = SwipeToTheRight | 8 // 慢慢的往右划一下
};
// 任务信息

View File

@@ -164,6 +164,10 @@ bool ProcessTask::_run()
case ProcessTaskAction::SwipeToTheRight:
exec_swipe_task(cur_task_ptr->action);
break;
case ProcessTaskAction::SlowlySwipeToTheLeft:
case ProcessTaskAction::SlowlySwipeToTheRight:
exec_slowly_swipe_task(cur_task_ptr->action);
break;
case ProcessTaskAction::DoNothing:
break;
case ProcessTaskAction::Stop:
@@ -237,3 +241,23 @@ void asst::ProcessTask::exec_swipe_task(ProcessTaskAction action)
break;
}
}
void asst::ProcessTask::exec_slowly_swipe_task(ProcessTaskAction action)
{
LogTraceFunction;
static Rect right_rect = Task.get("ProcessTaskSlowlySwipeRightRect")->specific_rect;
static Rect left_rect = Task.get("ProcessTaskSlowlySwipeLeftRect")->specific_rect;
static int duration = Task.get("ProcessTaskSlowlySwipeRightRect")->pre_delay;
static int extra_delay = Task.get("ProcessTaskSlowlySwipeRightRect")->rear_delay;
switch (action) {
case asst::ProcessTaskAction::SlowlySwipeToTheLeft:
Ctrler.swipe(left_rect, right_rect, duration, true, extra_delay, true);
break;
case asst::ProcessTaskAction::SlowlySwipeToTheRight:
Ctrler.swipe(right_rect, left_rect, duration, true, extra_delay, true);
break;
default: // 走不到这里TODO 报个错
break;
}
}

View File

@@ -26,6 +26,7 @@ namespace asst
void exec_click_task(const Rect& matched_rect);
void exec_swipe_task(ProcessTaskAction action);
void exec_slowly_swipe_task(ProcessTaskAction action);
std::vector<std::string> m_raw_tasks_name;
std::vector<std::string> m_cur_tasks_name;

View File

@@ -10,211 +10,217 @@
const std::shared_ptr<asst::TaskInfo> asst::TaskData::get(const std::string& name) const noexcept
{
if (auto iter = m_all_tasks_info.find(name);
iter != m_all_tasks_info.cend()) {
return iter->second;
}
else {
return nullptr;
}
if (auto iter = m_all_tasks_info.find(name);
iter != m_all_tasks_info.cend()) {
return iter->second;
}
else {
return nullptr;
}
}
const std::unordered_set<std::string>& asst::TaskData::get_templ_required() const noexcept
{
return m_templ_required;
return m_templ_required;
}
std::shared_ptr<asst::TaskInfo> asst::TaskData::get(std::string name)
{
return m_all_tasks_info[std::move(name)];
return m_all_tasks_info[std::move(name)];
}
void asst::TaskData::clear_cache() noexcept
{
for (auto&& [name, ptr] : m_all_tasks_info) {
ptr->region_of_appeared = Rect();
}
for (auto&& [name, ptr] : m_all_tasks_info) {
ptr->region_of_appeared = Rect();
}
}
bool asst::TaskData::parse(const json::value& json)
{
auto to_lower = [](char c) -> char {
return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
};
for (const auto& [name, task_json] : json.as_object()) {
std::string algorithm_str = task_json.get("algorithm", "matchtemplate");
std::transform(algorithm_str.begin(), algorithm_str.end(), algorithm_str.begin(), to_lower);
AlgorithmType algorithm = AlgorithmType::Invalid;
if (algorithm_str == "matchtemplate") {
algorithm = AlgorithmType::MatchTemplate;
}
else if (algorithm_str == "justreturn") {
algorithm = AlgorithmType::JustReturn;
}
else if (algorithm_str == "ocrdetect") {
algorithm = AlgorithmType::OcrDetect;
}
else if (algorithm_str == "hash") {
algorithm = AlgorithmType::Hash;
}
else {
m_last_error = "algorithm error " + algorithm_str;
return false;
}
auto to_lower = [](char c) -> char {
return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
};
for (const auto& [name, task_json] : json.as_object()) {
std::string algorithm_str = task_json.get("algorithm", "matchtemplate");
std::transform(algorithm_str.begin(), algorithm_str.end(), algorithm_str.begin(), to_lower);
AlgorithmType algorithm = AlgorithmType::Invalid;
if (algorithm_str == "matchtemplate") {
algorithm = AlgorithmType::MatchTemplate;
}
else if (algorithm_str == "justreturn") {
algorithm = AlgorithmType::JustReturn;
}
else if (algorithm_str == "ocrdetect") {
algorithm = AlgorithmType::OcrDetect;
}
else if (algorithm_str == "hash") {
algorithm = AlgorithmType::Hash;
}
else {
m_last_error = "algorithm error " + algorithm_str;
return false;
}
std::shared_ptr<TaskInfo> task_info_ptr = nullptr;
switch (algorithm) {
case AlgorithmType::JustReturn:
task_info_ptr = std::make_shared<TaskInfo>();
break;
case AlgorithmType::MatchTemplate: {
auto match_task_info_ptr = std::make_shared<MatchTaskInfo>();
match_task_info_ptr->templ_name = task_json.get("template", name + ".png");
m_templ_required.emplace(match_task_info_ptr->templ_name);
std::shared_ptr<TaskInfo> task_info_ptr = nullptr;
switch (algorithm) {
case AlgorithmType::JustReturn:
task_info_ptr = std::make_shared<TaskInfo>();
break;
case AlgorithmType::MatchTemplate: {
auto match_task_info_ptr = std::make_shared<MatchTaskInfo>();
match_task_info_ptr->templ_name = task_json.get("template", name + ".png");
m_templ_required.emplace(match_task_info_ptr->templ_name);
match_task_info_ptr->templ_threshold = task_json.get(
"templThreshold", TemplThresholdDefault);
match_task_info_ptr->special_threshold = task_json.get(
"specialThreshold", 0);
if (task_json.contains("maskRange")) {
match_task_info_ptr->mask_range = std::make_pair(
task_json.at("maskRange")[0].as_integer(),
task_json.at("maskRange")[1].as_integer());
}
match_task_info_ptr->templ_threshold = task_json.get(
"templThreshold", TemplThresholdDefault);
match_task_info_ptr->special_threshold = task_json.get(
"specialThreshold", 0);
if (task_json.contains("maskRange")) {
match_task_info_ptr->mask_range = std::make_pair(
task_json.at("maskRange")[0].as_integer(),
task_json.at("maskRange")[1].as_integer());
}
task_info_ptr = match_task_info_ptr;
} break;
case AlgorithmType::OcrDetect: {
auto ocr_task_info_ptr = std::make_shared<OcrTaskInfo>();
for (const json::value& text : task_json.at("text").as_array()) {
ocr_task_info_ptr->text.emplace_back(text.as_string());
}
ocr_task_info_ptr->need_full_match = task_json.get("need_match", false);
if (task_json.contains("ocrReplace")) {
for (const json::value& rep : task_json.at("ocrReplace").as_array()) {
ocr_task_info_ptr->replace_map.emplace(rep.as_array()[0].as_string(), rep.as_array()[1].as_string());
}
}
task_info_ptr = ocr_task_info_ptr;
} break;
case AlgorithmType::Hash:
{
auto hash_task_info_ptr = std::make_shared<HashTaskInfo>();
for (const json::value& hash : task_json.at("hash").as_array()) {
hash_task_info_ptr->hashs.emplace_back(hash.as_string());
}
hash_task_info_ptr->dist_threshold = task_json.get("threshold", 0);
if (task_json.contains("maskRange")) {
hash_task_info_ptr->mask_range = std::make_pair(
task_json.at("maskRange")[0].as_integer(),
task_json.at("maskRange")[1].as_integer());
}
hash_task_info_ptr->bound = task_json.get("bound", true);
task_info_ptr = match_task_info_ptr;
} break;
case AlgorithmType::OcrDetect: {
auto ocr_task_info_ptr = std::make_shared<OcrTaskInfo>();
for (const json::value& text : task_json.at("text").as_array()) {
ocr_task_info_ptr->text.emplace_back(text.as_string());
}
ocr_task_info_ptr->need_full_match = task_json.get("need_match", false);
if (task_json.contains("ocrReplace")) {
for (const json::value& rep : task_json.at("ocrReplace").as_array()) {
ocr_task_info_ptr->replace_map.emplace(rep.as_array()[0].as_string(), rep.as_array()[1].as_string());
}
}
task_info_ptr = ocr_task_info_ptr;
} break;
case AlgorithmType::Hash:
{
auto hash_task_info_ptr = std::make_shared<HashTaskInfo>();
for (const json::value& hash : task_json.at("hash").as_array()) {
hash_task_info_ptr->hashs.emplace_back(hash.as_string());
}
hash_task_info_ptr->dist_threshold = task_json.get("threshold", 0);
if (task_json.contains("maskRange")) {
hash_task_info_ptr->mask_range = std::make_pair(
task_json.at("maskRange")[0].as_integer(),
task_json.at("maskRange")[1].as_integer());
}
hash_task_info_ptr->bound = task_json.get("bound", true);
task_info_ptr = hash_task_info_ptr;
} break;
}
task_info_ptr->cache = task_json.get("cache", true);
task_info_ptr->algorithm = algorithm;
task_info_ptr->name = name;
std::string action = task_json.get("action", std::string());
std::transform(action.begin(), action.end(), action.begin(), to_lower);
if (action == "clickself") {
task_info_ptr->action = ProcessTaskAction::ClickSelf;
}
else if (action == "clickrand") {
task_info_ptr->action = ProcessTaskAction::ClickRand;
}
else if (action == "donothing" || action.empty()) {
task_info_ptr->action = ProcessTaskAction::DoNothing;
}
else if (action == "stop") {
task_info_ptr->action = ProcessTaskAction::Stop;
}
else if (action == "clickrect") {
task_info_ptr->action = ProcessTaskAction::ClickRect;
const json::value& rect_json = task_json.at("specificRect");
task_info_ptr->specific_rect = Rect(
rect_json[0].as_integer(),
rect_json[1].as_integer(),
rect_json[2].as_integer(),
rect_json[3].as_integer());
}
else if (action == "swipetotheleft") {
task_info_ptr->action = ProcessTaskAction::SwipeToTheLeft;
}
else if (action == "swipetotheright") {
task_info_ptr->action = ProcessTaskAction::SwipeToTheRight;
}
else {
m_last_error = "Task: " + name + " error: " + action;
return false;
}
task_info_ptr = hash_task_info_ptr;
} break;
}
task_info_ptr->cache = task_json.get("cache", true);
task_info_ptr->algorithm = algorithm;
task_info_ptr->name = name;
std::string action = task_json.get("action", std::string());
std::transform(action.begin(), action.end(), action.begin(), to_lower);
if (action == "clickself") {
task_info_ptr->action = ProcessTaskAction::ClickSelf;
}
else if (action == "clickrand") {
task_info_ptr->action = ProcessTaskAction::ClickRand;
}
else if (action == "donothing" || action.empty()) {
task_info_ptr->action = ProcessTaskAction::DoNothing;
}
else if (action == "stop") {
task_info_ptr->action = ProcessTaskAction::Stop;
}
else if (action == "clickrect") {
task_info_ptr->action = ProcessTaskAction::ClickRect;
const json::value& rect_json = task_json.at("specificRect");
task_info_ptr->specific_rect = Rect(
rect_json[0].as_integer(),
rect_json[1].as_integer(),
rect_json[2].as_integer(),
rect_json[3].as_integer());
}
else if (action == "swipetotheleft") {
task_info_ptr->action = ProcessTaskAction::SwipeToTheLeft;
}
else if (action == "swipetotheright") {
task_info_ptr->action = ProcessTaskAction::SwipeToTheRight;
}
else if (action == "slowlyswipetotheleft") {
task_info_ptr->action = ProcessTaskAction::SlowlySwipeToTheLeft;
}
else if (action == "slowlyswipetotheright") {
task_info_ptr->action = ProcessTaskAction::SlowlySwipeToTheRight;
}
else {
m_last_error = "Task: " + name + " error: " + action;
return false;
}
task_info_ptr->max_times = task_json.get("maxTimes", INT_MAX);
if (task_json.contains("exceededNext")) {
const json::array& excceed_next_arr = task_json.at("exceededNext").as_array();
for (const json::value& excceed_next : excceed_next_arr) {
task_info_ptr->exceeded_next.emplace_back(excceed_next.as_string());
}
}
else {
task_info_ptr->exceeded_next.emplace_back("Stop");
}
task_info_ptr->pre_delay = task_json.get("preDelay", 0);
task_info_ptr->rear_delay = task_json.get("rearDelay", 0);
if (task_json.contains("reduceOtherTimes")) {
const json::array& reduce_arr = task_json.at("reduceOtherTimes").as_array();
for (const json::value& reduce : reduce_arr) {
task_info_ptr->reduce_other_times.emplace_back(reduce.as_string());
}
}
if (task_json.contains("roi")) {
const json::array& area_arr = task_json.at("roi").as_array();
int x = area_arr[0].as_integer();
int y = area_arr[1].as_integer();
int width = area_arr[2].as_integer();
int height = area_arr[3].as_integer();
task_info_ptr->max_times = task_json.get("maxTimes", INT_MAX);
if (task_json.contains("exceededNext")) {
const json::array& excceed_next_arr = task_json.at("exceededNext").as_array();
for (const json::value& excceed_next : excceed_next_arr) {
task_info_ptr->exceeded_next.emplace_back(excceed_next.as_string());
}
}
else {
task_info_ptr->exceeded_next.emplace_back("Stop");
}
task_info_ptr->pre_delay = task_json.get("preDelay", 0);
task_info_ptr->rear_delay = task_json.get("rearDelay", 0);
if (task_json.contains("reduceOtherTimes")) {
const json::array& reduce_arr = task_json.at("reduceOtherTimes").as_array();
for (const json::value& reduce : reduce_arr) {
task_info_ptr->reduce_other_times.emplace_back(reduce.as_string());
}
}
if (task_json.contains("roi")) {
const json::array& area_arr = task_json.at("roi").as_array();
int x = area_arr[0].as_integer();
int y = area_arr[1].as_integer();
int width = area_arr[2].as_integer();
int height = area_arr[3].as_integer();
#ifdef ASST_DEBUG
if (x + width > WindowWidthDefault || y + height > WindowHeightDefault) {
m_last_error = name + " roi is out of bounds";
return false;
}
if (x + width > WindowWidthDefault || y + height > WindowHeightDefault) {
m_last_error = name + " roi is out of bounds";
return false;
}
#endif
task_info_ptr->roi = Rect(x, y, width, height);
}
else {
task_info_ptr->roi = Rect();
}
task_info_ptr->roi = Rect(x, y, width, height);
}
else {
task_info_ptr->roi = Rect();
}
if (task_json.contains("next")) {
for (const json::value& next : task_json.at("next").as_array()) {
task_info_ptr->next.emplace_back(next.as_string());
}
}
if (task_json.contains("rectMove")) {
const json::array& move_arr = task_json.at("rectMove").as_array();
task_info_ptr->rect_move = Rect(
move_arr[0].as_integer(),
move_arr[1].as_integer(),
move_arr[2].as_integer(),
move_arr[3].as_integer());
}
else {
task_info_ptr->rect_move = Rect();
}
if (task_json.contains("next")) {
for (const json::value& next : task_json.at("next").as_array()) {
task_info_ptr->next.emplace_back(next.as_string());
}
}
if (task_json.contains("rectMove")) {
const json::array& move_arr = task_json.at("rectMove").as_array();
task_info_ptr->rect_move = Rect(
move_arr[0].as_integer(),
move_arr[1].as_integer(),
move_arr[2].as_integer(),
move_arr[3].as_integer());
}
else {
task_info_ptr->rect_move = Rect();
}
m_all_tasks_info.emplace(name, task_info_ptr);
}
m_all_tasks_info.emplace(name, task_info_ptr);
}
#ifdef ASST_DEBUG
for (const auto& [name, task] : m_all_tasks_info) {
for (const auto& next : task->next) {
if (m_all_tasks_info.find(next) == m_all_tasks_info.cend()) {
m_last_error = name + "'s next " + next + " is null";
return false;
}
}
}
for (const auto& [name, task] : m_all_tasks_info) {
for (const auto& next : task->next) {
if (m_all_tasks_info.find(next) == m_all_tasks_info.cend()) {
m_last_error = name + "'s next " + next + " is null";
return false;
}
}
}
#endif
return true;
}
return true;
}