Module:ItemData/nav: Difference between revisions

Jump to navigation Jump to search
No edit summary
Refactored for performance optimization
Line 5: Line 5:
local util_module = require('Module:Utilities')
local util_module = require('Module:Utilities')


--exceptions if the link is different from the item name
-- Exceptions if the link is different from the item name
local linkOverrides = {
local linkOverrides = {
     ["Bullet Lifesteal"] = "Bullet Lifesteal (item)",
     ["Bullet Lifesteal"] = "Bullet Lifesteal (item)",
Line 11: Line 11:
}
}


--With debug_mode on, it outputs unprocessed wikitext
-- Cached items grouped by slot to prevent redundant full-table evaluations
--With debug_mode off/unspecified, it processes the wikitext
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)
local function process_debug_mode(wikitext, debug_mode)
if debug_mode == 'true' then
    if debug_mode == 'true' then
return wikitext
        return wikitext
elseif debug_mode == 'false' or debug_mode == nil then
    elseif debug_mode == 'false' or debug_mode == nil then
return mw.getCurrentFrame():preprocess(wikitext)
        return mw.getCurrentFrame():preprocess(wikitext)
else
    else
return "debug_mode must be 'true' or 'false'"
        return "debug_mode must be 'true' or 'false'"
end
    end
end
end


--Writes list of items of a certain slot within the min and max soul bounds
-- Writes list of items of a certain slot within the min and max soul bounds
-- Each item is wrapped and separated. For example of wrapping/separator, see
-- get_item_nav_bulletpoints. 'sep' should not be combined with 'right_wrap',
-- as the trailing separator should also be removed from the string
-- filter_mode parameter: nil/"all", "exclude_street_brawl", or "street_brawl_only"
local function write_wrapped_item_list(slot, min_souls, max_souls, template, sep, filter_mode)
local function write_wrapped_item_list(slot, min_souls, max_souls, template, sep, filter_mode)
     if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
     if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
Line 37: Line 47:
     if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
     if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
      
      
    -- Normalize filter_mode to handle unset parameter
     filter_mode = filter_mode or "all"
     filter_mode = filter_mode or "all"
      
      
     -- Retrieve all items that fit the bounds and filter criteria
     -- Lifted out of the loop: Language code doesn't change per item
    local lang_code = lang_module.get_lang_code()
   
     local items = {}
     local items = {}
     for item_key, item_data in pairs(items_data) do
    local slot_items = get_items_by_slot(slot) -- Drastically reduces search pool size
         -- future proofing; Disabled will be renamed to IsDisabled soon
   
     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 this_cost = tonumber(item_data["Cost"])
        local this_slot = item_data["Slot"]
       
        -- Filter logic for Street Brawl items
         local is_street_brawl = item_data["StreetBrawl"] == true
         local is_street_brawl = item_data["StreetBrawl"] == true
         local should_include = true
         local should_include = true
          
          
        -- Filter logic for Street Brawl items
         if filter_mode == "street_brawl_only" and not is_street_brawl then
         if filter_mode == "street_brawl_only" and not is_street_brawl then
             should_include = false
             should_include = false
Line 57: Line 70:
         end
         end
          
          
         if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil and this_slot ~= nil then
         if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil then
             if slot == this_slot and this_cost >= min_souls and this_cost < max_souls then
             if this_cost >= min_souls and this_cost < max_souls then
                 local item_name_english = lang_module.get_string(item_key, "en") -- Get the English name
                 local item_name_english = lang_module.get_string(item_key, "en")
                 local item_link = linkOverrides[item_name_english] or item_name_english -- Use override link if available
                 local item_link = linkOverrides[item_name_english] or item_name_english
                local lang_code = lang_module.get_lang_code() -- Get the language code from the subpage
                 local item_name_local = lang_module.get_string(item_key, lang_code)
                 local item_name_local = lang_module.get_string(item_key, lang_code) -- Get the localized name
                  
                  
                -- Construct the template based on the type
                 local item_template
                 local item_template
                 if template == "ItemIcon" then
                 if template == "ItemIcon" then
                     if lang_code == "en" then
                     if lang_code == "en" then
                        -- For English pages, do not include lang or localized name
                         item_template = "{{ItemIcon|" .. item_name_english .. "|size=26px}}"
                         item_template = "{{ItemIcon|" .. item_name_english .. "|size=26px}}"
                     else
                     else
                        -- For non-English pages, include lang and localized name
                         item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                         item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|l1=" .. item_name_local .. "|size=26px}}"
                     end
                     end
                      
                      
                    -- Build data attribute for searching (includes both English and localized names)
                     local search_data = item_name_english
                     local search_data = item_name_english
                     if lang_code ~= "en" and item_name_local then
                     if lang_code ~= "en" and item_name_local then
