Module:Item Navbox: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
No edit summary
added codename navbox function
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local items_data = mw.loadJsonData("Data:ItemData.json")
local items_data = mw.loadJsonData("Data:ItemData.json")
local items_module = require("Module:ItemData")
local lang_module = require('Module:Lang')
local lang_module = require('Module:Lang')


Line 29: Line 30:
end
end


-- Directly fetches and formats Item text for the navbox (Icons removed)
-- Directly fetches and formats ItemIcons for the navbox
local function get_item_text(slot, min_souls, max_souls, filter_mode)
local function get_item_icons(slot, min_souls, max_souls, filter_mode)
     if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then return '' end
     if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then return '' end
      
      
Line 57: Line 58:
                  
                  
                 local item_template
                 local item_template
                -- Output as a standard wikilink instead of an icon template
                 if lang_code == "en" then
                 if lang_code == "en" then
                     item_template = "[[" .. item_name_english .. "]]"
                     item_template = "{{ItemIcon|" .. item_name_english .. "|size=26px}}"
                 else
                 else
                     item_template = "[[" .. item_name_english .. "|" .. item_name_local .. "]]"
                     item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                 end
                 end
                  
                  
Line 89: Line 89:
     return table.concat(items, '')
     return table.concat(items, '')
end
end
-- Generates the Navbox using standard MediaWiki template structure
function p.generate_full_navbox(frame)
    local debug_mode = type(frame) == "table" and frame.args and frame.args["debug_mode"] or nil
    local wikitext = {}
      
      
    -- Insert standard stylesheet call
-- Gets list of item codenames
    table.insert(wikitext, '<templatestyles src="User:Kai/mp/itemnavbox/styles.css"/>\n')
local function get_item_codenames(slot, min_souls, max_souls, filter_mode, frame)
    if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then return '' end
   
    local lang_code = lang_module.get_lang_code()
    local items = {}
    local slot_items = get_items_by_slot(slot)  
      
      
     -- Start building the {{Navbox}} call
     for _, item_entry in ipairs(slot_items) do
    table.insert(wikitext, '{{Navbox')
        local item_key = item_entry.key
        local item_data = item_entry.data
       
        local this_cost = tonumber(item_data["Cost"])
        local is_street_brawl = item_data["StreetBrawl"] == true
        local should_include = true
       
        if filter_mode == "street_brawl_only" and not is_street_brawl then
            should_include = false
        elseif filter_mode == "exclude_street_brawl" and is_street_brawl then
            should_include = false
        end
       
        if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil then
            if this_cost >= min_souls and this_cost < max_souls then
                local item_name_english = lang_module.get_string(item_key, "en")
                local item_name_local = lang_module.get_string(item_key, lang_code)
               
                local codename_frame = {
                    args = { item_name_english }
                }
 
                local item_codename = items_module.get_codename(codename_frame)
               
                local item_template
                if lang_code == "en" then
                    item_template = "{{ItemIcon|" .. item_name_english .. "|l1=" .. item_codename .. "|size=26px}}"
                else
                    item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                end
               
                local search_data = item_name_english
                if lang_code ~= "en" and item_name_local then
                    search_data = search_data .. " " .. item_name_local
                end
               
                item_template = '<span class="item-nav-search-wrapper"><span class="item-nav-search-item" data-item-name="' .. search_data .. '">' .. item_template .. '</span>'
                table.insert(items, item_template)
            end
        end
    end
      
      
    -- IMPORTANT: Change "Template:ItemNavbox" below to the actual name of your template page
     table.sort(items)
    -- so that the v/d/e (view/discuss/edit) links in the top left work properly.
     table.insert(wikitext, '| name  = Template:ItemNavbox')
      
      
     -- Title including your custom search container element floated to the right
     -- Append closing tags and bullets
     table.insert(wikitext, '| title = [[Items]] <div class="item-nav-search-container" style="display:inline-block; float:right;"></div>')
    local total_items = #items
     for i = 1, total_items do
        if i == total_items then
            items[i] = items[i] .. '</span>'
        else
            items[i] = items[i] .. '<span class="item-nav-search-sep"> &bull; </span></span>'
        end
    end
      
      
    return table.concat(items, '')
