Module:Icon: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
mNo edit summary
LVL (talk | contribs)
mNo edit summary
 
(26 intermediate revisions by 6 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Load language codes
local iconDataRaw = mw.loadJsonData("Data:ResourceLookup.json")
local iconData2 = mw.loadJsonData("Data:ResourceLookup2.json")
local dictionary = mw.loadJsonData("Data:Dictionary")
 
local iconData = {}
 
for k, v in pairs(iconDataRaw) do
    iconData[k] = v
end
 
for k, v in pairs(iconData2) do
    iconData[k] = v
end
 
local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local lang_module = require("Module:Lang")
local table_data = require("Module:Icon/data")


-- Cache for compiled icon data (persists during page render)
local cachedLangCode
local iconDataCache = nil
local cachedLangCode, cachedLangData


-- Cache for expensive existence checks (persists during page render)
local linkOverrides = table_data.linkOverrides
local existenceCache = {}
local legacyItems = table_data.legacyItems
local missingImageOverrides = table_data.missingImageOverrides
local aliases = table_data.aliases
local renames = table_data.renames


-- Heroes/Items that don't have images uploaded yet
local function getLangCode()
local missingImageOverrides = {
     if cachedLangCode then
     -- Unreleased Heroes
        return cachedLangCode
    ["boho"] = "Module-Icon missing.png",
     end
     ["druid"] = "Module-Icon missing.png",
    ["fortuna"] = "Module-Icon missing.png",
    ["graf"] = "Module-Icon missing.png",
    ["gunslinger"] = "Module-Icon missing.png",
    ["skyrunner"] = "Module-Icon missing.png",
    ["swan"] = "Module-Icon missing.png",
    ["thumper"] = "Module-Icon missing.png",
    -- Add any others that show red links
}


-- Build unified lookup table from bot-uploaded JSONs
    local title = mw.title.getCurrentTitle().fullText
local function buildIconData()
    local subpageLang = title:match("/([^/]+)$")
     if iconDataCache then
    local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en"
         return iconDataCache
 
    cachedLangCode = langCode
    return langCode
end
 
-- Infer image path from name, with override support for missing images
local function getImage(iconInfo, args)
    local lowerName = iconInfo.name:lower()
 
     if args.image then
         return args.image
     end
     end
   
 
    local lookup = {}
     return missingImageOverrides[lowerName] or (iconInfo.name .. ".png")
   
end
    -- Load bot data
 
mw.log('Loading hero/ability/item data')
-- Get the translated name
    local heroData = mw.loadJsonData("Data:HeroData.json")
local function getTranslatedName(iconInfo, langCode)
    local abilityData = mw.loadJsonData("Data:AbilityData.json")
    if langCode == "en" then
    local itemData = mw.loadJsonData("Data:ItemData.json")
        return iconInfo.name
   
    -- Track which hero owns which ability (for linking)
    local abilityToHero = {}
      
    -- Process Heroes
    for heroKey, hero in pairs(heroData) do
        if type(hero) == "table" and hero.Name then
            local name = hero.Name
            local lowerName = name:lower()
           
            -- Check for missing image override
            local imageName = missingImageOverrides[lowerName] or (name .. ".png")
           
            lookup[lowerName] = {
                name = name,
                key = heroKey,
                link = name,
                image = imageName,
                type = "hero"
            }
           
            -- Map abilities to parent hero
            if hero.BoundAbilities then
                for slot, ability in pairs(hero.BoundAbilities) do
                    if ability.Key then
                        abilityToHero[ability.Key] = {
                            heroName = name,
                            abilityName = ability.Name
                        }
                    end
                end
            end
        end
     end
     end
      
 
-- Process Abilities
     -- 1. Direct key
     for abilityKey, ability in pairs(abilityData) do
     if iconInfo.key and iconInfo.key ~= "" then
        if type(ability) == "table" and ability.Name then
        local translated = lang_module.get_string(iconInfo.key, langCode, iconInfo.name
            local name = ability.Name
        )
            local lowerName = name:lower()
 
            local parentInfo = abilityToHero[abilityKey]
        if type(translated) == "string" then
           
            return (translated:gsub("<[^>]+>", ""))
            -- If we already have this ability name, prefer the one linked to a hero
            if lookup[lowerName] and lookup[lowerName].type == "ability" then
                local existingHasParent = lookup[lowerName].link ~= lookup[lowerName].name
                local newHasParent = parentInfo ~= nil
               
                -- Skip if existing is linked to hero but this one isn't (prevents orphans from overwriting real ones)
                if existingHasParent and not newHasParent then
                    -- Skip this orphan ability
                else
                    -- Overwrite: new has parent and existing doesn't, or both/neither have parents
                    local link = parentInfo and parentInfo.heroName or name
                    lookup[lowerName] = {
                        name = name,
                        key = abilityKey,
                        link = link,
                        image = name .. ".png",
                        type = "ability",
                        class = "theme"
                    }
                end
            else
                -- No existing entry, add normally
                local link = parentInfo and parentInfo.heroName or name
                lookup[lowerName] = {
                    name = name,
                    key = abilityKey,
                    link = link,
                    image = name .. ".png",
                    type = "ability",
                    class = "theme"
                }
            end
         end
         end
        return iconInfo.name
     end
     end
   
 
     -- Process Items
     -- 2. Dictionary lookup
     for itemKey, item in pairs(itemData) do
     if iconInfo.key_dictionary and iconInfo.key_dictionary ~= "" then
         if type(item) == "table" and item.Name then
        local entry = dictionary[iconInfo.key_dictionary]
             local name = item.Name
 
            local lowerName = name:lower()
         if type(entry) == "table" then
           
             return entry[langCode]
            lookup[lowerName] = {
                 or entry.en
                 name = name,
                 or iconInfo.name
                 key = itemKey,
                link = name,
                image = name .. ".png",
                type = "item"
            }
         end
         end
     end
     end
   
 
     iconDataCache = lookup
     -- No valid key > fallback
     return lookup
     return iconInfo.name
end
end


-- Get icon data by name (with alias support)
-- Infer link from type and available fields
local function getIconData(iconName)
local function getLink(iconInfo, langCode)
     local data = buildIconData()
     if iconInfo.type == "ability" then
    local lowerName = iconName:lower()
        local hero = iconInfo.hero_name or iconInfo.name
   
       
    -- Check main lookup
        -- Get the translated name for the specific ability
    if data[lowerName] then
        local abilityName = getTranslatedName(iconInfo, langCode)
         return data[lowerName], nil
       
        -- Format for URL: replace spaces with underscores
        local anchor = abilityName:gsub(" ", "_")
       
         return hero .. "#" .. anchor
     end
     end
   
 
    -- Check aliases (for edge cases like "doorman" -> "the doorman")
     return linkOverrides[iconInfo.name] or iconInfo.name
    local aliases = {
        ["doorman"] = "the doorman",
        ["mo and krill"] = "mo & krill",
        ["mo"] = "mo & krill",
        ["krill"] = "mo & krill",
        ["curse"] = "cursed relic",
        ["debuff remover"] = "dispel magic"
     }
   
    if aliases[lowerName] then
        return data[aliases[lowerName]], nil
    end
   
    return nil, string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", iconName)
end
end


-- Language handling
-- Returns the canonical display name for a given user input
local function getLangData()
-- Used by templates to store the correct casing in data attributes
     if cachedLangData then
-- Normalizes casing so exact string matches work reliably in Lua modules
         return cachedLangCode, cachedLangData
function p.getCanonicalName(frame)
    local name = frame.args[1] or ""
    local lowerName = name:lower()
 
     if renames[lowerName] then
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
         lowerName = aliases[lowerName]
     end
     end


     local title = mw.title.getCurrentTitle().fullText
     local iconInfo = iconData[lowerName] or legacyItems[lowerName]
    local subpageLang = title:match("/([^/]+)$")
    local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en"


     local langData = {}
     -- If unknown, return original input
     if langCode ~= "en" then
     if not iconInfo then
         local success, data = pcall(function()
         return name
            return mw.loadJsonData("Data:Lang " .. langCode .. ".json")
        end)
        if success and type(data) == "table" then
            langData = data
        end
     end
     end


     cachedLangCode = langCode
-- Quick hack: ability names are used by MediaWiki:Gadget-infobox-tooltip.js and expected to be localized
     cachedLangData = langData
     local langCode = getLangCode()
 
    if iconInfo.type == "ability" then
        return getTranslatedName(iconInfo, langCode)
     end


     return langCode, langData
     return iconInfo.name
end
end


-- Main render function
local function render(name, args)
function p.render(frame)
     local originalName = name
     local args = frame:getParent().args
    local lowerName = name:lower()
     local name = args[1] or ""
    args = args or {}
     local customText = args.l1 or ""
 
      
    if renames[lowerName] then
    -- Set Defaults
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
        lowerName = aliases[lowerName]
    end
 
    -- Lookup live data > legacy fallback
     local iconInfo = iconData[lowerName] or legacyItems[lowerName]
 
    -- Throw error if not found
    if not iconInfo then
        return string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", name)
    end
 
     local customText = args.l1 ~= "" and args.l1 or nil
     local customLink = args.link ~= "" and args.link or nil
     local size = args.size or "20px"
     local size = args.size or "20px"
     local noLink = args["no-link"] == "true"
     local noLink = args["no-link"] == "true"
    local noFile = args["no-file"] == "true"
     local iconOnly = args["icon-only"] == "true"
     local iconOnly = args["icon-only"] == "true"


    -- Scan unnamed parameters
     for k, v in pairs(args) do
     for k, v in pairs(args) do
         if type(k) == "number" and k > 1 then
         if type(k) == "number" and k > 1 then
             local val = mw.text.trim(v)
             local val = mw.text.trim(v)
           
             if val == "icon-only" then
             if val == "icon-only" then
                 iconOnly = true
                 iconOnly = true
             elseif val == "no-link" then
             elseif val == "no-link" then
                 noLink = true
                 noLink = true
             elseif val:match("^%d+px$") then  
             elseif val:match("^%d+px$") then
                 size = val
                 size = val
             end
             end
Line 214: Line 170:
     end
     end


    -- Get icon data automatically from JSON
     local langCode = getLangCode()
     local iconData, err = getIconData(name)
    if not iconData then
        return "Error: " .. err
    end


    local langCode, langData = getLangData()
     local displayName
   
     if customText then
    -- Determine display name
     local displayName = ""
     if customText ~= "" then
         displayName = customText
         displayName = customText
     elseif iconData.key and langData[iconData.key] then
     elseif renames[name:lower()] then
         displayName = langData[iconData.key]
        -- Preserve original if renamed
         displayName = originalName
     else
     else
         displayName = iconData.name
         displayName = getTranslatedName(iconInfo, langCode)
    end
 
    local baseLink = customLink or getLink(iconInfo, langCode)
    local fragment = ""
 
    -- Split the fragment (#Abilities) from the base link
    if baseLink:find("#") then
        local parts = mw.text.split(baseLink, "#", true)
        baseLink = parts[1]
        fragment = "#" .. (parts[2] or "")
     end
     end


    -- Localize link if on translated subpage
    local baseLink = iconData.link or ""
     local link = baseLink
     local link = baseLink
   
 
     if langCode ~= "en" and baseLink ~= "" then
     if not customLink and langCode ~= "en" and baseLink ~= "" then
         link = baseLink .. "/" .. langCode
         link = baseLink .. "/" .. langCode
     end
     end


     -- Style logic (only abilities get theme class by default)
     -- Reattach fragment at the end
     local class = iconData.class or ""
     link = link .. fragment
 
    -- Add CSS class for abilities
     local style
     local style
     if class:find("theme", 1, true) then
     if iconInfo.type == "ability" then
         style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
         style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
     else
     else
         style = 'style="position:relative; bottom:2px;"'
         style = 'class="c-icon" style="position:relative; bottom:2px;"'
     end
     end


    -- Generate icon HTML
     local imageLink = noLink and "" or link
     local imageLink = noLink and "" or link
     local iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, iconData.image, size, imageLink)
     local iconHtml = ""
 
    if not noFile then
        local image = getImage(iconInfo, args)
        iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, image, size, imageLink)
    end


     if iconOnly or displayName == "" then
     if iconOnly or displayName == "" then
