From 1d5f24dc1a98bc7c6ec29de0d14bfd171d650560 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Wed, 19 Jul 2023 20:56:19 +0200 Subject: [PATCH] Added check on repeated commands on scheduler --- client/src/server/server.ts | 2 +- scripts/OlympusCommand.lua | 4 ++-- src/core/include/commands.h | 30 +++++++++++++++--------------- src/core/src/commands.cpp | 28 ++++++++++++++-------------- src/core/src/core.cpp | 19 +++++++++---------- src/core/src/scheduler.cpp | 11 ++++++++--- src/logger/src/logger.cpp | 2 -- src/shared/include/defines.h | 2 +- 8 files changed, 50 insertions(+), 48 deletions(-) diff --git a/client/src/server/server.ts b/client/src/server/server.ts index fcfcd4bb..80a85da5 100644 --- a/client/src/server/server.ts +++ b/client/src/server/server.ts @@ -59,7 +59,7 @@ export function GET(callback: CallableFunction, uri: string, options?: { time?: const result = JSON.parse(xmlHttp.responseText); lastUpdateTimes[uri] = callback(result); - if (result.frameRate && result.load) + if (result.frameRate !== undefined && result.load !== undefined) getServerStatusPanel().update(result.frameRate, result.load); } } else if (xmlHttp.status == 401) { diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index f4fa8274..f1dd11c8 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -506,7 +506,7 @@ function Olympus.generateGroundUnitsTable(units) ["y"] = spawnLocation.z + value.dy, ["heading"] = 0, ["skill"] = "High", - ["name"] = "GroundUnit-" .. Olympus.unitCounter .. "-" .. #unitTable + 1 + ["name"] = "Olympus-" .. Olympus.unitCounter .. "-" .. #unitTable + 1 } end else @@ -517,7 +517,7 @@ function Olympus.generateGroundUnitsTable(units) ["y"] = spawnLocation.z, ["heading"] = 0, ["skill"] = "High", - ["name"] = "GroundUnit-" .. Olympus.unitCounter .. "-" .. #unitTable + 1 + ["name"] = "Olympus-" .. Olympus.unitCounter .. "-" .. #unitTable + 1 } end end diff --git a/src/core/include/commands.h b/src/core/include/commands.h index dd128127..263de2f0 100644 --- a/src/core/include/commands.h +++ b/src/core/include/commands.h @@ -95,7 +95,7 @@ class Command { public: unsigned int getPriority() { return priority; } - virtual string getString(lua_State* L) = 0; + virtual string getString() = 0; virtual unsigned int getLoad() = 0; protected: @@ -118,7 +118,7 @@ public: { priority = CommandPriority::HIGH; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 2; } private: @@ -142,7 +142,7 @@ public: { priority = CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 2; } private: @@ -162,7 +162,7 @@ public: { priority = immediate? CommandPriority::IMMEDIATE: CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return immediate? 1: 30; } private: @@ -184,7 +184,7 @@ public: { priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return immediate ? 1 : 30; } private: @@ -208,7 +208,7 @@ public: { priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return immediate ? 1 : 30; } private: @@ -235,7 +235,7 @@ public: { priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return immediate ? 1 : 30; } private: @@ -257,7 +257,7 @@ public: { priority = CommandPriority::LOW; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 30; } private: @@ -277,7 +277,7 @@ public: priority = CommandPriority::HIGH; immediate = immediate; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return immediate? 1: 5; } private: @@ -296,7 +296,7 @@ public: { priority = CommandPriority::MEDIUM; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 1; } private: @@ -313,7 +313,7 @@ public: { priority = CommandPriority::HIGH; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 1; } private: @@ -330,7 +330,7 @@ public: { priority = CommandPriority::HIGH; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 1; } private: @@ -361,7 +361,7 @@ public: { priority = CommandPriority::HIGH; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 1; } private: @@ -382,7 +382,7 @@ public: { priority = CommandPriority::HIGH; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 1; } private: @@ -400,7 +400,7 @@ public: { priority = CommandPriority::MEDIUM; }; - virtual string getString(lua_State* L); + virtual string getString(); virtual unsigned int getLoad() { return 4; } private: diff --git a/src/core/src/commands.cpp b/src/core/src/commands.cpp index 25d234c4..511abcd4 100644 --- a/src/core/src/commands.cpp +++ b/src/core/src/commands.cpp @@ -7,7 +7,7 @@ extern UnitsManager* unitsManager; /* Move command */ -string Move::getString(lua_State* L) +string Move::getString() { std::ostringstream commandSS; @@ -26,7 +26,7 @@ string Move::getString(lua_State* L) } /* Smoke command */ -string Smoke::getString(lua_State* L) +string Smoke::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -38,7 +38,7 @@ string Smoke::getString(lua_State* L) } /* Spawn ground units command */ -string SpawnGroundUnits::getString(lua_State* L) +string SpawnGroundUnits::getString() { if (unitTypes.size() != locations.size()) return ""; @@ -62,7 +62,7 @@ string SpawnGroundUnits::getString(lua_State* L) /* Spawn ground units command */ -string SpawnNavyUnits::getString(lua_State* L) +string SpawnNavyUnits::getString() { if (unitTypes.size() != locations.size()) return ""; @@ -85,7 +85,7 @@ string SpawnNavyUnits::getString(lua_State* L) } /* Spawn aircrafts command */ -string SpawnAircrafts::getString(lua_State* L) +string SpawnAircrafts::getString() { if (unitTypes.size() != locations.size() || unitTypes.size() != loadouts.size()) return ""; @@ -112,7 +112,7 @@ string SpawnAircrafts::getString(lua_State* L) /* Spawn helicopters command */ -string SpawnHelicopters::getString(lua_State* L) +string SpawnHelicopters::getString() { if (unitTypes.size() != locations.size() || unitTypes.size() != loadouts.size()) return ""; @@ -139,7 +139,7 @@ string SpawnHelicopters::getString(lua_State* L) /* Clone unit command */ -string Clone::getString(lua_State* L) +string Clone::getString() { Unit* unit = unitsManager->getUnit(ID); if (unit != nullptr) @@ -160,7 +160,7 @@ string Clone::getString(lua_State* L) } /* Delete unit command */ -string Delete::getString(lua_State* L) +string Delete::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -171,7 +171,7 @@ string Delete::getString(lua_State* L) } /* Set task command */ -string SetTask::getString(lua_State* L) +string SetTask::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -183,7 +183,7 @@ string SetTask::getString(lua_State* L) } /* Reset task command */ -string ResetTask::getString(lua_State* L) +string ResetTask::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -194,7 +194,7 @@ string ResetTask::getString(lua_State* L) } /* Set command command */ -string SetCommand::getString(lua_State* L) +string SetCommand::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -206,7 +206,7 @@ string SetCommand::getString(lua_State* L) } /* Set option command */ -string SetOption::getString(lua_State* L) +string SetOption::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -226,7 +226,7 @@ string SetOption::getString(lua_State* L) } /* Set onOff command */ -string SetOnOff::getString(lua_State* L) +string SetOnOff::getString() { std::ostringstream commandSS; commandSS.precision(10); @@ -239,7 +239,7 @@ string SetOnOff::getString(lua_State* L) } /* Explosion command */ -string Explosion::getString(lua_State* L) +string Explosion::getString() { std::ostringstream commandSS; commandSS.precision(10); diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index fa8690e8..502ad4ea 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -70,19 +70,18 @@ extern "C" DllExport int coreFrame(lua_State* L) frameCounter++; const std::chrono::duration executionDuration = std::chrono::system_clock::now() - lastExecution; - if (executionDuration.count() > EXECUTION_TIME_INTERVAL) { - if (scheduler != nullptr) { - scheduler->execute(L); - - if (executionDuration.count() > 0) { - scheduler->setFrameRate(frameCounter / executionDuration.count()); - frameCounter = 0; - } - - lastExecution = std::chrono::system_clock::now(); + if (executionDuration.count() > FRAMERATE_TIME_INTERVAL) { + if (executionDuration.count() > 0) { + scheduler->setFrameRate(frameCounter / executionDuration.count()); + frameCounter = 0; } + + lastExecution = std::chrono::system_clock::now(); } + if (scheduler != nullptr) + scheduler->execute(L); + return(0); } diff --git a/src/core/src/scheduler.cpp b/src/core/src/scheduler.cpp index a73cabfb..dac8bebf 100644 --- a/src/core/src/scheduler.cpp +++ b/src/core/src/scheduler.cpp @@ -18,9 +18,13 @@ Scheduler::~Scheduler() } -void Scheduler::appendCommand(Command* command) +void Scheduler::appendCommand(Command* newCommand) { - commands.push_back(command); + for (auto command : commands) { + if (command->getString().compare(newCommand->getString()) == 0 && command->getPriority() == newCommand->getPriority()) + return; + } + commands.push_back(newCommand); } int Scheduler::getLoad() @@ -47,13 +51,14 @@ void Scheduler::execute(lua_State* L) { if (command->getPriority() == priority) { - string commandString = "Olympus.protectedCall(" + command->getString(L) + ")"; + string commandString = "Olympus.protectedCall(" + command->getString() + ")"; if (dostring_in(L, "server", (commandString))) log("Error executing command " + commandString); else log("Command '" + commandString + "' executed correctly, current load " + to_string(getLoad())); load = command->getLoad(); commands.remove(command); + delete command; return; } } diff --git a/src/logger/src/logger.cpp b/src/logger/src/logger.cpp index 47b3ed37..a3605964 100644 --- a/src/logger/src/logger.cpp +++ b/src/logger/src/logger.cpp @@ -37,8 +37,6 @@ void Logger::Close() void Logger::toJSON(json::value& json, unsigned long long time) { lock_guard guard(mutexLock); - json[L"requestTime"] = time; - /* Loop on the logs in reverse since we are usually only interested in the very last added logs */ auto itr = m_logs.end(); while (itr != m_logs.begin()) diff --git a/src/shared/include/defines.h b/src/shared/include/defines.h index 3047901f..f061cd1c 100644 --- a/src/shared/include/defines.h +++ b/src/shared/include/defines.h @@ -10,4 +10,4 @@ #define BULLSEYE_URI "bullseyes" #define MISSION_URI "mission" -#define EXECUTION_TIME_INTERVAL 0.05 \ No newline at end of file +#define FRAMERATE_TIME_INTERVAL 0.05 \ No newline at end of file