diff --git a/MeoAssistance/Assistance.cpp b/MeoAssistance/Assistance.cpp index d0d550e680..3975404cdd 100644 --- a/MeoAssistance/Assistance.cpp +++ b/MeoAssistance/Assistance.cpp @@ -273,12 +273,15 @@ bool asst::Assistance::start_infrast() } std::unique_lock lock(m_mutex); - + auto ret = get_opers_idtf_result(); + if (!ret) { + DebugTraceInfo("Get opers info error"); + return false; + } auto task_ptr = std::make_shared(task_callback, (void*)this); - // TODO 这个参数写到配置文件里,TODO 滑动位置要根据分辨率缩放 - task_ptr->set_swipe_param(2100, Rect(2400, 800, 0, 0), Rect(400, 800, 0, 0), 20); task_ptr->set_facility("Manufacturing"); task_ptr->set_task_chain("Infrast"); + task_ptr->set_all_opers_info(std::move(ret.value())); m_tasks_queue.emplace(task_ptr); m_thread_idle = false; @@ -441,6 +444,9 @@ void Assistance::task_callback(AsstMsg msg, const json::value& detail, void* cus p_this->append_task(more_detail); return; // 这俩消息Assistance会新增任务,外部不需要处理,直接拦掉 break; + case AsstMsg::OpersIdtfResult: + set_opers_idtf_result(more_detail); // 保存到文件 + break; default: break; } @@ -513,4 +519,48 @@ void Assistance::clear_exec_times() { pair.second->exec_times = 0; } -} \ No newline at end of file +} + +void asst::Assistance::set_opers_idtf_result(const json::value& detail) +{ + constexpr static const char* Filename = "operators.json"; + std::ofstream ofs(GetCurrentDir() + Filename, std::ios::out); + ofs << detail.format() << std::endl; + ofs.close(); +} + +std::optional> asst::Assistance::get_opers_idtf_result() +{ + constexpr static const char* Filename = "operators.json"; + std::ifstream ifs(GetCurrentDir() + Filename, std::ios::in); + if (!ifs.is_open()) { + return std::nullopt; + } + std::stringstream iss; + iss << ifs.rdbuf(); + ifs.close(); + std::string content(iss.str()); + + auto&& parse_ret = json::parse(content); + if (!parse_ret) { + DebugTraceError(__FUNCTION__, "json parsing error!"); + return std::nullopt; + } + json::value root = std::move(parse_ret.value()); + std::unordered_set opers_info; + try { + for (const json::value& info_json : root["all"].as_array()) + { + OperInfrastInfo info; + info.name = info_json.at("name").as_string(); + info.elite = info_json.at("elite").as_integer(); + info.level = info_json.get("level", 0); + opers_info.emplace(std::move(info)); + } + } + catch (json::exception& exp) { + DebugTraceError(__FUNCTION__, "json parsing error!"); + return std::nullopt; + } + return opers_info; +} diff --git a/MeoAssistance/Assistance.h b/MeoAssistance/Assistance.h index c6e649a98b..1b5c2ecb95 100644 --- a/MeoAssistance/Assistance.h +++ b/MeoAssistance/Assistance.h @@ -63,6 +63,8 @@ namespace asst { void append_task(const json::value& detail); void append_callback(AsstMsg msg, json::value detail); void clear_exec_times(); + static void set_opers_idtf_result(const json::value& detail); // 保存干员识别结果 + static std::optional> get_opers_idtf_result(); // 读取干员识别结果 std::shared_ptr m_window_ptr = nullptr; std::shared_ptr m_view_ptr = nullptr; diff --git a/MeoAssistance/AsstMsg.h b/MeoAssistance/AsstMsg.h index c54090da1a..211a537640 100644 --- a/MeoAssistance/AsstMsg.h +++ b/MeoAssistance/AsstMsg.h @@ -35,7 +35,8 @@ namespace asst { RecruitSpecialTag, RecruitResult, /* Infrast Msg*/ - InfrastOpers, // 识别到基建干员s + OpersDetected, // 识别到了干员s + OpersIdtfResult, // 干员识别结果(总的) InfrastComb // 当前设置的最优干员组合 }; @@ -64,7 +65,8 @@ namespace asst { {AsstMsg::RecruitSpecialTag, "RecruitSpecialTag"}, {AsstMsg::RecruitResult, "RecruitResult"}, {AsstMsg::AppendTask, "AppendTask"}, - {AsstMsg::InfrastOpers, "InfrastOpers"}, + {AsstMsg::OpersDetected, "OpersDetected"}, + {AsstMsg::OpersIdtfResult, "OpersIdtfResult"}, {AsstMsg::InfrastComb, "InfrastComb"} }; return os << _type_name.at(type); diff --git a/MeoAssistance/IdentifyOperTask.cpp b/MeoAssistance/IdentifyOperTask.cpp index 40e1d9885b..28c4fb8929 100644 --- a/MeoAssistance/IdentifyOperTask.cpp +++ b/MeoAssistance/IdentifyOperTask.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -13,11 +14,15 @@ #include "InfrastConfiger.h" #include "Identify.h" #include "WinMacro.h" +#include "Logger.hpp" using namespace asst; asst::IdentifyOperTask::IdentifyOperTask(AsstCallback callback, void* callback_arg) - : OcrAbstractTask(callback, callback_arg) + : OcrAbstractTask(callback, callback_arg), + m_cropped_height_ratio(0.043), + m_cropped_upper_y_ratio(0.480), + m_cropped_lower_y_ratio(0.923) { ; } @@ -32,6 +37,49 @@ bool asst::IdentifyOperTask::run() return false; } + auto&& [width, height] = m_view_ptr->getAdbDisplaySize(); + + m_swipe_begin = Rect(width * 0.9, height * 0.5, 0, 0); + m_swipe_end = Rect(width * 0.1, height * 0.5, 0, 0); + + auto&& detect_ret = swipe_and_detect(); + if (!detect_ret) { + return false; + } + const auto& detected_opers = std::move(detect_ret.value()); + + json::value result_json; + std::vector opers_json_vec; + for (const OperInfrastInfo& info : detected_opers) { + json::value info_json; + info_json["name"] = info.name; + info_json["elite"] = info.elite; + //info_json["level"] = info.level; + opers_json_vec.emplace_back(std::move(info_json)); + } + result_json["all"] = json::array(opers_json_vec); + + // 查找没有哪些干员(也可能是没识别到) + std::vector not_found_vector; + for (const std::string& name : InfrastConfiger::get_instance().m_all_opers_name) { + auto iter = std::find_if(detected_opers.cbegin(), detected_opers.cend(), + [&](const OperInfrastInfo& oper) -> bool { + return oper.name == name; + }); + if (iter == detected_opers.cend()) { + not_found_vector.emplace_back(name); + DebugTrace("Not Found", Utf8ToGbk(name)); + } + } + result_json["not_found"] = json::array(not_found_vector); + + m_callback(AsstMsg::OpersIdtfResult, result_json, m_callback_arg); + + return true; +} + +std::optional> asst::IdentifyOperTask::swipe_and_detect() +{ json::value task_start_json = json::object{ { "task_type", "InfrastStationTask" }, { "task_chain", OcrAbstractTask::m_task_chain}, @@ -78,7 +126,7 @@ bool asst::IdentifyOperTask::run() opers_json_vec.emplace_back(std::move(info_json)); } opers_json["all"] = json::array(opers_json_vec); - m_callback(AsstMsg::InfrastOpers, opers_json, m_callback_arg); + m_callback(AsstMsg::OpersDetected, opers_json, m_callback_arg); // 正向滑动的时候 if (!reverse) { @@ -95,29 +143,17 @@ bool asst::IdentifyOperTask::run() } // 阻塞等待本次滑动结束 if (!swipe_future.get()) { - return false; + return std::nullopt; } } -#ifdef LOG_TRACE - for (const std::string& name : InfrastConfiger::get_instance().m_all_opers_name) { - auto iter = std::find_if(detected_opers.cbegin(), detected_opers.cend(), - [&](const OperInfrastInfo& oper) -> bool { - return oper.name == name; - }); - if (iter == detected_opers.cend()) { - std::cout << "未检测到:" << Utf8ToGbk(name) << std::endl; - } - } -#endif // LOG_TRACE - - return true; + return detected_opers; } std::vector