Module:AbilityTable

Revision as of 23:35, 10 May 2026 by Vergir (talk | contribs) (Merge analyze_charges into charges_cells; move human notes to framework level with extensible table; Notes column auto-injected when notes exist (with help from vergir-bot LLM))

This module exposes two functions.

  • render generates a table of hero abilities matching a named list, and is invoked via {{AbilityTable}}.
  • is_member(list, ability_key, ability_num) is a shared function that decides whether a single ability belongs to a list (applying the same properties and exclusions); it is used both internally by render and externally by Module:AbilityHints, so the ability-hint icons always stay in sync with these tables.

See also


-- Module:AbilityTables
-- Renders sortable wikitables of abilities filtered by a named stat.
-- Backing module for Template:AbilityTable.
--
-- Usage on wiki pages: {{AbilityTable|charges}}
--
-- ============================================================
-- ADDING A NEW STAT
-- ============================================================
-- 1. Add an entry to stat_to_props mapping the friendly name
--    (lowercase) to the internal AbilityData property keys that
--    determine whether an ability appears in the table.
--
-- 2. Optionally add a matching entry to extra_columns to show
--    additional data columns beyond Hero and Ability.
--    Copy the "charges" block as a template:
--      - headers:    ordered list of column header strings
--                    (do not include "Notes" — it is added automatically)
--      - get_cells:  function(ability) -> { ["Column Name"] = value }
--                    The ability is guaranteed to match the stat.
--                    Return nil or omit a key to show "—" in that cell.
--    If no extra_columns entry is added, only Hero and Ability
--    columns are shown (plus Notes if human notes exist).
--
-- 3. Optionally add entries to column_suffix to append a unit
--    string (e.g. "s", "%") after values in specific columns.
--
-- 4. Optionally add human-authored notes to human_notes:
--      human_notes["stat"]["Ability Name"] = "Note text."
--    Notes are appended as a Notes column automatically.
-- ============================================================

local p = {}
local GameData = require("Module:GameData")

-- Maps lowercase friendly stat names to internal AbilityData property keys.
local stat_to_props = {
    ["charges"] = { "AbilityCharges" },
}

-- Maps column header names to their display unit suffix.
local column_suffix = {
    ["Time Between Charges"] = "s",
}

-- Human-authored notes, appended to the Notes column.
-- Keyed by stat name, then by ability display Name.
local human_notes = {
    ["charges"] = {
        ["Jar of Dead"] = "Charges by being near when anything dies.",
    },
}

-- ============================================================
-- Shared helpers
-- ============================================================

local function num_val(v)
    if type(v) == "number" then return v end
    if type(v) == "table"  then return v["Value"] end
    return nil
end

-- Searches Upgrades[] for a field and returns { tier = N, value = V } at its
-- first appearance, or nil if the field is absent from all upgrade tiers.
local function find_in_upgrades(ability, field)
    local upgrades = ability["Upgrades"]
    if type(upgrades) ~= "table" then return nil end
    for i, tier in ipairs(upgrades) do
        if type(tier) == "table" and tier[field] ~= nil then
            return { tier = i, value = num_val(tier[field]) }
        end
    end
    return nil
end

-- ============================================================
-- Per-stat cell renderers
-- Each function receives a guaranteed-matching ability record
-- and returns { ["Column Name"] = value } for extra columns.
-- Human notes and the Notes column are handled by the framework.
-- ============================================================