end
-- Generates the full HTML item navbox recursively and efficiently
function p.generate_full_navbox(frame)
    local debug_mode = type(frame) == "table" and frame.args and frame.args["debug_mode"] or nil
    local html = {}
table.insert(html, '<div class="itemnav-header"> [[Items]] <div class="item-nav-search-container"></div></div>\n')
    table.insert(html, '<div class="itemnav-container"><templatestyles src="Item Navbox/styles.css"/>\n')
    -- Maps Headers to Slots and distinct CSS Classes
     local categories = {
     local categories = {
         { header = "Weapon", slot = "Weapon" },
         { header = "Weapon",   slot = "Weapon", css = "itemnav-weapon" },
         { header = "Vitality", slot = "Armor" },
         { header = "Vitality", slot = "Armor",  css = "itemnav-vitality" },
         { header = "Spirit", slot = "Tech" }
         { header = "Spirit",   slot = "Tech",  css = "itemnav-tech" }
     }
     }


    -- Maps tier boundaries and formatting
     local tiers = {
     local tiers = {
         { label = "{{Souls|800}}", min = 0, max = 801 },
         { label = "{{Souls|800}}", min = 0, max = 801 },
Line 120: Line 177:
         { label = "{{Souls|3200}}", min = 1601, max = 3201 },
         { label = "{{Souls|3200}}", min = 1601, max = 3201 },
         { label = "{{Souls|6400}}", min = 3201, max = 6401 },
         { label = "{{Souls|6400}}", min = 3201, max = 6401 },
         { label = '<span class="itemnav-legendary">[[Street Brawl|{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}]]</span>', min = 6401, max = 99999 }
         { label = "[[Street Brawl|{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}]]", min = 6401, max = 99999, extra_css = "itemnav-legendary " }
     }
     }


    -- Loop through categories and build Navbox subgroups
     for _, cat in ipairs(categories) do
     for i, cat in ipairs(categories) do
         table.insert(html, '\n<div class="itemnav-type-header">\'\'\'' .. cat.header .. '\'\'\'</div>\n')
         table.insert(wikitext, '| group' .. i .. ' = ' .. cat.header)
        table.insert(html, '<div class="itemnav-type-container">\n')
         table.insert(wikitext, '| list' .. i .. '  = {{Navbox|child')
 
          
        for _, tier in ipairs(tiers) do
         for j, tier in ipairs(tiers) do
            local css_class = "itemnav-soul " .. (tier.extra_css or "") .. cat.css
            table.insert(wikitext, ' | group' .. j .. ' = ' .. tier.label)
            local items_wikitext = get_item_icons(cat.slot, tier.min, tier.max, nil)
           
 
             local items_string = get_item_text(cat.slot, tier.min, tier.max, nil)
            table.insert(html, '\n<div class="itemnav-tier-row">\n')
             -- Wrap the items in our flex container for layout
            table.insert(html, '<div class="' .. css_class .. '">' .. tier.label .. '</div><!--\n')
             table.insert(wikitext, ' | list' .. j .. ' = <div class="itemnav-cost-type-container">' .. items_string .. '</div>')
            table.insert(html, '--><div class="itemnav-cost-type-container">' .. items_wikitext .. '</div>\n')
            table.insert(html, '</div>\n')
        end
 
         table.insert(html, '\n</div>\n')
    end
 
    table.insert(html, '\n</div>')
 
    -- Only ONE preprocess call for the entire structure
    return process_debug_mode(table.concat(html), debug_mode)
end
 
-- Generates navbox with codenames
function p.generate_full_navbox_codenames(frame)
    local debug_mode = type(frame) == "table" and frame.args and frame.args["debug_mode"] or nil
 
    local html = {}
