From 11e91f89753c01299de44f17e63644d097030275 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 8 Feb 2024 21:27:35 -0300 Subject: [PATCH 1/2] refactor: duplicate keys checking --- .../globalevents/server_initialization.lua | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/data/scripts/globalevents/server_initialization.lua b/data/scripts/globalevents/server_initialization.lua index 26494c82ce4..6f14c3af6a3 100644 --- a/data/scripts/globalevents/server_initialization.lua +++ b/data/scripts/globalevents/server_initialization.lua @@ -60,43 +60,37 @@ local function storeTownsInDatabase() end end +local seen = {} +local duplicatesValues = {} + -- Function to recursively check for duplicate values in a given variable's storage -local function checkDuplicateStorageValues(varTable, seen, duplicates) +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 -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 + return #duplicatesValues > 0 and duplicatesValues 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, ", ")) - else - logger.info("Checking " .. variableName .. ": No duplicate values found.") +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 " .. tableName .. ": Duplicate values found: " .. table.concat(duplicates, ", ")) + else + logger.info("Checking " .. tableName .. ": No duplicate values found.") + end end end end From f298ab399f270c9dd9c687a48718a94e52ec97a9 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 9 Feb 2024 12:31:43 -0300 Subject: [PATCH 2/2] resolve conversations --- .../globalevents/server_initialization.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/data/scripts/globalevents/server_initialization.lua b/data/scripts/globalevents/server_initialization.lua index 6f14c3af6a3..8b0ff53ceb8 100644 --- a/data/scripts/globalevents/server_initialization.lua +++ b/data/scripts/globalevents/server_initialization.lua @@ -60,10 +60,9 @@ local function storeTownsInDatabase() end end -local seen = {} -local duplicatesValues = {} +-- Functions to recursively check for duplicate values in a given variable's storage and log the results +local seen, duplicatesValues --- Function to recursively check for duplicate values in a given variable's storage local function checkDuplicateStorageValues(varTable) for _, value in pairs(varTable) do if type(value) == "table" then @@ -77,7 +76,6 @@ local function checkDuplicateStorageValues(varTable) return #duplicatesValues > 0 and duplicatesValues or false end --- Function to check duplicated variable values and log the results local function checkAndLogDuplicateValues(tableNames) for _, tableName in ipairs(tableNames) do local varTable = _G[tableName] @@ -87,11 +85,16 @@ local function checkAndLogDuplicateValues(tableNames) local duplicates = checkDuplicateStorageValues(varTable) if duplicates then - logger.warn("Checking " .. tableName .. ": Duplicate values found: " .. table.concat(duplicates, ", ")) + logger.warn("Checking {}: Duplicate values found: {}", tableName, table.concat(duplicates, ", ")) else - logger.info("Checking " .. tableName .. ": No duplicate values found.") + logger.info("Checking {}: No duplicate values found.", tableName) end + else + logger.warn("{} is not a table. Unable to check for duplicate values.", varTable) end + + seen = nil + duplicatesValues = nil end end