Line 258: Line 222:


     local textHtml = (noLink or link == "") and displayName or string.format('[[%s|%s]]', link, displayName)
     local textHtml = (noLink or link == "") and displayName or string.format('[[%s|%s]]', link, displayName)
     return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
 
     if iconHtml ~= "" then
        return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
    else
        return textHtml
    end
end
 
-- Template entrypoint
function p.render(frame)
    local args = frame:getParent().args
    local name = args[1] or ""
 
    return render(name, args)
end
 
-- Module entrypoint
function p._render(name, args)
    return render(name, args)
end
end


return p
return p

Latest revision as of 14:07, 29 July 2026

Main module for rendering game icons.

Note: This module is not intended for direct use. Use the templates below.

Data sources

[edit source]

The module reads from Data:ResourceLookup.json, a pre-indexed lookup table generated by Deadbot at deploy time. When the bot uploads new game files, hero, ability, and item icons update automatically.

An additional resource for other icons can be found at Data:ResourceLookup2.json, a manually maintained table; some entries may rely on a dictionary key.

See Module:Icon/data for manual overrides, legacy items, aliases, and renames used by this module.

Any of the following templates can be used. The module automatically detects the icon type:

Note: While all four templates work identically, it is recommended to use the appropriate one for clarity (e.g., use {{AbilityIcon|Siphon Life}} even though {{HeroIcon|Siphon Life}} will also work).

