Skip to content

Commit

Permalink
improve: checking for duplicate storage keys (opentibiabr#2180)
Browse files Browse the repository at this point in the history
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 <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Elson Costa <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent 6e134e3 commit 01aeed1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 78 deletions.
26 changes: 0 additions & 26 deletions data-canary/lib/core/storages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 0 additions & 26 deletions data-otservbr-global/lib/core/storages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 0 additions & 26 deletions data/libs/core/global_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions data/scripts/globalevents/startup.lua
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 01aeed1

Please sign in to comment.