forked from MrBesen/lolautoaccept
auto sync runes
This commit is contained in:
parent
41de5a8652
commit
d375c590f8
|
@ -38,6 +38,15 @@ public:
|
|||
RunePage runepage;
|
||||
};
|
||||
|
||||
struct GeneralRunePageConfig {
|
||||
GeneralRunePageConfig();
|
||||
GeneralRunePageConfig(const QJsonObject&);
|
||||
operator QJsonObject() const;
|
||||
|
||||
bool autoSync;
|
||||
std::vector<std::shared_ptr<RunePageConfig>> runePages;
|
||||
};
|
||||
|
||||
struct RootConfig {
|
||||
RootConfig();
|
||||
RootConfig(const QJsonObject&);
|
||||
|
@ -46,7 +55,7 @@ public:
|
|||
std::shared_ptr<Config::PositionConfig> getPositionConfig(Position position);
|
||||
|
||||
std::vector<std::shared_ptr<PositionConfig>> positionConfigs;
|
||||
std::vector<std::shared_ptr<RunePageConfig>> runePages;
|
||||
GeneralRunePageConfig runepagesConfig;
|
||||
|
||||
bool enabledAutoAccept;
|
||||
bool enabledSmiteWarn;
|
||||
|
|
|
@ -34,7 +34,11 @@ private slots:
|
|||
void deleteRunepageClient(int id);
|
||||
void deleteRunepageAA(int id);
|
||||
|
||||
void autoSyncToggled();
|
||||
|
||||
private:
|
||||
void syncRunes();
|
||||
|
||||
Ui::RuneManager* ui;
|
||||
std::shared_ptr<ClientAPI> client;
|
||||
Config* config = nullptr;
|
||||
|
|
|
@ -89,6 +89,35 @@ Config::RootConfig::RootConfig() {}
|
|||
|
||||
Config::RunePageConfig::RunePageConfig(QString name, const RunePage& rp) : name(name), runepage(rp) {}
|
||||
|
||||
Config::GeneralRunePageConfig::GeneralRunePageConfig() {}
|
||||
Config::GeneralRunePageConfig::GeneralRunePageConfig(const QJsonObject& j) {
|
||||
auto runepagesRef = j["pages"];
|
||||
if(runepagesRef.isArray()) {
|
||||
QJsonArray jpages = runepagesRef.toArray();
|
||||
|
||||
runePages.reserve(jpages.size());
|
||||
for(QJsonValue val : jpages) {
|
||||
if(val.isObject()) {
|
||||
runePages.push_back(std::make_shared<RunePageConfig>(val.toObject()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
autoSync = getValue(j, "autosync", true);
|
||||
}
|
||||
Config::GeneralRunePageConfig::operator QJsonObject() const {
|
||||
QJsonObject out;
|
||||
|
||||
QJsonArray runepagesArr;
|
||||
for(std::shared_ptr<RunePageConfig> rp : runePages) {
|
||||
runepagesArr.push_back((QJsonObject) *rp);
|
||||
}
|
||||
out.insert("pages", runepagesArr);
|
||||
out.insert("autosync", autoSync);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
Config::RootConfig::RootConfig(const QJsonObject& j) {
|
||||
if(j.contains("version")) {
|
||||
int version = j["version"].toInt();
|
||||
|
@ -114,17 +143,7 @@ Config::RootConfig::RootConfig(const QJsonObject& j) {
|
|||
}
|
||||
}
|
||||
|
||||
auto runepagesRef = j["runepages"];
|
||||
if(runepagesRef.isArray()) {
|
||||
QJsonArray jpages = runepagesRef.toArray();
|
||||
|
||||
runePages.reserve(jpages.size());
|
||||
for(QJsonValue val : jpages) {
|
||||
if(val.isObject()) {
|
||||
runePages.push_back(std::make_shared<RunePageConfig>(val.toObject()));
|
||||
}
|
||||
}
|
||||
}
|
||||
runepagesConfig = getValue<GeneralRunePageConfig>(j, "runepages");
|
||||
|
||||
enabledAutoAccept = getValue(j, "enabledAutoAccept", true);
|
||||
enabledSmiteWarn = getValue(j, "enabledSmiteWarn", true);
|
||||
|
@ -140,13 +159,8 @@ Config::RootConfig::operator QJsonObject() const {
|
|||
positionarr.push_back((QJsonObject) *pos.get());
|
||||
}
|
||||
|
||||
QJsonArray runepagesArr;
|
||||
for(std::shared_ptr<RunePageConfig> rp : runePages) {
|
||||
runepagesArr.push_back((QJsonObject) *rp);
|
||||
}
|
||||
|
||||
out["positions"] = positionarr;
|
||||
out["runepages"] = runepagesArr;
|
||||
out["runepages"] = (QJsonObject) runepagesConfig;
|
||||
out.insert("enabledAutoAccept", enabledAutoAccept);
|
||||
out.insert("enabledSmiteWarn", enabledSmiteWarn);
|
||||
out.insert("enabledAutoWrite", enabledAutoWrite);
|
||||
|
@ -182,30 +196,29 @@ Config::~Config() {}
|
|||
bool Config::load() {
|
||||
QFile conffile(QString::fromStdString(configFilePath));
|
||||
if(!conffile.open(QIODevice::ReadOnly)) {
|
||||
Log::error << "could not open configfile: " << configFilePath;
|
||||
qCritical() << "could not open configfile: " << configFilePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonParseError err;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err);
|
||||
if(err.error != QJsonParseError::NoError) {
|
||||
Log::error << "config parse error: " << err.errorString().toStdString() << " position: " << err.offset;
|
||||
qCritical() << "config parse error: " << err.errorString() << " position: " << err.offset;
|
||||
return false;
|
||||
}
|
||||
if(doc.isObject()) {
|
||||
// implicit cast
|
||||
root = doc.object();
|
||||
|
||||
Log::info << "config loaded";
|
||||
qInfo() << "config loaded";
|
||||
return true;
|
||||
}
|
||||
|
||||
Log::error << "config is not a json object!";
|
||||
qCritical() << "config is not a json object!";
|
||||
return false;
|
||||
}
|
||||
|
||||
void Config::save() {
|
||||
Log::note << "Config::save()";
|
||||
mkdirs(configFolderPath);
|
||||
|
||||
QFile conffile(QString::fromStdString(configFilePath));
|
||||
|
@ -217,7 +230,7 @@ void Config::save() {
|
|||
QJsonDocument doc;
|
||||
doc.setObject(root);
|
||||
conffile.write(doc.toJson());
|
||||
Log::info << "config saved";
|
||||
qInfo() << "config saved";
|
||||
}
|
||||
|
||||
Config::RootConfig& Config::getConfig() {
|
||||
|
|
|
@ -20,6 +20,8 @@ RuneManager::RuneManager(QWidget* parent) : QWidget(parent), ui(new Ui::RuneMana
|
|||
QObject::connect(ui->listaaRunes, &RunePageList::runepageDeleted, this, &RuneManager::deleteRunepageAA);
|
||||
QObject::connect(ui->listClientRunes, &RunePageList::runepageDeleted, this, &RuneManager::deleteRunepageClient);
|
||||
|
||||
QObject::connect(ui->chkAutoCopy, &QCheckBox::clicked, this, &RuneManager::autoSyncToggled);
|
||||
|
||||
loadRunes();
|
||||
}
|
||||
|
||||
|
@ -29,7 +31,13 @@ RuneManager::~RuneManager() {
|
|||
|
||||
void RuneManager::setConfig(Config& config) {
|
||||
this->config = &config;
|
||||
ui->listaaRunes->loadRunePages(config.getConfig().runePages);
|
||||
Config::GeneralRunePageConfig& rpc = config.getConfig().runepagesConfig;
|
||||
ui->listaaRunes->loadRunePages(rpc.runePages);
|
||||
ui->chkAutoCopy->setChecked(rpc.autoSync);
|
||||
|
||||
if(rpc.autoSync) {
|
||||
syncRunes();
|
||||
}
|
||||
}
|
||||
|
||||
void RuneManager::loadRunes() {
|
||||
|
@ -47,7 +55,9 @@ void RuneManager::loadRunes() {
|
|||
if(client) {
|
||||
// load meta data
|
||||
runeInfo = client->getAllRuneAspekts();
|
||||
QCoreApplication::processEvents();
|
||||
runeStyles = client->getAllRuneStyles();
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
this->ui->listClientRunes->setRuneInfos(runeInfo, runeStyles);
|
||||
this->ui->listaaRunes->setRuneInfos(runeInfo, runeStyles);
|
||||
|
@ -55,6 +65,11 @@ void RuneManager::loadRunes() {
|
|||
// load runepages
|
||||
const std::vector<ClientAPI::RunePage> runePages = client->getAllRunePages();
|
||||
ui->listClientRunes->loadRunePages(runePages);
|
||||
|
||||
// check if autosync is enabled
|
||||
if(config && config->getConfig().runepagesConfig.autoSync) {
|
||||
syncRunes();
|
||||
}
|
||||
}
|
||||
|
||||
setRunesEnabled(!!client); // cast to bool
|
||||
|
@ -94,7 +109,7 @@ void RuneManager::saveRunePageAA(int id, QString name, const RunePage& rp) {
|
|||
if(!config) return;
|
||||
|
||||
Config::RootConfig& rc = config->getConfig();
|
||||
auto& pages = rc.runePages;
|
||||
auto& pages = rc.runepagesConfig.runePages;
|
||||
if(id == -1) {
|
||||
// int newId = pages.size();
|
||||
pages.push_back(std::make_shared<Config::RunePageConfig>(name, rp));
|
||||
|
@ -126,7 +141,7 @@ void RuneManager::deleteRunepageAA(int id) {
|
|||
if(!config) return;
|
||||
|
||||
Config::RootConfig& rc = config->getConfig();
|
||||
auto& pages = rc.runePages;
|
||||
auto& pages = rc.runepagesConfig.runePages;
|
||||
if((int) pages.size() > id && id >= 0) {
|
||||
pages.erase(pages.begin() + id);
|
||||
|
||||
|
@ -138,3 +153,43 @@ void RuneManager::deleteRunepageAA(int id) {
|
|||
qWarning() << "unknown runepage id:" << id;
|
||||
}
|
||||
}
|
||||
|
||||
void RuneManager::autoSyncToggled() {
|
||||
bool autoSync = (ui->chkAutoCopy->isChecked());
|
||||
if(config) {
|
||||
config->getConfig().runepagesConfig.autoSync = autoSync;
|
||||
config->save();
|
||||
}
|
||||
|
||||
if(autoSync) {
|
||||
syncRunes();
|
||||
}
|
||||
}
|
||||
|
||||
void RuneManager::syncRunes() {
|
||||
qInfo() << "syncing" << ui->listClientRunes->count() << "runes";
|
||||
|
||||
std::vector<std::shared_ptr<Config::RunePageConfig>>& configs = config->getConfig().runepagesConfig.runePages;
|
||||
bool changed = false;
|
||||
|
||||
for(int i = 0; i < ui->listClientRunes->count(); ++i) {
|
||||
const QListWidgetItem* item = ui->listClientRunes->item(i);
|
||||
QString name = item->text();
|
||||
const RunePage* rp = (RunePage*) item->data(RunePageList::RolePointer).toULongLong();
|
||||
|
||||
auto itFound = std::find_if(configs.cbegin(), configs.cend(), [name, rp](const std::shared_ptr<Config::RunePageConfig>& rpc){
|
||||
return rpc->name == name && *rp == rpc->runepage;
|
||||
});
|
||||
|
||||
if(itFound == configs.cend()) {
|
||||
// no duplicate found -> add it
|
||||
configs.push_back(std::make_shared<Config::RunePageConfig>(name, *rp));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(changed) {
|
||||
config->save();
|
||||
ui->listaaRunes->loadRunePages(configs);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,19 +51,15 @@
|
|||
<string>Reload</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset theme="reload"/>
|
||||
<iconset theme="reload">
|
||||
<normaloff>.</normaloff>.</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QCheckBox" name="chkAutoCopy">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Runes from the client get copied to the autoacceptor automatically.
|
||||
|
||||
This Feature is in development.</string>
|
||||
<string>Runes from the client get copied to the autoacceptor automatically.</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string> Auto Copy Runes</string>
|
||||
|
|
Loading…
Reference in New Issue