From c2203b4b5dcb82e665d63461b2d8f81ac8a82e5e Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 10 Jul 2022 15:56:09 +0200 Subject: [PATCH] resolve runestyle names --- include/clientapi.h | 6 +++--- include/lolautoaccept.h | 3 +++ include/runedisplay.h | 5 +++++ include/runestyle.h | 17 +++++++++++++++++ src/clientapi.cpp | 19 +++++++++++++++++++ src/clientapi_json.cpp | 9 +++++++++ src/lolautoaccept.cpp | 11 +++++++++++ src/mainwindow.cpp | 1 + src/runedisplay.cpp | 19 +++++++++++++++++-- 9 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 include/runestyle.h diff --git a/include/clientapi.h b/include/clientapi.h index fd6910d..ff383a9 100644 --- a/include/clientapi.h +++ b/include/clientapi.h @@ -5,6 +5,7 @@ #include "position.h" #include "runeaspekt.h" #include "runepage.h" +#include "runestyle.h" class ClientAPI : public RestClient { public: @@ -170,10 +171,9 @@ public: bool editRunePage(const RunePage& page); std::vector getAllRuneAspekts(); + std::vector getAllRuneStyles(); -protected: - - + const std::string& getRuneStyleByID(uint32_t id); private: ClientAccess access; diff --git a/include/lolautoaccept.h b/include/lolautoaccept.h index 04726ad..c5a7ede 100644 --- a/include/lolautoaccept.h +++ b/include/lolautoaccept.h @@ -9,6 +9,7 @@ #include "config.h" #include "datadragon.h" #include "runepage.h" +#include "runestyle.h" class LolAutoAccept { public: @@ -40,6 +41,7 @@ protected: BlitzAPI blitzapi; std::vector runeaspekts; + std::vector runestyles; bool nextApplyRunes = false; @@ -63,6 +65,7 @@ public: void reload(); // reload the config, when something was changed const std::vector& getRuneAspekts(); + const std::vector& getRuneStyles(); void applyRunes(); void setOnRuneChangeFunc(onruneschange_func on); diff --git a/include/runedisplay.h b/include/runedisplay.h index 27f8e0e..6c102cb 100644 --- a/include/runedisplay.h +++ b/include/runedisplay.h @@ -4,6 +4,7 @@ #include "runeaspekt.h" #include "runepage.h" +#include "runestyle.h" namespace Ui { class RuneDisplay; @@ -17,6 +18,7 @@ public: ~RuneDisplay(); void setRuneMeta(const std::vector& runeinfo); + void setStyles(const std::vector& styleinfos); void setRunes(const RunePage& rp); private slots: @@ -28,10 +30,13 @@ signals: private: void updateText(); std::string getRuneText(uint32_t id); + std::string getRuneStyleByID(uint32_t id); Ui::RuneDisplay *ui; RunePage runepage; std::vector runeinfo; + std::vector runestyles; + }; diff --git a/include/runestyle.h b/include/runestyle.h new file mode 100644 index 0000000..a6b2e02 --- /dev/null +++ b/include/runestyle.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +// fwd. +class QJsonObject; + +struct RuneStyle { + uint32_t id; + std::string name; + std::string iconPath; + std::string tooltip; + + RuneStyle(); + explicit RuneStyle(const QJsonObject& json); +}; diff --git a/src/clientapi.cpp b/src/clientapi.cpp index f9834ce..8730ef2 100644 --- a/src/clientapi.cpp +++ b/src/clientapi.cpp @@ -247,3 +247,22 @@ std::vector ClientAPI::getAllRuneAspekts() { } return out; } + +std::vector ClientAPI::getAllRuneStyles() { + QJsonDocument doc = request("lol-perks/v1/styles"); + + if(!doc.isArray()) { + Log::warn << __PRETTY_FUNCTION__ << " doc is not array"; + return {}; + } + + QJsonArray arr = doc.array(); + std::vector out; + out.reserve(arr.size()); + for(auto it : arr) { + if(it.isObject()) { + out.push_back((RuneStyle) it.toObject()); + } + } + return out; +} diff --git a/src/clientapi_json.cpp b/src/clientapi_json.cpp index 6978ad9..e13b1fb 100644 --- a/src/clientapi_json.cpp +++ b/src/clientapi_json.cpp @@ -207,6 +207,15 @@ ClientAPI::RunePage::RunePage(const QJsonObject& json) { } } +RuneStyle::RuneStyle() {} +RuneStyle::RuneStyle(const QJsonObject& json) { + id = getValue(json, "id", 0); + name = getValue(json, "name"); + iconPath = getValue(json, "iconPath"); + tooltip = getValue(json, "tooltip"); +} + + RuneAspekt::RuneAspekt() {} RuneAspekt::RuneAspekt(const QJsonObject& json) { id = getValue(json, "id", 0); diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index ff6f992..573019f 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -83,6 +83,17 @@ const std::vector& LolAutoAccept::getRuneAspekts() { return runeaspekts; } +const std::vector& LolAutoAccept::getRuneStyles() { + if(runestyles.empty()) { + if(clientapi) { + runestyles = clientapi->getAllRuneStyles(); + Log::info << "Loaded " << runestyles.size() << " rune styles"; + } + } + + return runestyles; +} + void LolAutoAccept::applyRunes() { nextApplyRunes = true; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f07240f..eabf84c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -56,6 +56,7 @@ void MainWindow::toggleMainswitch(bool state) { } ui->runedisplay->setRuneMeta(lolaa.getRuneAspekts()); + ui->runedisplay->setStyles(lolaa.getRuneStyles()); lolaa.run(); diff --git a/src/runedisplay.cpp b/src/runedisplay.cpp index b40ce28..f3e00bc 100644 --- a/src/runedisplay.cpp +++ b/src/runedisplay.cpp @@ -3,6 +3,8 @@ #include +const static std::string EMPTY; + RuneDisplay::RuneDisplay(QWidget *parent) : QWidget(parent), ui(new Ui::RuneDisplay) { ui->setupUi(this); } @@ -15,6 +17,10 @@ void RuneDisplay::setRuneMeta(const std::vector& ri) { runeinfo = ri; } +void RuneDisplay::setStyles(const std::vector& styleinfos) { + runestyles = styleinfos; +} + void RuneDisplay::setRunes(const RunePage& rp) { runepage = rp; @@ -34,7 +40,7 @@ void RuneDisplay::updateText() { return; } - out << "primary: " << getRuneText(runepage.primaryStyle) << " secondary: " << getRuneText(runepage.secondaryStyle) << '\n'; + out << getRuneStyleByID(runepage.primaryStyle) << " with " << getRuneStyleByID(runepage.secondaryStyle) << '\n'; for(uint32_t rune : runepage.selectedAspects) { out << getRuneText(rune) << '\n'; @@ -51,4 +57,13 @@ std::string RuneDisplay::getRuneText(uint32_t id) { } return "(" + std::to_string(id) + ")"; -} \ No newline at end of file +} + +std::string RuneDisplay::getRuneStyleByID(uint32_t id) { + auto it = std::find_if(runestyles.begin(), runestyles.end(), [id](const RuneStyle& rs) { return rs.id == id; }); + if(it == runestyles.end()) { + return '(' + std::to_string(id) + ')'; + } + + return it->name; +}