From 3b82087eee893cbfc53eba17d74ff36f500c8580 Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 11 Feb 2025 00:12:36 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E5=B0=9D=E8=AF=95=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=80=89=E6=8B=A9task=E5=86=85=E5=AE=B9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=88=E6=9C=AA=E5=AE=8C=E6=88=90=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 2 +- client/client.cpp | 67 +++++++++++++++++++++++++++++++++++++++---- client/client.h | 2 ++ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 60a10c2..1af970f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,7 +21,7 @@ ], "visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis", "args": [ - "-n", "0" + "-n", "1" ] }, "cmake.environment": { diff --git a/client/client.cpp b/client/client.cpp index 1ccbf0a..7f02130 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -383,13 +383,14 @@ bool CClient::request_update_list(const std::string& param) // 校验格式是否正确 auto vec = COfStr::split(content, "\n"); bool valid = true; + int line = 1; + std::unordered_map mre{}; std::string handled_content; for (const auto& item : vec) { std::string hitem = COfStr::trim(item); if (hitem.empty()) { continue; } - mpinfo("---> check {}", hitem); auto v = COfStr::split(hitem, "|"); if (v.size() >= 2) { auto pr = variable_handle(list_file_full, v[0], true); @@ -398,8 +399,8 @@ bool CClient::request_update_list(const std::string& param) valid = false; break; } - mpinfo("---> check pass {}", pr); - handled_content.append(pr + "|" + v[1] + "\n"); + mpinfo("--->check pass {}:{}", line, pr); + mre[line++] = pr + "|" + v[1]; continue; } valid = false; @@ -411,15 +412,21 @@ bool CClient::request_update_list(const std::string& param) return false; } + auto handel_ret = handle_user_select(mre); + if (handel_ret.empty()) { + mperror("handle_user_select not pass, abort action!"); + return false; + } + #if defined(_WIN32) - handled_content = CCodec::ansi_to_u8(handled_content); + handel_ret = CCodec::ansi_to_u8(handel_ret); #endif list_file_ = list_file_full; std::shared_ptr buf = std::make_shared(); buf->type_ = TYPE_REQUEST_UPDATE_LIST; - buf->data_ = new char[handled_content.size() + 1](); - buf->len_ = std::snprintf(buf->data_, handled_content.size() + 1, "%s", handled_content.c_str()); + buf->data_ = new char[handel_ret.size() + 1](); + buf->len_ = std::snprintf(buf->data_, handel_ret.size() + 1, "%s", handel_ret.c_str()); buf->tid_ = task_list_[index]->id; if (!send_frame(buf.get())) { @@ -824,6 +831,54 @@ std::string CClient::variable_handle(const std::string& task_list_path, const st return result; } +std::string CClient::handle_user_select(const std::unordered_map& source) +{ + std::string handled_content{}; + std::string input{}; + + while (true) { + mpinfo("numbers by space, or '0' use all, 'end' to quit: "); + std::getline(std::cin, input); + + if (input == "end") { + handled_content.clear(); + break; + } + if (input == "0") { + handled_content.clear(); // 清空之前的内容 + for (const auto& pair : source) { + handled_content.append(pair.second + "\n"); + } + } else { + // 处理多个值的输入 + std::stringstream ss(input); + std::string num_str; + + while (ss >> num_str) { + // 判断输入的每个值是否为有效的数字 + try { + int key = std::stoi(num_str); + + if (source.find(key) != source.end()) { + handled_content.append(source.at(key) + "\n"); + } else { + // 如果mre中没有这个key + mperror("Invalid input, please enter valid numbers or '0' for all."); + break; + } + } catch (const std::exception& e) { + mperror("Invalid input, please enter valid numbers or '0' for all."); + break; + } + } + if (!handled_content.empty()) { + break; + } + } + } + return handled_content; +} + CFileOpr::CFileOpr() = default; CFileOpr::~CFileOpr() = default; diff --git a/client/client.h b/client/client.h index 9ef7662..28c3c6a 100644 --- a/client/client.h +++ b/client/client.h @@ -10,6 +10,7 @@ #include #include #include +#include using namespace ofen; struct DownClientInfo { @@ -65,6 +66,7 @@ private: void hearts(); void judget_down_active(); std::string variable_handle(const std::string& task_list_path, const std::string& source, bool is_send); + std::string handle_user_select(const std::unordered_map& source); private: std::mutex mutex_;