allow to re-load the config for the balancing app via button

This commit is contained in:
2026-05-03 20:53:34 +02:00
parent 6405ad6b3f
commit afd8cd28fa
4 changed files with 95 additions and 29 deletions

View File

@@ -1,13 +1,20 @@
#include "BalancingWindow.h"
#include <QScrollArea>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QVBoxLayout>
#include "ConfigLoader.h"
BalancingWindow::BalancingWindow(const BalancingConfig& balancingConfig,
const GameConfig& gameConfig,
GameConfig gameConfig,
const std::string& configDir,
const std::string& balancingConfigPath,
QWidget* parent)
: QWidget(parent)
, m_gameConfig(gameConfig)
, m_gameConfig(std::move(gameConfig))
, m_configDir(configDir)
, m_balancingConfigPath(balancingConfigPath)
, m_nextSeed(0)
{
setWindowTitle("DotaFactory — Balancing Tool");
@@ -16,14 +23,40 @@ BalancingWindow::BalancingWindow(const BalancingConfig& balancingConfig,
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
QHBoxLayout* buttonRow = new QHBoxLayout();
m_reloadButton = new QPushButton("Reload Config", this);
m_startAllButton = new QPushButton("Start All", this);
mainLayout->addWidget(m_startAllButton);
buttonRow->addWidget(m_reloadButton);
buttonRow->addWidget(m_startAllButton);
buttonRow->addStretch();
mainLayout->addLayout(buttonRow);
connect(m_reloadButton, &QPushButton::clicked, this, &BalancingWindow::reloadConfig);
connect(m_startAllButton, &QPushButton::clicked, this, &BalancingWindow::startAll);
QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true);
m_scrollArea = new QScrollArea(this);
m_scrollArea->setWidgetResizable(true);
mainLayout->addWidget(m_scrollArea);
QWidget* scrollContent = new QWidget(scrollArea);
populateArenas(balancingConfig);
m_pollTimer = new QTimer(this);
connect(m_pollTimer, &QTimer::timeout, this, &BalancingWindow::pollStatuses);
m_pollTimer->start(100);
}
BalancingWindow::~BalancingWindow()
{
m_pollTimer->stop();
stopAllArenas();
}
void BalancingWindow::populateArenas(const BalancingConfig& balancingConfig)
{
stopAllArenas();
m_arenas.clear();
QWidget* scrollContent = new QWidget(m_scrollArea);
QVBoxLayout* contentLayout = new QVBoxLayout(scrollContent);
contentLayout->setSpacing(8);
contentLayout->setContentsMargins(8, 8, 8, 8);
@@ -48,18 +81,13 @@ BalancingWindow::BalancingWindow(const BalancingConfig& balancingConfig,
}
contentLayout->addStretch();
scrollArea->setWidget(scrollContent);
mainLayout->addWidget(scrollArea);
m_scrollArea->setWidget(scrollContent);
m_pollTimer = new QTimer(this);
connect(m_pollTimer, &QTimer::timeout, this, &BalancingWindow::pollStatuses);
m_pollTimer->start(100);
updateButtons();
}
BalancingWindow::~BalancingWindow()
void BalancingWindow::stopAllArenas()
{
m_pollTimer->stop();
for (ArenaEntry& entry : m_arenas)
{
entry.simulation->requestStop();
@@ -83,7 +111,22 @@ void BalancingWindow::pollStatuses()
entry.widget->updateStatus(status);
}
}
updateStartAllButton();
updateButtons();
}
void BalancingWindow::reloadConfig()
{
try
{
GameConfig newGameConfig = ConfigLoader::loadFromDirectory(m_configDir);
BalancingConfig newBalancingConfig = loadBalancingConfig(m_balancingConfigPath);
m_gameConfig = std::move(newGameConfig);
populateArenas(newBalancingConfig);
}
catch (const std::exception& e)
{
QMessageBox::critical(this, "Reload Failed", QString::fromStdString(e.what()));
}
}
void BalancingWindow::startAll()
@@ -108,18 +151,29 @@ void BalancingWindow::startArena(int index)
entry.widget->updateStatus(entry.simulation->status());
ArenaSimulation* sim = entry.simulation.get();
entry.worker = std::thread([sim]() { sim->run(); });
updateStartAllButton();
updateButtons();
}
void BalancingWindow::updateStartAllButton()
void BalancingWindow::updateButtons()
{
bool anyRunning = false;
bool allRunning = true;
for (ArenaEntry& entry : m_arenas)
{
if (!entry.worker.joinable() || entry.simulation->status().finished)
if (entry.worker.joinable() && !entry.simulation->status().finished)
{
m_startAllButton->setEnabled(true);
return;
anyRunning = true;
}
else
{
allRunning = false;
}
}
m_startAllButton->setEnabled(false);
if (m_arenas.empty())
{
allRunning = false;
}
m_reloadButton->setEnabled(!anyRunning);
m_startAllButton->setEnabled(!allRunning);
}

View File

@@ -1,10 +1,12 @@
#pragma once
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <QPushButton>
#include <QScrollArea>
#include <QTimer>
#include <QWidget>
@@ -19,17 +21,22 @@ class BalancingWindow : public QWidget
public:
BalancingWindow(const BalancingConfig& balancingConfig,
const GameConfig& gameConfig,
GameConfig gameConfig,
const std::string& configDir,
const std::string& balancingConfigPath,
QWidget* parent = nullptr);
~BalancingWindow() override;
private slots:
void pollStatuses();
void reloadConfig();
void startAll();
void startArena(int index);
private:
void updateStartAllButton();
void populateArenas(const BalancingConfig& balancingConfig);
void stopAllArenas();
void updateButtons();
struct ArenaEntry
{
@@ -40,8 +47,12 @@ private:
};
std::vector<ArenaEntry> m_arenas;
const GameConfig& m_gameConfig;
GameConfig m_gameConfig;
std::string m_configDir;
std::string m_balancingConfigPath;
unsigned int m_nextSeed;
QPushButton* m_reloadButton;
QPushButton* m_startAllButton;
QScrollArea* m_scrollArea;
QTimer* m_pollTimer;
};

View File

@@ -27,7 +27,7 @@ int main(int argc, char* argv[])
GameConfig gameConfig = ConfigLoader::loadFromDirectory(CONFIG_DIR);
BalancingConfig balancingConfig = loadBalancingConfig(BALANCING_CONFIG);
BalancingWindow window(balancingConfig, gameConfig);
BalancingWindow window(balancingConfig, std::move(gameConfig), CONFIG_DIR, BALANCING_CONFIG);
window.show();
return application.exec();