From acdda9388ce8359e9eba015590bc9b405fccd7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Wed, 24 May 2023 09:24:52 +0300 Subject: [PATCH] add advanced solution window --- client/internal/core/include/Core.h | 2 +- client/internal/core/src/Core.cpp | 4 +-- client/internal/entities/include/Solution.h | 4 +++ client/internal/gui/include/SolutionsWindow.h | 6 ++++ client/internal/gui/src/SolutionsWindow.cpp | 35 +++++++++++++++---- .../httpClient/include/HttpClientManager.h | 2 +- .../internal/httpClient/include/Serializer.h | 1 + .../httpClient/src/HttpClientManager.cpp | 8 +++-- client/internal/httpClient/src/Serializer.cpp | 17 +++++++++ .../internal/httpServer/include/Serializer.h | 1 + server/internal/httpServer/src/Serializer.cpp | 12 +++++++ .../httpServer/src/SolutionManager.cpp | 7 ++-- 12 files changed, 83 insertions(+), 16 deletions(-) diff --git a/client/internal/core/include/Core.h b/client/internal/core/include/Core.h index 31d3cee..a981e96 100644 --- a/client/internal/core/include/Core.h +++ b/client/internal/core/include/Core.h @@ -14,7 +14,7 @@ public: static std::vector getAllTasks(); - static Solution submitSolution(const int& task_id, const std::string& filename, const std::string& path_to_file); + static std::pair submitSolution(const int& task_id, const std::string& filename, const std::string& path_to_file); static unsigned int createTask(const std::string& name, const std::string &desc, const double& threshold); diff --git a/client/internal/core/src/Core.cpp b/client/internal/core/src/Core.cpp index 3f8acb6..aff4b39 100644 --- a/client/internal/core/src/Core.cpp +++ b/client/internal/core/src/Core.cpp @@ -5,7 +5,7 @@ #include "HttpClientManager.h" const std::string CLIENT_IP = "0.0.0.0"; -const std::string CLIENT_PORT = "8080"; +const std::string CLIENT_PORT = "8081"; HttpClientManager client(CLIENT_IP, CLIENT_PORT); std::size_t Core::user_id = -1; @@ -30,7 +30,7 @@ std::vector Core::getAllTasks() { return client.getAllTasks(); } -Solution Core::submitSolution(const int &task_id, const std::string& filename, const std::string &path_to_file) { +std::pair Core::submitSolution(const int &task_id, const std::string& filename, const std::string &path_to_file) { return client.submitSolution(user_id, task_id, filename, path_to_file); } diff --git a/client/internal/entities/include/Solution.h b/client/internal/entities/include/Solution.h index 4cf7061..9de0537 100644 --- a/client/internal/entities/include/Solution.h +++ b/client/internal/entities/include/Solution.h @@ -8,4 +8,8 @@ struct Solution { size_t id; std::string source; std::string result; + struct Codes { + std::string original; + std::string current; + }; }; \ No newline at end of file diff --git a/client/internal/gui/include/SolutionsWindow.h b/client/internal/gui/include/SolutionsWindow.h index 3107c18..df2f09d 100644 --- a/client/internal/gui/include/SolutionsWindow.h +++ b/client/internal/gui/include/SolutionsWindow.h @@ -39,6 +39,12 @@ private: QPushButton* sendButton = nullptr; QTextEdit* result = nullptr; QPushButton* backButton = nullptr; + QWidget* solutionWidget = nullptr; + QGridLayout* solutionLayout = nullptr; + QLabel* originalLabel = nullptr; + QTextEdit* original = nullptr; + QLabel* currentLabel = nullptr; + QTextEdit* current = nullptr; void setupUi(QMainWindow *UserWindow); }; diff --git a/client/internal/gui/src/SolutionsWindow.cpp b/client/internal/gui/src/SolutionsWindow.cpp index af3d1f4..ce1adb9 100644 --- a/client/internal/gui/src/SolutionsWindow.cpp +++ b/client/internal/gui/src/SolutionsWindow.cpp @@ -34,19 +34,37 @@ void SolutionsWindow::setupUi(QMainWindow *SolutionsWindow) { taskLayout->addWidget(taskDescription); - filename = new QLabel(this); + filename = new QLabel(SolutionsWindow); - chooseFileButton = new QPushButton(this); + chooseFileButton = new QPushButton(SolutionsWindow); chooseFileButton->setText(QString::fromUtf8("Выберите файл")); - sendButton = new QPushButton(this); + sendButton = new QPushButton(SolutionsWindow); sendButton->setText(QString::fromUtf8("Отправить")); - result = new QTextEdit(this); + result = new QTextEdit(SolutionsWindow); result->setReadOnly(true); result->setText(QString::fromUtf8("Отправьте для принятия решения")); - backButton = new QPushButton(this); + solutionWidget = new QWidget(SolutionsWindow); + solutionLayout = new QGridLayout(solutionWidget); + + originalLabel = new QLabel(solutionWidget); + originalLabel->setText(QString::fromUtf8("Оригинальное решение")); + original = new QTextEdit(solutionWidget); + original->setReadOnly(true); + + currentLabel = new QLabel(solutionWidget); + currentLabel->setText(QString::fromUtf8("Ваше решение")); + current = new QTextEdit(solutionWidget); + current->setReadOnly(true); + + solutionLayout->addWidget(currentLabel, 0, 0); + solutionLayout->addWidget(originalLabel, 0, 1); + solutionLayout->addWidget(current, 1, 0); + solutionLayout->addWidget(original, 1, 1); + + backButton = new QPushButton(SolutionsWindow); backButton->setText(QString::fromUtf8("Назад")); verticalLayout->addWidget(taskBox); @@ -54,6 +72,7 @@ void SolutionsWindow::setupUi(QMainWindow *SolutionsWindow) { verticalLayout->addWidget(chooseFileButton); verticalLayout->addWidget(sendButton); verticalLayout->addWidget(result); + verticalLayout->addWidget(solutionWidget); verticalLayout->addWidget(backButton); SolutionsWindow->setCentralWidget(centralwidget); @@ -75,8 +94,12 @@ void SolutionsWindow::on_sendButton_clicked() { QMessageBox::warning(this, "Ошибка отправки", "Файл должен быть указан"); return; } - Solution sol = Core::submitSolution(task.id, filename->text().toUtf8().constData(), path_to_file); + Solution sol; + Solution::Codes codes; + std::tie(sol, codes) = Core::submitSolution(task.id, filename->text().toUtf8().constData(), path_to_file); result->setText(QString::fromStdString(sol.result)); + original->setText(QString::fromUtf8(codes.original)); + current->setText(QString::fromUtf8(codes.current)); } diff --git a/client/internal/httpClient/include/HttpClientManager.h b/client/internal/httpClient/include/HttpClientManager.h index 2d15f7f..be95a33 100644 --- a/client/internal/httpClient/include/HttpClientManager.h +++ b/client/internal/httpClient/include/HttpClientManager.h @@ -19,7 +19,7 @@ public: std::pair loginUser(const std::string &login, const std::string &password); std::pair registerUser(const std::string &login, const std::string &username, const std::string &password); - Solution submitSolution(const int& user_id, const int &task_id, const std::string& filename, + std::pair submitSolution(const int& user_id, const int &task_id, const std::string& filename, const std::string &path_to_solution); unsigned int getAllSolutionsForTask(const int& user_id, const int& task_id); std::vector getAllTasks(); diff --git a/client/internal/httpClient/include/Serializer.h b/client/internal/httpClient/include/Serializer.h index ba742f2..a63ffd3 100644 --- a/client/internal/httpClient/include/Serializer.h +++ b/client/internal/httpClient/include/Serializer.h @@ -17,6 +17,7 @@ public: User deserialUserData(std::string_view body); Solution deserialSolutionData(std::string_view body); + std::pair deserialNewSolutionData(std::string_view body); std::vector deserialAllTasks(std::string_view body); }; diff --git a/client/internal/httpClient/src/HttpClientManager.cpp b/client/internal/httpClient/src/HttpClientManager.cpp index a8831c0..2f19087 100644 --- a/client/internal/httpClient/src/HttpClientManager.cpp +++ b/client/internal/httpClient/src/HttpClientManager.cpp @@ -43,7 +43,7 @@ std::pair HttpClientManager::registerUser(const std::string &log return {status, user}; } -Solution HttpClientManager::submitSolution(const int &user_id, const int &task_id, const std::string& filename, +std::pair HttpClientManager::submitSolution(const int &user_id, const int &task_id, const std::string& filename, const std::string &path_to_sound) { std::string body = serializer->serialSolutionData(user_id, task_id, filename, path_to_sound); http::response res = client->makeGetRequest("/solution/submit", body); @@ -54,8 +54,10 @@ Solution HttpClientManager::submitSolution(const int &user_id, const int &task_i auto* cbuf = boost::asio::buffer_cast(seq); res_body.append(cbuf, boost::asio::buffer_size(seq)); } - Solution sol = serializer->deserialSolutionData(res_body); - return sol; + Solution sol; + Solution::Codes codes; + std::tie(sol, codes) = serializer->deserialNewSolutionData(res_body); + return {sol, codes}; } unsigned int HttpClientManager::getAllSolutionsForTask(const int &user_id, const int &task_id) { diff --git a/client/internal/httpClient/src/Serializer.cpp b/client/internal/httpClient/src/Serializer.cpp index c2d803d..c4a505e 100644 --- a/client/internal/httpClient/src/Serializer.cpp +++ b/client/internal/httpClient/src/Serializer.cpp @@ -82,6 +82,23 @@ Solution Serializer::deserialSolutionData(std::string_view body) { return res; } +std::pair Serializer::deserialNewSolutionData(std::string_view body) { + std::stringstream ss; + ss << body; + boost::property_tree::ptree json; + boost::property_tree::read_json(ss, json); + Solution sol = { + json.get("sol_id"), + json.get("source"), + json.get("result"), + }; + Solution::Codes codes = { + json.get("original"), + json.get("your_code"), + }; + return {sol, codes}; +} + std::string Serializer::serialNewTaskData(std::string_view name, std::string_view desc, double threshold) { boost::property_tree::ptree json; json.put("name", name); diff --git a/server/internal/httpServer/include/Serializer.h b/server/internal/httpServer/include/Serializer.h index 9790ac5..35ab279 100644 --- a/server/internal/httpServer/include/Serializer.h +++ b/server/internal/httpServer/include/Serializer.h @@ -22,6 +22,7 @@ class Serializer { std::string serialAllTasks(const std::vector& tasks); std::string serialUserData(const User& user); std::string serialSolution(const Solution& sol); + std::string serialNewSolution(const Solution& sol, const Solution::Codes& codes); std::string serialTask(const Task& task); }; diff --git a/server/internal/httpServer/src/Serializer.cpp b/server/internal/httpServer/src/Serializer.cpp index 72085bc..dda60b1 100644 --- a/server/internal/httpServer/src/Serializer.cpp +++ b/server/internal/httpServer/src/Serializer.cpp @@ -116,6 +116,18 @@ std::string Serializer::serialSolution(const Solution &sol) { return out.str(); } +std::string Serializer::serialNewSolution(const Solution &sol, const Solution::Codes& codes) { + boost::property_tree::ptree json; + json.put("sol_id", sol.getId()); + json.put("source", sol.getSource()); + json.put("result", sol.getResult()); + json.put("your_code", codes.current); + json.put("original", codes.original); + std::stringstream out; + boost::property_tree::write_json(out, json); + return out.str(); +} + std::string Serializer::serialTask(const Task &task) { boost::property_tree::ptree json; json.put("name", task.getName()); diff --git a/server/internal/httpServer/src/SolutionManager.cpp b/server/internal/httpServer/src/SolutionManager.cpp index 3bd96df..3a6a1f5 100644 --- a/server/internal/httpServer/src/SolutionManager.cpp +++ b/server/internal/httpServer/src/SolutionManager.cpp @@ -24,13 +24,14 @@ http::message_generator SolutionManager::createSolution(http::requestcreateSolution(user_id, task_id, filename, filedata).first; + Solution sol; + Solution::Codes codes; + std::tie(sol, codes) = solutionService->createSolution(user_id, task_id, filename, filedata); http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/plain"); res.keep_alive(req.keep_alive()); - res.body() = serializer->serialSolution(sol); - res.prepare_payload(); + res.body() = serializer->serialNewSolution(sol, codes); return res; } catch (const std::exception& e) { return getInternalServerError(req, e.what()); -- GitLab