Module:AbilityTable: Difference between revisionsGive feedback
Jump to navigation
Jump to search
m key => name when rendering |
Add extra columns support with per-stat header and cell definitions; charges shows Charges and Time Between Charges columns (with help from vergir-bot LLM) |
||
| Line 5: | Line 5: | ||
-- Usage on wiki pages: {{AbilityTable|charges}} | -- Usage on wiki pages: {{AbilityTable|charges}} | ||
-- | -- | ||
-- | -- ============================================================ | ||
-- | -- ADDING A NEW STAT | ||
-- | -- ============================================================ | ||
-- | -- 1. Add an entry to friendly_to_internal 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: list of column names shown in the table header | |||
-- - get_cells: function(ability) that returns a table of | |||
-- { ["Column Name"] = value } for a single ability row. | |||
-- 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. | |||
-- ============================================================ | |||
local p = {} | local p = {} | ||
| Line 14: | Line 27: | ||
-- Maps lowercase friendly stat names to internal AbilityData property keys. | -- Maps lowercase friendly stat names to internal AbilityData property keys. | ||
-- | -- Used to filter which abilities appear in the table. | ||
local friendly_to_internal = { | local friendly_to_internal = { | ||
["charges"] = { "AbilityCharges" }, | ["charges"] = { "AbilityCharges" }, | ||
} | |||
-- Optional extra columns for each stat. | |||
-- Each entry has: | |||
-- headers: ordered list of column header strings | |||
-- get_cells: function(ability) -> { ["Header"] = value, ... } | |||
local extra_columns = { | |||
["charges"] = { | |||
headers = { "Charges", "Time Between Charges" }, | |||
get_cells = function(ability) | |||
local charges = ability["AbilityCharges"] | |||
local cooldown = ability["AbilityCooldownBetweenCharge"] | |||
return { | |||
["Charges"] = charges and tostring(charges) or nil, | |||
-- AbilityCooldownBetweenCharge is -1 when unused; treat as absent | |||
["Time Between Charges"] = (cooldown and cooldown > 0) and (cooldown .. "s") or nil, | |||
} | |||
end, | |||
}, | |||
} | } | ||
-- ============================================================ | -- ============================================================ | ||
-- Hero lookup (ability key -> hero | -- Hero lookup (ability key -> hero info) | ||
-- ============================================================ | -- ============================================================ | ||
| Line 70: | Line 100: | ||
-- ============================================================ | -- ============================================================ | ||
function p.abilityPropTable(frame) | function p.abilityPropTable(frame) | ||
local stat = mw.text.trim(frame.args[1] or ""):lower() | local stat = mw.text.trim(frame.args[1] or ""):lower() | ||
| Line 80: | Line 108: | ||
end | end | ||
local lookup = build_hero_lookup() | 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 abilities = GameData.get_entities(GameData.Dataset.ABILITIES, internal_keys) | local abilities = GameData.get_entities(GameData.Dataset.ABILITIES, internal_keys) | ||
| Line 108: | Line 140: | ||
-- Render wikitable | -- Render wikitable | ||
local out = {} | local out = {} | ||
local header_row = "! Hero !! Ability" | |||
if #headers > 0 then | |||
header_row = header_row .. " !! " .. table.concat(headers, " !! ") | |||
end | |||
table.insert(out, '{| class="wikitable sortable"') | table.insert(out, '{| class="wikitable sortable"') | ||
table.insert(out, | table.insert(out, header_row) | ||
for _, ability in ipairs(filtered) do | for _, ability in ipairs(filtered) do | ||
| Line 120: | Line 157: | ||
local ability_cell = frame:expandTemplate{ | local ability_cell = frame:expandTemplate{ | ||
title = "AbilityIcon", | title = "AbilityIcon", | ||
args = { ability[" | args = { ability["Key"] } | ||
} | } | ||
local cells = { hero_cell, ability_cell } | |||
local extra = get_cells(ability) | |||
for _, header in ipairs(headers) do | |||
table.insert(cells, extra[header] or "—") | |||
end | |||
table.insert(out, "|-") | table.insert(out, "|-") | ||
table.insert(out, "| " .. | table.insert(out, "| " .. table.concat(cells, " || ")) | ||
end | end | ||