make LolAutoAccept a QObject

This commit is contained in:
mrbesen 2023-02-26 13:50:57 +01:00
parent 5f3ff9e292
commit 6ffdf23085
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 69 additions and 33 deletions

View File

@ -4,6 +4,8 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <QObject>
#include "blitzapi.h" #include "blitzapi.h"
#include "clientapi.h" #include "clientapi.h"
#include "config.h" #include "config.h"
@ -11,11 +13,11 @@
#include "runepage.h" #include "runepage.h"
#include "runestyle.h" #include "runestyle.h"
class LolAutoAccept { class LolAutoAccept : public QObject {
Q_OBJECT
public: public:
using onposchange_func = std::function<void(Position)>;
using onruneschange_func = std::function<void(const RunePage&)>; using onruneschange_func = std::function<void(const RunePage&)>;
using onfailed_func = std::function<void()>;
protected: protected:
struct Stage { struct Stage {
Stage(); Stage();
@ -34,9 +36,7 @@ protected:
Config::RootConfig& config; Config::RootConfig& config;
DataDragon& dd; DataDragon& dd;
onposchange_func onPoschange;
onruneschange_func onRuneschange; onruneschange_func onRuneschange;
onfailed_func onFailed;
bool shouldrun = false; bool shouldrun = false;
std::thread lolaathread; std::thread lolaathread;
@ -63,8 +63,15 @@ public:
PICK = 2, PICK = 2,
}; };
LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onfailed_func = {}, onposchange_func = {}, onruneschange_func = {}); enum class Status {
~LolAutoAccept(); Off,
Running,
Failed
};
Q_ENUM(Status)
LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onruneschange_func = {}, QObject* parent = nullptr);
virtual ~LolAutoAccept();
void setChamps(const std::vector<uint32_t>& champs, State s); void setChamps(const std::vector<uint32_t>& champs, State s);
void setEnabled(bool b, State s); void setEnabled(bool b, State s);
@ -73,6 +80,7 @@ public:
bool init(); // returns true on success bool init(); // returns true on success
void run(); void run();
void stop(); void stop();
Status getStatus();
void reload(); // reload the config, when something was changed void reload(); // reload the config, when something was changed
@ -82,6 +90,10 @@ public:
void setOnRuneChangeFunc(onruneschange_func on); void setOnRuneChangeFunc(onruneschange_func on);
void setAutoWriteText(bool enabled, const std::string& text = {}); void setAutoWriteText(bool enabled, const std::string& text = {});
signals:
void statusChanged(LolAutoAccept::Status); // new status: 0 = off, 1 = on, 2 = failed
void positionChanged(Position);
private: private:
void stopJoinThread(); void stopJoinThread();
void innerRun(); void innerRun();
@ -109,3 +121,5 @@ private:
const std::string& getChatid(); const std::string& getChatid();
void applyRunes_internal(uint32_t champid, Position pos); void applyRunes_internal(uint32_t champid, Position pos);
}; };
Q_DECLARE_METATYPE(LolAutoAccept::Status)

View File

@ -24,7 +24,6 @@ public:
protected: protected:
virtual void closeEvent(QCloseEvent* event) override; virtual void closeEvent(QCloseEvent* event) override;
virtual void resizeEvent(QResizeEvent *event) override;
public slots: public slots:
void resetSaveTimer(); void resetSaveTimer();
@ -48,7 +47,7 @@ signals:
private: private:
// returns empty string on no match // returns empty string on no match
void onPosChange(Position newpos); // to trigger the signal from a QObject void onPosChange(Position newpos); // to trigger the signal from a QObject
void onFail(); // get triggerd, when the autoacceptor fails (lost connection) void lolaaStatusChanged(LolAutoAccept::Status); // get triggerd, when the autoacceptor fails (lost connection)
bool loading; bool loading;
Ui::MainWindow *ui; Ui::MainWindow *ui;

View File