Error tracking

[edit source]

Icons that fail to render are added to Category:Module:Icon ERROR.


local p = {}

local iconDataRaw = mw.loadJsonData("Data:ResourceLookup.json")
local iconData2 = mw.loadJsonData("Data:ResourceLookup2.json")
local dictionary = mw.loadJsonData("Data:Dictionary")

local iconData = {}

for k, v in pairs(iconDataRaw) do
    iconData[k] = v
end

for k, v in pairs(iconData2) do
    iconData[k] = v
end

local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local lang_module = require("Module:Lang")
local table_data = require("Module:Icon/data")

local cachedLangCode

local linkOverrides = table_data.linkOverrides
local legacyItems = table_data.legacyItems
local missingImageOverrides = table_data.missingImageOverrides
local aliases = table_data.aliases
local renames = table_data.renames

local function getLangCode()
    if cachedLangCode then
        return cachedLangCode
    end

    local title = mw.title.getCurrentTitle().fullText
    local subpageLang = title:match("/([^/]+)$")
    local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en"

    cachedLangCode = langCode
    return langCode
end

-- Infer image path from name, with override support for missing images
local function getImage(iconInfo, args)
    local lowerName = iconInfo.name:lower()

    if args.image then
        return args.image
    end

    return missingImageOverrides[lowerName] or (iconInfo.name .. ".png")
