Module:AbilityTable: Difference between revisions

Vergir (talk | contribs)
Create AbilityTables module for data-driven ability tables using GameData framework (with help from vergir-bot LLM)
 
Vergir (talk | contribs)
Render Hero and Ability columns using HeroIcon and AbilityIcon, with hero lookup from AbilityCards (with help from vergir-bot LLM)
Line 19: Line 19:
     ["charges"] = { "AbilityCharges" },
     ["charges"] = { "AbilityCharges" },
}
}
-- ============================================================
-- Hero lookup (ability key -> hero display name)
-- Built lazily from AbilityCards.json and HeroData.json.
-- ============================================================
local _hero_lookup = nil
local function build_hero_lookup()
    if _hero_lookup then return _hero_lookup end
    _hero_lookup = {}
    -- Build set of active hero display names from HeroData
    local hero_data = mw.loadJsonData("Data:HeroData.json")
    local active_heroes = {}
    for _, hero in pairs(hero_data) do
        if type(hero) == "table"
            and hero["IsDisabled"] == false
            and type(hero["Name"]) == "string"
            and (hero["IsSelectable"] == nil or hero["IsSelectable"] ~= false)
        then
            active_heroes[hero["Name"]] = true
        end
    end
    -- Map ability key -> { heroName, abilityNum } using AbilityCards
    local cards = mw.loadJsonData("Data:AbilityCards.json")
    for _, hero_entry in pairs(cards) do
        if type(hero_entry) == "table" then
            local hero_name = hero_entry["Name"]
            if hero_name and active_heroes[hero_name] then
                for slot_str, ability in pairs(hero_entry) do
                    local slot_num = tonumber(slot_str)
                    if slot_num and type(ability) == "table" and ability["Key"] then
                        _hero_lookup[ability["Key"]] = {
                            heroName  = hero_name,
                            abilityNum = slot_num,
                        }
                    end
                end
            end
        end
    end
    return _hero_lookup
end
-- ============================================================
-- Entry point
-- ============================================================


-- Entry point invoked by Template:AbilityTable.
-- Entry point invoked by Template:AbilityTable.
Line 30: Line 80:
     end
     end


    local lookup = build_hero_lookup()
     local abilities = GameData.get_entities(GameData.Dataset.ABILITIES, internal_keys)
     local abilities = GameData.get_entities(GameData.Dataset.ABILITIES, internal_keys)


     if #abilities == 0 then
    -- Filter to abilities belonging to active, released heroes only
    local filtered = {}
    for _, ability in ipairs(abilities) do
        local key = ability["Key"]
        if key and lookup[key] then
            table.insert(filtered, ability)
        end
    end
 
     if #filtered == 0 then
         return "No abilities found for stat: " .. stat
         return "No abilities found for stat: " .. stat
     end
     end


     -- Sort alphabetically by ability name
     -- Sort by hero name (primary), then ability slot number (secondary)
     table.sort(abilities, function(a, b)
     table.sort(filtered, function(a, b)
         return (a["Name"] or "") < (b["Name"] or "")
         local ia = lookup[a["Key"]]
        local ib = lookup[b["Key"]]
        if ia.heroName ~= ib.heroName then
            return ia.heroName < ib.heroName
        end
        return ia.abilityNum < ib.abilityNum
     end)
     end)


Line 44: Line 109:
     local out = {}
     local out = {}
     table.insert(out, '{| class="wikitable sortable"')
     table.insert(out, '{| class="wikitable sortable"')
     table.insert(out, "! Ability")
     table.insert(out, "! Hero !! Ability")
 
    for _, ability in ipairs(filtered) do
        local info = lookup[ability["Key"]]


    for _, ability in ipairs(abilities) do
        local hero_cell = frame:expandTemplate{
            title = "HeroIcon",
            args  = { info.heroName }
        }
         local ability_cell = frame:expandTemplate{
         local ability_cell = frame:expandTemplate{
             title = "AbilityIcon",
             title = "AbilityIcon",
             args  = { ability["Key"] or ability["Name"] }
             args  = { ability["Key"] }
         }
         }
         table.insert(out, "|-")
         table.insert(out, "|-")
         table.insert(out, "| " .. ability_cell)
         table.insert(out, "| " .. hero_cell .. " || " .. ability_cell)
     end
     end