From b66e6740d8567e25b1ff0ccdd8cd35e1dd3e3e11 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sat, 23 Apr 2022 01:53:19 +0200 Subject: [PATCH] match best champion --- include/datadragon.h | 2 +- include/stagesettings.h | 2 +- src/datadragon.cpp | 53 ++++++++++++++++++++++++++++++++++++--- src/stagesettings.cpp | 20 ++++++++------- ui/mainwindow.ui | 33 +++++++++++++------------ ui/stagesettings.ui | 55 +++++++++++++++++++++++------------------ 6 files changed, 110 insertions(+), 55 deletions(-) diff --git a/include/datadragon.h b/include/datadragon.h index 18cd746..350f22e 100644 --- a/include/datadragon.h +++ b/include/datadragon.h @@ -33,7 +33,7 @@ public: const std::string& getVersion(); const std::vector& getChamps(); cv::Mat getImage(const std::string& champid, ImageType imgtype = ImageType::SQUARE); - const ChampData& getBestMatchingChamp(const std::string& name); + const ChampData& getBestMatchingChamp(const std::string& name, int* count = nullptr); static const ChampData EMPTYCHAMP; protected: diff --git a/include/stagesettings.h b/include/stagesettings.h index c19a7a4..2cd1078 100644 --- a/include/stagesettings.h +++ b/include/stagesettings.h @@ -46,7 +46,7 @@ private: Ui::StageSettings *ui; DataDragon* dd = nullptr; - int currentdisplayedChampKey; + int currentdisplayedChampKey = -1; cv::Mat img; }; diff --git a/src/datadragon.cpp b/src/datadragon.cpp index 1b9062b..86c83f1 100644 --- a/src/datadragon.cpp +++ b/src/datadragon.cpp @@ -9,6 +9,8 @@ #include #include +#include // std::max + static const std::string BASEURL = "https://ddragon.leagueoflegends.com/"; const DataDragon::ChampData DataDragon::EMPTYCHAMP; @@ -150,15 +152,58 @@ static std::string toLower(const std::string& in) { return QString::fromStdString(in).toLower().toStdString(); } -const DataDragon::ChampData& DataDragon::getBestMatchingChamp(const std::string& name) { +static size_t startinglength(const std::string& original, const std::string& prefix) { + size_t i = 0; + for(; i < original.size() && i < prefix.size(); ++i) { + if(original.substr(0, i+1) != prefix.substr(0, i+1)) { + return i; + } + } + return i; +} + +static size_t matchChamp(const DataDragon::ChampData& champ, const std::string& lower) { + std::string lowerid = toLower(champ.id); + std::string lowername = toLower(champ.name); + if(lowerid == lower || lowername == lower) { + return lower.size(); + } + + size_t lengtha = startinglength(lowerid, lower); + size_t lengthb = startinglength(lowername, lower); + + return std::max(lengtha, lengthb); +} + +const DataDragon::ChampData& DataDragon::getBestMatchingChamp(const std::string& name, int* count) { getChamps(); // for now: just check for a perfect hit std::string lower = toLower(name); - for(const ChampData& it : champs) { - if(toLower(it.id) == lower || toLower(it.name) == lower) { - return it; + int bestmatchingoffset = -1; + size_t bestmatchinglength = 0; + uint32_t bestmatchingcount = 0; + for(size_t offset = 0; offset < champs.size(); ++offset) { + const ChampData& it = champs.at(offset); + + size_t match = matchChamp(it, lower); + if(match > bestmatchinglength) { + bestmatchinglength = match; + bestmatchingcount = 1; + bestmatchingoffset = offset; + } else if(match == bestmatchinglength) { + bestmatchingcount ++; } } + + if(bestmatchinglength < lower.size()) { + if(count) *count = 0; + return EMPTYCHAMP; + } + + if(count) *count = bestmatchingcount; + if(bestmatchingcount == 1 && bestmatchingoffset >= 0) + return champs.at(bestmatchingoffset); + return EMPTYCHAMP; } diff --git a/src/stagesettings.cpp b/src/stagesettings.cpp index 04dfafe..0bc65a7 100644 --- a/src/stagesettings.cpp +++ b/src/stagesettings.cpp @@ -51,15 +51,14 @@ void StageSettings::championChangedinternal(const QString& str) { emit championChanged(str); if(dd) { - const DataDragon::ChampData& cd = dd->getBestMatchingChamp(str.toStdString()); - if(cd.key > 0) { - Log::info << "bestmatching: " << cd; - if(cd.key != currentdisplayedChampKey) { - img = dd->getImage(cd.id); - rescaleImage(); - currentdisplayedChampKey = cd.key; - } + int count = 0; + const DataDragon::ChampData& cd = dd->getBestMatchingChamp(str.toStdString(), &count); + if(cd.key != currentdisplayedChampKey) { + img = (cd.id.empty()) ? cv::Mat() : dd->getImage(cd.id); + currentdisplayedChampKey = cd.key; } + ui->champcount->setText(tr("Champions matched: %1").arg(QString::fromStdString(std::to_string(count)))); + rescaleImage(); } } @@ -68,7 +67,10 @@ void StageSettings::toggledinternal(int state) { } void StageSettings::rescaleImage() { - if(img.empty()) return; + if(img.empty()) { + ui->champImg->setPixmap(QPixmap()); + return; + } auto qimg = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); auto p = QPixmap::fromImage(qimg); diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index fef7b76..159b858 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -140,6 +140,7 @@ + mainswitch enableAll @@ -151,8 +152,8 @@ aatoggled(bool) - 16 - 106 + 17 + 268 393 @@ -167,8 +168,8 @@ pptoggled(bool) - 169 - 147 + 170 + 343 279 @@ -183,8 +184,8 @@ bantoggled(bool) - 139 - 345 + 140 + 418 277 @@ -199,8 +200,8 @@ picktoggled(bool) - 130 - 509 + 131 + 493 279 @@ -215,8 +216,8 @@ ppedited(QString) - 111 - 139 + 112 + 343 280 @@ -231,8 +232,8 @@ banedited(QString) - 144 - 366 + 145 + 418 280 @@ -247,8 +248,8 @@ pickedited(QString) - 151 - 508 + 152 + 493 279 @@ -263,8 +264,8 @@ toggleMainswitch(bool) - 85 - 83 + 86 + 168 1 diff --git a/ui/stagesettings.ui b/ui/stagesettings.ui index 63c06b3..da28b62 100644 --- a/ui/stagesettings.ui +++ b/ui/stagesettings.ui @@ -51,30 +51,7 @@ - - - - - 0 - 0 - - - - - 160 - 0 - - - - - - - - - - - - + @@ -111,6 +88,36 @@ + + + + + 0 + 0 + + + + + 100 + 0 + + + + + + + + + + + + + + + + + +