update tasks and config

This commit is contained in:
MistEO
2021-07-14 17:26:36 +08:00
parent d57bc069f2
commit dbbb5fd6b0
6 changed files with 137 additions and 104 deletions

View File

@@ -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;
};
}

View File

@@ -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;

View File

@@ -11,7 +11,7 @@ Assistance::Assistance()
Configer::reload();
m_Ider = std::make_shared<Identify>();
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<std::string> Assistance::setSimulator(const std::string& simulator
std::unique_lock<std::mutex> 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<std::string> Assistance::setSimulator(const std::string& simulator
void Assistance::start()
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_thread_running) {
return;
}
std::unique_lock<std::mutex> 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<std::mutex> 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<std::string, json::value> 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);

View File

@@ -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;

View File

@@ -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();

View File

@@ -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"
]
}
}
}