local function charges_cells(ability)
    local base_charges  = num_val(ability["AbilityCharges"])
    local base_cooldown = ability["AbilityCooldownBetweenCharge"]
    local has_base      = base_charges ~= nil and base_charges > 0
    local upgrade_info  = find_in_upgrades(ability, "AbilityCharges")

    local cooldown, upgrade_cooldown
    if has_base then
        if type(base_cooldown) == "number" and base_cooldown > 0 then
            cooldown = base_cooldown
        else
            local cd = find_in_upgrades(ability, "AbilityCooldownBetweenCharge")
            if cd and cd.value and cd.value > 0 then cooldown = cd.value end
        end
    else
        local cd = find_in_upgrades(ability, "AbilityCooldownBetweenCharge")
        if cd and cd.value and cd.value > 0 then upgrade_cooldown = cd.value end
    end

    local cells = {}

    if has_base then
        cells["Charges"] = tostring(base_charges)
        if cooldown then
            cells["Time Between Charges"] = tostring(cooldown)
        end
    end

    local note_parts = {}
    if upgrade_info then
        local n = upgrade_info.value or "?"
        local charge_str = n .. " charge" .. (n ~= 1 and "s" or "")
        if has_base then
            table.insert(note_parts, "+" .. charge_str .. " on T" .. upgrade_info.tier .. " upgrade.")
        else
            local cd_str = upgrade_cooldown and (upgrade_cooldown .. "s") or "unknown"
            table.insert(note_parts, "Becomes charged on T" .. upgrade_info.tier
                .. " upgrade with " .. charge_str
                .. " and " .. cd_str .. " time between charges.")
        end
    end
    if ability["AbilityChargesConditionally"] ~= nil then
        table.insert(note_parts, "Has a conditional charge.")
    end
    if #note_parts > 0 then
        cells["Notes"] = table.concat(note_parts, " ")
    end

    return cells
end

local extra_columns = {
    ["charges"] = {
        headers   = { "Charges", "Time Between Charges" },
        get_cells = charges_cells,
    },
}

-- ============================================================
-- Hero lookup (ability key -> { heroName, abilityNum })
-- ============================================================

local _hero_lookup = nil

local function build_hero_lookup()
    if _hero_lookup then return _hero_lookup end
    _hero_lookup = {}

    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

    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
-- ============================================================

function p.abilityPropTable(frame)
    local stat = mw.text.trim(frame.args[1] or ""):lower()

    local internal_keys = stat_to_props[stat]
    if not internal_keys then
        return '<span class="error">AbilityTable: unknown stat "' .. stat .. '".</span>'
    end

    local lookup      = build_hero_lookup()
    local spec        = extra_columns[stat]
    local headers     = spec and spec.headers or {}
    local get_cells   = spec and spec.get_cells or function() return {} end
    local stat_notes  = human_notes[stat]

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

    -- Keep only abilities belonging to active, released heroes
    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
    end

    -- Sort by hero name, then ability slot number
    table.sort(filtered, function(a, b)
        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)

    -- Determine whether a Notes column is needed:
    -- either the stat renderer produces notes, or human notes exist for this stat.
    local needs_notes = stat_notes ~= nil
    if not needs_notes then
        for _, ability in ipairs(filtered) do
            local extra = get_cells(ability)
            if extra["Notes"] then
                needs_notes = true
                break
            end
        end
    end

    -- Build final header list: declared headers + Notes if needed
    local final_headers = {}
    for _, h in ipairs(headers) do table.insert(final_headers, h) end
    if needs_notes then table.insert(final_headers, "Notes") end

    -- Render wikitable
    local out = {}
    local header_row = "! Hero !! Ability"
    if #final_headers > 0 then
        header_row = header_row .. " !! " .. table.concat(final_headers, " !! ")
    end

    table.insert(out, '{| class="wikitable sortable"')
    table.insert(out, header_row)

    for _, ability in ipairs(filtered) do
        local info = lookup[ability["Key"]]

        local hero_cell = frame:expandTemplate{
            title = "HeroIcon",
            args  = { info.heroName }
        }
        local ability_cell = frame:expandTemplate{
            title = "AbilityIcon",
            args  = { ability["Name"] }
        }

        local cells  = { hero_cell, ability_cell }
        local extra  = get_cells(ability)

        -- Append human note to any renderer-generated note
        if stat_notes and stat_notes[ability["Name"]] then
            local human = stat_notes[ability["Name"]]
            extra["Notes"] = extra["Notes"] and (extra["Notes"] .. " " .. human) or human
        end

        for _, header in ipairs(final_headers) do
            local value = extra[header]
            if value ~= nil then
                local suffix = column_suffix[header] or ""
                table.insert(cells, value .. suffix)
            else
                table.insert(cells, "—")
            end
        end

        table.insert(out, "|-")
        table.insert(out, "| " .. table.concat(cells, " || "))
    end

    table.insert(out, "|}")
    return table.concat(out, "\n")
end

return p