Module:Sandbox: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
test
 
No edit summary
Line 2: Line 2:
local data = mw.loadJsonData("Data:ItemData.json")
local data = mw.loadJsonData("Data:ItemData.json")


---------------------------------------- Utility functions
-- Utility function to get item by name
 
-- Returns the table of a specific item
-- @function  get_json_item
-- @param      {string}
-- @return    {table}
local function get_json_item(name)
local function get_json_item(name)
for _, v in pairs(data) do
    for _, v in pairs(data) do
if (v["Name"] == name and not v["IsDisabled"]) then
        if v["Name"] == name and not v["IsDisabled"] then
return v
            return v
end
        end
end
    end
return nil
    return nil
end
 
-- Returns an array of item tables that have the same properties
-- @function  get_similar_items
-- @param      {string}
-- @return    {array of tables}
local function get_similar_items(property)
local similarItems = {}
for _, v in pairs(data) do
if (v[property] ~= nil) then
table.insert(similarItems, v)
end
end
return similarItems
end
end


-- Adds commas delimiter to the thousands place
-- Format numbers with commas
local lang = mw.language.getContentLanguage()
local lang = mw.language.getContentLanguage()
local function Format(amount)
local function formatNum(amount)
    local formatted = amount
     return lang:formatNum(tonumber(amount))
    if(type(formatted) ~= "number") then return "<span style=\"color:red;\">Cannot format non-number value.</span>" end
     return lang:formatNum(tonumber(formatted))
end
 
--- Get cost of an item
-- @function  get_cost
-- @param      {string}
-- @return    {number}
local function get_cost(item_name)
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
local cost = 0
local cur_item = item
repeat
cost = cost + cur_item["Cost"]
if(cur_item["Components"] == nil) then
break
end
cur_item = data[cur_item["Components"][1]]
until cur_item == nil
 
return cost
end
 
--- Get type/slot/category of an item
-- @function  get_type
-- @param      {string}
-- @return    {string}
local function get_type(item_name)
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
if (item["Slot"] == "Weapon") then
return "Weapon"
elseif (item["Slot"] == "Armor") then
return "Vitality"
elseif (item["Slot"] == "Tech") then
return "Spirit"
else
return nil
end
 
end
 
---------------------------------------- Invocable functions
 