Line 81: Line 89:
                     end
                     end
                      
                      
                    -- Wrap item in an open searchable span (closing tags and bullets added post-sort)
                     item_template = '<span class="item-nav-search-wrapper"><span class="item-nav-search-item" data-item-name="' .. search_data .. '">' .. item_template .. '</span>'
                     item_template = '<span class="item-nav-search-wrapper"><span class="item-nav-search-item" data-item-name="' .. search_data .. '">' .. item_template .. '</span>'
                 elseif template == "ItemBox" then
                 elseif template == "ItemBox" then
                     if lang_code == "en" then
                     if lang_code == "en" then
                        -- For English pages, do not include item_loc or link
                         if is_street_brawl then
                        -- Display cost as "Legendary" for street brawl items
                            item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. item_link .. "|overrideTier=5}}"
                         if is_street_brawl == true then
item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. item_link .. "|overrideTier=5" .. "}}"
                         else
                         else
                             item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. item_link .. "}}"
                             item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. item_link .. "}}"
                         end
                         end
                     else
                     else
                        -- For non-English pages, include item_loc and link
                         local link_local = item_link .. "/" .. lang_code
                         local link_local = item_link .. "/" .. lang_code -- Add subpage for non-English languages
                         if is_street_brawl then
                         if is_street_brawl == true then
                             item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. link_local .. "|item_price={{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}"
                             item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. link_local ..
                            "|item_price={{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}" .. "}}"
                         else
                         else
                        item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. link_local .. "}}"
                            item_template = "{{ItemBox|itemName=" .. item_name_english .. "|overrideLinkClick=" .. link_local .. "}}"
                         end
                         end
                     end
                     end
Line 111: Line 114:
      
      
     -- Order list alphabetically
     -- Order list alphabetically
     table.sort(items) -- O(nlogn)
     table.sort(items)
      
      
     -- Append closing tags and bullets (omitting the bullet for the last item)
     -- Append closing tags and bullets
     if template == "ItemIcon" then
     if template == "ItemIcon" then
         for i = 1, #items do
        local total_items = #items
             if i == #items then
         for i = 1, total_items do
             if i == total_items then
                 items[i] = items[i] .. '</span>'
                 items[i] = items[i] .. '</span>'
             else
             else
Line 124: Line 128:
     end
     end
      
      
     -- Add each item to output
     return table.concat(items, sep)
    local ret = table.concat(items, sep)
   
    return ret
end
end


-- for [[Template:Item Navbox]]
-- for [[Template:Item Navbox]]
-- Supports filter parameter via frame.args["filter"]
function p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, filter_mode)
function p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, filter_mode)
    -- Handle the case where it's called via #invoke (i.e., from wikitext)
     if type(slot) == "table" and slot.args then
     if type(slot) == "table" and slot.args then
         local frame = slot
         local frame = slot
Line 142: Line 141:
         filter_mode = frame.args["filter"]
         filter_mode = frame.args["filter"]
     end
     end
      
     local sep = ''
     local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemIcon", '', filter_mode)
     local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemIcon", sep, filter_mode)
     return process_debug_mode(item_list, debug_mode)
     return process_debug_mode(item_list, debug_mode)
end
end


-- for [[Template:Infobox ShopItems]]
-- for [[Template:Infobox ShopItems]]
-- Supports filter parameter via frame.args["filter"]
function p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, filter_mode)
function p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, filter_mode)
    -- Handle the case where it's called via #invoke (i.e., from wikitext)
     if type(slot) == "table" and slot.args then
     if type(slot) == "table" and slot.args then
         local frame = slot
         local frame = slot
Line 162: Line 156:
         filter_mode = frame.args["filter"]
         filter_mode = frame.args["filter"]
     end
     end
      
     local sep = ' '
     local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemBox", ' ', filter_mode)
     local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemBox", sep, filter_mode)
     return process_debug_mode(item_list, debug_mode)
     return process_debug_mode(item_list, debug_mode)
end
end


-- for [[Template:Item Navbox]] subgroup rows
-- for [[Template:Item Navbox]] subgroup rows
-- Writes price tiers and appends Street Brawl section if items exist
function p.write_item_slot_subgroup(frame)
function p.write_item_slot_subgroup(frame)
local slot = frame.args[1]
    local slot = frame.args[1]
local type = frame.args[2]
    local type_func = frame.args[2]
local debug_mode = frame.args['debug_mode']
    local debug_mode = frame.args['debug_mode']
local street_brawl_label = frame.args['street_brawl_label'] or '{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}'
    local street_brawl_label = frame.args['street_brawl_label'] or '{{Legendary|{{#invoke:Lang|get_string|Citadel_ItemDraft_Legendary}}}}'
   
