Module:Sandbox: Difference between revisions

No edit summary
No edit summary
Line 7: Line 7:
         return value
         return value
     elseif type(value) == "string" then
     elseif type(value) == "string" then
        -- Remove any formatting (commas, etc.) before conversion
         local cleaned = value:gsub("[^%d.-]", "")
         local cleaned = value:gsub("[^%d.-]", "")
         return tonumber(cleaned) or 0
         return tonumber(cleaned) or 0
Line 34: Line 33:
end
end


-- Format numbers with localization
-- Find items that use this item as a component
local lang = mw.language.getContentLanguage()
local function get_parent_items(item_name)
local function formatNum(amount)
     local parents = {}
    return lang:formatNum(toNumber(amount))
end
 
-- Find all recipes where this item is used as a component
local function find_recipes(item_name)
     local recipes = {}
     for _, item in pairs(data) do
     for _, item in pairs(data) do
         if item.Components and type(item.Components) == "table" then
         if item.Components and type(item.Components) == "table" then
Line 48: Line 41:
                 local component = data[comp_id]
                 local component = data[comp_id]
                 if component and component.Name == item_name then
                 if component and component.Name == item_name then
                     table.insert(recipes, item.Name)
                     table.insert(parents, item.Name)
                    break  -- Only need to find one match per item
                 end
                 end
             end
             end
         end
         end
     end
     end
     return recipes
     return parents
end
 
-- Format numbers with localization
local lang = mw.language.getContentLanguage()
local function formatNum(amount)
    return lang:formatNum(toNumber(amount))
end
end


-- Main function with complete item relationship handling
-- Main function with improved error handling
p.generate_infobox = function(frame)
p.generate_infobox = function(frame)
     local item_name = frame.args[1]
     local item_name = frame.args[1]
    local manual_component = frame.args.component -- Manual override for "Is component of"
   
     if not item_name or item_name == "" then
     if not item_name or item_name == "" then
         return frame:preprocess("{{Error|No item name specified}}")
         return frame:preprocess("{{Error|No item name specified}}")
Line 82: Line 82:
     -- Color definitions
     -- Color definitions
     local colors = {
     local colors = {
         Weapon = {
         Weapon = {bg = "#80550F", header = "#C97A03", border = "#C97A03"},
            bg = "#80550F", header = "#C97A03", border = "#C97A03",
         Vitality = {bg = "#4D7214", header = "#659818", border = "#659818"},
            components = {bg = "#9E630C", text = "#DCCFC6"},
         Spirit = {bg = "#623585", header = "#8B56B4", border = "#8B56B4"}
            recipes = {bg = "#704A0C", text = "#D1CBC6"}
        },
         Vitality = {
            bg = "#4D7214", header = "#659818", border = "#659818",
            components = {bg = "#203500", text = "#C7C9C6"},
            recipes = {bg = "#436310", text = "#CACFC7"}
        },
         Spirit = {
            bg = "#623585", header = "#8B56B4", border = "#8B56B4",
            components = {bg = "#372248", text = "#C9C7CB"},
            recipes = {bg = "#552D74", text = "#CCC8D2"}
        }
     }
     }
     local color = colors[item_type] or colors.Weapon
     local color = colors[item_type] or colors.Weapon


     -- Process components (what this item is made of)
     -- Process components (what this item is made of)
     local component_text = ""
     local components_text = ""
     if item.Components and type(item.Components) == "table" and #item.Components > 0 then
     if item.Components and type(item.Components) == "table" then
         local valid_components = {}
         local valid_components = {}
         for _, comp_id in ipairs(item.Components) do
         for _, comp_id in ipairs(item.Components) do
