Module:Sandbox: Difference between revisions

Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {};
local p = {};
local data = mw.loadJsonData("Data:ItemData.json")
local data = mw.loadJsonData("Data:ItemData.json")
-- Utility function to safely convert to number
local function toNumber(value)
    if type(value) == "number" then
        return value
    elseif type(value) == "string" then
        -- Remove any formatting (commas, etc.) before conversion
        local cleaned = value:gsub("[^%d.-]", "")
        return tonumber(cleaned) or 0
    end
    return 0
end


-- Normalize strings for comparison
-- Normalize strings for comparison
local function normalize(str)
local function normalize(str)
     return mw.ustring.lower(mw.text.trim(str or ""))
     return mw.ustring.lower(mw.text.trim(str or ""))
end
-- Clean and parse numbers
local function cleanNumber(num)
    if type(num) == "number" then return num end
    if not num then return 0 end
    return tonumber(tostring(num):gsub("[^%d.-]", "")) or 0
end
end


Line 32: Line 37:
local lang = mw.language.getContentLanguage()
local lang = mw.language.getContentLanguage()
local function formatNum(amount)
local function formatNum(amount)
     return lang:formatNum(cleanNumber(amount))
     return lang:formatNum(toNumber(amount))
end
end


Line 47: Line 52:
     end
     end
      
      
     -- Determine item type with validation
     -- Determine item type
     local item_type = "Weapon" -- default
     local item_type = "Weapon" -- default
    local valid_types = {
        Weapon = {bg = "#80550F", header = "#C97A03", border = "#C97A03"},
        Vitality = {bg = "#4D7214", header = "#659818", border = "#659818"},
        Spirit = {bg = "#623585", header = "#8B56B4", border = "#8B56B4"}
    }
   
     if item.Slot then
     if item.Slot then
         local slot = normalize(item.Slot)
         local slot = normalize(item.Slot)
Line 64: Line 63:
     end
     end
      
      
     local colors = valid_types[item_type] or valid_types.Weapon
    -- Color definitions
     local colors = {
        Weapon = {bg = "#80550F", header = "#C97A03", border = "#C97A03"},
        Vitality = {bg = "#4D7214", header = "#659818", border = "#659818"},
        Spirit = {bg = "#623585", header = "#8B56B4", border = "#8B56B4"}
    }
    local color = colors[item_type] or colors.Weapon


     -- Process components
     -- Process components
Line 77: Line 82:
     end
     end


     -- Process stats dynamically
     -- Process stats
     local stats = {}
     local stats = {}
     local stat_formats = {
     local stat_config = {
         TechPower = {icon = "Spirit icon.png", suffix = "Spirit Power"},
         TechPower = {icon = "Spirit icon.png", suffix = "Spirit Power"},
         WeaponPower = {icon = "Weapon Icon.png", suffix = "Weapon Damage"},
         WeaponPower = {icon = "Weapon Icon.png", suffix = "Weapon Damage"},
Line 89: Line 94:
     }
     }


     for stat, format in pairs(stat_formats) do
     for stat, config in pairs(stat_config) do
         local value = item[stat]
         local value = item[stat]
         if value and value ~= "0" and value ~= 0 then
         if value and value ~= "0" and value ~= 0 then
             local text = "• "
             local text = "• "
             if format.prefix then text = text .. format.prefix end
             if config.prefix then text = text .. config.prefix end
             text = text .. tostring(value)
             text = text .. tostring(value)
             if format.icon then text = text .. " [[File:" .. format.icon .. "|20px]]" end
             if config.icon then text = text .. " [[File:" .. config.icon .. "|20px]]" end
             if format.suffix then text = text .. " " .. format.suffix end
             if config.suffix then text = text .. " " .. config.suffix end
             table.insert(stats, text)
             table.insert(stats, text)
         end
         end
Line 102: Line 107:


     -- Calculate shop bonus
     -- Calculate shop bonus
    local tier = toNumber(item.Tier or 1)
     local shop_bonus = "+0"
     local shop_bonus = "+0"
    local tier = cleanNumber(item.Tier or 1)
     if item_type == "Weapon" then
     if item_type == "Weapon" then
         shop_bonus = string.format("+%d%% Weapon Damage", 6 + (tier-1)*4)
         shop_bonus = string.format("+%d%% Weapon Damage", 6 + (tier-1)*4)
Line 119: Line 124:
             '{| class="wikitable" style="text-align:left; border-collapse:collapse; width:312px; max-width:100%%; color: #FFEFD7; padding:12px; font-family:\'Retail Demo Regular\',serif; background-color: %s"\n' ..
             '{| class="wikitable" style="text-align:left; border-collapse:collapse; width:312px; max-width:100%%; color: #FFEFD7; padding:12px; font-family:\'Retail Demo Regular\',serif; background-color: %s"\n' ..
             '! colspan="4" style="width:280px; padding:5px 12px; text-align:center; font-size:24px; font-family:\'Retail Demo Bold\',serif; background-color: %s" | %s',
             '! colspan="4" style="width:280px; padding:5px 12px; text-align:center; font-size:24px; font-family:\'Retail Demo Bold\',serif; background-color: %s" | %s',
             colors.bg, colors.header, item.Name
             color.bg, color.header, item.Name
         ),
         ),
          
          
Line 132: Line 137:
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Cost\n' ..
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Cost\n' ..
             '| colspan=2 style="padding-left:0.5em;" | {{Souls|%s}}',
             '| colspan=2 style="padding-left:0.5em;" | {{Souls|%s}}',
             colors.border, formatNum(item.Cost)
             color.border, formatNum(item.Cost)
         ),
         ),
          
          
Line 139: Line 144:
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Tier\n' ..
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Tier\n' ..
             '| colspan=2 style="padding-left:0.5em;" | %s',
             '| colspan=2 style="padding-left:0.5em;" | %s',
             colors.border, tier
             color.border, tier
         ),
         ),
          
          
Line 146: Line 151:
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Shop Bonus\n' ..
             '|-\n| colspan=2 style="text-align:right; width:50%%; border-right:1px solid %s; padding-right:0.5em;" | Shop Bonus\n' ..
             '| colspan=2 style="padding-left:0.5em;" | %s',
             '| colspan=2 style="padding-left:0.5em;" | %s',
             colors.border, shop_bonus
             color.border, shop_bonus
         )
         )
     }
     }
Line 166: Line 171:
         string.format(
         string.format(
             '|-\n| colspan="4" style="text-align:left; padding:0 12px; color:#FFEFD7; background-color: %s" | %s',
             '|-\n| colspan="4" style="text-align:left; padding:0 12px; color:#FFEFD7; background-color: %s" | %s',
             colors.bg, item.Description or ""
             color.bg, item.Description or ""
         )
         )
     )
     )