Module:ItemTables: Difference between revisionsGive feedback
Gammaton32 (talk | contribs) No edit summary |
Prevent script crash on nil values and correctly display stats from complex JSON objects. |
||
| Line 165: | Line 165: | ||
end | end | ||
-- Formats | -- Formats a stat value with a leading + or - sign and makes it bold. | ||
-- The function is robust and handles nil, number, string, or table inputs safely. | |||
-- @function signPrefix | -- @function signPrefix | ||
-- @param { | -- @param {any} The stat value to format. | ||
-- @return {string} | -- @return {string} The formatted wikitext string. | ||
local function signPrefix(value) | local function signPrefix(value) | ||
-- Returns an empty string if the input is nil to prevent errors. | |||
value = value:gsub( "m", "" ) | if value == nil then | ||
return "" | |||
end | |||
-- Converts the input to a string to safely use string methods. This allows | |||
-- the function to handle numbers from JSON (e.g., 10) that are not strings. | |||
value = tostring(value) | |||
-- Removes the 'm' suffix if it exists (e.g., from range values like "7m"). | |||
value = value:gsub("m", "") | |||
-- Convert the cleaned string into a number for comparison. | |||
value = tonumber(value) | value = tonumber(value) | ||
-- Returns an empty string if the value cannot be converted to a number, | |||
-- which prevents errors and malformed output. | |||
if not value then | |||
return "" | |||
end | |||
if (value >= 0) then | if (value >= 0) then | ||
return "+<b>" .. value .. "</b>" | return "+<b>" .. value .. "</b>" | ||
| Line 223: | Line 241: | ||
-- filter out items that: don't have the property and the value isn't zero | -- filter out items that: don't have the property and the value isn't zero | ||
if(itemName[t] ~= nil and itemName[t] ~= "0" ) then | if(itemName[t] ~= nil and itemName[t] ~= "0" ) then | ||
-- Handles complex stat values from the JSON data. | |||
-- A stat can be a simple value (e.g., "BonusMoveSpeed": "1") or a table containing | |||
-- scaling info (e.g., "BonusFireRate": { "Value": 10, "Scale": ... }). | |||
-- This code checks if the stat is a table and extracts the base numerical value from its 'Value' key. | |||
local statValue = itemName[t] | |||
if type(statValue) == 'table' and statValue.Value ~= nil then | |||
statValue = statValue.Value | |||
end | |||
listofItems = listofItems .. itemName["Name"] .. "<br/>" | listofItems = listofItems .. itemName["Name"] .. "<br/>" | ||
copyVar = copyVar:gsub("^%l", string.upper) | copyVar = copyVar:gsub("^%l", string.upper) | ||
| Line 250: | Line 278: | ||
:tag('td'):wikitext(commas._add(get_cost(itemName["Name"]))):done() | :tag('td'):wikitext(commas._add(get_cost(itemName["Name"]))):done() | ||
:tag('td'):wikitext(categoryDisplay):done() | :tag('td'):wikitext(categoryDisplay):done() | ||
-- Construct the stat change string. | |||
:tag('td'):wikitext(signPrefix(statValue) .. appendSuffix(t) .. " " .. copyVar):done() | |||
table.insert(requirements, tableData) | table.insert(requirements, tableData) | ||