if slot == nil then return "'slot' parameter is required" end
    if slot == nil then return "'slot' parameter is required" end
   
-- Define base args
    local template_args = {
local template_title = "Navbox subgroup"
        ["groupstyle"] = "background-color:" .. util_module.get_slot_color(slot) .. ";width:10%;min-width:70px;border-radius: 8px 0 0 8px",
local template_args = {
        ["grouppadding"] = "5px",
["groupstyle"] = "background-color:" .. util_module.get_slot_color(slot) .. ";width:10%;min-width:70px;border-radius: 8px 0 0 8px",
        ["listpadding"] = "0 0.25rem",
["grouppadding"] = "5px",
    }
["listpadding"] = "0 0.25rem",
   
}
    local soul_style = "font-size: 12px; text-shadow: 1px 1px rgba(0, 0, 0, 0.3);"
    local prices = generic_module.get_item_price_per_tier()
local soul_style = "font-size: 12px; text-shadow: 1px 1px rgba(0, 0, 0, 0.3);"
    local group_index = 1
local prices = generic_module.get_item_price_per_tier()
   
local group_index = 1
    for i, souls in ipairs(prices) do
        local min_souls = souls
for i, souls in ipairs(prices) do
       
min_souls = souls
        if min_souls ~= 0 then  
            local max_souls = prices[i+1] or (min_souls * 10)
--Skip 0 to i1 as no items cost less than 500
           
if min_souls ~= 0 then  
            local list
-- Determine upper bound for soul tier
            if type_func == 'get_item_nav_cards' then
max_souls = prices[i+1]
                list = p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
if max_souls == nil then
            elseif type_func == 'get_item_nav_bulletpoints' then
max_souls = min_souls * 10
                list = p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
end
            else
                return "'type' should be get_item_nav_cards or get_item_nav_bulletpoints"
-- Generate list for this price tier, excluding Street Brawl items
            end
local list
           
if type=='get_item_nav_cards' then
            if list and list ~= "" then
list = p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
                template_args["group" .. group_index] = frame:expandTemplate{title="Souls", args={[1] = min_souls, ["Shadow"] = soul_style}}
elseif type=='get_item_nav_bulletpoints' then
                template_args["list" .. group_index] = list
list = p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
                group_index = group_index + 1
else
            end
return "'type' should be get_item_nav_cards or get_item_nav_bulletpoints"
        end
end
    end
   
-- Only add the group if there are items in this tier
    local sb_list
if list and list ~= "" then
    if type_func == 'get_item_nav_cards' then
template_args["group" .. group_index] = frame:expandTemplate{title="Souls", args={[1] = min_souls, ["Shadow"] = soul_style}}
        sb_list = p.get_item_nav_cards(slot, 0, 999999, debug_mode, "street_brawl_only")
template_args["list" .. group_index] = list
    elseif type_func == 'get_item_nav_bulletpoints' then
group_index = group_index + 1
        sb_list = p.get_item_nav_bulletpoints(slot, 0, 999999, debug_mode, "street_brawl_only")
end
    end
end
   
end
    if sb_list and sb_list ~= "" then
        local sb_style = "color:#98ffde; font-family:'Retail Demo', sans-serif; text-wrap: nowrap; text-shadow: 0px 0px 2px #2c312e, 1.3px 1.3px rgba(0, 0, 0, 0.2);padding:5px;"
-- Add Street Brawl section at the bottom of the slot
        local sb_link_text = "[[Street Brawl{{if_lang}}|<span style=\"" .. sb_style .. "\"><b>" .. street_brawl_label .. "</b></span>]]"
local sb_min = 0
        template_args["group" .. group_index] = frame:preprocess(sb_link_text)
local sb_max = 999999
        template_args["list" .. group_index] = sb_list
    end
local sb_list
   
if type=='get_item_nav_cards' then
    return frame:expandTemplate{title="Navbox subgroup", args=template_args}
sb_list = p.get_item_nav_cards(slot, sb_min, sb_max, debug_mode, "street_brawl_only")
elseif type=='get_item_nav_bulletpoints' then
sb_list = p.get_item_nav_bulletpoints(slot, sb_min, sb_max, debug_mode, "street_brawl_only")
end
-- Only add Street Brawl group if there are items for this slot
if sb_list and sb_list ~= "" then
-- Style the link to match other tier headers with white text inside the link to prevent visited link color
local sb_style = "color:#98ffde; font-family:'Retail Demo', sans-serif; text-wrap: nowrap; text-shadow: 0px 0px 2px #2c312e, 1.3px 1.3px rgba(0, 0, 0, 0.2);padding:5px;"
local sb_link_text = "[[Street Brawl{{if_lang}}|<span style=\"" .. sb_style .. "\"><b>" .. street_brawl_label .. "</b></span>]]"
template_args["group" .. group_index] = frame:preprocess(sb_link_text)
template_args["list" .. group_index] = sb_list
end
return frame:expandTemplate{title=template_title, args=template_args}
end
end