table.insert(html, '<div class="itemnav-header"> [[Items]] <div class="item-nav-search-container"></div></div>\n')
    table.insert(html, '<div class="itemnav-container"><templatestyles src="Item Navbox/styles.css"/>\n')
 
    -- Maps Headers to Slots and distinct CSS Classes
    local categories = {
        { header = "Weapon",  slot = "Weapon", css = "itemnav-weapon" },
        { header = "Vitality", slot = "Armor", css = "itemnav-vitality" },
        { header = "Spirit",  slot = "Tech",  css = "itemnav-tech" }
    }
 
    -- Maps tier boundaries and formatting
    local tiers = {
        { label = "{{Souls|800}}", min = 0, max = 801 },
        { label = "{{Souls|1600}}", min = 801, max = 1601 },
        { label = "{{Souls|3200}}", min = 1601, max = 3201 },
         { label = "{{Souls|6400}}", min = 3201, max = 6401 },
         { label = "[[Street Brawl|{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}]]", min = 6401, max = 99999, extra_css = "itemnav-legendary " }
    }
 
    for _, cat in ipairs(categories) do
        table.insert(html, '\n<div class="itemnav-type-header">\'\'\'' .. cat.header .. '\'\'\'</div>\n')
        table.insert(html, '<div class="itemnav-type-container">\n')
 
        for _, tier in ipairs(tiers) do
            local css_class = "itemnav-soul " .. (tier.extra_css or "") .. cat.css
             local items_wikitext = get_item_codenames(cat.slot, tier.min, tier.max, nil)
 
             table.insert(html, '\n<div class="itemnav-tier-row">\n')
             table.insert(html, '<div class="' .. css_class .. '">' .. tier.label .. '</div><!--\n')
            table.insert(html, '--><div class="itemnav-cost-type-container">' .. items_wikitext .. '</div>\n')
            table.insert(html, '</div>\n')
         end
         end
       
 
         table.insert(wikitext, '}}') -- Close subgroup navbox
         table.insert(html, '\n</div>\n')
     end
     end


     table.insert(wikitext, '}}') -- Close parent navbox
     table.insert(html, '\n</div>')


     -- Only ONE preprocess call for the entire structure
     -- Only ONE preprocess call for the entire structure
     return process_debug_mode(table.concat(wikitext, '\n'), debug_mode)
     return process_debug_mode(table.concat(html), debug_mode)
end
end


return p
return p

Latest revision as of 14:39, 21 August 2026

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

local p = {}
local items_data = mw.loadJsonData("Data:ItemData.json")
local items_module = require("Module:ItemData")
local lang_module = require('Module:Lang')

-- Cached items grouped by slot to prevent redundant full-table evaluations
local cached_slots = nil
local function get_items_by_slot(slot)
    if not cached_slots then
        cached_slots = { table = {}, Weapon = {}, Armor = {}, Tech = {} }
        for item_key, item_data in pairs(items_data) do
            local this_slot = item_data["Slot"]
            if this_slot and cached_slots[this_slot] then
                table.insert(cached_slots[this_slot], { key = item_key, data = item_data })
            end
        end
    end
    return cached_slots[slot] or {}
end

-- With debug_mode on, it outputs unprocessed wikitext
local function process_debug_mode(wikitext, debug_mode)
    if debug_mode == 'true' then
        return wikitext
    elseif debug_mode == 'false' or debug_mode == nil then
        return mw.getCurrentFrame():preprocess(wikitext)
    else
        return "debug_mode must be 'true' or 'false'"
    end
end

