Skip to content

Commit

Permalink
refactor: duplicate keys checking (#2198)
Browse files Browse the repository at this point in the history
Improves the duplicate checking logic, now redefining the seen and
duplicatesValues variables before checking each table.
  • Loading branch information
omarcopires authored Feb 15, 2024
1 parent bf2424a commit d083d04
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions data/scripts/globalevents/server_initialization.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,41 @@ local function storeTownsInDatabase()
end
end

-- Function to recursively check for duplicate values in a given variable's storage
local function checkDuplicateStorageValues(varTable, seen, duplicates)
-- Functions to recursively check for duplicate values in a given variable's storage and log the results
local seen, duplicatesValues

local function checkDuplicateStorageValues(varTable)
for _, value in pairs(varTable) do
if type(value) == "table" then
checkDuplicateStorageValues(value, seen, duplicates)
checkDuplicateStorageValues(value)
elseif seen[value] then
table.insert(duplicates, value)
table.insert(duplicatesValues, value)
else
seen[value] = true
end
end
return #duplicatesValues > 0 and duplicatesValues or false
end

-- Function to check for duplicate values in a given variable's storage
local function checkDuplicateStorageValuesWrapper(varName)
local seen = {}
local duplicates = {}

local varTable = _G[varName]
if type(varTable) == "table" then
checkDuplicateStorageValues(varTable, seen, duplicates)
else
logger.warn("Warning: '" .. varName .. "' is not a table.")
end

return #duplicates > 0 and duplicates or false
end

-- Function to check duplicated variable values and log the results
local function checkAndLogDuplicateValues(variableNames)
for _, variableName in ipairs(variableNames) do
local duplicates = checkDuplicateStorageValuesWrapper(variableName)

if duplicates then
logger.warn("Checking " .. variableName .. ": Duplicate values found: " .. table.concat(duplicates, ", "))
local function checkAndLogDuplicateValues(tableNames)
for _, tableName in ipairs(tableNames) do
local varTable = _G[tableName]
if type(varTable) == "table" then
seen = {}
duplicatesValues = {}

local duplicates = checkDuplicateStorageValues(varTable)
if duplicates then
logger.warn("Checking {}: Duplicate values found: {}", tableName, table.concat(duplicates, ", "))
else
logger.info("Checking {}: No duplicate values found.", tableName)
end
else
logger.info("Checking " .. variableName .. ": No duplicate values found.")
logger.warn("{} is not a table. Unable to check for duplicate values.", varTable)
end

seen = nil
duplicatesValues = nil
end
end

Expand Down

0 comments on commit d083d04

Please sign in to comment.