Line 249: Line 224:
     local active_items = {}
     local active_items = {}
      
      
    -- 1. Collect active items and their cost
     for item_key, item_data in pairs(items_data) do
     for item_key, item_data in pairs(items_data) do
        -- Convert cost to a number; will be nil if the JSON value is null or missing
         local cost = tonumber(item_data["Cost"])
         local cost = tonumber(item_data["Cost"])
       
         if item_data["Name"] and  
         if item_data["Name"] and  
           item_data["IsDisabled"] == false and  
           item_data["IsDisabled"] == false and  
           cost ~= nil and cost > 0 and             -- FILTER: Must have a cost greater than 0
           cost ~= nil and cost > 0 and  
           item_data["Activation"] and  
           item_data["Activation"] and  
           item_data["Activation"] ~= "Passive" then
           item_data["Activation"] ~= "Passive" then
Line 270: Line 242:
     end
     end
      
      
    -- 2. Sort by name
     table.sort(active_items, function(a, b) return a.name < b.name end)
     table.sort(active_items, function(a, b) return a.name < b.name end)
      
      
     -- 3. Generate wiki markup
     -- Optimized string compilation using a structural buffer table
     local wikitext = [=[{| class="wikitable mw-collapsible sortable" style="width:100%"  
     local rows = {}
    table.insert(rows, [=[{| class="wikitable mw-collapsible sortable" style="width:100%"  
|+{{#invoke:Dictionary|translate|Active Items}}
|+{{#invoke:Dictionary|translate|Active Items}}
! colspan="2" | {{#invoke:Lang|get_string|Citadel_HeroBuilds_CategoryNameLabel}}
! colspan="2" | {{#invoke:Lang|get_string|Citadel_HeroBuilds_CategoryNameLabel}}
Line 280: Line 252:
! {{Souls|{{#invoke:Dictionary|translate|Cost}}}}
! {{Souls|{{#invoke:Dictionary|translate|Cost}}}}
! {{#invoke:Lang|get_string|Citadel_Mod_Tooltip_Active}}
! {{#invoke:Lang|get_string|Citadel_Mod_Tooltip_Active}}
! {{#invoke:Lang|get_string|AbilityCooldown_label}}
! {{#invoke:Lang|get_string|AbilityCooldown_label}}]=])
]=]
      
      
    -- 4. Add rows for each item
     for _, item in ipairs(active_items) do
     for _, item in ipairs(active_items) do
        -- Determine background color and item type
         local bg_color, item_type
         local bg_color, item_type
         if item.slot == "Armor" then
         if item.slot == "Armor" then
Line 298: Line 267:
         end
         end
          
          
         -- The cost cell uses 'data-sort-value' to provide a clean number for the client-side sorter,
         local row = string.format([=[
        -- while the cell's visible content is formatted by the {{Souls}} template.
        wikitext = wikitext .. "\n" .. string.format([=[
|-
|-
! style="background-color:%s" | [[File:%s.png|64x64px]]
! style="background-color:%s" | [[File:%s.png|64x64px]]
Line 307: Line 274:
| data-sort-value="%d" style="text-align:center;font-size:15px" | {{Souls|%d}}
| data-sort-value="%d" style="text-align:center;font-size:15px" | {{Souls|%d}}
| {{#invoke:Utilities|process_variables|%s_active_desc|%s}}{{#invoke:Utilities|process_variables|%s_desc|%s}}{{#invoke:Utilities|process_variables|%s_active|%s}}
| {{#invoke:Utilities|process_variables|%s_active_desc|%s}}{{#invoke:Utilities|process_variables|%s_desc|%s}}{{#invoke:Utilities|process_variables|%s_active|%s}}
| style="text-align:center;font-size:16px" | {{#invoke:ItemData|get_prop|%s|AbilityCooldown}}s
| style="text-align:center;font-size:16px" | {{#invoke:ItemData|get_prop|%s|AbilityCooldown}}s]=],
]=],
             bg_color,  
             bg_color,  
             item.name,  
             item.name,  
Line 316: Line 282:
             item.key, item.name, item.key, item.name, item.key, item.name,
             item.key, item.name, item.key, item.name, item.key, item.name,
             item.name)
             item.name)
           
        table.insert(rows, row)
     end
     end
      
      
     wikitext = wikitext .. "\n|}"
     table.insert(rows, "\n|}")
   
     return frame:preprocess(table.concat(rows, "\n"))
     return frame:preprocess(wikitext)
end
end


return p
return p