-- Directly fetches and formats ItemIcons for the navbox
local function get_item_icons(slot, min_souls, max_souls, filter_mode)
    if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then return '' end
    
    local lang_code = lang_module.get_lang_code() 
    local items = {}
    local slot_items = get_items_by_slot(slot) 
    
    for _, item_entry in ipairs(slot_items) do
        local item_key = item_entry.key
        local item_data = item_entry.data
        
        local this_cost = tonumber(item_data["Cost"])
        local is_street_brawl = item_data["StreetBrawl"] == true
        local should_include = true
        
        if filter_mode == "street_brawl_only" and not is_street_brawl then
            should_include = false
        elseif filter_mode == "exclude_street_brawl" and is_street_brawl then
            should_include = false
        end
        
        if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil then
            if this_cost >= min_souls and this_cost < max_souls then
                local item_name_english = lang_module.get_string(item_key, "en")
                local item_name_local = lang_module.get_string(item_key, lang_code)
                
                local item_template
                if lang_code == "en" then
                    item_template = "{{ItemIcon|" .. item_name_english .. "|size=26px}}"
                else
                    item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                end
                
                local search_data = item_name_english
                if lang_code ~= "en" and item_name_local then
                    search_data = search_data .. " " .. item_name_local
                end
                
                item_template = '<span class="item-nav-search-wrapper"><span class="item-nav-search-item" data-item-name="' .. search_data .. '">' .. item_template .. '</span>'
                table.insert(items, item_template)
            end
        end
    end
    
    table.sort(items)
    
    -- Append closing tags and bullets
    local total_items = #items
    for i = 1, total_items do
        if i == total_items then
            items[i] = items[i] .. '</span>'
        else
            items[i] = items[i] .. '<span class="item-nav-search-sep"> &bull; </span></span>'
        end
    end
    
    return table.concat(items, '')
end
    
-- Gets list of item codenames
local function get_item_codenames(slot, min_souls, max_souls, filter_mode, frame)
    if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then return '' end
    
    local lang_code = lang_module.get_lang_code() 
    local items = {}
    local slot_items = get_items_by_slot(slot) 
    
    for _, item_entry in ipairs(slot_items) do
        local item_key = item_entry.key
        local item_data = item_entry.data
        
        local this_cost = tonumber(item_data["Cost"])
        local is_street_brawl = item_data["StreetBrawl"] == true
        local should_include = true
        
        if filter_mode == "street_brawl_only" and not is_street_brawl then
            should_include = false
        elseif filter_mode == "exclude_street_brawl" and is_street_brawl then
            should_include = false
        end
        
        if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil then
            if this_cost >= min_souls and this_cost < max_souls then
                local item_name_english = lang_module.get_string(item_key, "en")
                local item_name_local = lang_module.get_string(item_key, lang_code)
                
                local codename_frame = {
                     args = { item_name_english }
                }

                local item_codename = items_module.get_codename(codename_frame)
                
                local item_template
                if lang_code == "en" then
                    item_template = "{{ItemIcon|" .. item_name_english .. "|l1=" .. item_codename .. "|size=26px}}"
                else
                    item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                end
                
                local search_data = item_name_english
                if lang_code ~= "en" and item_name_local then
                    search_data = search_data .. " " .. item_name_local
                end
                
                item_template = '<span class="item-nav-search-wrapper"><span class="item-nav-search-item" data-item-name="' .. search_data .. '">' .. item_template .. '</span>'
                table.insert(items, item_template)
            end
        end
    end
    
    table.sort(items)
    
    -- Append closing tags and bullets
    local total_items = #items
    for i = 1, total_items do
        if i == total_items then
            items[i] = items[i] .. '</span>'
        else
            items[i] = items[i] .. '<span class="item-nav-search-sep"> &bull; </span></span>'
        end
    end
    
    return table.concat(items, '')
end

