From 01aeed1ff052fb60cd1167fea23c5b4351c81cb6 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 6 Feb 2024 11:18:12 -0300 Subject: [PATCH] improve: checking for duplicate storage keys (#2180) Removed all duplicate code and added a new function to global to check storage keys, any table can be checked. This makes maintenance easier and avoids duplicate codes. --------- Co-authored-by: GitHub Actions Co-authored-by: Elson Costa --- data-canary/lib/core/storages.lua | 26 --------------- data-otservbr-global/lib/core/storages.lua | 26 --------------- data/libs/core/global_storage.lua | 26 --------------- data/scripts/globalevents/startup.lua | 37 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 78 deletions(-) create mode 100644 data/scripts/globalevents/startup.lua diff --git a/data-canary/lib/core/storages.lua b/data-canary/lib/core/storages.lua index 7646a8cc51e..6aac447358a 100644 --- a/data-canary/lib/core/storages.lua +++ b/data-canary/lib/core/storages.lua @@ -41,29 +41,3 @@ GlobalStorage = { Example = 60000, }, } - --- Values extraction function -local function extractValues(tab, ret) - if type(tab) == "number" then - table.insert(ret, tab) - else - for _, v in pairs(tab) do - extractValues(v, ret) - end - end -end - -local extraction = {} -extractValues(Storage, extraction) -- Call function -table.sort(extraction) -- Sort the table --- The choice of sorting is due to the fact that sorting is very cheap O (n log2 (n)) --- And then we can simply compare one by one the elements finding duplicates in O(n) - --- Scroll through the extracted table for duplicates -if #extraction > 1 then - for i = 1, #extraction - 1 do - if extraction[i] == extraction[i + 1] then - logger.warn("Duplicate storage value found: {}", extraction[i]) - end - end -end diff --git a/data-otservbr-global/lib/core/storages.lua b/data-otservbr-global/lib/core/storages.lua index 11cd653f0ae..e67b5331c4c 100644 --- a/data-otservbr-global/lib/core/storages.lua +++ b/data-otservbr-global/lib/core/storages.lua @@ -3130,29 +3130,3 @@ startupGlobalStorages = { GlobalStorage.FerumbrasAscendant.Elements.Third, GlobalStorage.FerumbrasAscendant.Elements.Done, } - --- Values extraction function -local function extractValues(tab, ret) - if type(tab) == "number" then - table.insert(ret, tab) - else - for _, v in pairs(tab) do - extractValues(v, ret) - end - end -end - -local extraction = {} -extractValues(Storage, extraction) -- Call function -table.sort(extraction) -- Sort the table --- The choice of sorting is due to the fact that sorting is very cheap O (n log2 (n)) --- And then we can simply compare one by one the elements finding duplicates in O(n) - --- Scroll through the extracted table for duplicates -if #extraction > 1 then - for i = 1, #extraction - 1 do - if extraction[i] == extraction[i + 1] then - logger.warn("Duplicate storage value found: {}", extraction[i]) - end - end -end diff --git a/data/libs/core/global_storage.lua b/data/libs/core/global_storage.lua index 63d17166332..778073f3ca6 100644 --- a/data/libs/core/global_storage.lua +++ b/data/libs/core/global_storage.lua @@ -27,29 +27,3 @@ Global = { FamiliarSummonEvent60 = 30055, }, } - --- Values extraction function -local function extractValues(tab, ret) - if type(tab) == "number" then - table.insert(ret, tab) - else - for _, v in pairs(tab) do - extractValues(v, ret) - end - end -end - -local extraction = {} -extractValues(Storage, extraction) -- Call function -table.sort(extraction) -- Sort the table --- The choice of sorting is due to the fact that sorting is very cheap O (n log2 (n)) --- And then we can simply compare one by one the elements finding duplicates in O(n) - --- Scroll through the extracted table for duplicates -if #extraction > 1 then - for i = 1, #extraction - 1 do - if extraction[i] == extraction[i + 1] then - logger.warn("Duplicate global storage value found: {}", extraction[i]) - end - end -end diff --git a/data/scripts/globalevents/startup.lua b/data/scripts/globalevents/startup.lua new file mode 100644 index 00000000000..53d22740231 --- /dev/null +++ b/data/scripts/globalevents/startup.lua @@ -0,0 +1,37 @@ +-- Function to check for duplicate keys in a given variable's storage +local function checkDuplicateStorageKeys(varName) + local keys = _G[varName] + local seen = {} + local duplicates = {} + + for k, v in pairs(keys) do + if seen[v] then + table.insert(duplicates, v) + else + seen[v] = true + end + end + + return next(duplicates) and duplicates or false +end + +-- Function to check duplicated variable keys and log the results +local function checkAndLogDuplicateKeys(variableNames) + for _, variableName in ipairs(variableNames) do + local duplicates = checkDuplicateStorageKeys(variableName) + if duplicates then + local message = "Duplicate keys found: " .. table.concat(duplicates, ", ") + logger.warn("Checking " .. variableName .. ": " .. message) + else + logger.info("Checking " .. variableName .. ": No duplicate keys found.") + end + end +end + +local startup = GlobalEvent("Server Initialization") + +function startup.onStartup() + checkAndLogDuplicateKeys({ "Global", "GlobalStorage", "Storage" }) +end + +startup:register()