From 4de7c57a46d4ef5d3248907957ffc9a607b59991 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 3 Jul 2022 00:31:13 +0200 Subject: [PATCH] store and restore settings --- include/lolautoaccept.h | 6 +- include/mainwindow.h | 6 +- include/stagesettings.h | 7 +- lolautoaccept.pro | 1 + makeXInputSimulator.sh | 7 -- src/championsearch.cpp | 3 + src/datadragon.cpp | 17 +---- src/lolautoaccept.cpp | 34 +++++++-- src/mainwindow.cpp | 37 ++++++--- src/stagesettings.cpp | 38 +++------- ui/mainwindow.ui | 162 ++++++++++++++++++++-------------------- ui/stagesettings.ui | 104 ++++++++------------------ 12 files changed, 196 insertions(+), 226 deletions(-) delete mode 100755 makeXInputSimulator.sh diff --git a/include/lolautoaccept.h b/include/lolautoaccept.h index aa8b37b..885f73d 100644 --- a/include/lolautoaccept.h +++ b/include/lolautoaccept.h @@ -13,6 +13,7 @@ protected: std::vector champids; // not every stage has a champ bool enabled = false; + uint32_t currentOffset = 0; }; std::vector stages; @@ -36,7 +37,7 @@ public: LolAutoAccept(); ~LolAutoAccept(); - void setChamps(std::vector champs, State s); + void setChamps(const std::vector& champs, State s); void setEnabled(bool b, State s); bool init(); // returns true on success @@ -46,6 +47,9 @@ public: private: void stopJoinThread(); void innerRun(); + void resetAllOffsets(); + + uint32_t getNextChampOfState(State s); using ownactions_t = std::vector; ownactions_t getOwnActions(int32_t cellid, const std::vector actions); diff --git a/include/mainwindow.h b/include/mainwindow.h index f18cf31..c9c6183 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -26,11 +26,11 @@ private slots: void toggleMainswitch(bool); void aatoggled(bool); void pptoggled(bool); - void ppedited(const QString&); + void ppedited(); void bantoggled(bool); - void banedited(const QString&); + void banedited(); void picktoggled(bool); - void pickedited(const QString&); + void pickedited(); private: // returns empty string on no match diff --git a/include/stagesettings.h b/include/stagesettings.h index cbea8bf..26dbbfb 100644 --- a/include/stagesettings.h +++ b/include/stagesettings.h @@ -41,16 +41,15 @@ private slots: void toggledinternal(int state); void addChamp(); void removeChamp(); - void moveUp(); - void moveDown(); + void moved(); signals: void toggled(bool); - void championChanged(int position, const QString&); + void championsChanged(); private: // delete all items - void resolveAndAddChamp(const std::string& name); + void resolveAndAddChamp(const std::string& name, bool emitchange = false); void clear(); void updateEnabled(); diff --git a/lolautoaccept.pro b/lolautoaccept.pro index 6ac075d..3a1eb82 100644 --- a/lolautoaccept.pro +++ b/lolautoaccept.pro @@ -96,6 +96,7 @@ QMAKE_CLEAN += $$TRANSLATIONSQM unix { $$PWD/linuxdeploy-x86_64.AppImage.commands = wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage && chmod u+x linuxdeploy-x86_64.AppImage + $$PWD/lolautoaccept.png.depends = $$PWD/lolautoaccept.svg $$PWD/lolautoaccept.png.commands = inkscape -z -e $$PWD/lolautoaccept.png -w 512 -h 512 $$PWD/lolautoaccept.svg appimg.depends = $$PWD/linuxdeploy-x86_64.AppImage $${TARGET} $$PWD/lolautoaccept.png diff --git a/makeXInputSimulator.sh b/makeXInputSimulator.sh deleted file mode 100755 index 1270dce..0000000 --- a/makeXInputSimulator.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -rm -rf thirdparty/XInputSimulator/build -mkdir -p thirdparty/XInputSimulator/build -cd thirdparty/XInputSimulator/build/ -cmake ../XInputSimulator/ -make -j diff --git a/src/championsearch.cpp b/src/championsearch.cpp index 1add0bc..7b80399 100644 --- a/src/championsearch.cpp +++ b/src/championsearch.cpp @@ -12,6 +12,9 @@ ChampionSearch::~ChampionSearch() { } ChampRow* ChampionSearch::getSearchResult() { + if(ui->championList->count() == 1) { + return (ChampRow*) ui->championList->takeItem(0); + } int row = ui->championList->currentRow(); return (ChampRow*) ui->championList->takeItem(row); } diff --git a/src/datadragon.cpp b/src/datadragon.cpp index f0db6e9..36c1f5a 100644 --- a/src/datadragon.cpp +++ b/src/datadragon.cpp @@ -173,25 +173,10 @@ std::vector DataDragon::getMatchingChamp(const std matches.resize(champs.size()); std::transform(champs.begin(), champs.end(), std::insert_iterator(matches, matches.begin()), std::bind(&matchChamp, std::placeholders::_1, lower)); - { - Log::Entry e = Log::info << "matches content: "; - for(size_t m : matches) { - e << m << " "; - } - } - // find smallest element std::vector matches_sort = matches; - std::nth_element(matches_sort.begin(), matches_sort.begin(), matches_sort.end(), [](size_t a, size_t b) { return b < a; }); + std::nth_element(matches_sort.begin(), matches_sort.begin(), matches_sort.end(), std::greater{}); size_t border = matches_sort.at(0); - - { - Log::Entry e = Log::info << "matches_sort content: "; - for(size_t m : matches_sort) { - e << m << " "; - } - } - Log::trace << "border: " << border; std::vector out; diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index eb1699a..a759994 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -23,14 +23,15 @@ LolAutoAccept::~LolAutoAccept() { } } -void LolAutoAccept::setChamps(std::vector champs, State s) { +void LolAutoAccept::setChamps(const std::vector& champs, State s) { if(s == State::LOBBY || s >= State::GAME) { Log::warn << "setChamps() called on invalid State"; return; } stages.at((int) s)->champids = champs; - Log::info << "set champs on state: " << (int) s; + stages.at((int) s)->currentOffset = 0; + Log::info << "set champs on state: " << (int) s << " count: " << champs.size(); } void LolAutoAccept::setEnabled(bool b, State s) { @@ -97,6 +98,7 @@ void LolAutoAccept::innerRun() { clientapi->acceptMatch(); } } + resetAllOffsets(); } else if(phase == ClientAPI::GameflowPhase::CHAMPSELECT) { champSelect(); } @@ -110,6 +112,28 @@ void LolAutoAccept::innerRun() { } } +void LolAutoAccept::resetAllOffsets() { + for(Stage* stage : stages) { + stage->currentOffset = 0; + } +} + +uint32_t LolAutoAccept::getNextChampOfState(State s) { + assert(((int) s) >= 0 && s <= State::PICK); + + auto stage = stages[(int) s]; + int size = stage->champids.size(); + if(size >= stage->currentOffset) { + // no champ to try left + Log::warn << "no champ left at stage: " << (int) s; + Log::warn << "stage size: " << stage->champids.size(); + stage->currentOffset = 0; + return 0; + } + + return stage->champids[stage->currentOffset++]; +} + LolAutoAccept::ownactions_t LolAutoAccept::getOwnActions(int32_t cellid, const std::vector actions) { ownactions_t out; for(const auto& it : actions) { @@ -130,7 +154,7 @@ void LolAutoAccept::prepickPhase(const ownactions_t& ownactions) { Log::info << "pre-pick action anvailable: " << it.id << " champid: " << it.championID; if(it.championID == 0) { // try to prepick a champion - uint32_t champid = stages.at((int) State::PREPICK)->champids.at(0); // TODO: do on multi selection + uint32_t champid = getNextChampOfState(State::PREPICK); Log::info << "try to prepick champ: " << champid; clientapi->setChampSelectAction(it.id, champid, false); break; @@ -147,7 +171,7 @@ void LolAutoAccept::banPhase(const ownactions_t& ownactions) { Log::info << "ban action anvailable: " << it.id << " champid: " << it.championID; if(it.championID == 0) { // try to ban a champion - uint32_t champid = stages.at((int) State::BAN)->champids.at(0); // TODO + uint32_t champid = getNextChampOfState(State::BAN); Log::info << "try to ban champ: " << champid; clientapi->setChampSelectAction(it.id, champid, true); return; @@ -162,7 +186,7 @@ void LolAutoAccept::pickPhase(const ownactions_t& ownactions) { for(auto it : ownactions) { if(it.type == ClientAPI::ChampSelectActionType::PICK) { Log::info << "pick action anvailable: " << it.id << " champid: " << it.championID; - uint32_t champid = stages.at((int) State::PICK)->champids.at(0); // TODO + uint32_t champid = getNextChampOfState(State::PICK); if(it.championID != champid || !it.completed) { // try to prepick a champion Log::info << "try to pick champ: " << champid; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index be7399f..3b2879d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -71,16 +71,27 @@ static std::vector toChampionNameList(const std::vector toChampionIDList(const std::vector& scs) { + std::vector out; + + for(const StageSettings::SelectedChamp& sc : scs) { + out.push_back(sc.id); + } + + return out; +} + void MainWindow::pptoggled(bool state) { Log::info << "enablePrePick checkbox toggled " << state; lolaa.setEnabled(state, LolAutoAccept::State::PREPICK); conf.getConfig().prepick.enabled = state; } -void MainWindow::ppedited(const QString& b) { - Log::info << "prepick edited: " << b.toStdString(); - //lolaa.setChamps(dd.getBestMatchingChamp(b.toStdString()).key, LolAutoAccept::State::PREPICK); - conf.getConfig().prepick.champs = toChampionNameList(ui->prepickstage->getChampions()); +void MainWindow::ppedited() { + Log::info << "prepick edited"; + auto champs = ui->prepickstage->getChampions(); + lolaa.setChamps(toChampionIDList(champs), LolAutoAccept::State::PREPICK); + conf.getConfig().prepick.champs = toChampionNameList(champs); } void MainWindow::bantoggled(bool state) { @@ -89,10 +100,11 @@ void MainWindow::bantoggled(bool state) { conf.getConfig().ban.enabled = state; } -void MainWindow::banedited(const QString& b) { - Log::info << "ban edited: " << b.toStdString(); - //lolaa.setChamp(dd.getBestMatchingChamp(b.toStdString()).key, LolAutoAccept::State::BAN); - conf.getConfig().ban.champs = toChampionNameList(ui->banstage->getChampions()); +void MainWindow::banedited() { + Log::info << "ban edited"; + auto champs = ui->banstage->getChampions(); + lolaa.setChamps(toChampionIDList(champs), LolAutoAccept::State::BAN); + conf.getConfig().ban.champs = toChampionNameList(champs); } void MainWindow::picktoggled(bool state) { @@ -101,8 +113,9 @@ void MainWindow::picktoggled(bool state) { conf.getConfig().pick.enabled = state; } -void MainWindow::pickedited(const QString& b) { - Log::info << "pick edited: " << b.toStdString(); - //lolaa.setChamp(dd.getBestMatchingChamp(b.toStdString()).key, LolAutoAccept::State::PICK); - conf.getConfig().pick.champs = toChampionNameList(ui->pickstage->getChampions()); +void MainWindow::pickedited() { + Log::info << "pick edited"; + auto champs = ui->pickstage->getChampions(); + lolaa.setChamps(toChampionIDList(champs), LolAutoAccept::State::PICK); + conf.getConfig().pick.champs = toChampionNameList(champs); } diff --git a/src/stagesettings.cpp b/src/stagesettings.cpp index ca2105d..f6c5d9e 100644 --- a/src/stagesettings.cpp +++ b/src/stagesettings.cpp @@ -11,7 +11,6 @@ StageSettings::StageSettings(QWidget *parent) : QWidget(parent), ui(new Ui::StageSettings) { ui->setupUi(this); setMinimumSize(ui->gridLayout->minimumSize()); - } StageSettings::~StageSettings() { @@ -54,10 +53,7 @@ void StageSettings::setChampions(const std::vector& champs) { // add new champs for(const std::string& champ : champs) { - auto champr = new ChampRow(); - ui->championList->addItem(champr); - - champr->setChamp(champ, 0, QPixmap("/home/yannis/.cache/lolautoaccept/square/Orianna.png")); // TODO + resolveAndAddChamp(champ, champ == champs.back()); } } @@ -69,7 +65,7 @@ void StageSettings::addChamp(const std::string& champname, uint32_t id, QPixmap auto champr = new ChampRow(); ui->championList->addItem(champr); - champr->setChamp(champname, id, icon); // TODO + champr->setChamp(champname, id, icon); } void StageSettings::toggledinternal(int state) { @@ -79,8 +75,8 @@ void StageSettings::toggledinternal(int state) { void StageSettings::addChamp() { Log::trace << "add champ"; - // TODO: popup with champion search + // popup with champion search ChampionSearch cs(dd, this); int res = cs.exec(); Log::info << "championsearch result: " << res; @@ -88,10 +84,8 @@ void StageSettings::addChamp() { auto cr = cs.getSearchResult(); if(cr) { ui->championList->addItem(cr); - // TODO: emit value changed something + emit championsChanged(); } - } else { - // not accepted } } @@ -101,27 +95,21 @@ void StageSettings::removeChamp() { delete ui->championList->takeItem(row); } -void StageSettings::moveUp() { - Log::trace << "move up"; - if(ui->championList->selectedItems().isEmpty()) return; - - auto selection = ui->championList->selectedItems().at(0); - ui->championList->removeItemWidget(selection); - ui->championList->insertItem(0, selection); +void StageSettings::moved() { + Log::trace << "moved"; + emit championsChanged(); } -void StageSettings::moveDown() { - Log::trace << "move down"; -} - -void StageSettings::resolveAndAddChamp(const std::string& name) { +void StageSettings::resolveAndAddChamp(const std::string& name, bool emitchange) { if(dd) { int count = 0; auto cd = dd->getBestMatchingChamp(name, &count); - dd->getImageAsnyc(cd.id, [this, name, cd](QPixmap img) { + dd->getImageAsnyc(cd.id, [this, name, cd, emitchange](QPixmap img) { addChamp(name, cd.key, img); - emit championChanged(0, QString::fromStdString(cd.name)); + if(emitchange) { + emit championsChanged(); + } }); } else { Log::error << __PRETTY_FUNCTION__ << " Datadragon not set!"; @@ -139,7 +127,5 @@ void StageSettings::updateEnabled() { ui->championList->setEnabled(active); ui->addChampion->setEnabled(active); - ui->moveup->setEnabled(active); - ui->movedown->setEnabled(active); ui->removeChampion->setEnabled(active); } diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index 159b858..15ecccb 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -58,7 +58,7 @@ 200 - 0 + 10 @@ -80,7 +80,7 @@ 200 - 0 + 10 @@ -102,7 +102,7 @@ 200 - 0 + 10 @@ -135,7 +135,7 @@
stagesettings.h
toggled(bool) - championChanged(QString) + championsChanged() @@ -153,7 +153,7 @@ 17 - 268 + 258 393 @@ -168,12 +168,12 @@ pptoggled(bool) - 170 - 343 + 252 + 338 - 279 - 140 + 296 + 309 @@ -184,76 +184,12 @@ bantoggled(bool) - 140 - 418 + 227 + 413 - 277 - 292 - - - - - pickstage - toggled(bool) - MainWindow - picktoggled(bool) - - - 131 - 493 - - - 279 - 444 - - - - - prepickstage - championChanged(QString) - MainWindow - ppedited(QString) - - - 112 - 343 - - - 280 - 204 - - - - - banstage - championChanged(QString) - MainWindow - banedited(QString) - - - 145 - 418 - - - 280 - 344 - - - - - pickstage - championChanged(QString) - MainWindow - pickedited(QString) - - - 152 - 493 - - - 279 - 519 + 265 + 390 @@ -265,7 +201,7 @@ 86 - 168 + 163 1 @@ -273,15 +209,79 @@ + + pickstage + toggled(bool) + MainWindow + picktoggled(bool) + + + 201 + 489 + + + 265 + 476 + + + + + pickstage + championsChanged() + MainWindow + pickedited() + + + 172 + 498 + + + 122 + 473 + + + + + banstage + championsChanged() + MainWindow + banedited() + + + 149 + 418 + + + 111 + 379 + + + + + prepickstage + championsChanged() + MainWindow + ppedited() + + + 184 + 335 + + + 122 + 308 + + + aatoggled(bool) pptoggled(bool) - ppedited(QString) + ppedited() bantoggled(bool) picktoggled(bool) - banedited(QString) - pickedited(QString) + banedited() + pickedited() toggleMainswitch(bool) diff --git a/ui/stagesettings.ui b/ui/stagesettings.ui index 243ec9c..d49315e 100644 --- a/ui/stagesettings.ui +++ b/ui/stagesettings.ui @@ -51,6 +51,24 @@
+ + + + + + Add Champion + + + + + + + Remove Champion + + + + + @@ -71,40 +89,17 @@ QAbstractItemView::InternalMove + + true + + + QListView::TopToBottom + + + QListView::Adjust + - - - - - - Add Champion - - - - - - - Move Up - - - - - - - Move Down - - - - - - - Remove Champion - - - - - @@ -138,8 +133,8 @@ addChamp() - 643 - 209 + 739 + 328 757 @@ -147,38 +142,6 @@ - - moveup - clicked() - StageSettings - moveUp() - - - 683 - 371 - - - 758 - 401 - - - - - movedown - clicked() - StageSettings - moveDown() - - - 702 - 526 - - - 758 - 536 - - - removeChampion clicked() @@ -186,8 +149,8 @@ removeChamp() - 717 - 691 + 739 + 598 758 @@ -197,13 +160,12 @@ - championChanged(QString) toggled(bool) + championsChanged() championChangedinternal(QString) toggledinternal(int) addChamp() removeChamp() - moveUp() - moveDown() + moved()