From afd8cd28face95955dd4705900d95346b37f9ca5 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Sun, 3 May 2026 20:53:34 +0200 Subject: [PATCH] allow to re-load the config for the balancing app via button --- docs/requirements.md | 7 ++- src/balancing/BalancingWindow.cpp | 98 ++++++++++++++++++++++++------- src/balancing/BalancingWindow.h | 17 +++++- src/balancing/main.cpp | 2 +- 4 files changed, 95 insertions(+), 29 deletions(-) diff --git a/docs/requirements.md b/docs/requirements.md index 47991a1..10b7f7e 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -258,7 +258,7 @@ A separate executable target (`balancing`) that links against `lib` but contains ### Config -- REQ-BAL-CONFIG: The balancing tool reads arena definitions from a `balancing.toml` file. The file is read once at startup. If parsing fails or required fields are missing, the tool aborts with a clear error message. +- REQ-BAL-CONFIG: The balancing tool reads arena definitions from a `balancing.toml` file. The file is read at startup and again each time the player triggers a config reload (REQ-BAL-UI-RELOAD). If parsing fails or required fields are missing at startup, the tool aborts with a clear error message. If parsing fails during a reload, a modal error dialog is shown describing the failure and the current arena list is left unchanged. - REQ-BAL-CONFIG-GAME: Ship stats are read from `ships.toml` and defence station stats are read from `stations.toml`, using the same config loading as the main game. Formula evaluation uses the levels specified in the arena config. ### Arena Definition @@ -284,8 +284,9 @@ A separate executable target (`balancing`) that links against `lib` but contains ### UI -- REQ-BAL-UI-WINDOW: On startup the tool displays a window containing a "Start All" button at the top, followed by a scrollable vertical list of arena widgets, one per arena defined in `balancing.toml`. Simulations do not start automatically on startup. -- REQ-BAL-UI-START-ALL: The "Start All" button is placed above the scrollable arena list. Clicking it starts (or restarts) the simulation for every arena that is not currently running. The button is disabled when all arenas are currently running. +- REQ-BAL-UI-WINDOW: On startup the tool displays a window containing a "Reload Config" button and a "Start All" button at the top (in that order, left to right), followed by a scrollable vertical list of arena widgets, one per arena defined in `balancing.toml`. Simulations do not start automatically on startup. +- REQ-BAL-UI-RELOAD: The "Reload Config" button reloads all config files from disk (`balancing.toml`, `ships.toml`, `stations.toml`), stops any running simulations, and replaces the arena widget list with freshly created widgets from the reloaded config. The button is disabled while any arena simulation is currently running. +- REQ-BAL-UI-START-ALL: The "Start All" button is placed above the scrollable arena list, to the right of the "Reload Config" button. Clicking it starts (or restarts) the simulation for every arena that is not currently running. The button is disabled when all arenas are currently running. - REQ-BAL-UI-WIDGET: Each arena widget displays the arena name and two columns (one per team). Each column shows the team name as a header, followed by a list of entries. The HQ is always the first entry in each column. Below the HQ, ship types are listed, followed by defence stations (if any). Each entry uses the format `surviving/total TypeName Llevel` — for example `2/3 Fighter L5` or `1/1 HQ L1`. The surviving count updates live as the simulation progresses. When the fight ends, the winning team's name header is prefixed with `[WON]`. - REQ-BAL-UI-WIDGET-START: Each arena widget contains a "Start" button that starts the simulation for that arena. The button is disabled while the arena's simulation is running. When a finished arena's Start button is clicked, a fresh simulation is created and started (the widget resets to initial unit counts, the border returns to blue, and the previous results are replaced). - REQ-BAL-UI-WIDGET-BORDER: Each arena widget has a colored border indicating its state: grey when not yet started, blue while its simulation is running, and green when the fight has ended. diff --git a/src/balancing/BalancingWindow.cpp b/src/balancing/BalancingWindow.cpp index b652edf..9520a25 100644 --- a/src/balancing/BalancingWindow.cpp +++ b/src/balancing/BalancingWindow.cpp @@ -1,13 +1,20 @@ #include "BalancingWindow.h" -#include +#include +#include #include +#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); } diff --git a/src/balancing/BalancingWindow.h b/src/balancing/BalancingWindow.h index b58dc44..c0276bb 100644 --- a/src/balancing/BalancingWindow.h +++ b/src/balancing/BalancingWindow.h @@ -1,10 +1,12 @@ #pragma once #include +#include #include #include #include +#include #include #include @@ -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 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; }; diff --git a/src/balancing/main.cpp b/src/balancing/main.cpp index f21ddc3..71e7ee3 100644 --- a/src/balancing/main.cpp +++ b/src/balancing/main.cpp @@ -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();