--- {{#invoke:ItemData|get_cost|ITEM_NAME}} access point
p.get_cost = function(frame)
local item_name = frame.args[1]
return Format(get_cost(item_name))
end
 
-- Module access point
function p._get_cost(a)
a = tostring(a) or '0'
return get_cost(a)
end
end


-- {{#invoke:ItemData|get_type|ITEM_NAME}} access point
-- Main function to generate infobox content
p.get_type = function(frame)
p.generate_infobox = function(frame)
local item_name = frame.args[1]
return get_type(item_name)
end
 
-- Module access point
function p._get_type(a)
a = tostring(a) or ""
return get_type(a)
end
 
--{{#invoke:ItemData|is_active|ITEM_NAME}}--
p.is_active = function(frame)
local item_name = frame.args[1]
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
if (item["Activation"] == "Passive") then
return false
else
return true
end
end
 
--{{#invoke:ItemData|get_prop|ITEM_NAME|PROPERTY}}--
--check Data:ItemData.json for properties
p.get_prop = function(frame)
local tableValues = {}
local item_name = frame.args[1]
local property = frame.args[2]
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
if(type(item[property]) == "table") then
for i,v in pairs(item[property]) do
local tableValues = table.insert(tableValues, v)
end
return table.concat(tableValues, ", ")
end
return item[property]
end
 
 
p.get_component_name = function(frame)
local item_name = frame.args[1]
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
local components = item['Components']
--return empty string if no component found
if(components == nil) then return nil end
local component_key = components[1]
local component = data[component_key]
if (component == nil) then return nil end
return component['Name']
end
 
-- {{Items count}} access point
function p.get_headcount()
local iter = 0
local tbl = get_similar_items("IsDisabled")
for _, v in ipairs(tbl) do
if (v["IsDisabled"] == false and v["Name"] ~= nil) then
iter = iter + 1
end
end
return iter
end
 
-- New function to get all item data as a table
p.get_item_data = function(frame)
     local item_name = frame.args[1]
     local item_name = frame.args[1]
     local item = get_json_item(item_name)
     local item = get_json_item(item_name)
     if not item then return "<span style=\"color:red;\">Item Not Found.</span>" end
     if not item then return "<span class='error'>Item not found: " .. item_name .. "</span>" end
     return item
      
end
    -- Determine item type
 
     local item_type = "Weapon"
-- Function to get formatted stats for the infobox
     if item.Slot == "Armor" then
p.get_formatted_stats = function(frame)
        item_type = "Vitality"
     local item_name = frame.args[1]
     elseif item.Slot == "Tech" then
     local item = get_json_item(item_name)
        item_type = "Spirit"
     if not item then return "" end
    end
      
      
    -- Generate stats section
     local stats = {}
     local stats = {}
   
    -- Add basic stats
     if item.TechPower and item.TechPower ~= "0" then
     if item.TechPower and item.TechPower ~= "0" then
         table.insert(stats, "• "..item.TechPower.." [[File:Spirit icon.png|20px]] Spirit Power")
         table.insert(stats, "• "..item.TechPower.." [[File:Spirit icon.png|20px]] Spirit Power")
Line 194: Line 46:
         table.insert(stats, "• "..item.BulletResist.." [[File:Bullet Resist Icon.png|20px]] Bullet Resist")
         table.insert(stats, "• "..item.BulletResist.." [[File:Bullet Resist Icon.png|20px]] Bullet Resist")
     end
     end
    local stats_text = table.concat(stats, "\n|-\n| ")
      
      
     -- Add ability stats if active item
     -- Generate components section if exists
     if item.Activation ~= "Passive" then
    local components_text = ""
         if item.AbilityCooldown then
     if item.Components then
             table.insert(stats, "• [[File:Cooldown Icon.png|20px]] "..item.AbilityCooldown.."s Cooldown")
         local component = data[item.Components[1]]
        end
        if component then
        if item.AbilityCastRange and item.AbilityCastRange ~= "0" then
             components_text = [=[
            table.insert(stats, "• "..item.AbilityCastRange.."m Cast Range")
! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:'Retail Demo Bold',serif; ]=] ..
                (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;") .. [=[">COMPONENTS:<br/>{{ItemIcon|]=] .. component.Name .. [=[}}]=]
         end
         end
     end
     end
      
      
     return table.concat(stats, "\n|-\n| ")
     -- Generate the full infobox
    local infobox = [=[
<div class="infobox_item" style="float:right; display:inline-block; vertical-align:top;">
{| style="text-align:left; border-collapse:collapse; width:312px; max-width:100%; height:30px; color: #FFEFD7; padding:12px; font-family:'Retail Demo Regular',serif; ]=] ..
    (item_type == "Weapon" and "background-color: #80550F;" or
    item_type == "Vitality" and "background-color: #4D7214;" or
    "background-color: #623585;") .. [=["
! colspan="4"; style="width:280px; padding:5px 12px; text-align:center; font-size:24px; font-family:'Retail Demo Bold',serif; ]=] ..
    (item_type == "Weapon" and "background-color: #C97A03;" or
    item_type == "Vitality" and "background-color: #659818;" or
    "background-color: #8B56B4;") .. [=[">]=] .. item.Name .. [=[
|-
| colspan=4 class="infobox-image" style="padding:5px; text-align:center;">[[File:]=] .. item.Name .. [=[.png|144px]]
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Cost
| colspan=2 style="padding-left:0.5em;">{{Souls|]=] .. formatNum(item.Cost) .. [=[}}}
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Tier
| colspan=2 style="padding-left:0.5em;">]=] .. item.Tier .. [=[
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Shop Bonus
| colspan=2 style="padding-left:0.5em;">]=] ..
    (item_type == "Weapon" and "+" .. (6 + (tonumber(item.Tier)-1)*4) .. "% Weapon Damage" or
    item_type == "Vitality" and "+" .. (11 + (tonumber(item.Tier)-1)*3) .. "% Base Health" or
    "+" .. (4 * tonumber(item.Tier)) .. " Spirit Power") .. [=[
|-
]=] .. components_text .. [=[
|-
| colspan="4" style="text-align:left; padding:0 12px; color:#FFEFD7; ]=] ..
    (item_type == "Weapon" and "background-color: #80550F;" or
    item_type == "Vitality" and "background-color: #4D7214;" or
    "background-color: #623585;") .. [=[">]=] .. item.Description .. [=[
|-
| ]=] .. stats_text .. [=[
|}
</div>]=]
   
    return infobox
end
end


return p
return p

Revision as of 18:08, 1 April 2025

Documentation for this module may be created at Module:Sandbox/doc

local p = {};
local data = mw.loadJsonData("Data:ItemData.json")

-- Utility function to get item by name
local function get_json_item(name)
    for _, v in pairs(data) do
        if v["Name"] == name and not v["IsDisabled"] then
            return v
        end
    end
    return nil
end

-- Format numbers with commas
local lang = mw.language.getContentLanguage()
local function formatNum(amount)
    return lang:formatNum(tonumber(amount))
end

-- Main function to generate infobox content
p.generate_infobox = function(frame)
    local item_name = frame.args[1]
    local item = get_json_item(item_name)
    if not item then return "<span class='error'>Item not found: " .. item_name .. "</span>" end
    
    -- Determine item type
    local item_type = "Weapon"
    if item.Slot == "Armor" then
        item_type = "Vitality"
    elseif item.Slot == "Tech" then
        item_type = "Spirit"
    end
    
    -- Generate stats section
    local stats = {}
    if item.TechPower and item.TechPower ~= "0" then
        table.insert(stats, "• "..item.TechPower.." [[File:Spirit icon.png|20px]] Spirit Power")
    end
    if item.WeaponPower and item.WeaponPower ~= "0" then
        table.insert(stats, "• "..item.WeaponPower.." [[File:Weapon Icon.png|20px]] Weapon Damage")
    end
    if item.TechResist and item.TechResist ~= "0" then
        table.insert(stats, "• "..item.TechResist.." [[File:Spirit Resist Icon.png|20px]] Tech Resist")
    end
    if item.BulletResist and item.BulletResist ~= "0" then
        table.insert(stats, "• "..item.BulletResist.." [[File:Bullet Resist Icon.png|20px]] Bullet Resist")
    end
    local stats_text = table.concat(stats, "\n|-\n| ")
    
    -- Generate components section if exists
    local components_text = ""
    if item.Components then
        local component = data[item.Components[1]]
        if component then
            components_text = [=[
! colspan="4" style="text-align:left; padding:0 12px; font-weight:bold; font-size:16px; font-family:'Retail Demo Bold',serif; ]=] ..
                (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;") .. [=[">COMPONENTS:<br/>{{ItemIcon|]=] .. component.Name .. [=[}}]=]
        end
    end
    
    -- Generate the full infobox
    local infobox = [=[
<div class="infobox_item" style="float:right; display:inline-block; vertical-align:top;">
{| style="text-align:left; border-collapse:collapse; width:312px; max-width:100%; height:30px; color: #FFEFD7; padding:12px; font-family:'Retail Demo Regular',serif; ]=] ..
    (item_type == "Weapon" and "background-color: #80550F;" or
     item_type == "Vitality" and "background-color: #4D7214;" or
     "background-color: #623585;") .. [=["
! colspan="4"; style="width:280px; padding:5px 12px; text-align:center; font-size:24px; font-family:'Retail Demo Bold',serif; ]=] ..
    (item_type == "Weapon" and "background-color: #C97A03;" or
     item_type == "Vitality" and "background-color: #659818;" or
     "background-color: #8B56B4;") .. [=[">]=] .. item.Name .. [=[
|-
| colspan=4 class="infobox-image" style="padding:5px; text-align:center;">[[File:]=] .. item.Name .. [=[.png|144px]]
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Cost
| colspan=2 style="padding-left:0.5em;">{{Souls|]=] .. formatNum(item.Cost) .. [=[}}}
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Tier
| colspan=2 style="padding-left:0.5em;">]=] .. item.Tier .. [=[
|-
| colspan=2 style="text-align:right; width:50%; ]=] ..
    (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;") .. [=[ padding-right:0.5em;">Shop Bonus
| colspan=2 style="padding-left:0.5em;">]=] ..
    (item_type == "Weapon" and "+" .. (6 + (tonumber(item.Tier)-1)*4) .. "% Weapon Damage" or
     item_type == "Vitality" and "+" .. (11 + (tonumber(item.Tier)-1)*3) .. "% Base Health" or
     "+" .. (4 * tonumber(item.Tier)) .. " Spirit Power") .. [=[
|-
]=] .. components_text .. [=[
|-
| colspan="4" style="text-align:left; padding:0 12px; color:#FFEFD7; ]=] ..
    (item_type == "Weapon" and "background-color: #80550F;" or
     item_type == "Vitality" and "background-color: #4D7214;" or
     "background-color: #623585;") .. [=[">]=] .. item.Description .. [=[
|-
| ]=] .. stats_text .. [=[
|}
</div>]=]
    
    return infobox
end

return p