end

-- Get the translated name
local function getTranslatedName(iconInfo, langCode)
    if langCode == "en" then
        return iconInfo.name
    end

    -- 1. Direct key
    if iconInfo.key and iconInfo.key ~= "" then
        local translated = lang_module.get_string(iconInfo.key, langCode, iconInfo.name
        )

        if type(translated) == "string" then
            return (translated:gsub("<[^>]+>", ""))
        end

        return iconInfo.name
    end

    -- 2. Dictionary lookup
    if iconInfo.key_dictionary and iconInfo.key_dictionary ~= "" then
        local entry = dictionary[iconInfo.key_dictionary]

        if type(entry) == "table" then
            return entry[langCode]
                or entry.en
                or iconInfo.name
        end
    end

    -- No valid key > fallback
    return iconInfo.name
end

-- Infer link from type and available fields
local function getLink(iconInfo, langCode)
    if iconInfo.type == "ability" then
        local hero = iconInfo.hero_name or iconInfo.name
        
        -- Get the translated name for the specific ability
        local abilityName = getTranslatedName(iconInfo, langCode)
        
        -- Format for URL: replace spaces with underscores
        local anchor = abilityName:gsub(" ", "_")
        
        return hero .. "#" .. anchor
    end

    return linkOverrides[iconInfo.name] or iconInfo.name
