Module:AbilityTable: Difference between revisions

Jump to navigation Jump to search
Vergir (talk | contribs)
m formatting
Vergir (talk | contribs)
Add compact parameter: one row per hero, abilities comma-separated, no extra columns (with help from vergir-bot LLM)
Line 376: Line 376:
     end
     end


     local key_lookup, name_to_key = build_lookup()
    local compact = mw.text.trim(frame.args["compact"] or "") ~= ""
 
     local key_lookup, _ = build_lookup()
     local spec      = extra_columns[stat]
     local spec      = extra_columns[stat]
     local headers    = spec and spec.headers or {}
     local headers    = spec and spec.headers or {}
Line 415: Line 417:
         return ia.abilityNum < ib.abilityNum
         return ia.abilityNum < ib.abilityNum
     end)
     end)
    -- Determine final headers
    local has_notes_header = false
    local final_headers = {}
    for _, h in ipairs(headers) do
        if h == "Notes" then
            has_notes_header = true
        elseif not hide_set[h] then
            table.insert(final_headers, h)
        end
    end
    local needs_notes = (has_notes_header or stat_notes ~= nil) and not hide_set["Notes"]
    if needs_notes then table.insert(final_headers, "Notes") end


     -- Disclaimer
     -- Disclaimer
Line 443: Line 432:
     table.insert(out, '{| class="wikitable sortable ability-table"')
     table.insert(out, '{| class="wikitable sortable ability-table"')


     local header_row = '! class="ability-table-col-hero" | Hero'
     if compact then
                    .. ' !! class="ability-table-col-ability" | Ability'
        -- Compact mode: one row per hero, abilities comma-separated in a single column.
    for _, h in ipairs(final_headers) do
        table.insert(out, '! class="ability-table-col-hero" | Hero'
        header_row = header_row .. ' !! class="ability-table-col-'
                      .. ' !! class="ability-table-col-abilities" | Abilities')
            .. h:lower():gsub("%s+", "-") .. '" | ' .. h
    end
    table.insert(out, header_row)


    for _, ability in ipairs(filtered) do
        -- Group abilities by hero, preserving sort order.
        local info = key_lookup[ability["Key"]]
        local hero_order  = {}  -- ordered list of hero names
        local hero_abilities = {}  -- heroName -> list of ability names
        for _, ability in ipairs(filtered) do
            local info = key_lookup[ability["Key"]]
            local hero = info and info.heroName or "—"
            if not hero_abilities[hero] then
                table.insert(hero_order, hero)
                hero_abilities[hero] = {}
            end
            table.insert(hero_abilities[hero], ability["Name"])
        end


         local hero_cell
         for _, hero in ipairs(hero_order) do
        if info then
             local hero_cell = frame:expandTemplate{ title = "HeroIcon", args = { hero } }
             hero_cell = frame:expandTemplate{ title = "HeroIcon", args = { info.heroName } }
            local ability_parts = {}
        else
            for _, name in ipairs(hero_abilities[hero]) do
             hero_cell = ""
                table.insert(ability_parts,
                    frame:expandTemplate{ title = "AbilityIcon", args = { name } })
            end
            table.insert(out, "|-")
             table.insert(out,
                '| class="ability-table-cell-hero" | '      .. hero_cell
            .. ' || class="ability-table-cell-abilities" | ' .. table.concat(ability_parts, ", "))
         end
         end
         local ability_cell = frame:expandTemplate{ title = "AbilityIcon", args = { ability["Name"] } }
    else
        -- Normal mode: one row per ability with stat-specific columns.
         local has_notes_header = false
        local final_headers = {}
        for _, h in ipairs(headers) do
            if h == "Notes" then
                has_notes_header = true
            elseif not hide_set[h] then
                table.insert(final_headers, h)
            end
        end
        local needs_notes = (has_notes_header or stat_notes ~= nil) and not hide_set["Notes"]
        if needs_notes then table.insert(final_headers, "Notes") end


         local extra = get_cells(ability)
         local header_row = '! class="ability-table-col-hero" | Hero'
                        .. ' !! class="ability-table-col-ability" | Ability'
        for _, h in ipairs(final_headers) do
            header_row = header_row .. ' !! class="ability-table-col-'
                .. h:lower():gsub("%s+", "-") .. '" | ' .. h
        end
        table.insert(out, header_row)


         if stat_notes and stat_notes[ability["Name"]] then
         for _, ability in ipairs(filtered) do
             local human = stat_notes[ability["Name"]]
             local info = key_lookup[ability["Key"]]
            extra["Notes"] = extra["Notes"] and (extra["Notes"] .. " " .. human) or human
        end


        local row = '| class="ability-table-cell-hero" | '    .. hero_cell
            local hero_cell
                .. ' || class="ability-table-cell-ability" | ' .. ability_cell
             if info then
        for _, header in ipairs(final_headers) do
                 hero_cell = frame:expandTemplate{ title = "HeroIcon", args = { info.heroName } }
            local value = extra[header]
            local cls  = "ability-table-cell-" .. header:lower():gsub("%s+", "-")
            local content
             if value ~= nil then
                 local suffix = column_suffix[header] or ""
                content = (type(value) == "string" and value:sub(1,1) == "(")
                    and value or (value .. suffix)
             else
             else
                 content = "—"
                 hero_cell = "—"
             end
             end
             row = row .. ' || class="' .. cls .. '" | ' .. content
             local ability_cell = frame:expandTemplate{ title = "AbilityIcon", args = { ability["Name"] } }
 
            local extra = get_cells(ability)
 
            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
 
            local row = '| class="ability-table-cell-hero" | '    .. hero_cell
                    .. ' || class="ability-table-cell-ability" | ' .. ability_cell
            for _, header in ipairs(final_headers) do
                local value = extra[header]
                local cls  = "ability-table-cell-" .. header:lower():gsub("%s+", "-")
                local content
                if value ~= nil then
                    local suffix = column_suffix[header] or ""
                    content = (type(value) == "string" and value:sub(1,1) == "(")
                        and value or (value .. suffix)
                else
                    content = "—"
                end
                row = row .. ' || class="' .. cls .. '" | ' .. content
            end
 
            table.insert(out, "|-")
            table.insert(out, row)
         end
         end
        table.insert(out, "|-")
        table.insert(out, row)
     end
     end