diff --git a/MeoAssistance/include/Assistance.h b/MeoAssistance/include/Assistance.h index d189bad2c9..0918609cf9 100644 --- a/MeoAssistance/include/Assistance.h +++ b/MeoAssistance/include/Assistance.h @@ -36,6 +36,7 @@ namespace asst { std::condition_variable m_condvar; bool m_thread_exit = false; bool m_thread_running = false; + json::array m_next_tasks; }; } \ No newline at end of file diff --git a/MeoAssistance/include/Configer.h b/MeoAssistance/include/Configer.h index 9070c54572..a8df53d07d 100644 --- a/MeoAssistance/include/Configer.h +++ b/MeoAssistance/include/Configer.h @@ -12,9 +12,9 @@ namespace asst { static bool reload(); static std::string getCurDir(); static std::string getResDir(); - static json::object dataObj; - static json::object handleObj; - static json::object optionsObj; + static json::object tasksJson; + static json::object handleJson; + static json::object optionsJson; private: Configer() = default; diff --git a/MeoAssistance/src/Assistance.cpp b/MeoAssistance/src/Assistance.cpp index 4313569e17..0ffb93d225 100644 --- a/MeoAssistance/src/Assistance.cpp +++ b/MeoAssistance/src/Assistance.cpp @@ -11,7 +11,7 @@ Assistance::Assistance() Configer::reload(); m_Ider = std::make_shared(); - for (auto&& pair : Configer::dataObj) + for (auto&& pair : Configer::tasksJson) { m_Ider->addImage(pair.first, Configer::getResDir() + pair.second["filename"].as_string()); } @@ -50,7 +50,7 @@ std::optional Assistance::setSimulator(const std::string& simulator std::unique_lock lock(m_mutex); if (simulator_name.empty()) { - for (auto&& [name, value] : Configer::handleObj) + for (auto&& [name, value] : Configer::handleJson) { ret = create_handles(name); if (ret) { @@ -72,8 +72,13 @@ std::optional Assistance::setSimulator(const std::string& simulator void Assistance::start() { - std::unique_lock lock(m_mutex); + if (m_thread_running) { + return; + } + std::unique_lock lock(m_mutex); + m_next_tasks.clear(); + m_next_tasks.emplace_back("Begin"); m_thread_running = true; m_condvar.notify_one(); } @@ -83,6 +88,7 @@ void Assistance::stop() std::unique_lock lock(m_mutex); m_thread_running = false; + m_next_tasks.clear(); } void Assistance::working_proc(Assistance* pThis) @@ -92,67 +98,46 @@ void Assistance::working_proc(Assistance* pThis) if (pThis->m_thread_running) { auto curImg = pThis->m_pView->getImage(pThis->m_pView->getWindowRect()); - double max_value = -1; - Rect cor_rect; - std::pair matched; - for (auto&& pair : Configer::dataObj) { - auto&& [value, rect] = pThis->m_Ider->findImage(curImg, pair.first); - DebugTrace("%-20s %f", pair.first.c_str(), value); - if (value >= pair.second["threshold"].as_double()) { - if (value >= max_value) { - max_value = value; - cor_rect = rect; - matched = pair; - } + std::string matched_task; + Rect matched_rect; + for (auto&& task_jstr : pThis->m_next_tasks) { + std::string task_name = task_jstr.as_string(); + auto&& [value, rect] = pThis->m_Ider->findImage(curImg, task_name); + DebugTrace("%-20s %f", task_name.c_str(), value); + if (value >= Configer::tasksJson[task_name]["threshold"].as_double()) { + matched_task = task_name; + matched_rect = rect; + break; } } - if (max_value >= 0) { - auto task = Configer::dataObj[matched.first].as_object(); - std::string opType = task["type"].as_string(); - DebugTraceInfo("Matched: %s, type: %s", matched.first.c_str(), opType.c_str()); - bool next_loop = true; - while (next_loop) { - if (opType == "clickSelf") { - pThis->m_pCtrl->clickRange(cor_rect); - } - else if (opType == "clickRand") { - pThis->m_pCtrl->clickRange(pThis->m_pCtrl->getWindowRect()); - } - else if (opType == "next") { - json::value nextObj = task["next"]; - std::string name = nextObj["name"].as_string(); - std::string filepath = Configer::getResDir() + nextObj["filename"].as_string(); - auto&& [value, rect] = pThis->m_Ider->findImageWithFile(curImg, filepath); - DebugTrace("Next: %-20s %f", name.c_str(), value); - if (value >= nextObj["threshold"].as_double()) { - DebugTraceInfo("Next matched: %s", name.c_str()); - task = nextObj.as_object(); - opType = nextObj["type"].as_string(); - cor_rect = rect; - max_value = value; - next_loop = true; - continue; - } - else { - DebugTraceInfo("Next not matched: %s", name.c_str()); - } - } - else if (opType == "stop") { - DebugTrace("opType == stop"); - pThis->m_thread_running = false; - break; - } - else if (opType == "doNothing") { - // do nothing - } - else { - DebugTraceError("Unknow option type: %s", opType.c_str()); - } - next_loop = false; + if (!matched_task.empty()) { + auto task = Configer::tasksJson[matched_task].as_object(); + std::string opType = task["type"].as_string(); + DebugTraceInfo("Matched: %s, type: %s", matched_task, opType.c_str()); + + if (opType == "clickSelf") { + pThis->m_pCtrl->clickRange(matched_rect); } + else if (opType == "clickRand") { + pThis->m_pCtrl->clickRange(pThis->m_pCtrl->getWindowRect()); + } + else if (opType == "doNothing") { + // do nothing + } + else if (opType == "stop") { + DebugTrace("opType == stop"); + pThis->m_thread_running = false; + pThis->m_next_tasks.clear(); + continue; + } + else { + DebugTraceError("Unknow option type: %s", opType.c_str()); + } + + pThis->m_next_tasks = Configer::tasksJson[matched_task]["next"].as_array(); } - pThis->m_condvar.wait_for(lock, std::chrono::milliseconds(Configer::optionsObj["delay"].as_integer())); + pThis->m_condvar.wait_for(lock, std::chrono::milliseconds(Configer::optionsJson["delay"]["fixedTime"].as_integer())); } else { pThis->m_condvar.wait(lock); diff --git a/MeoAssistance/src/Configer.cpp b/MeoAssistance/src/Configer.cpp index c847f1aa55..406184fea1 100644 --- a/MeoAssistance/src/Configer.cpp +++ b/MeoAssistance/src/Configer.cpp @@ -8,9 +8,9 @@ using namespace asst; -json::object Configer::dataObj; -json::object Configer::handleObj; -json::object Configer::optionsObj; +json::object Configer::tasksJson; +json::object Configer::handleJson; +json::object Configer::optionsJson; std::string Configer::m_curDir; @@ -32,9 +32,9 @@ bool Configer::reload() } auto root = std::move(ret).value(); - dataObj = root["data"].as_object(); - handleObj = root["handle"].as_object(); - optionsObj = root["options"].as_object(); + tasksJson = root["tasks"].as_object(); + handleJson = root["handle"].as_object(); + optionsJson = root["options"].as_object(); return true; diff --git a/MeoAssistance/src/WinMacro.cpp b/MeoAssistance/src/WinMacro.cpp index a5831cc493..8adf8c0733 100644 --- a/MeoAssistance/src/WinMacro.cpp +++ b/MeoAssistance/src/WinMacro.cpp @@ -29,7 +29,7 @@ bool WinMacro::captured() const noexcept bool WinMacro::findHandle() { json::array handle_arr; - json::value simulator_json = Configer::handleObj[m_simulator_name]; + json::value simulator_json = Configer::handleJson[m_simulator_name]; switch (m_handle_type) { case HandleType::Window: m_width = simulator_json["Width"].as_integer(); diff --git a/resource/config.json b/resource/config.json index 0046fe9dc7..5a9350f96a 100644 --- a/resource/config.json +++ b/resource/config.json @@ -1,7 +1,10 @@ { - "version": 0.1, + "version": 0.2, "options": { - "delay": 2000 + "delay": { + "type": "fixedTime", + "fixedTime": 2000 + } }, "handle": { "BlueStacks": { @@ -145,53 +148,97 @@ "yOffset": -42 } }, - "data": { - "Random": { + "tasks": { + "Begin": { "filename": "", "threshold": 0, - "type": "clickRand" - }, - "UseMedicine": { - "filename": "UseMedicine.png", - "threshold": 0.99, - "type": "next", - "next": { - "name": "MedicineConfirm", - "filename": "MedicineConfirm.png", - "threshold": 0.98, - "type": "clickSelf" - } - }, - "PRTS": { - "filename": "PRTS.png", - "threshold": 0.98, - "type": "doNothing" - }, - "UseStone": { - "filename": "UseStone.png", - "threshold": 0.99, - "type": "stop" + "type": "doNothing", + "next": [ + "StartButton1", + "StartButton2", + "PRTS", + "UseMedicine", + "UseStone", + "PrtsErrorConfirm" + ] }, "StartButton1": { "filename": "StartButton1.png", "threshold": 0.99, - "type": "clickSelf" + "type": "clickSelf", + "next": [ + "StartButton2", + "UseMedicine", + "UseStone" + ] }, "StartButton2": { + "id": 2, "filename": "StartButton2.png", "threshold": 0.99, - "type": "clickSelf" + "type": "clickSelf", + "next": [ + "PRTS" + ] + }, + "PRTS": { + "filename": "PRTS.png", + "threshold": 0.98, + "type": "doNothing", + "next": [ + "PRTS", + "PrtsErrorConfirm", + "Random" + ] + }, + "Random": { + "filename": "", + "threshold": 0, + "type": "clickRand", + "next": [ + "StartButton1", + "Random" + ] + }, + "UseMedicine": { + "filename": "UseMedicine.png", + "threshold": 0.99, + "type": "doNothing", + "next": [ + "MedicineConfirm" + ] + }, + "UseStone": { + "filename": "UseStone.png", + "threshold": 0.99, + "type": "stop", + "next": [ + "MedicineConfirm" + ] + }, + "MedicineConfirm": { + "filename": "MedicineConfirm.png", + "threshold": 0.98, + "type": "clickSelf", + "next": [ + "StartButton1" + ] }, "PrtsErrorConfirm": { "filename": "PrtsErrorConfirm.png", "threshold": 0.98, - "type": "next", - "next": { - "name": "AbandonAction", - "filename": "AbandonAction.png", - "threshold": 0.98, - "type": "clickSelf" - } + "type": "doNothing", + "next": [ + "AbandonAction" + ] + }, + "AbandonAction": { + "filename": "AbandonAction.png", + "threshold": 0.98, + "type": "clickSelf", + "next": [ + "Random" + ] } } } \ No newline at end of file