end

-- Returns the canonical display name for a given user input
-- Used by templates to store the correct casing in data attributes
-- Normalizes casing so exact string matches work reliably in Lua modules
function p.getCanonicalName(frame)
    local name = frame.args[1] or ""
    local lowerName = name:lower()

    if renames[lowerName] then
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
        lowerName = aliases[lowerName]
    end

    local iconInfo = iconData[lowerName] or legacyItems[lowerName]

    -- If unknown, return original input
    if not iconInfo then
        return name
    end

	-- Quick hack: ability names are used by MediaWiki:Gadget-infobox-tooltip.js and expected to be localized
    local langCode = getLangCode()

    if iconInfo.type == "ability" then
        return getTranslatedName(iconInfo, langCode)
    end

    return iconInfo.name
end

local function render(name, args)
    local originalName = name
    local lowerName = name:lower()
    args = args or {}

    if renames[lowerName] then
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
        lowerName = aliases[lowerName]
    end

    -- Lookup live data > legacy fallback
    local iconInfo = iconData[lowerName] or legacyItems[lowerName]

    -- Throw error if not found
    if not iconInfo then
        return string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", name)
    end

    local customText = args.l1 ~= "" and args.l1 or nil
    local customLink = args.link ~= "" and args.link or nil
    local size = args.size or "20px"
    local noLink = args["no-link"] == "true"
    local noFile = args["no-file"] == "true"
    local iconOnly = args["icon-only"] == "true"

    for k, v in pairs(args) do
        if type(k) == "number" and k > 1 then
            local val = mw.text.trim(v)
            if val == "icon-only" then
                iconOnly = true
            elseif val == "no-link" then
                noLink = true
            elseif val:match("^%d+px$") then
                size = val
            end
        end
    end

    local langCode = getLangCode()

    local displayName
    if customText then
        displayName = customText
    elseif renames[name:lower()] then
        -- Preserve original if renamed
        displayName = originalName
    else
        displayName = getTranslatedName(iconInfo, langCode)
    end

    local baseLink = customLink or getLink(iconInfo, langCode)
    local fragment = ""

    -- Split the fragment (#Abilities) from the base link
    if baseLink:find("#") then
        local parts = mw.text.split(baseLink, "#", true)
        baseLink = parts[1]
        fragment = "#" .. (parts[2] or "")
    end

    local link = baseLink

    if not customLink and langCode ~= "en" and baseLink ~= "" then
        link = baseLink .. "/" .. langCode
    end

    -- Reattach fragment at the end
    link = link .. fragment

    -- Add CSS class for abilities
    local style
    if iconInfo.type == "ability" then
        style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
    else
        style = 'class="c-icon" style="position:relative; bottom:2px;"'
    end

    local imageLink = noLink and "" or link
    local iconHtml = ""

    if not noFile then
        local image = getImage(iconInfo, args)
        iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, image, size, imageLink)
    end

    if iconOnly or displayName == "" then
        return iconHtml
    end

    local textHtml = (noLink or link == "") and displayName or string.format('[[%s|%s]]', link, displayName)

    if iconHtml ~= "" then
        return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
    else
        return textHtml
    end
end

-- Template entrypoint
function p.render(frame)
    local args = frame:getParent().args
    local name = args[1] or ""

    return render(name, args)
end

-- Module entrypoint
function p._render(name, args)
    return render(name, args)
end

return p