match best champion

This commit is contained in:
mrbesen 2022-04-23 01:53:19 +02:00
parent 41ecc12e75
commit b66e6740d8
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
6 changed files with 110 additions and 55 deletions

View File

@ -33,7 +33,7 @@ public:
const std::string& getVersion();
const std::vector<ChampData>& 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:

View File

@ -46,7 +46,7 @@ private:
Ui::StageSettings *ui;
DataDragon* dd = nullptr;
int currentdisplayedChampKey;
int currentdisplayedChampKey = -1;
cv::Mat img;
};

View File

@ -9,6 +9,8 @@
#include <QJsonArray>
#include <QJsonObject>
#include <algorithm> // 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;
}

View File

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

View File

@ -140,6 +140,7 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mainswitch</tabstop>
<tabstop>enableAll</tabstop>
</tabstops>
<resources/>
@ -151,8 +152,8 @@
<slot>aatoggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>16</x>
<y>106</y>
<x>17</x>
<y>268</y>
</hint>
<hint type="destinationlabel">
<x>393</x>
@ -167,8 +168,8 @@
<slot>pptoggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>169</x>
<y>147</y>
<x>170</x>
<y>343</y>
</hint>
<hint type="destinationlabel">
<x>279</x>
@ -183,8 +184,8 @@
<slot>bantoggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>139</x>
<y>345</y>
<x>140</x>
<y>418</y>
</hint>
<hint type="destinationlabel">
<x>277</x>
@ -199,8 +200,8 @@
<slot>picktoggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>130</x>
<y>509</y>
<x>131</x>
<y>493</y>
</hint>
<hint type="destinationlabel">
<x>279</x>
@ -215,8 +216,8 @@
<slot>ppedited(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>111</x>
<y>139</y>
<x>112</x>
<y>343</y>
</hint>
<hint type="destinationlabel">
<x>280</x>
@ -231,8 +232,8 @@
<slot>banedited(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>144</x>
<y>366</y>
<x>145</x>
<y>418</y>
</hint>
<hint type="destinationlabel">
<x>280</x>
@ -247,8 +248,8 @@
<slot>pickedited(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>151</x>
<y>508</y>
<x>152</x>
<y>493</y>
</hint>
<hint type="destinationlabel">
<x>279</x>
@ -263,8 +264,8 @@
<slot>toggleMainswitch(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>85</x>
<y>83</y>
<x>86</x>
<y>168</y>
</hint>
<hint type="destinationlabel">
<x>1</x>

View File

@ -51,30 +51,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
<item row="0" column="2" rowspan="2">
<item row="0" column="2" rowspan="3">
<widget class="QLabel" name="champImg">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@ -111,6 +88,36 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QLabel" name="champcount">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>