@ -9,7 +9,9 @@
LolAutoAccept::Stage::Stage() {} LolAutoAccept::Stage::Stage() {}
LolAutoAccept::Stage::~Stage() {} LolAutoAccept::Stage::~Stage() {}
LolAutoAccept::LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onfailed_func fail, onposchange_func onposch, onruneschange_func onrunch) : config(config), dd(dd), onPoschange(onposch), onRuneschange(onrunch), onFailed(fail) { LolAutoAccept::LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onruneschange_func onrunch, QObject* parent) : QObject(parent), config(config), dd(dd), onRuneschange(onrunch) {
qRegisterMetaType<LolAutoAccept::Status>();
std::lock_guard lock(stagesMutex); std::lock_guard lock(stagesMutex);
stages.resize(3); // accept, ban, pick stages.resize(3); // accept, ban, pick
} }
@ -73,6 +75,10 @@ void LolAutoAccept::stop() {
shouldrun = false; shouldrun = false;
} }
LolAutoAccept::Status LolAutoAccept::getStatus() {
return shouldrun ? Status::Running : Status::Off;
}
void LolAutoAccept::reload() { void LolAutoAccept::reload() {
Log::note << "reload LolAutoAccept"; Log::note << "reload LolAutoAccept";
if(currentPositionSet) if(currentPositionSet)
@ -134,6 +140,7 @@ void LolAutoAccept::innerRun() {
auto convs = clientapi->getAllConversations(); auto convs = clientapi->getAllConversations();
Log::info << "got " << convs.size() << " conversations"; Log::info << "got " << convs.size() << " conversations";
emit statusChanged(Status::Running);
while(shouldrun) { while(shouldrun) {
uint32_t extrasleep = 800; uint32_t extrasleep = 800;
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
@ -193,6 +200,7 @@ void LolAutoAccept::innerRun() {
std::this_thread::sleep_for(std::chrono::milliseconds(400 + extrasleep)); std::this_thread::sleep_for(std::chrono::milliseconds(400 + extrasleep));
} }
emit statusChanged(Status::Off);
} catch(RestClient::WebException& e) { } catch(RestClient::WebException& e) {
Log::error << "WebException catched: " << e.curlresponse; Log::error << "WebException catched: " << e.curlresponse;
if(e.curlresponse == CURLE_COULDNT_CONNECT) { if(e.curlresponse == CURLE_COULDNT_CONNECT) {
@ -202,11 +210,9 @@ void LolAutoAccept::innerRun() {
// disable this thread // disable this thread
shouldrun = false; shouldrun = false;
// notify the ui
if(onFailed) {
onFailed();
}
} }
// notify the ui
emit statusChanged(Status::Failed);
} }
} }
@ -428,9 +434,8 @@ void LolAutoAccept::champSelect() {
if(pos != currentPosition || !currentPositionSet) { if(pos != currentPosition || !currentPositionSet) {
Log::note << "LolAutoAccept reloading config for position: " << pos << " because it was: " << currentPosition; Log::note << "LolAutoAccept reloading config for position: " << pos << " because it was: " << currentPosition;
loadPosition(pos); loadPosition(pos);
if(onPoschange) {
onPoschange(pos); emit positionChanged(pos);
}
} }
Log::debug << "cellid: " << cellid << " position: " << pos << " counter: " << session.counter << " timer: timeleftip: " << session.timer.adjustedTimeLeftInPhase << " totaltimephase: " << session.timer.totalTimeInPhase; Log::debug << "cellid: " << cellid << " position: " << pos << " counter: " << session.counter << " timer: timeleftip: " << session.timer.adjustedTimeLeftInPhase << " totaltimephase: " << session.timer.totalTimeInPhase;

View File

@ -6,8 +6,12 @@
#include <Log.h> #include <Log.h>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(QLocale().name().toStdString()), MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(QLocale().name().toStdString()),
lolaa(conf.getConfig(), dd, std::bind(&MainWindow::onFail, this), std::bind(&MainWindow::onPosChange, this, std::placeholders::_1)) { lolaa(conf.getConfig(), dd) {
ui->setupUi(this); ui->setupUi(this);
this->ui->runesPage->setLolAA(&lolaa);
QObject::connect(&lolaa, &LolAutoAccept::statusChanged, this, &MainWindow::lolaaStatusChanged);
QObject::connect(&lolaa, &LolAutoAccept::positionChanged, this, &MainWindow::onPosChange);
saveTimer->setInterval(std::chrono::minutes(1)); saveTimer->setInterval(std::chrono::minutes(1));
saveTimer->setSingleShot(true); saveTimer->setSingleShot(true);
@ -53,19 +57,14 @@ MainWindow::~MainWindow() {
lolaa.stop(); lolaa.stop();
conf.save(); conf.save();
delete ui; delete this->ui;
} }
void MainWindow::closeEvent([[maybe_unused]] QCloseEvent* event) { void MainWindow::closeEvent([[maybe_unused]] QCloseEvent* event) {
lolaa.stop();
conf.save(); conf.save();
} }
void MainWindow::resizeEvent([[maybe_unused]] QResizeEvent *event) {
ui->verticalLayoutWidget->setMinimumSize(ui->centralwidget->size());
ui->verticalLayoutWidget->setMaximumSize(ui->centralwidget->size());
ui->verticalLayoutWidget->setMinimumSize(ui->centralwidget->size());
}
void MainWindow::resetSaveTimer() { void MainWindow::resetSaveTimer() {
saveTimer->start(); saveTimer->start();
qDebug() << "resetTimer"; qDebug() << "resetTimer";
@ -75,10 +74,18 @@ void MainWindow::toggleMainswitch(bool state) {
qDebug() << "mainswitch toggled: " << state; qDebug() << "mainswitch toggled: " << state;
if(state) { if(state) {
ui->mainswitch->setCheckState(Qt::CheckState::PartiallyChecked);
ui->mainswitch->setEnabled(false);
// make sure the changes to the mainswitch are rendered, before going into the blocking call
QCoreApplication::processEvents();
// TODO: make this non blocking
if(!lolaa.init()) { if(!lolaa.init()) {
Log::error << "League Client not found!"; Log::error << "League Client not found!";
ui->statusbar->showMessage(tr("League of Legends Client not found!")); ui->statusbar->showMessage(tr("League of Legends Client not found!"));
ui->mainswitch->setCheckState(Qt::CheckState::Unchecked); ui->mainswitch->setCheckState(Qt::CheckState::Unchecked);
ui->mainswitch->setEnabled(true);
return; return;
} }
@ -86,12 +93,10 @@ void MainWindow::toggleMainswitch(bool state) {
ui->runedisplay->setStyles(lolaa.getRuneStyles()); ui->runedisplay->setStyles(lolaa.getRuneStyles());
lolaa.run(); lolaa.run();
ui->statusbar->showMessage(tr("Auto-Acceptor started!"));
} else { } else {
lolaa.stop(); lolaa.stop();
ui->statusbar->showMessage(tr("Auto-Acceptor stoped!")); ui->mainswitch->setCheckState(Qt::CheckState::PartiallyChecked);
ui->mainswitch->setEnabled(false);
} }
} }
@ -171,8 +176,21 @@ void MainWindow::onPosChange(Position newpos) {
emit requestTabChange((int) newpos); emit requestTabChange((int) newpos);
} }
void MainWindow::onFail() { void MainWindow::lolaaStatusChanged(LolAutoAccept::Status status) {
ui->mainswitch->setChecked(false); qDebug() << "new status: " << (int) status;
ui->statusbar->showMessage(tr("Auto-Acceptor failed!"));
}
switch(status) {
case LolAutoAccept::Status::Off:
this->ui->statusbar->showMessage(tr("Auto-Acceptor stoped!"));
break;
case LolAutoAccept::Status::Running:
this->ui->statusbar->showMessage(tr("Auto-Acceptor started!"));
break;
case LolAutoAccept::Status::Failed:
this->ui->statusbar->showMessage(tr("Auto-Acceptor failed!"));
break;
}
this->ui->mainswitch->setEnabled(true);
this->ui->mainswitch->setChecked(status == LolAutoAccept::Status::Running);
}