-- Generates the full HTML item navbox recursively and efficiently
function p.generate_full_navbox(frame)
    local debug_mode = type(frame) == "table" and frame.args and frame.args["debug_mode"] or nil

    local html = {}
	table.insert(html, '<div class="itemnav-header"> [[Items]] <div class="item-nav-search-container"></div></div>\n')
    table.insert(html, '<div class="itemnav-container"><templatestyles src="Item Navbox/styles.css"/>\n')

    -- Maps Headers to Slots and distinct CSS Classes
    local categories = {
        { header = "Weapon",   slot = "Weapon", css = "itemnav-weapon" },
        { header = "Vitality", slot = "Armor",  css = "itemnav-vitality" },
        { header = "Spirit",   slot = "Tech",   css = "itemnav-tech" }
    }

    -- Maps tier boundaries and formatting
    local tiers = {
        { label = "{{Souls|800}}", min = 0, max = 801 },
        { label = "{{Souls|1600}}", min = 801, max = 1601 },
        { label = "{{Souls|3200}}", min = 1601, max = 3201 },
        { label = "{{Souls|6400}}", min = 3201, max = 6401 },
        { label = "[[Street Brawl|{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}]]", min = 6401, max = 99999, extra_css = "itemnav-legendary " }
    }

    for _, cat in ipairs(categories) do
        table.insert(html, '\n<div class="itemnav-type-header">\'\'\'' .. cat.header .. '\'\'\'</div>\n')
        table.insert(html, '<div class="itemnav-type-container">\n')

        for _, tier in ipairs(tiers) do
            local css_class = "itemnav-soul " .. (tier.extra_css or "") .. cat.css
            local items_wikitext = get_item_icons(cat.slot, tier.min, tier.max, nil)

            table.insert(html, '\n<div class="itemnav-tier-row">\n')
            table.insert(html, '<div class="' .. css_class .. '">' .. tier.label .. '</div><!--\n')
            table.insert(html, '--><div class="itemnav-cost-type-container">' .. items_wikitext .. '</div>\n')
            table.insert(html, '</div>\n')
        end

        table.insert(html, '\n</div>\n')
    end

    table.insert(html, '\n</div>')

    -- Only ONE preprocess call for the entire structure
    return process_debug_mode(table.concat(html), debug_mode)
end

-- Generates navbox with codenames
function p.generate_full_navbox_codenames(frame)
    local debug_mode = type(frame) == "table" and frame.args and frame.args["debug_mode"] or nil

    local html = {}
	table.insert(html, '<div class="itemnav-header"> [[Items]] <div class="item-nav-search-container"></div></div>\n')
    table.insert(html, '<div class="itemnav-container"><templatestyles src="Item Navbox/styles.css"/>\n')

    -- Maps Headers to Slots and distinct CSS Classes
    local categories = {
        { header = "Weapon",   slot = "Weapon", css = "itemnav-weapon" },
        { header = "Vitality", slot = "Armor",  css = "itemnav-vitality" },
        { header = "Spirit",   slot = "Tech",   css = "itemnav-tech" }
    }

    -- Maps tier boundaries and formatting
    local tiers = {
        { label = "{{Souls|800}}", min = 0, max = 801 },
        { label = "{{Souls|1600}}", min = 801, max = 1601 },
        { label = "{{Souls|3200}}", min = 1601, max = 3201 },
        { label = "{{Souls|6400}}", min = 3201, max = 6401 },
        { label = "[[Street Brawl|{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}]]", min = 6401, max = 99999, extra_css = "itemnav-legendary " }
    }

    for _, cat in ipairs(categories) do
        table.insert(html, '\n<div class="itemnav-type-header">\'\'\'' .. cat.header .. '\'\'\'</div>\n')
        table.insert(html, '<div class="itemnav-type-container">\n')

        for _, tier in ipairs(tiers) do
            local css_class = "itemnav-soul " .. (tier.extra_css or "") .. cat.css
            local items_wikitext = get_item_codenames(cat.slot, tier.min, tier.max, nil)

            table.insert(html, '\n<div class="itemnav-tier-row">\n')
            table.insert(html, '<div class="' .. css_class .. '">' .. tier.label .. '</div><!--\n')
            table.insert(html, '--><div class="itemnav-cost-type-container">' .. items_wikitext .. '</div>\n')
            table.insert(html, '</div>\n')
        end

        table.insert(html, '\n</div>\n')
    end

    table.insert(html, '\n</div>')

    -- Only ONE preprocess call for the entire structure
    return process_debug_mode(table.concat(html), debug_mode)
end

return p