Module:AbilityTable: Difference between revisionsGive feedback
Create AbilityTables module for data-driven ability tables using GameData framework (with help from vergir-bot LLM) |
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 # | -- 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 | -- Sort by hero name (primary), then ability slot number (secondary) | ||
table.sort( | 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) | 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"]] | |||
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 | args = { ability["Key"] } | ||
} | } | ||
table.insert(out, "|-") | table.insert(out, "|-") | ||
table.insert(out, "| " .. ability_cell) | table.insert(out, "| " .. hero_cell .. " || " .. ability_cell) | ||
end | end | ||