feat: add 'resume' and 'pause' functions to Stats class (#1764)

New Features:
- Added the ability to pause and resume statistics collection.
- While paused, new statistics are no longer recorded; previously gathered statistics remain intact.
- Lua scripts can now control statistics collection using new pause and resume commands.
This commit is contained in:
karlo 2026-07-18 21:49:34 +02:00 committed by GitHub
parent 350e506aa7
commit caba9cef63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 1 deletions

View file

@ -295,6 +295,8 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_stats", "get", &Stats::get, &g_stats);
g_lua.bindSingletonFunction("g_stats", "clear", &Stats::clear, &g_stats);
g_lua.bindSingletonFunction("g_stats", "clearAll", &Stats::clearAll, &g_stats);
g_lua.bindSingletonFunction("g_stats", "pause", &Stats::pause, &g_stats);
g_lua.bindSingletonFunction("g_stats", "resume", &Stats::resume, &g_stats);
g_lua.bindSingletonFunction("g_stats", "getSlow", &Stats::getSlow, &g_stats);
g_lua.bindSingletonFunction("g_stats", "clearSlow", &Stats::clearSlow, &g_stats);
g_lua.bindSingletonFunction("g_stats", "getSleepTime", &Stats::getSleepTime, &g_stats);

View file

@ -36,8 +36,10 @@
Stats g_stats;
void Stats::add(int type, Stat* stat) {
if (type < 0 || type > STATS_LAST)
if (type < 0 || type > STATS_LAST || paused) {
delete stat;
return;
}
std::lock_guard<std::mutex> lock(m_mutex);
auto it = stats[type].data.emplace(stat->description, StatsData(0, 0, stat->extraDescription)).first;

View file

@ -107,6 +107,9 @@ public:
inline void addCreature() { createdCreatures += 1; }
inline void removeCreature() { destroyedCreatures += 1; }
inline void pause() { paused = true; }
inline void resume() { paused = false; }
private:
struct
{
@ -124,6 +127,7 @@ private:
int destroyedThings = 0;
int createdCreatures = 0;
int destroyedCreatures = 0;
std::atomic_bool paused { false };
std::mutex m_mutex;
};