Line 112: Line 100:
          
          
         if #valid_components > 0 then
         if #valid_components > 0 then
             component_text = string.format(
             components_text = string.format(
                 '|-\n! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:\'Retail Demo Bold\',serif; background-color: %s; color: %s" | COMPONENTS:<br/>%s',
                 '|-\n! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:\'Retail Demo Bold\',serif; background-color: %s; color: %s" | COMPONENTS:<br/>%s',
                 color.components.bg, color.components.text,
                 (item_type == "Weapon" and "#9E630C" or item_type == "Vitality" and "#203500" or "#372248"),
                (item_type == "Weapon" and "#DCCFC6" or item_type == "Vitality" and "#C7C9C6" or "#C9C7CB"),
                 table.concat(valid_components, "<br/>")
                 table.concat(valid_components, "<br/>")
             )
             )
Line 120: Line 109:
     end
     end


     -- Process recipes (what items this is used in)
     -- Process parent items (what this item is used in)
     local recipe_text = ""
     local parent_items_text = ""
     local recipes = find_recipes(item.Name)
     local parent_items = {}
     if #recipes > 0 then
   
         local recipe_links = {}
    -- Check for manual override first
         for _, recipe_name in ipairs(recipes) do
    if manual_component and manual_component ~= "" then
             table.insert(recipe_links, "{{ItemIcon|" .. recipe_name .. "}}")
        table.insert(parent_items, manual_component)
    else
        -- Otherwise look up automatically
        parent_items = get_parent_items(item.Name)
    end
   
     if #parent_items > 0 then
         local parent_icons = {}
         for _, parent in ipairs(parent_items) do
             table.insert(parent_icons, "{{ItemIcon|" .. parent .. "}}")
         end
         end
          
          
         recipe_text = string.format(
         parent_items_text = string.format(
             '|-\n! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:\'Retail Demo Bold\',serif; background-color: %s; color: %s" | USED IN:<br/>%s',
             '|-\n! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:\'Retail Demo Bold\',serif; background-color: %s; color: %s" | IS COMPONENT OF:<br/>%s',
             color.recipes.bg, color.recipes.text,
             (item_type == "Weapon" and "#704A0C" or item_type == "Vitality" and "#436310" or "#552D74"),
             table.concat(recipe_links, "<br/>")
            (item_type == "Weapon" and "#D1CBC6" or item_type == "Vitality" and "#CACFC7" or "#CCC8D2"),
             table.concat(parent_icons, "<br/>")
         )
         )
     end
     end


     -- Process stats - comprehensive stat handling
     -- Process stats
     local stats = {}
     local stats = {}
     local stat_config = {
     local stat_config = {
Line 146: Line 145:
         AbilityCastRange = {suffix = "m Cast Range"},
         AbilityCastRange = {suffix = "m Cast Range"},
         AbilityUnitTargetLimit = {prefix = "Targets: "},
         AbilityUnitTargetLimit = {prefix = "Targets: "},
        -- Rapid Recharge specific stats
         CooldownBetweenChargeReduction = {suffix = "% Charge Cooldown Reduction"},
         CooldownBetweenChargeReduction = {suffix = "% Charge Cooldown Reduction"},
         BonusAbilityCharges = {prefix = "+", suffix = " Bonus Charges"},
         BonusAbilityCharges = {prefix = "+", suffix = " Bonus Charges"},
         CooldownReductionOnChargedAbilities = {suffix = "% Charged Ability Cooldown Reduction"},
         CooldownReductionOnChargedAbilities = {suffix = "% Charged Ability Cooldown Reduction"},
         BaseAttackDamagePercent = {suffix = "% Base Attack Damage"},
         BaseAttackDamagePercent = {suffix = "% Base Attack Damage"}
        -- Add more stats as needed
     }
     }


Line 215: Line 212:
     }
     }


     -- Add components section if exists
     -- Add components if they exist
     if component_text ~= "" then
     if components_text ~= "" then
         table.insert(parts, component_text)
         table.insert(parts, components_text)
     end
     end


     -- Add recipes section if exists
     -- Add parent items if they exist
     if recipe_text ~= "" then
     if parent_items_text ~= "" then
         table.insert(parts, recipe_text)
         table.insert(parts, parent_items_text)
     end
     end


     -- Add description if it exists
     -- Add description if it exists
     if item.Description and item.Description ~= "" then
     if item.Description then
         table.insert(parts,
         table.insert(parts,
             string.format(
             string.format(