Module:Sandbox: Difference between revisions

No edit summary
mNo edit summary
 
(38 intermediate revisions by 4 users not shown)
Line 1: Line 1:
local p = {};
This page is protected from editing. Please see [[Deadlock:User pages]].
local data = mw.loadJsonData("Data:ItemData.json")
 
-- Utility function to get item by name (case-insensitive)
local function get_json_item(name)
    if not name or type(name) ~= 'string' then return nil end
    local lowerName = mw.ustring.lower(name)
   
    for _, v in pairs(data) do
        if v["Name"] and mw.ustring.lower(v["Name"]) == lowerName then
            if v["IsDisabled"] == false or v["IsDisabled"] == nil then
                return v
            end
        end
    end
    return nil
end
 
-- Safe number formatting with error handling
local lang = mw.language.getContentLanguage()
local function formatNum(amount)
    if amount == nil then return "0" end
    if type(amount) == 'string' then
        amount = tonumber(amount) or 0
    end
    return lang:formatNum(amount)
end
 
-- Main function to generate infobox content
p.generate_infobox = function(frame)
    local item_name = frame.args[1]
    if not item_name or item_name == '' then
        return frame:preprocess("<span class='error'>No item name specified</span>")
    end
   
    local item = get_json_item(item_name)
    if not item then
        return frame:preprocess("<span class='error'>Item not found: " .. item_name .. "</span>")
    end
   
    -- Determine item type with fallback
    local item_type = "Weapon" -- default
    if item.Slot then
        if item.Slot == "Armor" then
            item_type = "Vitality"
        elseif item.Slot == "Tech" then
            item_type = "Spirit"
        end
    end
 
    -- Generate stats section with all possible stats
    local stats = {}
    local stat_order = {
        'TechPower', 'WeaponPower', 'TechResist', 'BulletResist',
        'AbilityCooldown', 'AbilityCastRange', 'AbilityUnitTargetLimit'
    }
   
    for _, stat in ipairs(stat_order) do
        if item[stat] and item[stat] ~= "0" and item[stat] ~= 0 then
            local stat_text = "• "
            if stat == 'TechPower' then
                stat_text = stat_text .. item[stat] .. " [[File:Spirit icon.png|20px]] Spirit Power"
            elseif stat == 'WeaponPower' then
                stat_text = stat_text .. item[stat] .. " [[File:Weapon Icon.png|20px]] Weapon Damage"
            elseif stat == 'TechResist' then
                stat_text = stat_text .. item[stat] .. " [[File:Spirit Resist Icon.png|20px]] Tech Resist"
            elseif stat == 'BulletResist' then
                stat_text = stat_text .. item[stat] .. " [[File:Bullet Resist Icon.png|20px]] Bullet Resist"
            elseif stat == 'AbilityCooldown' then
                stat_text = stat_text .. "[[File:Cooldown Icon.png|20px]] " .. item[stat] .. "s Cooldown"
            elseif stat == 'AbilityCastRange' then
                stat_text = stat_text .. item[stat] .. "m Cast Range"
            elseif stat == 'AbilityUnitTargetLimit' then
                stat_text = stat_text .. "Targets: " .. item[stat]
            end
           
            table.insert(stats, stat_text)
        end
    end
   
    local stats_text = table.concat(stats, "\\n|\\n| ")
   
    -- Generate components section if exists
    local components_text = ""
    if item.Components and #item.Components > 0 then
        local component_names = {}
        for _, comp_id in ipairs(item.Components) do
            local component = data[comp_id]
            if component and component.Name then
                table.insert(component_names, component.Name)
            end
        end
       
        if #component_names > 0 then
            components_text = string.format(
                "! colspan=\"4\" style=\"text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:'Retail Demo Bold',serif; %s\" | COMPONENTS:<br/>%s",
                (item_type == "Weapon" and "background-color: #9E630C; color: #DCCFC6;" or
                item_type == "Vitality" and "background-color: #203500; color: #C7C9C6;" or
                "background-color: #372248; color: #C9C7CB;"),
                table.concat(component_names, "<br/>{{ItemIcon|", "{{ItemIcon|", "}}")
            )
        end
    end
   
    -- Calculate shop bonus dynamically
    local shop_bonus = "+0"
    if item.Tier then
        local tier = tonumber(item.Tier) or 1
        if item_type == "Weapon" then
            shop_bonus = "+" .. (6 + (tier-1)*4) .. "% Weapon Damage"
        elseif item_type == "Vitality" then
            shop_bonus = "+" .. (11 + (tier-1)*3) .. "% Base Health"
        else -- Spirit
            shop_bonus = "+" .. (4 * tier) .. " Spirit Power"
        end
    end
   
    -- Build the infobox as separate parsed chunks
    local infobox_parts = {
        frame:preprocess(string.format(
            [[<div class="infobox_item" style="float:right; display:inline-block; vertical-align:top;">
            {| class="wikitable" style="text-align:left; border-collapse:collapse; width:312px; max-width:100%%; color: #FFEFD7; padding:12px; font-family:'Retail Demo Regular',serif; %s"
            ! colspan="4" style="width:280px; padding:5px 12px; text-align:center; font-size:24px; font-family:'Retail Demo Bold',serif; %s" | %s]],
            (item_type == "Weapon" and "background-color: #80550F;" or
            item_type == "Vitality" and "background-color: #4D7214;" or
            "background-color: #623585;"),
            (item_type == "Weapon" and "background-color: #C97A03;" or
            item_type == "Vitality" and "background-color: #659818;" or
            "background-color: #8B56B4;"),
            item.Name
        )),
       
        frame:preprocess(string.format(
            "|-\n| colspan=4 class=\"infobox-image\" style=\"padding:5px; text-align:center;\" | [[File:%s.png|144px]]",
            item.Name
        )),
       
        frame:preprocess(string.format(
            "|-\n| colspan=2 style=\"text-align:right; width:50%%; %s padding-right:0.5em;\" | Cost\n| colspan=2 style=\"padding-left:0.5em;\" | {{Souls|%s}}",
            (item_type == "Weapon" and "border-right:1px solid #C97A03;" or
            item_type == "Vitality" and "border-right:1px solid #659818;" or
            "border-right:1px solid #8B56B4;"),
            formatNum(item.Cost or 0)
        )),
       
        frame:preprocess(string.format(
            "|-\n| colspan=2 style=\"text-align:right; width:50%%; %s padding-right:0.5em;\" | Tier\n| colspan=2 style=\"padding-left:0.5em;\" | %s",
            (item_type == "Weapon" and "border-right:1px solid #C97A03;" or
            item_type == "Vitality" and "border-right:1px solid #659818;" or
            "border-right:1px solid #8B56B4;"),
            item.Tier or "1"
        )),
       
        frame:preprocess(string.format(
            "|-\n| colspan=2 style=\"text-align:right; width:50%%; %s padding-right:0.5em;\" | Shop Bonus\n| colspan=2 style=\"padding-left:0.5em;\" | %s",
            (item_type == "Weapon" and "border-right:1px solid #C97A03;" or
            item_type == "Vitality" and "border-right:1px solid #659818;" or
            "border-right:1px solid #8B56B4;"),
            shop_bonus
        )),
       
        components_text ~= "" and frame:preprocess("|-\n"..components_text) or "",
       
        frame:preprocess(string.format(
            "|-\n| colspan=\"4\" style=\"text-align:left; padding:0 12px; color:#FFEFD7; %s\" | %s",
            (item_type == "Weapon" and "background-color: #80550F;" or
            item_type == "Vitality" and "background-color: #4D7214;" or
            "background-color: #623585;"),
            item.Description or ""
        )),
       
        stats_text ~= "" and frame:preprocess("|-\n| "..stats_text) or "",
       
        frame:preprocess("|}\n</div>")
    }
   
    -- Combine all parts and return
    return table.concat(infobox_parts)
end
 
return p